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.
In an earlier post, we described the installation of Nginx on Ubuntu. This post will describe the installation of Nginx on CentOS.
You will require:
- CentOS system
- A user with sudo access
Note: We will be demonstrating the procedure on CentOS 8 machine.
Step 1: Install Nginx on CentOS
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.
After the Nginx server is installed, you should see a similar output:
Step 2: 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
Step 3: 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.
Step 4: 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 this link.
If the Nginx server has been installed successfully, you should see the following Nginx default page.
Nginx Management Commands
Other than to start and enable the Nginx server commands (as discussed 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
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
There you have how to install the Nginx server on a CentOS machine and add a firewall rule to allow traffic coming towards the Nginx server. In the end, we’ve discussed how to remove Nginx from CentOS.
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.