How to Install and Configure Nginx on CentOS/Red Hat 7
Nginx is a powerful and versatile open-source software that excels as a web server, reverse proxy, load balancer, and HTTP cache. Renowned for its high performance, stability, and minimal resource footprint, Nginx is a popular choice for serving static content and handling dynamic requests in modern web applications. This guide provides a comprehensive walkthrough on how to install and configure Nginx on CentOS/Red Hat 7 systems.

Nginx is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. Nginx is known for its stability, rich feature set, simple configuration, and low resource consumption.
This guide will show you how to install and configure Nginx on your CentOS/Red Hat 7 server.
Step 1 : Installing Nginx
Nginx is readily available in the default CentOS/Red Hat 7 repository, making the installation process straightforward. To install Nginx, execute the following command using the yum
package manager:
$ sudo yum install nginx
This command will download and install Nginx and its dependencies from the official repository. Once the installation is complete, you need to start the Nginx service and enable it to automatically start during system boot. Use the systemctl
command for this purpose:
$ sudo systemctl start nginx
$ sudo systemctl enable nginx
The first command starts the Nginx service immediately. The second command ensures that Nginx will start automatically whenever the server is rebooted. To verify that the Nginx service is running correctly, you can check its status using the following command:
$ sudo systemctl status nginx
This command will display the current status of the Nginx service, including whether it is active (running), any recent log messages, and other relevant information.
Step 2 : Configuring Nginx
The core Nginx configuration file is located at /etc/nginx/nginx.conf
. This file contains global directives that govern the behavior of the entire Nginx server. Virtual host configurations, which define how Nginx handles different websites or applications, are managed through separate files. The /etc/nginx/sites-available/
directory stores configuration files for virtual hosts that are available but not yet enabled, while the /etc/nginx/sites-enabled/
directory contains symbolic links to configuration files for virtual hosts that are currently active.
To create a new virtual host configuration, first, create a new file in the /etc/nginx/sites-available/
directory. For example, to create a configuration for example.com
, you would run:
$ sudo vi /etc/nginx/sites-available/example.com
Remember to replace example.com
with your actual domain name.
Next, add the following configuration block to the file:
server {
listen 80;
listen [::]:80;
root /var/www/example.com;
index index.html;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ =404;
}
}
This configuration block defines a server that listens on port 80 (the standard HTTP port) for both IPv4 and IPv6 addresses. The root
directive specifies the document root directory for the website, which is where Nginx will look for files to serve. The index
directive specifies the default file to serve when a user requests a directory. The server_name
directive specifies the domain names that this virtual host will handle. The location /
block defines how Nginx handles requests for the root directory of the website. The try_files
directive attempts to serve the requested URI as a file or directory. If neither exists, it returns a 404 error.
Save and close the file after making these changes.
To enable the virtual host, create a symbolic link from the /etc/nginx/sites-enabled/
directory to the corresponding file in the /etc/nginx/sites-available/
directory:
$ sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
To disable a virtual host, simply remove the symbolic link from the /etc/nginx/sites-enabled/
directory:
$ sudo rm /etc/nginx/sites-enabled/example.com
Step 3 : Creating the Document Root Directory
By default, Nginx often uses /usr/share/nginx/html
as the document root for the default configuration. However, for virtual hosts, it’s crucial to define a dedicated document root directory as specified in the root
directive within the virtual host configuration file.
To create the document root directory for the example.com
virtual host, use the following command:
$ sudo mkdir -p /var/www/example.com
The -p
option ensures that any necessary parent directories are also created.
Setting the correct permissions
The Nginx web server typically runs under the nginx
user. To ensure that Nginx can properly access and serve files from the document root directory, it’s essential to grant the nginx
user appropriate read and write permissions.
To set the correct permissions, execute the following command:
$ sudo chown -R nginx:nginx /var/www/example.com
This command recursively changes the ownership of the /var/www/example.com
directory and all its contents to the nginx
user and group.
Creating the index.html file
The index.html
file serves as the default webpage that is displayed when a visitor accesses a directory without specifying a particular file. It’s a common practice to create an index.html
file to provide a welcome message or basic information about the website.
To create the index.html
file, use the following command:
$ sudo vi /var/www/example.com/index.html
Then, add the following HTML code to the file:
<html>
<head>
<title>Welcome to Example.com!</title>
</head>
<body>
<h1>Success! The example.com server block is working!</h1>
</body>
</html>
This simple HTML code will display a heading indicating that the example.com
server block is functioning correctly. Save and close the file after adding the code.
Step 4 : Testing your Configuration
After creating the virtual host configuration file and the document root directory, it’s essential to test your Nginx configuration for any syntax errors before applying the changes. This can be done by running the following command:
$ sudo nginx -t
If the configuration is valid, you will receive a Syntax OK
message, confirming that there are no syntax errors in your configuration files.
To apply the changes, restart the Nginx service using the following command:
$ sudo systemctl restart nginx
This command will reload the Nginx configuration and apply any changes you have made.
You can now access your website by navigating to http://example.com
in your web browser. If everything is configured correctly, you should see the "Welcome to Example.com!" page. You have successfully installed and configured Nginx on CentOS/Red Hat 7.
Alternative Solutions for Virtual Host Configuration
While the symbolic link method described above is a common and effective way to manage virtual hosts in Nginx, there are alternative approaches that can offer different advantages in certain scenarios. Here are two different ways to achieve virtual host configuration:
1. Include Directives
Instead of using symbolic links, you can directly include the virtual host configuration files within the main nginx.conf
file using the include
directive. This approach keeps all configuration files in a single location and can simplify management in some cases.
Explanation:
The include
directive allows you to incorporate the contents of another file into the current configuration file. By including the virtual host configuration files directly in nginx.conf
, you avoid the need for symbolic links and can manage all configurations from a central location.
Code Example:
-
Create the virtual host configuration file as described in the original article (e.g.,
/etc/nginx/sites-available/example.com
). -
Edit the
/etc/nginx/nginx.conf
file and add the following line within thehttp
block:
include /etc/nginx/sites-available/*.conf;
This line tells Nginx to include all files with the .conf
extension in the /etc/nginx/sites-available/
directory.
- Remove the default symbolic links from
/etc/nginx/sites-enabled/
:
rm /etc/nginx/sites-enabled/default
-
Ensure the virtual host file ends in
.conf
-
Test the configuration and restart Nginx as described in the original article.
Advantages:
- Centralized configuration management.
- No need for symbolic links.
Disadvantages:
- Can make the
nginx.conf
file longer and more complex. - Requires careful management of file names to avoid conflicts.
2. Using Environment Variables
Another approach is to use environment variables within your Nginx configuration files. This can be particularly useful in containerized environments where configurations might need to be dynamically adjusted based on environment variables. This can also be useful in more complex applications where certain configurations are required.
Explanation:
Nginx allows you to access environment variables within your configuration files using the $variable_name
syntax. By setting environment variables before starting Nginx, you can dynamically configure various aspects of your virtual hosts, such as the document root directory or server name.
Code Example:
- Set the environment variable before starting Nginx:
export DOMAIN_NAME=example.com
export DOCUMENT_ROOT=/var/www/example.com
- Create the virtual host configuration file (e.g.,
/etc/nginx/sites-available/example.com
) and use the environment variables:
server {
listen 80;
listen [::]:80;
root $DOCUMENT_ROOT;
index index.html;
server_name $DOMAIN_NAME www.$DOMAIN_NAME;
location / {
try_files $uri $uri/ =404;
}
}
-
Enable the virtual host as described in the original article.
-
Test the configuration and restart Nginx as described in the original article.
Advantages:
- Dynamic configuration based on environment.
- Suitable for containerized environments.
Disadvantages:
- Requires careful management of environment variables.
- Can make the configuration less readable if environment variables are not well-documented.
These alternative solutions provide different ways to configure Nginx on CentOS/Red Hat 7, offering flexibility depending on your specific needs and environment. Remember to choose the approach that best suits your requirements and ensures maintainability and scalability of your Nginx configuration.