Beginners Guide to Host Static Content on a Server

Beginners Guide to Host Static Content on a Server

Recently, I took a session on hosting static content on a web server for Free Software Camp 2020. This is somewhat a written write-up for that.

Basic Requirements

  1. A server with ssh access.
  2. A domain (an IP would also work, but we’ll also add TLS certificate to it so recommended).
  3. A Debian based system.

Table of Content

  1. Firing up a virtual private server
  2. Adding DNS entry
  3. Adding content
  4. Writing nginx config
  5. TLS certificate or making the site HTTPS

Firing up a virtual private server

As every provider has a different interface to configure and launch an instance, I’m mentioning the generic steps/requirements:

Note: Egress traffic (outgoing traffic) is usually open on all ports.

Adding DNS entry

Now some addition in you’re DNS records:

ping <domain-name>

If you see the corresponding IP address in response, it has propagated successfully.

Adding content

Now we have to add the content we want to serve through the server.

Writing nginx config

We’re going to use nginx. Nginx is a server software (to handle content requests) that can also be used as reverse proxy, load balancer and a bunch of other stuff.

sudo apt install nginx

Note: sites-available and sites-enabled are Debian family specific directories. Others use/recommend conf.d.

server {
	# ports nginx should listen to
	listen 80;
	listen [::]:80;

	# name and location of our site content
	root /var/www/fscamp;	

	# defining index file in root
	index index.html;

	# defining server name
	# you can add wwww.fscamp.sahilister.in in addition to point and serve same content
	server_name fscamp.sahilister.in;

	# defining a 404, when content not found
	location / {
		try_files $uri $uri/ =404;
	}
}
ln -s /etc/nginx/sites-available/<file> /etc/nginx/sites-enabled/<file>

# In our case, it would look like this
ln -s /etc/nginx/sites-available/fscamp /etc/nginx/sites-enabled/fscamp
sudo systemctl restart nginx

TLS certificate or making the site HTTPS

Now to get a TLS certificate and encrypt the connection between a user and our server, we’ll use Let’s Encrypt and certbot. It does the heavy lifting, adds additional required nginx configs and get us 90 days TLS certificate (which has to be renewed after that).

sudo apt install python-certbot-nginx

That’s your basic website on your server.