Back to Blog
by Davis Stanko · August 9, 2022 · 4 min read

Self-Hosting a Static Site

In this guide you will register a domain, rent a server, point the domain at the server, and serve the site over HTTPS.

Step 1: Register a Domain

A domain name is the address people type to reach your site. A registrar is a company that reserves that name for you. I use Porkbun but all registrars are practically the same.

Search for a name on your registrar's site. Add it to your cart and buy it. Domains renew each year. Most common TLDs, like .com or .net, cost around $10 a year.

Note the price difference between TLDs. Specialty TLDs, like .io or .game, usually cost more per year.

Also make sure you understand the annual renewal fee. Many registrars give large discounts for the first year then raise the price upon renewal.

Step 2: Get a Server

This is a the computer that your website will be served from. A VPS is a virtual server that runs on shared hardware in a data center. It gives you a real Linux machine for a few dollars a month. Renting one of these is generally better than self hosting for security and uptime. I use Vultr.

You will pick a few options when you create the server:

  • Location. Pick one close to you or your audience. The difference in load time is small either way. Note some locations may cause you to pay sales tax. It may be worth paying for a further location to save a few cents a month.
  • Operating system. I recommend Debian. It requires minimal maintenance. This guide uses Debian commands.
  • Size. Pick the cheapest plan that is not IPv6-only. A static site needs very little CPU or RAM.
  • IPv6. Even though we support IPv4 still turn this on. Some networks connect over IPv6 only.

Once you confirm these settings, Vultr deploys the server. It will show you an IPv4 address within a minute.

Step 3: Point Your Domain at Your Server

Domains and servers connect through DNS records. A DNS record tells the internet which IP address to reach when someone types your domain.

Open your registrar's DNS settings for the domain. Add two A records, using your server's IPv4 address:

  • One record with the host field left blank. This handles yourdomain.com.
  • One record with * in the host field. This handles any subdomain, like blog.yourdomain.com.

Now get your server's IPv6 address. On Vultr, open the server, then open its IPv6 settings tab. Add two AAAA records with this address, using the same blank and * host pattern as above.

Save the changes. DNS updates can take a few minutes to spread across the internet.

Test the connection:

ping yourdomain.com

If the reply shows your server's IP address, the domain points at your server.

Step 4: Install and Configure Nginx

Log into your server:

ssh root@yourdomain.com

Enter the password from your Vultr dashboard when prompted.

Update the system and install nginx:

apt update
apt upgrade
apt install nginx

Nginx reads its site configs from /etc/nginx/sites-available. Create one:

nano /etc/nginx/sites-available/mysite

Add this block. Replace yourdomain.com with your domain:

server {
        listen 80;
        listen [::]:80;
        server_name yourdomain.com;
        root /var/www/mysite;
        index index.html;
        location / {
                try_files $uri $uri/ =404;
        }
}

Create the site folder and a starting page:

mkdir /var/www/mysite
nano /var/www/mysite/index.html

Add any HTML you want. Even one line like hello world works to test the setup.

Enable the site by linking it into sites-enabled:

ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled
systemctl reload nginx

UFW is likely active on your server, open the web ports:

ufw allow 80
ufw allow 443

Open /etc/nginx/nginx.conf and uncomment server_tokens off;. This hides your nginx version from error pages. This is so that bad actors can't learn which version of nginx you are using.

At this point, your site loads over plain HTTP. The next step adds encryption.

Step 5: Add HTTPS with Certbot

Certbot creates and installs a free SSL certificate for your site.

Install it:

apt install python3-certbot-nginx

Confirm ports 80 and 443 are open, then run:

certbot --nginx

Certbot will ask for an email address. It uses this to warn you if renewal ever fails. You can skip this with the --register-unsafely-without-email flag instead.

Agree to the terms of service. Certbot will list the domains it found in your nginx config. Press enter to select all of them.

When asked about redirecting HTTP to HTTPS, choose the redirect option. This sends all visitors to the encrypted version of your site automatically.

Certificates expire after three months. Set up a cron job to renew them automatically:

crontab -e

Add this line, then save and exit:

0 0 1 * * certbot --nginx renew

This checks for renewal on the first of every month.

Closing Thoughts

At this point, your site is live, encrypted, and reachable at your own domain. Everything past this point is just adding content. Add to your index.html, add more pages, design your website with a css file, or make it interactive with javascript. You can do whatever you want with your new digital real estate! Just make sure to log on to your server every few months to update your packages.