Host a Website Fast

Oct 16, 2022

Hosting a website (like this one you are browsing!) is becoming much easier thanks to all the pre-configured packages. This is a very brief note on how I build this website from scratch.

Find a VPS

To host a website, some server needs to provide service for the website constantly. A VPS is (grossly simplifying) a remote server that keeps running. Google and Amazon both provide free options, provided that you are willing to do a bit navigation to find exactly what is free (which is unfortunately very confusing). I personally use InterServer, which is also a cheap option that has a more direct interface. We can use whatever Linux distribution on the VPS, but I choose Ubuntu 22 because it is the one I am most familiar with.

Google Compute Engine: https://cloud.google.com/compute

Amazon EC2: https://aws.amazon.com/ec2/

InterServer: https://www.interserver.net/

Find a domain name

People generally search for website through a domain name (like zhtluo.com) rather than a raw IP that our VPS provider will give us (like 209.159.157.108). This fact unfortunately means that we will need to buy our own domain name. I like Google Domain a lot more than traditional providers such as GoDaddy and Namecheap because Google Domain is very straightforward and easy to use (again, I hate navigating through ten pages with constant ads of their service before I can configure something that I am well aware of, which is unfortunately the case with traditional providers).

Google Domain: https://domains.google/

GoDaddy: https://www.godaddy.com/

Namecheap: https://www.namecheap.com/

Set up the domain name

Now that we have a domain name and an IP of our VPS, we can link them together. This is generally done through the domain name provider.

The easiest way is to add a A-record to the DNS service that consists of our IP (how-to on Google Domain).

SSH into the server

Generally the service provider will let us configure a way to use SSH to log into the server. Google and Amazon both ask us to set up a SSH key, but the VPS from InterServer I used is more primitive and allows us to log in to root with a pre-set password (which is not very secure):

ssh root@[domain-name]

Side note: Unsecured server is prone to getting hacked because there are tons of automatic scanners for SSH that looks through every ip. This guide is good as a starter to secure a server: https://sollove.com/2013/03/03/my-first-5-minutes-on-a-server-or-essential-security-for-linux-servers/.

Install Apache2

We can start hosting a website in one step through installing Apache2:

apt install apache2

Now when we visit the domain name in the browser, we can see a default page 'It works', which means that we have sucessfully hosted a website!

Configure Apache2

By default Apache2 hosts everything in /etc/var/www/html, which means that we can just throw HTML pages into that folder and it works out of the box. Apache2 is very configurable and can host a lot of different websites by itself. Just as an example, we will change the hosting root folder from /etc/var/www/html to /etc/var/www/zhtluo.com by giving it a new website configuration and disable the default one.

First we create a new website configuration 001-root.conf from the default one:

cd /etc/apache2/sites-available/ cp 000-default.conf 001-root.conf

Then we edit 001-root.conf so that it uses the folder that we want by changing DocumentRoot in the file:

DocumentRoot /var/www/zhtluo.com/

Finally we disable the default site and enable the new site:

a2dissite 000-default.conf a2ensite 001-root.conf systemctl reload apache2

Now we create this folder on our server:

mkdir /var/www/zhtluo.com chown zhtluo /var/www/zhtluo.com/

Makefile

We can use a Makefile to automate the synchronization process by rsync:
all: deploy deploy: rsync -a . [email protected]:/var/www/zhtluo.com

Setup HTTPS

There are many ways to setup HTTPS. Certbot is the easiest one in my opinion - we can follow the steps on Certbot's website.