We can use Nginx as a web server and as a reverse proxy for another web server like Apache.
If we compare Nginx and Apache, Nginx is capable of handling more number of concurrent connections.
In this tutorial article, we will learn how to install Nginx on CentOS 7 Operating System.
Before starting the Nginx installation process, make sure you have a CentOS 7 running system and a user to log in system with sudo privileges.
You should also make sure the other web server like apache not installed into the system and port 80 and 443 opened from outside.
Nginx install on CentOS
To install high performance web server Nginx, you need to follow below steps:
Step 1 – Enable EPEL repository
The Nginx package is available in EPEL repository, So to install Nginx web server first need to install EPEL repository if it is not in your server. To install EPEL repository type the below command in your terminal and press Enter.
$ sudo yum install epel-release
Once the EPEL repository installs in your system, you can install Nginx by using the following command:
$ sudo yum install nginx
After successful installation of the Nginx web server, you should enable the Nginx service to make it auto-start on system start and start the Nginx service by using the following command:
$ sudo systemctl enable nginx $ sudo systemctl start nginx
$ sudo systemctl status nginx
If your server’s firewall configured you should configure it for port http and https also so your web server reachable from the outside, to perform firewall configuration you should execute below command on terminal:
$ sudo firewall-cmd --permanent --zone=public --add-service=http $ sudo firewall-cmd --permanent --zone=public --add-service=https $ sudo firewall-cmd --reload
To verify Nginx web server is installed and running properly, you should open your serer IP into any web browser with typing address in the address bar like http://YOUR_IP, if you get the below screen means Nginx is running properly:
Nginx Services manage with systemctl
CentOS 7 comes with systemctl to manage service; you can manage Nginx service with systemctl like below examples.
To stop the Nginx server, type below command:
$ sudo systemctl stop nginx
$ sudo systemctl start nginx
$ sudo systemctl restart nginx
$ sudo systemctl reload nginx
$ sudo systemctl disable nginx
$ sudo systemctl enable nginx
Nginx important and configuration file structure
- /etc/nginx/ is the leading directory of the Nginx server; all configuration files located here.
- /etc/nginx/nginx.conf is the main configuration file of Nginx Server. Create new Nginx server block files with extension “.conf” and it is stored into /etc/nginx/conf.d/ directory.
- Nginx server always read sites configuration from /etc/nginx/conf-d/ directories.
It is recommended to make separate configuration files for each site to make changes quickly.
- Nginx server’s log files saved with name access.log and error.log are located in the /var/log/nginx/ directory.
- The website files you can store in any location and set the path into configuration files but commounly used webroot directories are below:
Now you have learned how to install Nginx Web Server on CentOS 7. You are ready to deploy any website or application behind Nginx web server.