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 updatesudo apt install -y nginxClick Run to see what this code prints.
nginx -vClick 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 startservice nginx statusClick 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 3curl -s http://localhost | grep -o "<title>.*</title>"Click Run to see what this code prints.
That default page lives in /var/www/html:
ls -l /var/www/htmlClick 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/portfoliosudo nano /var/www/portfolio/index.htmlType or paste this page, then save with Ctrl+O and exit with Ctrl+X:
<!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/portfoliols -l /var/www/portfolioClick 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:
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 8080serves 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.rootis the folder the files come from;indexis the file served for a folder.try_filesserves 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/portfoliols -l /etc/nginx/sites-enabledClick 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 -tClick Run to see what this code prints.
Reloading applies the new configuration without dropping visitors’ connections:
sudo service nginx reloadcurl -s http://localhost:8080Click 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.htmlsudo tail -n 2 /var/log/nginx/portfolio.access.logClick 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 -tClick 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 -tClick Run to see what this code prints.
Put the safe way to change an Nginx site in order.
- Run sudo nginx -t
- Reload Nginx
- Check the site with curl and read the logs
- Edit the file in /etc/nginx/sites-available
- 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.