Nginx, the most popular open-source, HTTP and reverse proxy server with high-performance, use to handle the load of largest sites on the internet.
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 8 Operating System.
Before starting the Nginx installation process, make sure you have a CentOS 8 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
Step 2 – Install Nginx
Once the EPEL repository installs in your system, you can install Nginx by using the following command:
$ sudo yum install nginx
Step 3 – Enable and start Nginx Service
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
You can verify the Nginx service is running properly by checking the status of Nginx service, as shown below:
$ sudo systemctl status nginx
If service is running correctly you will get the screen with Active and running status as below image:
Step 4 – Configure Firewall for Port 80 and 443
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
Step 5 – Verify Nginx installation
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 8 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
To start Nginx server use the following command:
$ sudo systemctl start nginx
To restart Nginx server run the below command:
$ sudo systemctl restart nginx
You can reload the Nginx server by using the following command:
$ sudo systemctl reload nginx
To disable Nginx service, so it not auto start on system boot, you can use below command:
$ sudo systemctl disable nginx
To re-enable Nginx service again type:
$ 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 8. You are ready to deploy any website or application behind Nginx web server.