Web Servers

How to Install NGINX on AlmaLinux 8

In this article, we’ll discuss how it is how to install NGINX on AlmaLinux 8.

Ngnix (Pronounce as Engine X) is a free, open source web server which can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. Unlike traditional servers, NGINX doesn’t rely on threads to handle requests. Instead it uses a much more scalable event-driven (asynchronous) architecture. This architecture uses small, but more importantly, predictable amounts of memory under load.

Install NGINX on AlmaLinux 8

Prerequisites

  • You need to install AlmaLinux 8 on KVM VPS or dedicated server.
  • SSH to the server using root user or normal user with administrative privileges.

1. Keep the server up-to-date:

# dnf update -y

2. Install NGINX using following command:

# dnf install nginx -y

After the installation is finished, run the following commands to enable and start the server:

3. Start and enable Nginx service

Once it is installed, you can start and enable Nginx service using following command:

# systemctl start nginx

# systemctl enable nginx

4. Configure firewall

Add http and https ports in firwall using following command:

# firewall-cmd --permanent --add-service=http

# firewall-cmd --permanent --add-service=https

To apply the changes, you’ll need to reload the firewall service:

# firewall-cmd --reload

That’s it, now you can access it using you server IP:

Open in a web browser:
http://server_domain_name_or_IP/

It will take you to Nginx’s default landing page:

Easy to Install Nginx on CentOS 8

The default server root directory is /usr/share/nginx/html. Files that are placed in there will be served on your web server. This location is specified in the default server block configuration file that ships with Nginx, which is located at /etc/nginx/conf.d/default.conf.

Configure server blocks

If you want to host multiple websites in the server, you need to configure server block for each website. On CentOS 8, server blocks are defined in .conf files located at /etc/nginx/conf.d.

The default server root directory is /usr/share/nginx/html this is works for single website but if you want to host multiple website, it can become unmanageable. Instead of that we’ll create a directory structure withing /var/www for website_name.

Create the directory for website_name as follows, using the -p flag to create any necessary parent directories:

# mkdir -p /var/www/website_name/html

Next, assign ownership of the directory with the $USER environment variable, which should reference your current system user:

# chown -R $USER:$USER /var/www/your_domain/html

Next, we’ll create a index.html file to test the server block configuration.

# vi /var/www/website_name/html/index.html

Inside that file, add the following HTML code:

<html>
<head>
<title>Welcome to Website name</title>
</head>
<body>
<h1>Success! Your Nginx server is successfully configured for <em>your_domain</em>. </h1>
<p>This is a sample page.</p>
</body>
</html>

Save and close.

In order for Nginx to serve this content, we need to create a server block with the correct directives that point to our custom web root. We’ll create a new server block at /etc/nginx/conf.d/your_domain.conf:

# vi /etc/nginx/conf.d/website_name.conf

Paste in the following configuration block:

server {
listen 80;
listen [::]:80;

root /var/www/website_name/html;
index index.html index.htm index.nginx-debian.html;

server_name your_domain www.your_domain;

location / {
try_files $uri $uri/ =404;
}
}

Save and close.

To make sure that there are no syntax errors in any of your Nginx files, run:

# nginx -t

Once your configuration test passes, restart Nginx to enable your changes:

# systemctl restart nginx

Update the SELinux security contexts so that Nginx is allowed to serve content from the /var/www/website_name directory.

The following command will allow your custom document root to be served as HTTP content:

# chcon -vR system_u:object_r:httpd_sys_content_t:s0 /var/www/website_name/

Now open your browser and navigate to http://website_name.

We have successfully installed and configured Nginx.

In this article, we have discussed how to install NGINX on AlmaLinux 8.

Related Articles