Skip to content

Guide to Install and Configure Nginx on Ubuntu 22.04

Step 1: Install Nginx

Update your package index and install Nginx:

bash
sudo apt update
sudo apt install nginx

Step 2: Start and enable the Nginx service:

bash
sudo systemctl start nginx
sudo systemctl enable nginx

Step 3: Check Nginx Status

Check the status of the Nginx service:

bash
sudo systemctl status nginx

sudo systemctl stop nginx

You should see output indicating that Nginx is active and running.

Step 4: Configure Nginx

Create a new configuration file for your website:

bash
sudo vim /etc/nginx/sites-available/your_domain

Add the following configuration:

nginx
server {
    listen 80;
    server_name your_domain_or_IP;

    location / {
        root /var/www/html;
        index index.html;
    }
}

Enable the configuration:

bash
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

Step 5: Adjust Firewall Settings

If you have UFW enabled, allow HTTP traffic:

bash
sudo ufw allow 'Nginx HTTP'

You can now access your website by entering your server's IP address or domain name in a web browser.

Step 6: Allow Nginx HTTPS Traffic

If you plan to use SSL/TLS certificates for your website, you will need to allow HTTPS traffic through the firewall:

bash
sudo ufw allow 'Nginx Full'

You can now proceed to set up SSL certificates for your Nginx server.

Step 7: Set Up SSL Certificates

Using Let's Encrypt

Install Certbot:

bash
sudo apt install certbot python3-certbot-nginx

Obtain an SSL certificate:

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

Follow the prompts to complete the installation. Certbot will automatically configure SSL for your Nginx configuration.

Renewing SSL Certificates

Certbot automatically renews certificates. To test the renewal process, you can run:

bash
sudo certbot renew --dry-run

Congratulations! You have successfully installed and configured Nginx on your Ubuntu 22.04 server. You can now serve web content securely using HTTPS.