Search

How To Install Nginx on CentOS 7

How To Install Nginx on CentOS 7

Introduction

Nginx is a high performance web server software. It is a much more flexible and lightweight program than Apache HTTP Server. Nginx, stylized as NGINX, nginx or NginX, is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. The software was created by Igor Sysoev and publicly released in 2004. Nginx is free and open-source software, released under the terms of the 2-clause BSD license.

Prerequisites

  • A CentOS 7 Server Minimal Install
  • A RHEL 7 Server Minimal Install
  • A CentOS/RHEL 7 system with static IP address

 

1. Add Nginx Repository from nginx.org or EPEL repository :

For EPEL repository, To add the CentOS 7 EPEL repository, open terminal and use the following command:

 sudo yum install epel-release 

2. For EPEL repository : nginx.org

Install the prerequisites:

 sudo yum install yum-utils 

To set up the yum repository, create the file named /etc/yum.repos.d/nginx.repo with the following contents:

echo "[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true" >> /etc/yum.repos.d/nginx.repo

By default, the repository for stable nginx packages is used. If you would like to use mainline nginx packages, run the following command:

 sudo yum-config-manager --enable nginx-mainline 

To install nginx, run the following command:

 sudo yum install nginx 

When prompted to accept the GPG key, verify that the fingerprint matches 573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62, and if so, accept it.

Step Two—Install Nginx

Now that the Nginx repository is installed on your server, install Nginx using the following yum command:

 sudo yum install nginx 

After you answer yes to the prompt, Nginx will finish installing on your virtual private server (VPS).

Step Three—Start Nginx

Nginx does not start on its own. To get Nginx running, type:

 sudo systemctl start nginx 

Next, let’s take a moment to ensure that Nginx starts at boot, using the systemctl enable command, which will create the necessary symlinks.

 sudo systemctl enable nginx 

If you are running a firewall, run the following commands to allow HTTP and HTTPS traffic:

sudo firewall-cmd --permanent --zone=public --add-service=http 
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

You can do a spot check right away to verify that everything went as planned by visiting your server’s public IP address in your web browser (see the note under the next heading to find out what your public IP address is if you do not have this information already):

Just typing this, which should give you your public IP address as seen from another location on the internet:

 curl -4 icanhazip.com  
 http://server_domain_name_or_IP/ 

You will see the default CentOS 7 Nginx web page, which is there for informational and testing purposes. It should look something like this:

Conclusion

In this tutorial, we’ve installed and secured Nginx on your server.

Sys Admin

Sys Admin

I am a Red Hat Certified Engineer (RHCE) and working as an IT Professional since 2012...