# 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.

---

**<mark>Why is Nginx Popular?</mark>**

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
        

---

**<mark>How Does Nginx Work?</mark>**

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.
    

---

**<mark>Key Features of Nginx</mark>**

**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**.

**<mark>Step 1: Install Nginx</mark>**

First, update the package lists and install Nginx:

```plaintext
sudo apt update
sudo apt-get install nginx -y
```

Start and restart the Nginx service:

```plaintext
sudo service nginx start
sudo service nginx restart
sudo service nginx status
```

Enable Nginx to start on boot:

```plaintext
sudo systemctl enable nginx
```

---

**<mark>Step 2: Set Up Your Website Files</mark>**

1. **Create a directory for your website:**
    
    ```plaintext
    sudo mkdir -p /var/www/html/web1
    ```
    
2. **Change ownership of the directory to your user:**
    
    ```plaintext
    sudo chown -R $USER:$USER /var/www/html/web1
    ```
    
3. **Create a sample** `index.html` file for your website:
    
    ```plaintext
    vi /var/www/html/web1/index.html
    ```
    
    Add the following sample content:
    
    ```plaintext
    htmlCopy code<!DOCTYPE html>
    <html>
    <head>
        <title>Welcome to Web1!</title>
    </head>
    <body>
        <h1>Hello, World! Welcome to Web1!</h1>
    </body>
    </html>
    ```
    

---

**<mark>Step 3: Configure Nginx for Your Website</mark>**

1. **Create a virtual host configuration file:**
    
    ```plaintext
    sudo vi /etc/nginx/sites-available/web1.conf
    ```
    
    Add the following configuration:
    
    ```plaintext
    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:**
    
    ```plaintext
    sudo ln -s /etc/nginx/sites-available/web1.conf /etc/nginx/sites-enabled/
    ```
    
3. **Set the appropriate permissions:**
    
    ```plaintext
    sudo chmod -R 755 /var/www/html/
    ```
    
4. **Test your Nginx configuration:**
    
    ```plaintext
    sudo nginx -t
    ```
    
5. **Restart Nginx to apply changes:**
    
    ```plaintext
    sudo service nginx restart
    sudo service nginx status
    ```
    

---

**<mark>Step 4: Set Up DNS A Record</mark>**

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.
    

---

**<mark>Step 5: Install Certbot for SSL</mark>**

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

1. **Install Certbot and the Nginx plugin:**
    
    ```plaintext
    sudo apt update
    sudo apt install certbot python3-certbot-nginx -y
    ```
    
2. **Verify the Nginx configuration:**
    
    ```plaintext
    sudo nginx -t
    sudo systemctl reload nginx
    ```
    
3. **Obtain and install the SSL certificate:**
    
    ```plaintext
    sudo certbot --nginx
    ```
    
    Follow the prompts to select your domain and automatically configure SSL.
    

---

**<mark>Step 6: Verify SSL Installation</mark>**

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

```plaintext
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:
    
    ```plaintext
    sudo certbot renew --dry-run
    ```
    
* Explore advanced Nginx configurations for performance optimization.
    

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