Setting Up a Website with Nginx and Certbot

Setting Up a Website with Nginx and Certbot

Nginx (pronounced as "Engine-X") is a powerful and high-performance web server and reverse proxy server. It is widely used for its ability to handle a large number of concurrent connections with low resource usage. Originally designed as a web server, Nginx has evolved to support multiple roles, making it a versatile tool in modern web architecture.


Why is Nginx Popular?

  1. Performance and Speed:
    Nginx is built to handle massive traffic and provides lightning-fast responses, making it suitable for high-demand applications.

  2. Lightweight and Efficient:
    It uses an event-driven, asynchronous architecture, allowing it to serve thousands of requests simultaneously with minimal hardware.

  3. Versatile Use Cases:

    • Web server to host websites

    • Reverse proxy to distribute traffic across servers

    • Load balancer to ensure high availability

    • Content caching for improved performance


How Does Nginx Work?

Unlike traditional web servers that handle each request with a new thread, Nginx uses an event-driven model. This means:

  • It processes requests asynchronously.

  • A single worker process can handle thousands of connections.


Key Features of Nginx

1. Web Server

Nginx can serve static content like HTML, CSS, and JavaScript files efficiently. Its ability to manage multiple connections simultaneously makes it ideal for dynamic content delivery.

2. Reverse Proxy

Nginx can act as an intermediary, forwarding client requests to backend servers. This helps:

  • Improve security by hiding backend server details.

  • Distribute load across multiple servers.

3. Load Balancer

Distribute incoming requests to multiple servers to ensure:

  • High availability.

  • Optimized resource usage.

  • Fault tolerance.

4. Content Caching

Caching frequently requested content improves website speed and reduces server load.

5. HTTPS and SSL/TLS Termination

Nginx supports Secure Sockets Layer (SSL) and Transport Layer Security (TLS) for encrypting connections and providing secure communication.


In this tutorial, we’ll cover the steps to set up a website using Nginx, configure a virtual host, and secure it with an SSL certificate using Certbot.

Step 1: Install Nginx

First, update the package lists and install Nginx:

sudo apt update
sudo apt-get install nginx -y

Start and restart the Nginx service:

sudo service nginx start
sudo service nginx restart
sudo service nginx status

Enable Nginx to start on boot:

sudo systemctl enable nginx

Step 2: Set Up Your Website Files

  1. Create a directory for your website:

     sudo mkdir -p /var/www/html/web1
    
  2. Change ownership of the directory to your user:

     sudo chown -R $USER:$USER /var/www/html/web1
    
  3. Create a sample index.html file for your website:

     vi /var/www/html/web1/index.html
    

    Add the following sample content:

     htmlCopy code<!DOCTYPE html>
     <html>
     <head>
         <title>Welcome to Web1!</title>
     </head>
     <body>
         <h1>Hello, World! Welcome to Web1!</h1>
     </body>
     </html>
    

Step 3: Configure Nginx for Your Website

  1. Create a virtual host configuration file:

     sudo vi /etc/nginx/sites-available/web1.conf
    

    Add the following configuration:

     server {
         listen 80;
         server_name yourdomain.com www.yourdomain.com;
    
         root /var/www/html/web1;
         index index.html;
    
         location / {
             try_files $uri $uri/ =404;
         }
     }
    
  2. Enable the virtual host:

     sudo ln -s /etc/nginx/sites-available/web1.conf /etc/nginx/sites-enabled/
    
  3. Set the appropriate permissions:

     sudo chmod -R 755 /var/www/html/
    
  4. Test your Nginx configuration:

     sudo nginx -t
    
  5. Restart Nginx to apply changes:

     sudo service nginx restart
     sudo service nginx status
    

Step 4: Set Up DNS A Record

To make your site accessible via a domain:

  1. Log in to your domain registrar's control panel.

  2. Create an A Record for your domain, pointing it to your server's public IP address.

  3. Wait for the DNS changes to propagate.


Step 5: Install Certbot for SSL

Secure your website with a free SSL certificate from Let's Encrypt using Certbot.

  1. Install Certbot and the Nginx plugin:

     sudo apt update
     sudo apt install certbot python3-certbot-nginx -y
    
  2. Verify the Nginx configuration:

     sudo nginx -t
     sudo systemctl reload nginx
    
  3. Obtain and install the SSL certificate:

     sudo certbot --nginx
    

    Follow the prompts to select your domain and automatically configure SSL.


Step 6: Verify SSL Installation

Once the process is complete, verify your website is now accessible via HTTPS by visiting:

https://yourdomain.com

Conclusion

You’ve successfully set up a website using Nginx, configured a virtual host, and secured it with an SSL certificate from Let's Encrypt using Certbot. This setup ensures your site is both accessible and secure.


Further Steps

  • Renew SSL certificates automatically by running:

      sudo certbot renew --dry-run
    
  • Explore advanced Nginx configurations for performance optimization.

Let us know if this guide helped you get your website online!