
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 available in the default CentOS/Red Hat 7 repository. To install Nginx, run the following command:
$ sudo yum install nginxOnce the installation is complete, start the Nginx service and enable it to start automatically at boot time:
$ sudo systemctl start nginx
$ sudo systemctl enable nginxTo check the status of the Nginx service, run the following command:
$ sudo systemctl status nginxStep 2 : Configuring Nginx
The main Nginx configuration file is located at /etc/nginx/nginx.conf. This file contains directives that affect the entire Nginx server.
The /etc/nginx/sites-available/ directory contains configuration files for virtual hosts that are available, but not yet enabled. The /etc/nginx/sites-enabled/ directory contains configuration files for virtual hosts that are enabled.
To create a new virtual host configuration file in the /etc/nginx/sites-available/ directory, run the following command:
$ sudo vi /etc/nginx/sites-available/example.comReplace example.com with your domain name.
Add the following lines 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;
}
}Save and close the file.
To enable the virtual host, create a symbolic link from the /etc/nginx/sites-enabled/ directory to the /etc/nginx/sites-available/ directory:
$ sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/To disable a virtual host, remove the symbolic link from the /etc/nginx/sites-enabled/ directory:
$ sudo rm /etc/nginx/sites-enabled/example.comStep 3 : Creating the Document Root Directory
By default, the document root directory for the default.conf file is /usr/share/nginx/html. For virtual hosts, the document root directory is specified in the root directive.
To create the document root directory for the example.com virtual host, run the following command:
$ sudo mkdir -p /var/www/example.comSetting the correct permissions
The Nginx web server runs as the nginx user. This user needs to have read and write permissions for the document root directory.
To set the correct permissions, run the following command:
$ sudo chown -R nginx:nginx /var/www/example.comCreating the index.html file
The index.html file is the default file that is served when a visitor requests a directory instead of a specific file.
To create the index.html file, run the following command:
$ sudo vi /var/www/example.com/index.htmlAdd the following lines 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>Save and close the file.
Step 4 : Testing your Configuration
After you have created the virtual host configuration file and the document root directory, you can test your configuration for syntax errors by running the following command:
$ sudo nginx -tIf you get the Syntax OK message, it means that your configuration is valid.
To apply the changes, run the following command:
$ sudo systemctl restart nginxYou can now access your website at http://example.com.
