How to install Nginx Web Server on RHEL/CentOS

Nginx is a widely popular and open-source web server that you can use as a web server, reverse proxy, cache server and load balancer among other things. It is very powerful and lightweight and takes very few resources. In this tutorial, you will learn how to install Nginx Web Server on RHEL/CentOS server machine.

There are two methods with which you can install the Nginx on RHEL/Centos based OS.

  • Using EPEL Repositories
  • Using Nginx Official Repositories.

Prerequisite

  • RHEL/CentOS Machines
  • User with the sudo user or root user privileges.
  • Good Internet Connection

Method: 1 – Installing Nginx using the EPEL Repository

By default, Nginx packages are not maintained on RHEL/CentOS Official Repositories. But it is available on the EPEL repository. So you have to install EPEL repositories first.

  • CentOS/RHEL 6/7
$  sudo yum install epel-release
  • CentOS/RHEL 8
$  sudo dnf install epel-release

Once EPEL repository has been installed, then you will be able to install nginx package with the below commands:

$  sudo yum install nginx

$  sudo dnf install nginx

Method: 2 – Installing Nginx using the Official Nginx Repository

To enable the official Nginx repository, you have to create the repository files for nginx repos. Create a file “/etc/yum.repos.d/ngnix.repo” & put the following details,

$  vi /etc/yum.repos.d/nginx.repo

[nginx]
name=nginx repo
baseurl=https://nginx.org/packages/$OS/$release-server/$basearch/
enabled=1
gpgcheck=0

Here you have to update the following entries as per the OS type & version number

  • OS – RHEL or CentOS
  • Release-server – 6, 7 or 8

For Instance, you will update entries for Centos 8.

[nginx]
name=nginx repo
baseurl=https://nginx.org/packages/centos/8/$basearch/
enabled=1
gpgcheck=0

Similarly replace Centos with rhel for rhel 8 Nginx repository. Now, all we need is to install Nginx with the following command,

$  sudo dnf install nginx

$  sudo yum install nginx

Starting and Enabling Nginx Service

You can start the nginx server using below command:

$  sudo systemctl start nginx

You must enable the nginx server at the boot time. You can enable it using below command:

$  sudo systemctl enable nginx

You can check the nginx server status using following command.

$ sudo systemctl status nginx

When the nginx service started, you can access the default nginx webpage with the system IP address or hostname on web browser.

http://ip_address_or_hostname_of_the_server

Open Default Ports in Firewall

By default, nginx runs on port 80, so we have to open tcp port using following commands:

  • Using Firewalld Utility
$  sudo firewall-cmd --permanent --add-service=80/tcp

$  sudo firewall-cmd --complete-reload

To list all firewall rules, you can use following commands:

$  sudo firewall-cmd --list-all

Read More on Iptables : Firewalld Tutorial: Beginners to Advanced Guide To Linux Firewall

  • Using iptables Utility
$  sudo iptables –A INPUT –p tcp ––dport 80 –j ACCEPT

$  sudo iptables –A OUTPUT –p tcp ––dport 80 –j ACCEPT

$  service iptables save

To list all iptables rules, you can use following commands:

$  sudo iptables -L

Read More on Iptables : Iptables Tutorial: Beginners to Advanced Guide To Linux Firewall

Conclusion

Hence, this finishes our tutorial on how to install nginx web server on RHEL/CentOS. Stay tuned for more on Linux, Devops, Python and Cloud Computing. Thanks!!

Read Also : Getting Started with Vi/Vim Editor – Advanced

Share on:

Ravindra Kumar

Hi, This is Ravindra. I am founder of TheCodeCloud. I am AWS Certified Solutions Architect Associate & Certified in Oracle Cloud as well. I am DevOps and Data Science Enthusiast.

Recommended Reading..

2 thoughts on “How to install Nginx Web Server on RHEL/CentOS”

Leave a Comment