Installing Nginx on Ubuntu 22.04 and Launching Your First Website

Installing Nginx on Ubuntu 22.04 and Launching Your First Website

Update and Install Nginx:

sudo apt update && sudo apt upgrade -y
sudo apt install nginx -y

Start and Enable Nginx:

Start Nginx and enable it to run on startup:

sudo systemctl start nginx
sudo systemctl enable nginx

Configure Your First Website Create a directory for your website:

sudo mkdir -p /var/www/testsite1
sudo nano /etc/nginx/sites-available/testsite1

Copy and paste the following configuration, then save and exit (Ctrl+X, Y, Enter):

server {
        listen 80;
        server_name _;

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

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

Create a symbolic link to enable this configuration:

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

Test the configuration for syntax errors:

sudo nginx -t

If everything is okay, you’ll see a message: syntax is okay, test is successful.

Reload Nginx to apply the changes:

sudo systemctl reload nginx

Create a Basic Index.html File:

CreateNew File and name it index.html

Now, double click index.html to open it. Copy and paste the following HTML code into the file:

<!DOCTYPE html>
<html>
<head>
    <title>Test Site 1</title>
</head>
<body>
    <h1>Welcome to My First Website on Ubuntu 22.04!</h1>
</body>
</html>

Save the file by clicking File on the menu bar, then Save. Alternatively, you can use the shortcut Ctrl+S

Your basic HTML file is now ready.

Access Your Website:

You’ve successfully installed Nginx on Ubuntu 22.04, configured a basic website, and accessed it using your server’s public IP.