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:
sudo yum update -y
##For Ubuntu:
sudo apt update -y
sudo apt upgrade -y
Step 2: Install Nginx
##For RHEL:
- Enable the EPEL repository:
sudo yum install epel-release -y
- Install Nginx:
sudo yum install nginx -y
##For Ubuntu:
- Install Nginx:
sudo apt install nginx -y
Step 3: Start and Enable Nginx
Ensure that Nginx starts on boot and is running.
##For RHEL:
sudo systemctl start nginx
sudo systemctl enable nginx
##For Ubuntu:
sudo systemctl start nginx
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=http
sudo firewall-cmd --zone=public --permanent --add-service=https
sudo firewall-cmd --reload
##For Ubuntu:
sudo ufw allow 'Nginx Full'
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:
sudo systemctl status nginx
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.
sudo vim /etc/nginx/nginx.conf
After making changes, you need to restart Nginx to apply the changes:
sudo systemctl restart nginx
Step 7: Creating a Simple Website
To create a simple HTML page served by Nginx, follow these steps:
- Create a directory for your website:
sudo mkdir -p /var/www/html
- Create an HTML file:
sudo vim /var/www/html/index.html
Add the following content:
<!DOCTYPE html>
<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:
sudo vim /etc/nginx/conf.d/default.conf
##For Ubuntu:
sudo vim /etc/nginx/sites-available/default
Change the root directive to your new directory:
server {
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:
sudo systemctl 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!
Top comments (2)
What about SELinux? Is this completely handled by the firewall exception?
Very informative..thank you for this blog