Two commands, one small config file, and your website is live on your own server. In this episode we install nginx on our Ubuntu server on AWS Lightsail, write an nginx server block line by line, test it, reload, and serve our website straight from the server’s IP.
- nginx installed and running
- Your website files in
/var/www - An nginx server block you understand line by line
- Your site live at your server’s IP
Install nginx
Logged in to the server over SSH (episode 3.1 shows how), refresh the package list and install nginx:
sudo apt update
sudo apt install -y nginx
Check the version, and that the service is running:
nginx -v
systemctl status nginx --no-pager | head -5
The status should say active (running).
Check nginx in the browser
Open http://YOUR_SERVER_IP in a browser. The nginx welcome page means nginx is installed and answering on port 80. Want a domain here instead of an IP? That’s episode 2.
Copy the website files
Our website is just an index.html file and an assets folder with the logo, already uploaded to the server in ~/fineshot_social. We create a folder for it under /var/www, the usual place for web files, and copy everything into it:
ls -l ~/fineshot_social
sudo mkdir -p /var/www/fineshot
sudo cp -r ~/fineshot_social/* /var/www/fineshot/
ls -l /var/www/fineshot
Write the nginx server block
A server block tells nginx which port to listen on and where the website files live. Create one in sites-available:
sudo vim /etc/nginx/sites-available/fineshot
Its contents:
server {
listen 80;
server_name _;
root /var/www/fineshot;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
listen 80;answer normal HTTP traffic.server_name _;accept requests for any domain or IP.root /var/www/fineshot;the folder with our website.index index.html;the page to show for/.try_files $uri $uri/ =404;serve the file or folder if it exists, otherwise return a 404.
Enable the site
nginx only loads the sites linked in sites-enabled. Link ours, and remove the default site so ours is the only one answering on port 80:
sudo ln -s /etc/nginx/sites-available/fineshot /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
ls -l /etc/nginx/sites-enabled/
Test and reload nginx
Before every reload, run nginx -t. It checks the configuration for mistakes, so a typo never takes your site down:
sudo nginx -t
sudo systemctl reload nginx
curl -s http://localhost | head -5
nginx -t should say “syntax is ok” and “test is successful”. A reload applies the change without dropping visitors, and curl to localhost already returns our own HTML.
The website is live
Open http://YOUR_SERVER_IP again. The nginx welcome page is gone, and nginx now serves our site.
If it doesn’t work
nginx -t reports an error. It prints the file and line number. The usual cause is a missing ; at the end of a line or an unclosed }. Fix it, run nginx -t again, and reload only when it passes.
You still see the nginx welcome page. Check that the default site link is gone from sites-enabled and that you reloaded nginx afterwards. A hard refresh in the browser rules out caching.
403 Forbidden or 404 Not Found. The root path doesn’t match where the files are, or index.html is missing. Compare ls -l /var/www/fineshot with the server block.
Next
A static site is a great start. In episode 5 we turn this same website into a Node.js app, with a contact form that saves every message.
