
Nginx is open-source, high performance and free web server. It is a modern and efficient web server to handle large number of concurrent connections. Nginx can be used as a standalone web server, and as a reverse proxy for Apache and other web servers. It is a much more flexible and lightweight program than Apache HTTP Server. In this tutorial, you will learn how to install nginx on CentOS 7.
Prerequisites
- First of all you need CentOS 7 server with a non-root sudo enabled user account.
- Make sure Apache or any other services are not running on port
80
or443
.
Install Nginx on CentOS
First, you need to update system software packages to the latest version by type:
sudo yum -y update
Nginx packages are available in the EPEL repositories so we will install EPEL repository by below command:
sudo yum install epel-release
Now, Install Nginx by typing the following yum command:
sudo yum install nginx
If you are installing a package first time from the EPEL repository, it 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) <epel@fedoraproject.org>"
Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
Package : epel-release-7-11.noarch (@extras)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Is this ok [y/N]:
Press Y and hit Enter key to go ahead.
After that, we will start and enable nginx service by following commands:
sudo systemctl enable nginx
sudo systemctl start nginx
You can check status of nginx service by typing:
sudo systemctl status nginx
It will show output as given below:
● 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 Sat 2019-04-13 20:28:32 IST; 47s ago
Process: 17772 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 17766 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 17761 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 17774 (nginx)
Tasks: 2
CGroup: /system.slice/nginx.service
├─17774 nginx: master process /usr/sbin/nginx
└─17775 nginx: worker process
Adjusting Firewall
If your server is protected by firewall then you need to open both HTTP(80
) and HTTPS(443
) ports. Execute the following commands to open both HTTP(80
) and HTTPS(443
) ports:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
You can verify your Nginx installation by visiting below URL in your browser:
http://YOUR_SERVER_IP_ADDRESS
It will show you default Nginx welcome page as shown below:

Managing Nginx Service
Finally, you installed Nginx web server on your machine. We will see few basic commands to manage Nginx service:
To stop Nginx service execute below command:
sudo systemctl stop nginx
You can start it again by typing:
sudo systemctl start nginx
Restart (stop and start) Nginx service the Apache service by:
sudo systemctl restart nginx
If you made changes to configuration file and want to reload nginx service then you can do it by typing:
sudo systemctl reload nginx
For disable Nginx to auto start after boot execute below command:
sudo systemctl disable nginx
Again to do re-enable type:
sudo systemctl enable nginx
Nginx Configuration Files Structure
- All configuration files are located in the
/etc/nginx/
directory. - Nginx main configuration file is at
/etc/nginx/nginx.conf
. - It’s best practice to create a separate configuration file of each domain for better maintainability.
- New server blocks (configuration file) of each domain should be stored in
/etc/nginx/conf.d
- The default server web root directory is
/usr/share/nginx/html
- It’s best practice to to follow standard naming convention. Nginx server block files name should as domain name and must end with
.conf
extension. For example, your domain name isexample.com
then server block file name shouldexample.com.conf
- Nginx log files (
access.log
anderror.log
) are located in the/var/log/nginx/
directory. It’s also recommended to have a different access and error log files for each server block.
Conclusion
Finally, you have successfully installed Nginx on your CentOS 7 server. Now you can deploy your applications and use Nginx as a web or proxy server. If you have any question or suggestion don’t hesitate to comment below.
Leave a Reply