Here’s how I run my site(davidfindlay.com.au) in a sort of serverless way using Amazon S3. I say it’s sort of serverless because you still need an Apache/MySQL/WordPress installation, but it doesn’t need to be running all the time and can just run on your local computer.
Why host your WordPress site on Amazon S3?
Firstly S3 is very fast. WordPress hosted on LAMP has to bootstrap WordPress, talk to the database, process your request and generate a page before sending it to the browser. This all takes time. It makes sense if you host dynamic content. However if your content doesn’t change much, it doesn’t.
If you update your site maybe once a day, why have the HTML generated every time a visitor hits the site? With Static WordPress hosting you generate the HTML once when you make a change and the generated HTML is then served to each new visitor. This is much faster.
As mentioned S3 is very fast, but it’s also scalable. If your site suddenly gets visited by 10000 people in an hour, S3 can handle it. Your typical WordPress installation on a LAMP hosting provider probably can’t.
Secondly static hosting is more secure. Because your WordPress installation is hidden behind a firewall on your local network, you don’t have to worry about security updates and zero-day exploits as much. Sure you still should keep up to date, but because attackers don’t have any access to the PHP pages or database you’re kept much safer. Amazon has good security measures on S3 and as long as you use them, your S3 should be kept safe.
- Basic set up and installation of WordPress and WordPress plugins
- Using hosting software such as MAMP or a LAMP server or Docker
- Working at the command line
Step 1: WordPress installation
Firstly install WordPress locally. Perhaps using MAMP or on an Apache/MySQL/PHP installation on a linux box on your local network. How you do this part is up to you. I’ve actually got mine running on a small EC2 micro instance, that I just turn on and off when I want to make changes to my site.
No one will actually visit this WordPress installation, so it can just be local on your machine, not world accessible via the internet. Firewall it off so no one can reach it for maximum safety.
You’ll also need to install the AWS CLI. If you’re using an EC2 instance with an Amazon AMI, you’ll already have this.
Step 2: Set up an S3 Bucket
You’ll need an Amazon Web Services account first, a free-tier account should be fine for most small sites for at least the first year. Afterwards you may need to pay, but S3 is really cheap.
There’s a lot of steps to setting up an S3 Bucket for web site hosting, so I’ve put them in a separate article here: How to Set Up Amazon S3 for Website Hosting.
Once you’ve got the S3 bucket set up return here.
Step 3: Install Simply Static WordPress Plugin
This is pretty much a standard WordPress plugin install, so I want explain it too much.
The Simply Static plugin automatically generates a plain html version of your site and exports it to a directory on your WordPress host.
Static means that it’s plain HTML, no PHP. It can run on any sort of hosting without needing a PHP or MySQL installation.
Once Simply Static is installed, activate it.
- Select Simply Static, then Settings from the left hand menu.
- Set Destination URLs to Use Relative URLs.
- Set Delivery Method to Local Directory.
- Set Local Directory to a suitable location, for instance on my linux installation “/var/www/html_static”. Take note of this path as you may need to modify the script in Step 4 to match.
Step 4: Configure AWS IAMs user and AWS CLI
You’ll need an AWS IAM account set up to use the AWS CLI.
- Click Services at the top of the screen and in the search box type IAM. Click on the IAM option that appears in the drop down.
- Click Add User.
- Enter a user name such as “s3hosting”. Under Access Type, select Programmatic access. This is required so that the AWS CLI can use the user credentials. Click Next.
- Under Set Permissions, select Attach existing policies directly, then search for s3. Select the AmazonS3FullAccess policy. Click Next. Note that this policy means that using this AWS Access Key ID and Secret Key, someone could access any file in any bucket on your AWS account. This can be dangerous!
- Continue through to the review page with default settings. The review page should look like this. Click Create User.
- You’ve now created the AWS CLI user. You’ll need the Access key ID and Secret access key displayed on this page for the next part of the process.
Next move back to your terminal where you’ve installed your WordPress.
Run the AWS Configure command. You’ll need to supply user IAM user Key ID and Secret Key as well as the default region, which should be the region that your S3 bucket is in:
Create the following bash script and call it syncStatic.sh:
#!/bin/bash aws s3 sync /var/www/html_static s3://my-serverless-wp/
Change ‘my-serverless-wp’ to match the name of your bucket and you may need to change ‘/var/www/html_static’ to match the local directory you set in Step 3.
Step 5: Generate Static HTML
In the WordPress Admin pages, select Simply Static from the side menu. Click Generate.
The log will show progress as the static html pages are generated. When the log shows “Done!” move to the next step.
Step 6: Sync to S3
In your terminal, run the syncStatic.sh script. It’ll quickly transfer the files to S3. If you’re running in a small EC2 instance this will be super quick, but a bit slower otherwise.
Step 7: Test the site
Go to your S3 public endpoint URL in your browser. For instance: http://my-serverless-wp.s3-website-ap-southeast-2.amazonaws.com/
You can get your URL from the S3 bucket configuration by going to Services->S3->Select your bucket->Properties->Static Web Hosting:
After clicking on that URL or pasting it in your browser, you should be able to see your WordPress site and browse it.
Step 8: Set up DNS CNAME
Your site is now on the web, but it’s on an ugly Amazon AWS S3 url. You don’t want to direct people to that.
The next step depends on how you want to host your site. You’ll need to set up CNAME(canonical name) which points your website domain to the the AWS S3 bucket address.
I’ll show how to do this for Amazon Route 53 DNS hosting in another article.