Task 2: Creating and Enabling New Sites on Different Ports

1. Create a Directory for the New Site

Command:

sudo mkdir -p /var/www/port8080site/html
sudo chmod -R 755 /var/www/port8080site

Explanation: This step sets up a directory where the site’s content will reside. Setting the appropriate permissions ensures that the web server can serve the content properly.

2. Create an index.html File for the New Site

Command:

sudo nano /var/www/port8080site/html/index.html

Content Example:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to Port 8080 Site!</title>
</head>
<body>
    <h1>Success! You are viewing the site on port 8080!</h1>
</body>
</html>
            

Explanation: This file serves as the landing page for the new site. Students can use this to verify that Nginx is correctly serving the site.

3. Create a New Server Block Configuration File for the Site

Command:

sudo nano /etc/nginx/sites-available/port8080site

Configuration Example:

server {
    listen 8080;
    server_name localhost;

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

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

Explanation: This configuration tells Nginx to listen on port 8080 and serve content from the /var/www/port8080site/html directory. The server_name directive is set to localhost for local testing.

4. Enable the New Site by Creating a Symbolic Link

Command:

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

Explanation: Enabling the site by creating a symbolic link in sites-enabled allows Nginx to serve this configuration. The role of the sites-available and sites-enabled directories in managing multiple sites should be explained.

5. Test the Nginx Configuration

Command:

sudo nginx -t

Explanation: Before applying changes, it’s crucial to ensure the configuration syntax is correct. This command helps catch errors that could prevent Nginx from reloading properly.

6. Reload Nginx to Apply the Configuration

Command:

sudo systemctl reload nginx

Explanation: Reloading Nginx will apply the new configuration, making the site available on port 8080 without disrupting existing connections.

7. Test the New Site

Command: Open a web browser or use curl to access the site:

curl http://localhost:8080

Explanation: Visiting http://localhost:8080 should display the index.html content. Discuss how different ports can be used to run multiple sites on the same server and the implications of using non-standard ports.

8. Disable the Site if Needed

Command:

sudo unlink /etc/nginx/sites-enabled/port8080site
sudo systemctl reload nginx

Explanation: This demonstrates how to disable a site by removing the symbolic link, making it easier to manage which sites are active.