Read in 4 minutes

last updated 

How To Install Nginx on CentOS 7

Nginx pronounced engine x is a free, open-source, high-performance HTTP and reverse proxy server responsible for handling the load of some of the largest sites on the Internet.

This tutorial will teach you how to install and manage Nginx on your CentOS 7 machine.


Before starting with the tutorial, make sure you are logged in as a user with sudo privileges and you don’t have Apache or any other service running on port 80 or 443.

Enable EPEL Repository

Nginx packages are available in the EPEL repositories. If you don’t have EPEL repository already installed you can do it by typing:

sudo yum install epel-release

Install Nginx

Once the EPEL repository is enabled we can install the Nginx package with yum by typing the following command:

sudo yum install nginx

If this is the first time you are installing a package from the EPEL repository yum may prompt you to import the EPEL GPG key:

Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
 Userid     : "Fedora EPEL (7) <[email protected]>"
 Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
 Package    : epel-release-7-9.noarch (@extras)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Is this ok [y/N]:

If that’s the case, type y and hit Enter.

Once the installation is completed, enable and start the Nginx service:

sudo systemctl enable nginx
sudo systemctl start nginx

Adjust the Firewall

If you are running a firewall you also need to open HTTP and HTTPS ports, 80 and 443. You can use the following commands to open the necessary ports:

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

Verify the Nginx installation

We can check the status of the Nginx service, with:

sudo systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2018-03-12 16:12:48 UTC; 2s ago
  Process: 1677 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 1675 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 1673 ExecStartPre=/usr/bin/rm -f /run/ (code=exited, status=0/SUCCESS)
 Main PID: 1680 (nginx)
   CGroup: /system.slice/nginx.service
           ├─1680 nginx: master process /usr/sbin/nginx
           └─1681 nginx: worker process

As you can see from the output above the Nginx service is active and running.

The following command will print the Nginx version:

sudo nginx -v
nginx version: nginx/1.12.2

You can verify if the Nginx is running properly by opening http://YOUR_IP in your browser of choice, and you should be able to see the default Nginx welcome page as shown below:


Manage the Nginx service with systemctl

We can manage the Nginx service same as any other systemd unit.

To stop the Nginx service, run:

sudo systemctl stop nginx

To start it again, type:

sudo systemctl start nginx

To restart the Nginx service:

sudo systemctl restart nginx

To reload the Nginx service after you made some configuration changes:

sudo systemctl reload nginx

If you want to disable the Nginx service to start at boot:

sudo systemctl disable nginx

And to re-enable it again:

sudo systemctl enable nginx

Nginx Configuration File’s Structure and Best Practices

  • All Nginx configuration files are located in the /etc/nginx/ directory.
  • The main Nginx configuration file is /etc/nginx/nginx.conf.
  • To make Nginx configuration easier to maintain it is recommended to create a separate configuration file for each domain.
  • New Nginx server block files must end with .conf and be stored in /etc/nginx/conf.d directory. You can have as many server blocks as you need.
  • It is a good idea to follow a standard naming convention, for example if your domain name is then you the configuration file should be named /etc/nginx/conf.d/
  • If you use repeatable configuration segments in your domains server blocks then it is a good idea to create a directory named /etc/nginx/snippets refactoring those segments into snippets and include the snippet file to the server blocks.
  • Nginx log files (access.log and error.log) are located in the /var/log/nginx/ directory. It is recommended to have a different access and error log files for each server block.
  • You can set your domain document root directory to any location you want. The most common locations for webroot include:
    • /home/<user_name>/<site_name>
    • /var/www/<site_name>
    • /var/www/html/<site_name>
    • /opt/<site_name>
    • /usr/share/nginx/html


Congratulations, you have successfully installed Nginx on your CentOS 7 server. You’re now ready to start deploying your applications and use Nginx as a web or proxy server. A secure certificate is a ‘must-have’ feature for all websites nowadays, to secure your website with a free Let’s Encrypt SSL certificate, you can check out this tutorial on how to Secure Nginx with Let’s Encrypt on CentOS 7.

This is a post in the Install LEMP Stack on CentOS 7 series.
Other posts in this series:

How To Install Nginx on CentOS 7