Nginx is an open-source and the most popular web server which is more flexible and lightweight than Apache server. It is designed for best stability and performance and can handle multiple client requests simultaneously with limited resources. It can also serve as a load balancer and a reverse proxy server.
Basically, Nginx is more resource-friendly than Apache in most cases.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Nginx queries.
In this context, we shall look into how to Install Nginx on CentOS 8 OS.
Previously, we described the installation of Nginx on Ubuntu.
How to Install Nginx on CentOS ?
Before proceeding with this Installation procedure, ensure that you are using a user with sudo access.
To install Nginx on CentOS, we can use the dnf package manager. Use the command below to install Nginx on your CentOS system:
$ sudo dnf install nginx
Provide sudo password and hit y when prompted with y/N. After that installation will start.
Steps to configure Nginx on CentOS
1. Start and Enable Nginx on Boot
After the installation, you can start Nginx using the command below:
$ sudo systemctl start nginx
To verify if the service has started running, use the command below:
$ sudo systemctl status nginx
If you do not want to manually start the Nginx each time the server is started, you can enable it to start automatically on server boot.
To start the Nginx service automatically at system boot, here is the command:
$ sudo systemctl enable nginx
2. Allow Nginx Traffic in Firewall
In CentOS, the firewall is enabled by default and hence it blocks access to port 80 and 443 used by the Nginx web server. To allow external HTTP and HTTPS traffic towards your NGINX server, you will need to add a rule in the firewall. Remember if you are just testing Nginx server from your local system, then you do not need to add this rule.
Run the command below to allow access to HTTP traffic coming to your Nginx server:
$ sudo firewall-cmd --permanent --add-service=http
Then run the command below to allow access to HTTPS traffic coming to your Nginx server:
$ sudo firewall-cmd --permanent --add-service=https
Then run the command below to reload firewalld:
$ sudo firewall-cmd --reload
To check if the rule has been added, use the command below:
$ sudo firewall-cmd --permanent --list-all
In the output of the above command, you should see http listed in the services line.
3. Test Nginx Server
Now to test if the Nginx server is working, access your server's IP address in a web browser. If you don't know how to find the IP address of a system, visit the methods to find IP address in CentOS.
If the Nginx server has been installed successfully, you should see the following Nginx default welcome page.
How to Manage Nginx ?
Apart from starting and enabling the Nginx service as described above, there are few more commands that you will find helpful in managing the Nginx server. We are sharing some of them:
To stop Nginx service, the command would be:
$ sudo systemctl stop nginx
To restart Nginx service, the command would be:
$ sudo systemctl restart nginx
To disable Nginx service, the command would be:
$ sudo systemctl disable nginx
When you make any configuration changes, you will need to reload firewall configuration. Here is the command you can use:
$ sudo systemctl reload nginx
Nginx Main Files and Directories
Here are some main Nginx files and directories:
- /etc/nginx: Nginx configuration files can be found in this directory.
- /etc/nginx/nginx.conf: This is the Nginx global configuration file.
- /etc/nginx/conf.d: Nginx server block configuration files can be found in this directory.
- /usr/share/nginx/html: Web content served by the Nginx server can be found in this directory.
How to Uninstall Nginx from CentOS Linux System ?
If you ever need to remove / uninstall Nginx from your CentOS machine, you can do so as follows:
Stop Nginx services as follows:
$ sudo systemctl stop nginx.service
Then to remove Nginx, use the command below:
$ sudo yum remove nginx
To remove Nginx configuration and log files too, use the command below:
$ sudo rm -R /etc/nginx
$ sudo rm -R /var/log/nginx
[Need assistance in fixing Nginx configuration errors? We can help you. ]