How to install and use Nginx on CentOS 7 / RHEL 7

in Categories , , last updated January 19, 2018

How do I install and configure the latest version of Nginx web server on a CentOS 7 or RHEL 7 server using the CLI and host a static site?

Nginx [engine x] is free and open source high-performance web server. It also acts as a reverse proxy server, as well as. This page shows how to install Nginx server on a CentOS 7 or RHEL 7 and configure a static web site.

How to install Nginx on CentOS 7 or RHEL 7

The procedure to install Nginx weber server on an RHEL 7 or CentOS Linux 7 is as follows:

  1. Login to your cloud server or bare metal server using ssh command:br/>
    ssh user@cloud-server-ip
  2. Create the file named /etc/yum.repos.d/nginx.repo using a text editor such as vim command
    sudo vi /etc/yum.repos.d/nginx.repo
  3. Install nginx package using the yum command:
    sudo yum update
    sudo yum install nginx

Let us see all steps, commands and configuration in details.

Step 1 – Configure Nginx repo for CentOS 7

Run command:
$ vi /etc/yum.repos.d/nginx.repo
Append following for CentOS 7.x:

name=nginx repo

To save and quit the file in vi press Esc followed by :x and Enter.

A note about configuring Nginx repo for RHEL 7

Create or edit file using vim/vi/joe text editor:
$ vi /etc/yum.repos.d/nginx.repo
Append following for RHEL (Red Hat Enterprise Linux) version 7.x:

name=nginx repo

Save and close the file in vi.

Step 2 – Install Nginx on CentOS 7

To install latest stable nginx server, run the following yum command:
$ sudo yum install nginx
Install Nginx on CentOS 7 server

Install Nginx on RHEL 7

Type the following yum command on your RHEL 7.x server:
$ sudo yum install nginx

Step 3 – Start/stop/restart nginx server

First enable nginx service by running systemctl command so that it start at server boot time:
$ sudo systemctl enable nginx
Sample outputs:

Created symlink from /etc/systemd/system/ to /usr/lib/systemd/system/nginx.service.

Start Nginx command

$ sudo systemctl start nginx

Stop Nginx command

$ sudo systemctl stop nginx

Restart Nginx command

$ sudo systemctl restart nginx

Find status of Nginx server command

$ sudo systemctl status nginx
Sample outputs:

* nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2018-01-18 21:45:54 UTC; 8s ago
  Process: 656 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 657 (nginx)
   CGroup: /system.slice/nginx.service
           ??657 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           ??658 nginx: worker process
Jan 18 21:45:54 cenots7-theosin systemd[1]: Starting nginx - high performance web server...
Jan 18 21:45:54 cenots7-theosin systemd[1]: Started nginx - high performance web server.

Step 4 – Open port 80 and 443 using firewall-cmd

You must open and enable port 80 and 443 using the firewall-cmd command:
$ sudo firewall-cmd --permanent --zone=public --add-service=http
$ sudo firewall-cmd --permanent --zone=public --add-service=https
$ sudo firewall-cmd --reload

RHEL7 CentOS7 allow HTTP and HTTPS traffic with firewall-cmd

Step 5 – Test it

Verify that port 80 or 443 opened using ss command:
$ sudo ss -tulpn
Sample outputs:

Netid State      Recv-Q Send-Q         Local Address:Port                        Peer Address:Port              
udp   UNCONN     0      0                          *:60139                                  *:*                   users:(("dhclient",pid=242,fd=20))
udp   UNCONN     0      0                          *:68                                     *:*                   users:(("dhclient",pid=242,fd=6))
udp   UNCONN     0      0                         :::34050                                 :::*                   users:(("dhclient",pid=242,fd=21))
tcp   LISTEN     0      128                        *:80                                     *:*                   users:(("nginx",pid=696,fd=6),("nginx",pid=695,fd=6))

If you do not know your server IP address run the following ip command:
$ ip a
Sample outputs:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
4: eth0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP qlen 1000
    link/ether 00:16:3e:ac:ba:1e brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet brd scope global dynamic eth0
       valid_lft 3348sec preferred_lft 3348sec
    inet6 fe80::216:3eff:feac:ba1e/64 scope link 
       valid_lft forever preferred_lft forever

So my IP address is Fire a web browser and type the ip address:
Welcome to Nginx
You can also use the curl command to get same info using the cli:
$ curl -I
$ curl

Test Nginx working from the CLI using curl command

Step 6 – Configure Nginx server

  • Config dir – /etc/nginx/
  • Master/Global config file – /etc/nginx/nginx.conf
  • Port 80 http config file – /etc/nginx/conf.d/default
  • TCP ports opened by Nginx – 80 (HTTP), 443 (HTTPS)
  • Document root directory – /usr/share/nginx/html

To edit files use a text editor such as vi
$ sudo vi /etc/nginx/conf.d/default
You can upload or copy your html/css/js and images to /usr/share/nginx/html/
$ cd /usr/share/nginx/html/
$ sudo cp /backups/*.html .
$ sudo cp /backups/*.css .

Copy from local desktop to the remote server using the rsync command or rsync command:
$ rsync ~/projects/static/* root@
In next part of the series I will show you how to install the latest version of PHP 7.x.x on a CentOS 7/RHEL 7.

This entry is 1 of 3 in the Linux, Nginx, MySQL, PHP (LEMP) Stack for CentOS/RHEL 7 Tutorial series. Keep reading the rest of the series:
  1. How to install and use Nginx on CentOS 7 / RHEL 7
  2. How to install PHP 7.2 on CentOS 7/RHEL 7
  3. How to configure Nginx with Let's Encrypt on CentOS 7

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Share this on (or read 0 comments/add one below):

Start the discussion at