Step-by-Step Guide to Hosting a Website with Nginx on Linux

Nginx (pronounced “engine-x”) is a popular open-source web server, reverse proxy server, mail proxy server, and load balancer. It is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption. Here are some key aspects of Nginx:

Key Features

  1. Web Server: Nginx can serve static content, such as HTML, CSS, JavaScript, images, and videos, very efficiently.

  2. Reverse Proxy: It can act as an intermediary server that forwards client requests to other backend servers, which can improve load balancing and reduce server load.

  3. Load Balancing: Nginx can distribute incoming traffic across multiple servers to ensure no single server becomes overloaded.

  4. Security: Provides features like access control, SSL/TLS termination, and DoS attack mitigation.

  5. High Concurrency: Efficiently handles thousands of simultaneous connections using an asynchronous, event-driven approach.

  6. FastCGI, uWSGI, and SCGI support: Allows Nginx to interact with dynamic web applications written in various languages like PHP, Python, and Ruby.

This guide will help you set up a website on a Linux server using Nginx

Prerequisites

Make sure you have:

  • A Linux server (e.g., Ubuntu).

  • SSH access to your server.

  • A domain name (optional but helpful).

Step 1: Install Nginx

Update your server’s package list:

sudo apt update

Install Nginx:

sudo apt install nginx

Start Nginx and enable it to start on boot:

sudo systemctl start nginx 
sudo systemctl enable nginx

Step 2: Configure the Firewall

Instead of using UFW, we will use iptables to allow web traffic.

Allow HTTP traffic (port 80):

sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT

Allow HTTPS traffic (port 443), if you plan to use SSL:

sudo iptables -I INPUT -p tcp --dport 443 -j ACCEPT

Save these firewall rules so they persist after a reboot:

sudo sh -c "iptables-save > /etc/iptables/rules.v4"

Step 3: Set Up Your Website

Create a directory for your website:

sudo mkdir -p /var/www/your_domain/html 
sudo chown -R $USER:$USER /var/www/your_domain/html 
sudo chmod -R 755 /var/www/your_domain

Create a sample HTML file:

nano /var/www/your_domain/html/index.html

Add this simple content:

<html>
<head>
    <title>Welcome to Your Domain!</title>
</head>
<body>
    <h1>Success! Your Nginx server is working!</h1>
</body>
</html>

Create an Nginx server block configuration file:

sudo nano /etc/nginx/sites-available/your_domain

Add the following configuration:

server {
    listen 80;
    listen [::]:80;

    server_name your_domain www.your_domain;

    root /var/www/your_domain/html;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

Enable this configuration:

sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/

Check the configuration for errors:

sudo nginx -t

Reload Nginx to apply the changes:

sudo systemctl reload nginx

Step 4: Update DNS Settings (Optional)

If you have a domain name, log in to your domain registrar’s website and point your domain to your server’s IP address.

Step 5: Secure Your Website with SSL (Optional)

Install Certbot:

sudo apt install certbot python3-certbot-nginx

Obtain an SSL certificate:

sudo certbot --nginx -d your_domain -d www.your_domain

Follow the prompts to set up SSL.

Step 6: Test Your Website

Open your web browser and go to:

You should see the sample HTML page you created.

By following these steps, you can host your website on a Linux server using Nginx.