LearnAI ToolsCareerPractice BuildsPlayDevelopmentContact
Linux & Command LineIntermediate~1.5 hours

Host a Website with Nginx

Install the Nginx web server, publish your own site from /var/www, configure a server block on its own port, test and reload safely, and read the access log.

aptNginxPermissionsServicesLogs

Overview

Nginx is one of the most widely used web servers in the world. In this project you will install it, publish your own page and configure a dedicated site — the same steps used to host real websites on a Linux server, and good preparation for putting Nginx in front of applications later.

Prerequisites

  • Installing packages with apt — lesson 10.
  • Permissions, ownership and sudo — lesson 6.
  • Services, logs and ss — lessons 13 and 14.
  • An Ubuntu machine: WSL, a cloud server or a virtual machine.

Step 1: Install Nginx

sudo apt update
sudo apt install -y nginx
Output (last lines)

Click Run to see what this code prints.

nginx -v
Output

Click Run to see what this code prints.

Step 2: Start It and Check

On a standard Ubuntu server, Nginx starts automatically after installation and systemctl status nginx shows it running. If it is not running, start it:

sudo service nginx start
service nginx status
Output

Click Run to see what this code prints.

Request the default page with curl. On a cloud server you can also open its public IP address in a browser:

curl -sI http://localhost | head -n 3
curl -s http://localhost | grep -o "<title>.*</title>"
Output

Click Run to see what this code prints.

That default page lives in /var/www/html:

ls -l /var/www/html
Output

Click Run to see what this code prints.

Step 3: Publish Your Page

Create a folder for your site. Because /var/www belongs to root, writing there needs sudo:

sudo mkdir -p /var/www/portfolio
sudo nano /var/www/portfolio/index.html

Type or paste this page, then save with Ctrl+O and exit with Ctrl+X:

/var/www/portfolio/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Priya's Linux Server</title>
</head>
<body>
<h1>Hello from my own Linux web server!</h1>
<p>Served by Nginx.</p>
</body>
</html>

Then hand the files to www-data, the user Nginx runs as, and check:

sudo chown -R www-data:www-data /var/www/portfolio
ls -l /var/www/portfolio
Output

Click Run to see what this code prints.

Step 4: Configure a Server Block

Nginx describes each site in a “server block”. On Ubuntu they live in /etc/nginx/sites-available, and a site is switched on by linking it into sites-enabled. Create this file with sudo nano /etc/nginx/sites-available/portfolio:

/etc/nginx/sites-available/portfolio
server {
listen 8080;
server_name _;
root /var/www/portfolio;
index index.html;
location / {
try_files $uri $uri/ =404;
}
access_log /var/log/nginx/portfolio.access.log;
error_log /var/log/nginx/portfolio.error.log;
}
  • listen 8080 serves this site on port 8080, alongside the default site on port 80. For a real domain you would use listen 80 and set server_name to the domain.
  • root is the folder the files come from; index is the file served for a folder.
  • try_files serves the file if it exists, otherwise returns a 404.
  • Each site gets its own access and error log.
sudo ln -s /etc/nginx/sites-available/portfolio /etc/nginx/sites-enabled/portfolio
ls -l /etc/nginx/sites-enabled
Output

Click Run to see what this code prints.

Step 5: Test, Reload and Verify

Always test the configuration before applying it — a broken config can take every site offline:

sudo nginx -t
Output

Click Run to see what this code prints.

Reloading applies the new configuration without dropping visitors’ connections:

sudo service nginx reload
curl -s http://localhost:8080
Output

Click Run to see what this code prints.

Request a page that does not exist, then read the site’s access log — every request is recorded with its status code:

curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8080/missing.html
sudo tail -n 2 /var/log/nginx/portfolio.access.log
Output

Click Run to see what this code prints.

Step 6: Catching Mistakes

To see why testing matters, remove the semicolon from the index line and test again:

sudo nginx -t
Output

Click Run to see what this code prints.

nginx -t names the file and the line where it noticed the problem — line 8, where the next block starts, just after the line that is missing its semicolon. Nothing was reloaded, so the running site is unaffected. Put the semicolon back and test again:

sudo nginx -t
Output

Click Run to see what this code prints.

Mini Challenge · Put It in Order

Put the safe way to change an Nginx site in order.

  1. Run sudo nginx -t
  2. Reload Nginx
  3. Check the site with curl and read the logs
  4. Edit the file in /etc/nginx/sites-available
  5. Fix any error it reports and test again

Extend This Project

  • On a cloud server with a domain name, switch to listen 80 with server_name set to your domain, and add free HTTPS with Certbot (Let’s Encrypt).
  • Allow only web and SSH traffic with the ufw firewall: sudo ufw allow OpenSSH, sudo ufw allow "Nginx Full", sudo ufw enable.
  • Deploy your files with rsync from your own computer over SSH.
  • Run the log analyzer project on this site’s access log.

Summary

You installed Nginx, published a site with correct ownership, configured a server block, and followed the professional routine of testing before reloading and checking logs afterwards. These are the same steps used to host websites on Linux servers everywhere.