Nginx, pronounced “engine-ex,” is a free and open-source web server used to host websites on the Internet. It is lightweight and high-performance and can be used for reverse proxy, caching, load balancing, media streaming, and more. It is a fast, highly scalable, and highly available web server compared to the Apache web server. It is one of the best web servers in the world. Nginx is the first choice for many websites due to its ability to handle massive connections.

In this post, we will show you how to install the latest Nginx Mainline on Oracle Linux 8.

Step 1 – Add an Nginx Repository

By default, the latest Nginx mainline version is not included in the Oracle Linux Appresteam repository. For a production environment, it is always recommended to install the latest version.

First, install the dnf-utils package using the following command:

dnf update -y
dnf install dnf-utils -y

Next, create an Nginx repo with the following command:

nano /etc/yum.repos.d/nginx.repo

Add the following lines:

[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

Save and close the file, then enable the Nginx Mainline repo using the following command:

yum-config-manager --enable nginx-mainline

Step 2 – Install Nginx Mainline on Oracle Linux 8

Now, install the latest Nginx Mainline package by running the following command:

dnf install nginx

After Nginx is successfully installed, start the Nginx service and enable it to start at system reboot:

systemctl start nginx
systemctl enable nginx

Next, verify the status of Nginx with the following command:

systemctl status nginx

You should get the following output:

● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2022-06-08 12:21:41 EDT; 19s ago
     Docs: http://nginx.org/en/docs/
  Process: 14372 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 14373 (nginx)
    Tasks: 3 (limit: 23694)
   Memory: 2.8M
   CGroup: /system.slice/nginx.service
           ├─14373 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           ├─14374 nginx: worker process
           └─14375 nginx: worker process

Jun 08 12:21:41 oraclelinux8 systemd[1]: Starting nginx - high performance web server...
Jun 08 12:21:41 oraclelinux8 systemd[1]: Started nginx - high performance web server.

You can now verify the Nginx version using the following command:

nginx -v

You should see the Nginx version in the following output:

nginx version: nginx/1.22.0

Step 3 – Configure Firewall

By default, Nginx listens on ports 80 and 443. If any firewall is installed and configured to your server, then you will need to allow both ports via firewalld. You can allow them with the following command:

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

Next, reload firewalld to apply the changes:

firewall-cmd --reload

Step 4 – Access Nginx Default Page

Now, open your web browser and access the Nginx default page using the URL http://your-server-ip. You should see the Nginx default page on the following screen:
Nginx test page

Step 5 – Host a Simple Website with Nginx

First, create a directory to hold the website data with the following command:

mkdir /var/www/html/test.example.com

Next, create a simple HTML file inside the website directory:

nano /var/www/html/test.example.com/index.html

Add the following HTML code:

<html>
<h1>Welcome to the Example Website!</h1>
</html>

Save and close the file, then change the ownership and permissions of the website directory:

chown -R www-data:www-data /var/www/html/test.example.com/
chmod -R 775 /var/www/html/test.example.com/

Next, create an Nginx virtual host configuration file to define the website path.

nano /etc/nginx/conf.d/test.example.com.conf

Add the following configuration:

server {
listen 80;
server_name test.example.com;
root /var/www/html/test.example.com;
index index.html;
}

Save and close the file, then edit the Nginx main configuration file:

nano /etc/nginx/nginx.conf

Add the following line below http {:

server_names_hash_bucket_size 64;

Save and close the file, then verify Nginx for any syntax configuration errors:

nginx -t

You should get the following output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Next, restart the Nginx service to apply the configuration changes:

systemctl restart nginx

Now, open your web browser and verify your website using the URL http://test.example.com. You should see your website page on the following screen:
Verify Nginx

Conclusion

In the above post, we explained how to install the latest Nginx mainline version on Oracle Linux 8. We also explained how to create a new website and host it using the Nginx virtual host. Try it on VPS hosting from Atlantic.Net!