How to install and use Nginx on CentOS 8

last updated in Categories , , ,

How do I Install Nginx on CentOS 8 Linux server? How can configure the latest version of Nginx web server on a CentOS Enterprise Linux 8 server using the CLI and host a static site?

Nginx [engine X] is a free and open-source high-performance web server. It also acts as a reverse proxy server and load balancer. This page shows how to install the Nginx server on a CentOS 8 and configure a static web site.

How to install Nginx on CentOS 8

The procedure to install Nginx web server on a CentOS Linux 8 is as follows:

  1. Login to your cloud server or bare metal server using ssh command:
    ssh user@cloud-server-ip
  2. Search for Nginx package:
    sudo yum search nginx
  3. Install nginx package using the yum command on CentOS 8:
    sudo yum update
    sudo yum install nginx
  4. Update firewall settings and open TCP port 80 and 443. Run:
    sudo firewall-cmd --permanent --zone=public --add-service=https --add-service=http
    sudo firewall-cmd --reload

Let us see all commands and examples in details.

Step 1 – Update the system

Keeping your system, kernel, and the installed application is an essential sysadmin task. So update the system, run:
sudo yum updateinfo
sudo yum update
## Reboot the system if a new kernel update was installed ##
sudo reboot

Step 2 – Search for Nginx package

Is web server available in my Linux distro? Let us find out:
sudo yum search nginx
sudo yum list nginx

 Last metadata expiration check: 1:09:02 ago on Sun Nov 24 17:24:15 2019.
============================== Name Exactly Matched: nginx ==============================
nginx.x86_64 : A high performance web server and reverse proxy server
============================= Name & Summary Matched: nginx =============================
nginx-mod-mail.x86_64 : Nginx mail modules
nginx-mod-stream.x86_64 : Nginx stream modules
collectd-nginx.x86_64 : Nginx plugin for collectd
nginx-mod-http-perl.x86_64 : Nginx HTTP perl module
nginx-mod-http-xslt-filter.x86_64 : Nginx XSLT module
nginx-mod-http-image-filter.x86_64 : Nginx HTTP image filter module
nginx-filesystem.noarch : The basic directory layout for the Nginx server
pcp-pmda-nginx.x86_64 : Performance Co-Pilot (PCP) metrics for the Nginx Webserver
nginx-all-modules.noarch : A meta package that installs all available Nginx modules

What version of Nginx am I going to install? Get Nginx version information that you are going to install, execute:
sudo yum info nginx
Sample outputs:

Last metadata expiration check: 1:11:11 ago on Sun Nov 24 17:24:15 2019.
Installed Packages
Name         : nginx
Epoch        : 1
Version      : 1.14.1
Release      : 9.module_el8.0.0+184+e34fea82
Arch         : x86_64
Size         : 1.7 M
Source       : nginx-1.14.1-9.module_el8.0.0+184+e34fea82.src.rpm
Repo         : @System
From repo    : AppStream
Summary      : A high performance web server and reverse proxy server
URL          : http://nginx.org/
License      : BSD
Description  : Nginx is a web server and a reverse proxy server for HTTP, SMTP, POP3 and
             : IMAP protocols, with a strong focus on high concurrency, performance and
             : low memory usage.

Step 3 – Install Nginx on CentOS 8

To install the latest stable nginx server, run the following [nixmd name=”yum”]:
$ sudo yum install nginx

Install Nginx on CentOS 8 server
Installing Nginx on CentOS Enterprise Linux 8 server

Step 4 – Enable nginx server

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

Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service ? /usr/lib/systemd/system/nginx.service.

Start the service, run:
sudo systemctl start nginx

Commands to start/stop/restart nginx server

Run command as per your needs.
sudo systemctl start nginx ## &lt-- start the server ##
sudo systemctl stop nginx ## &lt-- stop the server ##
sudo systemctl restart nginx ## &lt-- restart the server ##
sudo systemctl reload nginx ## &lt-- reload the server ##
sudo systemctl status nginx ## &lt-- get status of the server ##

Start restart Nginx server command

Step 5 – 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 --add-service=https
$ sudo firewall-cmd --reload
$ sudo firewall-cmd --list-services --zone=public

CentOS 8 nginx open port TCP port 80 and 443
Firewall configuration to open http/https port

See “how to set up a firewall using FirewallD on CentOS 8” for more info

Step 6 – Test it

Verify that port 80 or 443 opened using ss command command:
sudo ss -tulpn
Sample outputs (look out for :80 and :443 lines) :

Netid       State         Recv-Q        Send-Q                    Local Address:Port               Peer Address:Port                                                                                               
udp         UNCONN        0             0                     10.147.164.2%eth0:68                      0.0.0.0:*           users:(("NetworkManager",pid=50,fd=15))                                                
tcp         LISTEN        0             128                             0.0.0.0:80                      0.0.0.0:*           users:(("nginx",pid=1316,fd=6),("nginx",pid=1315,fd=6),("nginx",pid=1314,fd=6))        
tcp         LISTEN        0             128                                [::]:80                         [::]:*           users:(("nginx",pid=1316,fd=7),("nginx",pid=1315,fd=7),("nginx",pid=1314,fd=7)) 
tcp         LISTEN        0             128                                [::]:443                         [::]:*           users:(("nginx",pid=1316,fd=7),("nginx",pid=1315,fd=7),("nginx",pid=1314,fd=7))

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 group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
6: eth0@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:16:3e:6b:8d:f7 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 10.147.164.2/24 brd 10.147.164.255 scope global dynamic noprefixroute eth0
       valid_lft 3067sec preferred_lft 3067sec
    inet6 fe80::216:3eff:fe6b:8df7/64 scope link 
       valid_lft forever preferred_lft forever

So my IP address is 10.21.136.134. Fire a web browser and type the URL(domain name)/IP address:
http://10.147.164.2

Nginx running on a CentOS Enterprise Linux 8 server
Nginx running on a CentOS Enterprise Linux 8 server

One can also use the curl command to get same info using the cli:
curl -I http://10.147.164.2
curl http://10.147.164.2

Centos 8 Curl command demo

Step 7 – Configure Nginx server

  • CentOS 8 Nginx Config directory – /etc/nginx/
  • Master/Global config file – /etc/nginx/nginx.conf
  • 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 command/nano command:
$ sudo vi /etc/nginx/nginx.conf
Sample outputs:

# For more information on configuration, see:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
 
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
 
events {
    worker_connections 1024;
}
 
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log  /var/log/nginx/access.log  main;
 
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
 
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
 
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
 
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
 
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
 
        location / {
        }
 
        error_page 404 /404.html;
            location = /40x.html {
        }
 
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

See Nginx server docs here.
You can upload or copy your html/css/js and images to /usr/share/nginx/html/
cd /usr/share/nginx/html/
sudo cp /backups/cyberciti.biz/*.html .
sudo cp /backups/cyberciti.biz/*.css .

Copy from local desktop to the remote server using the rsync command or scp command/sftp command:
rsync ~/projects/static/www.cyberciti.biz/prod/* your-username@10.147.164.2:/usr/share/nginx/html/

How to secure Nginx server

See “Top 25 Nginx Web Server Best Security Practices” and “40 Linux server security tips” for more info.

Conclusion

You just learned how to install, set up and configure Nginx server on a CentOS Enterprise Linux 8 server. In the next part of the series, I will show you how to install the latest version of PHP 7.x.x on a CentOS 8.

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.

Start the discussion at www.nixcraft.com

Historical Comment Archive

1 comment

    Still, have a question? Get help on our forum!