DEV Community

Deploying Nginx on Linux (RHEL and Ubuntu)

Image description

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
Enter fullscreen mode Exit fullscreen mode

##For Ubuntu:

sudo apt update -y
sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Nginx

##For RHEL:

  • Enable the EPEL repository:
sudo yum install epel-release -y
Enter fullscreen mode Exit fullscreen mode
  • Install Nginx:
sudo yum install nginx -y
Enter fullscreen mode Exit fullscreen mode

##For Ubuntu:

  • Install Nginx:
sudo apt install nginx -y
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

##For Ubuntu:

sudo systemctl start nginx
sudo systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

##For Ubuntu:

sudo ufw allow 'Nginx Full'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

After making changes, you need to restart Nginx to apply the changes:

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Create an HTML file:
sudo vim /var/www/html/index.html
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

##For Ubuntu:

sudo vim /etc/nginx/sites-available/default
Enter fullscreen mode Exit fullscreen mode

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;
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Restart Nginx:
sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
janarbeit profile image
Jan Wittig

What about SELinux? Is this completely handled by the firewall exception?

Collapse
 
shantanu_deo_015638630af2 profile image
Shantanu Deo

Very informative..thank you for this blog