In this tutorial, we will show you how to install Nginx on AlmaLinux 8. For those of you who didn’t know, Nginx (Pronounce as Engine X) is a powerful web server software that can be used on your server. It is also known for its high performance and low memory usage which will allow fewer resources to be used while getting the job done efficiently. A popular setup is to use it as a proxy for Apache, which can then serve application requests.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step installation of the Nginx on an AlmaLinux 8.
Prerequisites
- A server running one of the following operating systems: AlmaLinux 8.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Install Nginx on AlmaLinux 8
Step 1. First, let’s start by ensuring your system is up-to-date.
sudo dnf update sudo dnf install epel-release
Step 2. Installing Nginx on AlmaLinux 8.
Now we run the following command to install the Nginx webserver:
sudo dnf install nginx
Once it is installed, you can start and enable the Nginx service using the following command:
sudo systemctl enable nginx sudo systemctl start nginx
After that, we add HTTP and HTTPS ports in the firewall using the following command:
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload
Next, verify that the webserver is running and accessible by accessing your server’s IP address:
http://your-server-ipadress
Step 3. Nginx Configuration File’s Structure.
You don’t need to configure Nginx after successful installation. However, you should know the location of the configuration files and the Nginx root directory in case you need to modify the configuration.
- Nginx configuration directory:
/etc/nginx
- Nginx root directory:
/usr/share/nginx/html
- Master/Global configuration file:
/etc/nginx/nginx.conf
Step 4. Set up the Nginx Server Blocks.
Nginx server blocks are the same as the Apache virtual hosts, allowing a single server to respond to multiple domain names and serve different content for each of them. In the tutorial, you will set up a domain called your-domian.com, but you should replace this with your domain name. The tutorial will create the web directories and configure the site files in the parent directory /var/www/
.
sudo mkdir -p /var/www/your-domain/public_html
Next, set directory permissions:
sudo chown -R $USER:$USER /var/www/your-domain/public_html sudo chmod -R 755 /var/www/your-domain
Then, create a test page server block configuration using your favorite text editor:
nano /var/www/your-domain.com/public_html/index.html
Add the following HTML code to the file:
<html> <head> <title>Welcome to your-domain.com</title> </head> <body> <h1>Success! Your Nginx server is successfully configured for <em>your-domain.com</em>. </h1> <p>This is a sample page.</p> </body> </html>
Save and close your file when you are finished, Then create a new server block configuration:
sudo nano /etc/nginx/conf.d/your-domian.com.conf
Add the following file:
server { server_name www.your-domain.com your-domain.com; server_tokens off; access_log /var/www/your-domain.com/logs/access.log ; error_log /var/www/your-domain.com/logs/error.log ; gzip on; gzip_http_version 1.1; gzip_vary on; gzip_comp_level 5; gzip_proxied any; gzip_types text/plain text/css application/json application/x-javascript te$ gzip_disable "MSIE [1-6]\.(?!.*SV1)"; gzip_buffers 16 8k; root /var/www/your-domain.com/public_html; index index.php index.html index.htm; if (!-e $request_filename) { rewrite ^(.*)$ /index.php?q=$1 last; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { try_files $uri =404; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_read_timeout 300; root /var/www/your-domain.com/public_html; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_nam$ include fastcgi_params; } location ~ /\.ht { deny all; }
Finally, restart Nginx with the following command for the changes to take place:
nginx -t sudo systemctl restart nginx
Now you can test your custom domain setup by typing your domain name in your web browser:
http://your-domain.com
Congratulations! You have successfully installed Nginx. Thanks for using this tutorial for installing the Nginx web server on your AlmaLinux 8 system. For additional help or useful information, we recommend you check the official Nginx website.