Logo

How to install and configure nginx on ubuntu 22.10

Feb 03, 2023

Install Nginx

To install Nginx on Ubuntu 22.10, follow these steps:

Step 1 : Update the package index:

sudo apt update

Step 2 : Install Nginx:

sudo apt install nginx

Step 3 : Start Nginx:

sudo systemctl start nginx

Step 4 : Verify that Nginx is running by visiting http://localhost in your web browser or by running:

sudo systemctl status nginx

Step 5 : Enable Nginx to start at boot time:

sudo systemctl enable nginx

Configuration Nginx

To add a new configuration to Nginx and have it enabled, you can create a new configuration file in the /etc/nginx/sites-available directory and then create a symbolic link to it in the /etc/nginx/sites-enabled directory. Here's the step-by-step process:

Step 6 : Create a new configuration file for your site in the /etc/nginx/sites-available directory:

sudo nano /etc/nginx/sites-available/example.com

Replace example.com with the name of your domain.

Step 7 : Add the following server block to the configuration file:

server {
    listen 80;
    server_name example.com;
    root /var/www/example.com;

    index index.html;

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

Replace example.com with the name of your domain and /var/www/example.com with the path to the root directory for the website.

Step 8 : Save the file and close it.

Step 9 : Create a symbolic link to the configuration file in the /etc/nginx/sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Replace example.com with the name of your domain.

Step 10 : Verify that the Nginx configuration is correct:

sudo nginx -t

Step 11 : If the configuration is correct, reload Nginx to apply the changes:

sudo systemctl reload nginx

Step 12 : If necessary, create the root directory for the domain:

sudo mkdir /var/www/example.com

Replace /var/www/example.com with the path to the root directory for the website.

Step 13 : Create an HTML file in the domain's root directory:

sudo nano /var/www/example.com/index.html

Step 14 : Add the following HTML code to the file:

<html>
  <head>
    <title>Example Domain</title>
  </head>
  <body>
    <h1>Welcome to Example Domain</h1>
    <p>This is a sample HTML page for the domain example.com.</p>
  </body>
</html>

Step 1, You can access the page by visiting : https://example.com/

Generate Let's Encrypt SSL

To generate a Let's Encrypt SSL certificate for Nginx, you can use the certbot tool. Here's how to do it:

Step 15 : Map the domain to the server using DNS.

Step 16 : Install certbot:

sudo apt install certbot python3-certbot-nginx

Step 17 : Run the certbot command to obtain and install the SSL certificate:

sudo certbot --nginx

Step 18 : Follow the on-screen instructions to configure certbot and obtain the SSL certificate. certbot will automatically configure Nginx to use the newly obtained certificate.

Step 19 : Verify that the SSL certificate is properly installed by visiting your website using https:// in a web browser and checking for the padlock icon in the address bar.