How to install Nginx on CentOS 7

How to install Nginx on CentOS 7

NGINX is one of the popular web servers on Linux. Nginx offers a variety of features such as web serving, load balancing, reverse proxy, caching, streaming, etc. Nginx also offers a load balancer proxy and reverse proxy for UDP, TCP, and HTTP. In mail servers (POP3, SMTP, and IMAP) Nginx also works as a proxy server.

In this tutorial, we will learn how to install and manage the Nginx service on the CentOS 7 server.

Prerequisites

  • CentOS 7 Operating System
  • Root user or another user with Sudo privileges

Install Nginx Web Server

  • Update the server to install the latest Nginx version.
sudo yum update -y
  • After updating the server packages, we need to enable the EPEL repository on the server with the following command.
sudo yum install epel-release -y
  • Run the following command to install the latest version of Nginx.
sudo yum install nginx -y
  • Start the Nginx service.
sudo systemctl start nginx
  • Check the status of the Nginx service.
sudo systemctl status nginx

You will get the following output:-

Check the status of Nginx service
  • Check, that the Nginx web server is working properly. Enter the server IP (For local system, use localhost) in the browser. You will get the following output.
Default Nginx page on CentOS
  • To find the public IP of the server, run the following command.
curl ifconfig.me

Manage Nginx Service

  • To enable the Nginx service
sudo systemctl enable nginx
  • To disable the Nginx service
sudo systemctl disable nginx
  • To start the Nginx service
sudo systemctl start nginx
  • To stop the Nginx service
sudo systemctl stop nginx
  • To check the status of the Nginx service
sudo systemctl status nginx

Allow ports from Firewall

If the server has an active firewall, then you need to allow the Nginx port 80 and 443 for incoming traffic. If the firewall of the server is inactive, then you can skip this step.

  • To allow Nginx ports, run the following command.
sudo firewall-cmd --add-service=http
sudo firewall-cmd --add-service=https
sudo firewall-cmd --runtime-to-permanent
  • If the firewall of the server is running with iptables, then the command which allows the port will depend on the current rule set. For the basic rules, run the following command to allow the ports.
sudo iptables -I INPUT -p tcp -m tcp --dport 80 -j ACCEPT
sudo iptables -I INPUT -p tcp -m tcp --dport 443 -j ACCEPT

Conclusion

In today’s tutorial, we have learned how to install and manage the Nginx with simple steps. Nginx is one of the most popular web servers on Linux. Nginx is very fast compared to Apache web server. It offers a variety of features such as load balancer, reverse proxy, etc.

If you guys have any issues, let me know in the comments sections.