Since I nabbed free Amazon Web Services (AWS) credit from my student days, and since they offer a year's free access on top of that, I wanted to use it to set up an Apache (ah-patch-ee? appa-shay?) virtual host, which allows me to run multiple websites from one server. This blog now sits on said server, although only static sites can be hosted as I haven't installed any database stuff just yet — I'll cross that bridge when I need to.
I was completely baffled by the idea of virtual hosts way back when, but they make deploying and updating sites from git repos such a doddle (because shell access, duh) — and to think, I was dragging and dropping files using FTP on shared hosting for years.
The dashboard for AWS can be really, really daunting. What we're looking for is the EC2 (Elastic Compute Cloud) service.
On the EC2 dashboard, create a key pair and save the private
.pem
key somewhere recoverable on your computer. We'll use this later to stop unauthorised access from an imposter.Launch a new EC2 instance from a Ubuntu 14.04 virtual machine image. There are two versions to pick from: HVM and PV. I'll admit that I have little knowledge of the difference, so I chose HVM since Amazon seem to be recommending it and because this guy said so.
Choose the instance type — t2.micro is a good choice if you're not expecting a lot of traffic, and it's free! I like free. Use default settings for the config, storage and tag options, unless something compels you to choose otherwise.
Create a security group that allows incoming SSH access from your own IP address, and that allows HTTP and HTTPS access from anywhere. Then launch!
Optional: If you have a domain name you'd like to use, set up a CNAME or A record on your domain's control panel that points to the IP address of your new EC2 instance. This is the bit that can sometimes take a while to propagate, so do this as soon as you can.
Now whip open terminal to SSH into your server, making sure to include the private key generated earlier —
ubuntu
is the default user. I kept it as is, because ain't nobody got time to be changing it.ssh -i path-of-file.pem ubuntu@ec2-public-ip-address
Install the Apache web server using Ubuntu's package manager.
sudo apt-get update sudo apt-get install apache2
Once that's all done and dusted, sort out the directory structure — the default document root should be
/home/ubuntu/public
. Create an empty directory inside it (orgit clone
your content) using the desired url as the name, then create some dummy content.sudo mkdir /home/ubuntu/public/testing.com nano /home/ubuntu/public/testing.com/index.html
Managing the virtual host files is the fiddly bit. These specify the configuration of the virtual host, and Apache will use them to respond to domain requests.
Apache already has a default virtual host file. Shamelessly copy this and edit it for each site you'd like to serve.
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/testing.com.conf ```bash sudo nano /etc/apache2/sites-available/testing.com.conf
These directives need to be updated:
ServerName
— matches this virtual host to the domain, eg. testing.comServerAlias
— points additional domains (if you have any) to this virtual host as well, eg. testing.co.ukDocumentRoot
— where the files live
Once the file is saved, the site needs to be enabled and the server needs to be reloaded.
sudo a2ensite /etc/apache2/sites-available/testing.com.conf sudo service apache2 reload
Optional: Update the default virtual host file to handle errors somewhat. Open up the file editor:
sudo nano /etc/apache2/sites-available/000-default.conf
Then add these lines somewhere reasonable in it, updating the error page path as necessary:
ErrorDocument 404 /path-of-error-page.html ErrorDocument 500 /path-of-error-page.html ErrorDocument 502 /path-of-error-page.html ErrorDocument 503 /path-of-error-page.html ErrorDocument 504 /path-of-error-page.html
You'll see that when this blog throws a server error (eg. 404) it points to oops.html, so that's that in action!