How to Install NGINX on CentOS 8 Server

Introduction

Pronounced as “Engine X”, it is one of the famous web server available on the market today. NGINX is very well known with the expertise of serving static files, load balancer, reverse proxy and HTTP caching. There two method to install NGINX. You can NGINX nginx from repository or you can compile from the source code. This tuorial will guide you on how to install NGINX on CentOS from repository.

Prerequisites

This tutorial requires user to have root privileges to do installation and configuration. You can use this tutorial to install NGINX on CentosOS 8 and AlmaLinux 8.

Step 1 : Update repository

Before we start the NGINX installation, we have to update the operating system to make sure we have the latest list of repository.

sudo dnf update -y

Note : -y is optional parameter. It allow you to execute the update command without any prompt for confirmation. If you did not include the -y, you can enter the confirmation later.

Step 2 : Install Extra Packages for Enterprise Linux (EPEL)

sudo dnf install epel-release -y

By default, NGINX does not come with standard CentOS repositories. So we need to install EPEL repository to allow us install NGINX.

Step 3 : Install NGINX

Previous step has install EPEL repository. Now we can install NGINX using following command :

sudo dnf install nginx -y

Step 4 : Start NGINX service

By default NGINX should start automatically on CentOS 8 after the installation. If the service not running automatically, you can execute start NGINX service command :

sudo systemctl start nginx

Alternatively, you can execute this command to start NGINX service :

sudo service nginx start

To make sure NGINX service is running, execute this command to check :

sudo systemctl status nginx

Alternatively, you can execute this command to check NGINX service status :

sudo service nginx status

Here is the result of the status command :

Redirecting to /bin/systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/nginx.service.d
           └─php-fpm.conf
   Active: active (running) since Sat 2021-04-24 05:41:16 EDT; 2min 58s ago
 Main PID: 1927 (nginx)
    Tasks: 3 (limit: 11236)
   Memory: 22.4M
   CGroup: /system.slice/nginx.service
           ├─1927 nginx: master process /usr/sbin/nginx
           ├─1928 nginx: worker process
           └─1929 nginx: worker process

Apr 24 05:41:16 localhost.localdomain systemd[1]: Starting The nginx HTTP and reverse proxy server...
Apr 24 05:41:16 localhost.localdomain nginx[871]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Apr 24 05:41:16 localhost.localdomain nginx[871]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Apr 24 05:41:16 localhost.localdomain systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Apr 24 05:41:16 localhost.localdomain systemd[1]: Started The nginx HTTP and reverse proxy server.

Step 5 : Configure NGINX to auto start on boot

Nginx will not auto start after the system boot up by default. Thus we need to configure the operating system to automatically start NGINX after the system boot up. This command will help us to do that :

sudo systemctl enable nginx

The output of the command should be like this :

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

Step 6 : Configure firewall to allow traffic access NGINX page

By default, CentOS enable the firewall and block access to port 80 (HTTP) & 443 (HTTPS). To allow inbound traffic to these port, execute these commands :

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

Step 7 : Verify NGINX access

To verify the NGINX is accessible from outside of the server, you need to access the server IP and you should get the NGINX default page.

First of all, check the server IP by execute this command :

ip a

We should get this output. Find you server IP

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
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:be:b8:90 brd ff:ff:ff:ff:ff:ff
    inet 192.168.64.129/24 brd 192.168.64.255 scope global dynamic noprefixroute ens33
       valid_lft 1449sec preferred_lft 1449sec
    inet6 fe80::9024:7dd3:836a:a2f5/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

From the output, we can see that the server IP is 192.168.64.129. Copy the IP and put in the web browser and hit enter. We should get the default NGINX page.

Well done ! You have completely install NGINX on CentOS 8 server. Now you can serve static content such as HTML and images. To extend more function of your CentOS server, you can proceed to install LEMP stack which will require you to install MySQL and PHP to serve dynamic website such as WordPress.

Kindly support me by sharing this article. It will help me to produce more tutorials. Thank you !

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x