Nginx is a popular open-source web server used for serving static content, reverse proxying, load balancing, and more. This guide will walk you through installing and configuring Nginx on both RHEL and Ubuntu Linux distributions.
Prerequisites
A running instance of either RHEL or Ubuntu.
Root or sudo privileges.
Step 1: Update the System
Before installing any new packages, it’s a good practice to update your package index.
##For RHEL:
##For Ubuntu:
sudo apt upgrade -y
Step 2: Install Nginx
##For RHEL:
Enable the EPEL repository:
Install Nginx:
##For Ubuntu:
Install Nginx:
Step 3: Start and Enable Nginx
Ensure that Nginx starts on boot and is running.
##For RHEL:
sudo systemctl enable nginx
##For Ubuntu:
sudo systemctl enable nginx
Step 4: Configure Firewall
Allow HTTP and HTTPS traffic through the firewall.
##For RHEL:
sudo firewall-cmd –zone=public –permanent –add-service=https
sudo firewall-cmd –reload
##For Ubuntu:
Step 5: Verify Nginx Installation
To verify that Nginx is installed and running, open a web browser and navigate to your server’s IP address. You should see the default Nginx welcome page.
Alternatively, you can use the following command to check Nginx’s status:
Step 6: Basic Nginx Configuration
Nginx configuration files are located in /etc/nginx directory.
Main Configuration File:
The main configuration file is located at /etc/nginx/nginx.conf.
Site-Specific Configuration:
For RHEL: /etc/nginx/conf.d/default.conf
For Ubuntu: /etc/nginx/sites-available/default
Editing the Configuration
You can edit the default configuration file to customize your Nginx setup. For example, to change the root directory of your web server or to set up a reverse proxy.
After making changes, you need to restart Nginx to apply the changes:
Step 7: Creating a Simple Website
To create a simple HTML page served by Nginx, follow these steps:
Create a directory for your website:
Create an HTML file:
Add the following content:
<html>
<head>
<title>Welcome to Nginx</title>
</head>
<body>
<h1>Hello, World!</h1>
<p>This is a simple web page served by Nginx on Linux.</p>
</body>
</html>
Point Nginx to your website directory:
Edit the site-specific configuration file to set the root directory.
##For RHEL:
##For Ubuntu:
Change the root directive to your new directory:
listen 80;
server_name your_domain_or_IP;
root /var/www/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
Restart Nginx:
Now, navigate to your server’s IP address in a web browser, and you should see your “Hello, World!” page.
Conclusion
You have successfully installed and configured Nginx on both RHEL and Ubuntu servers. From here, you can further customize Nginx to suit your needs, whether it’s for serving static content, setting up reverse proxies, or load balancing. Happy hosting!