Install Nginx Web Server on AlmaLinux 9 | Best Web Server
In this comprehensive guide, brought to you by Orcacore, we will explore the process of installing the Nginx web server on AlmaLinux 9. Nginx is a powerful and versatile open-source web server renowned for its capabilities as an email proxy, reverse proxy, and load balancer. Its asynchronous, event-driven architecture allows it to handle a large number of concurrent requests efficiently, making it a highly scalable solution for websites experiencing growth in traffic.
Before we begin, ensure you have the following prerequisites in place:
- Access to an AlmaLinux 9 server as a non-root user with sudo privileges. You can refer to Orcacore’s guide on "Initial Server Setup with AlmaLinux 9" for assistance.
- A domain name pointing to your server’s IP address.
Let’s dive into the installation process of the Install Nginx Web Server on AlmaLinux 9.
1. Install Nginx on AlmaLinux 9
First, it is important to update your local package index. This ensures that you have the latest information about available packages. Run the following command:
sudo dnf update -y
Next, install the Nginx web server using the following command:
sudo dnf install nginx -y
Output:
Installed:
almalinux-logos-httpd-90.5.1-1.1.el9.noarch
nginx-1:1.20.1-10.el9.alma.x86_64
nginx-filesystem-1:1.20.1-10.el9.alma.noarch
Complete!
Once the installation is complete, enable and start the Nginx web server:
sudo systemctl enable nginx
sudo systemctl start nginx
Verify that Nginx is active and running:
sudo systemctl status nginx
Output:
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor pre>
Active: **active** (**running**) since Mon 2022-09-19 04:17:16 EDT; 36s ago
Process: 4568 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, stat>
Process: 4569 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCES>
Process: 4570 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 4571 (nginx)
Tasks: 3 (limit: 23609)
Memory: 2.8M
CPU: 34ms
...
2. Configure Firewall For Nginx Web Server
Assuming you have already enabled firewalld
, you need to allow HTTP connections through the firewall:
sudo firewall-cmd --permanent --add-service=http
Verify the addition of the HTTP service:
sudo firewall-cmd --permanent --list-all
Output:
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: cockpit dhcpv6-client **http** ssh
ports:
protocols:
forward: yes
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
Reload the firewall to apply the changes:
sudo firewall-cmd --reload
Nginx is now installed on your AlmaLinux 9 server. Let’s verify its operation.
Access Default Nginx Landing Page
You can confirm that your server is running by accessing your server’s public IP address in a web browser. If you don’t know your IP address, use the following command:
hostname -I
Alternatively, use curl
to fetch your IP from icanhazip.com
:
curl -4 icanhazip.com
Enter your IP address in your web browser:
http://your_server_IP_address
If you see the default Nginx welcome page, your installation was successful.
3. Manage Nginx Process on AlmaLinux 9
Now that Nginx is running, you can manage its process using systemctl
.
To stop Nginx:
sudo systemctl stop nginx
To start Nginx:
sudo systemctl start nginx
To restart Nginx:
sudo systemctl restart nginx
To reload Nginx (applies configuration changes without downtime):
sudo systemctl reload nginx
To disable Nginx from starting at boot:
sudo systemctl disable nginx
To enable Nginx to start at boot:
sudo systemctl enable nginx
4. Set up Nginx Server Blocks on AlmaLinux 9
Nginx server blocks, similar to Apache virtual hosts, allow you to host multiple websites on a single server.
First, create a directory for your domain:
sudo mkdir -p /var/www/your-domain/html
Assign ownership of the directory to your user:
sudo chown -R $USER:$USER /var/www/your-domain/html
Create a sample index.html
file:
sudo vi /var/www/your-domain/html/index.html
Add the following HTML code:
<html>
<head>
<title>Welcome to your-domain</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 the file.
Create a server block configuration file:
sudo vi /etc/nginx/conf.d/your-domain.conf
Paste the following configuration block:
server {
listen 80;
listen [::]:80;
root /var/www/your-domain/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 the file.
Test the Nginx configuration for syntax errors:
sudo nginx -t
Output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Restart Nginx to apply the changes:
sudo systemctl restart nginx
Allow HTTP content to be served from your custom document root:
chcon -vR system_u:object_r:httpd_sys_content_t:s0 /var/www/your-domain/
Now, access your domain name in a web browser:
http://your_domain_name
You should see the sample page you created, confirming that your server block is correctly configured.
That concludes the basic setup of Install Nginx Web Server on AlmaLinux 9.
Conclusion
Installing Nginx on AlmaLinux 9 and setting up server blocks allows you to efficiently host multiple websites on a single server. It provides a high-performance, scalable, and flexible web hosting solution with easy domain management.
Hope you enjoyed the guide. Please subscribe to Orcacore on Facebook, X, and YouTube.
You may also like to read the following articles:
Ubuntu 24.04 Nginx Reverse Proxy Setup
Install Nginx Proxy Manager on Ubuntu 22.04
Run Nginx in Docker Container on AlmaLinux 9
Alternative Solutions for Installing Nginx on AlmaLinux 9
While the above method using dnf
is straightforward, here are two alternative approaches for installing and managing Nginx on AlmaLinux 9:
1. Using a Pre-built Docker Image
Docker provides a containerization platform that simplifies application deployment. Instead of installing Nginx directly on the host operating system, you can run it within a Docker container. This offers benefits such as isolation, portability, and simplified management.
Explanation:
Docker containers package an application and its dependencies into a single unit, ensuring consistent behavior across different environments. By using a pre-built Nginx Docker image, you avoid the manual installation and configuration steps.
Steps:
-
Install Docker: If Docker is not already installed, install it using the following commands:
sudo dnf install docker -y sudo systemctl start docker sudo systemctl enable docker
-
Pull the Nginx Image: Pull the official Nginx image from Docker Hub:
sudo docker pull nginx:latest
-
Run the Nginx Container: Run the container, mapping port 80 on the host to port 80 on the container:
sudo docker run -d -p 80:80 --name mynginx nginx:latest
-
Verify the Installation: Access your server’s IP address in a web browser. You should see the default Nginx welcome page.
-
Manage the Container: Use Docker commands to manage the Nginx container, such as:
sudo docker stop mynginx
: Stop the container.sudo docker start mynginx
: Start the container.sudo docker restart mynginx
: Restart the container.sudo docker logs mynginx
: View the container logs.
Code Example (docker-compose.yml):
For a more structured approach, you can use Docker Compose to define and manage your Nginx container. Create a docker-compose.yml
file with the following content:
version: "3.8"
services:
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./html:/usr/share/nginx/html
restart: always
Then, run the container using:
sudo docker-compose up -d
This will start an Nginx container, mapping port 80 and mounting a local directory (./html
) as the web root.
2. Using Ansible for Automated Deployment
Ansible is an automation tool that allows you to provision and configure servers in a repeatable and consistent manner. You can use Ansible to automate the installation and configuration of Nginx on AlmaLinux 9.
Explanation:
Ansible uses playbooks, which are YAML files that define the desired state of your system. By creating an Ansible playbook for Nginx installation, you can easily deploy Nginx to multiple servers with a single command.
Steps:
-
Install Ansible: If Ansible is not already installed, install it using
pip
:sudo dnf install python3-pip -y pip3 install ansible
-
Create an Ansible Playbook: Create a file named
nginx_install.yml
with the following content:--- - hosts: all become: true tasks: - name: Update package cache apt: update_cache: yes when: ansible_distribution == "AlmaLinux" - name: Install Nginx dnf: name: nginx state: present - name: Start and enable Nginx systemd: name: nginx state: started enabled: yes
-
Run the Playbook: Run the playbook against your target server(s):
ansible-playbook nginx_install.yml -i "your_server_ip," -u your_username -k
Replace
your_server_ip
with the IP address of your AlmaLinux 9 server,your_username
with your username, and-k
to prompt for the sudo password.
This playbook will update the package cache, install Nginx, and start and enable the Nginx service.
These alternative methods provide more flexibility and automation for installing and managing Nginx on AlmaLinux 9, catering to different needs and preferences. The best choice will depend on your specific requirements and infrastructure. However, understanding these options expands your ability to effectively deploy and manage the Install Nginx Web Server on AlmaLinux 9.