How to Install Nginx on AlmaLinux

Categories:
5 minute read
Nginx (pronounced “Engine-X”) is a powerful, lightweight, and highly customizable web server that also functions as a reverse proxy, load balancer, and HTTP cache. Its performance, scalability, and ease of configuration make it a popular choice for hosting websites and managing web traffic.
For users of AlmaLinux, a robust and RHEL-compatible operating system, Nginx offers a seamless way to deploy and manage web applications. This guide will walk you through the step-by-step process of installing and configuring Nginx on AlmaLinux.
Prerequisites
Before we begin, ensure you meet these prerequisites:
- A Running AlmaLinux Instance: The tutorial assumes AlmaLinux 8 or later is installed.
- Sudo or Root Access: You’ll need administrative privileges for installation and configuration.
- A Basic Understanding of the Command Line: Familiarity with Linux commands will be helpful.
Step 1: Update Your AlmaLinux System
Keeping your system updated ensures that all installed packages are current and secure. Open a terminal and run the following commands:
Rebooting ensures all updates are applied correctly.
Step 2: Install Nginx
Add the EPEL Repository
Nginx is available in AlmaLinux’s Extra Packages for Enterprise Linux (EPEL) repository. First, ensure the EPEL repository is installed:
Install Nginx
Once the EPEL repository is enabled, install Nginx using the dnf
package manager:
Verify Installation
Check the installed Nginx version to ensure it was installed correctly:
You should see the version of Nginx that was installed.
Step 3: Start and Enable Nginx
After installation, start the Nginx service:
Enable Nginx to start automatically on boot:
Verify that Nginx is running:
You should see an output indicating that Nginx is active and running.
Step 4: Adjust the Firewall to Allow HTTP and HTTPS Traffic
By default, AlmaLinux’s firewall blocks web traffic. To allow HTTP and HTTPS traffic, update the firewall settings:
Confirm that the changes were applied:
You should see HTTP and HTTPS listed under “services”.
Step 5: Verify Nginx Installation
Open a web browser and navigate to your server’s IP address:
You should see the default Nginx welcome page, confirming that the installation was successful.
Step 6: Configure Nginx
Understanding Nginx Directory Structure
The main configuration files for Nginx are located in the following directories:
- /etc/nginx/nginx.conf: The primary Nginx configuration file.
- /etc/nginx/conf.d/: A directory for additional configuration files.
- /usr/share/nginx/html/: The default web document root directory.
Create a New Server Block
A server block in Nginx is equivalent to a virtual host in Apache. It allows you to host multiple websites on the same server.
Create a new configuration file for your website:
Add the following configuration:
Replace yourdomain.com
with your actual domain name or IP address. Save and close the file.
Create the Document Root
Create the document root directory for your website:
Add a sample index.html
file:
Set proper ownership and permissions:
Step 7: Test Nginx Configuration
Before restarting Nginx, test the configuration for syntax errors:
If the output indicates “syntax is ok” and “test is successful,” restart Nginx:
Step 8: Secure Nginx with SSL/TLS
To secure your website with HTTPS, install SSL/TLS certificates. You can use Let’s Encrypt for free SSL certificates.
Install Certbot
Install Certbot and its Nginx plugin:
Obtain and Configure SSL Certificate
Run the following command to obtain and install an SSL certificate for your domain:
Follow the prompts to complete the process. Certbot will automatically configure Nginx to use the certificate.
Verify HTTPS Setup
Once completed, test your HTTPS configuration by navigating to:
You should see a secure connection with a padlock in the browser’s address bar.
Set Up Automatic Renewal
Ensure your SSL certificate renews automatically:
Test the renewal process:
Step 9: Monitor and Maintain Nginx
Log Files
Monitor Nginx logs for troubleshooting and performance insights:
- Access Logs:
/var/log/nginx/access.log
- Error Logs:
/var/log/nginx/error.log
Use the tail
command to monitor logs in real-time:
Restart and Reload Nginx
Reload Nginx after making configuration changes:
Restart Nginx if it’s not running properly:
Update Nginx
Keep Nginx updated to ensure you have the latest features and security patches:
Conclusion
By following this guide, you’ve successfully installed and configured Nginx on AlmaLinux. From serving static files to securing your server with SSL/TLS, Nginx is now ready to host your websites or applications efficiently.
For further optimization, consider exploring advanced Nginx features such as reverse proxying, load balancing, caching, and integrating dynamic content through FastCGI or uWSGI. By leveraging Nginx’s full potential, you can ensure high-performance and secure web hosting tailored to your needs.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.