How to Setup WordPress on AWS (a.k.a. How I Built This Site)

This is largely a reference for myself, but it may be useful for other people. Credits to the following for help:

Steps

  • Create an EC2 instance in AWS
    • Amazon Linux 2 AMI
    • t2.micro 64-bit (x86)
    • Use defaults for everything
  • Once launched, edit the Security Group and allow inbound HTTP and HTTPS traffic (for IPv4 and IPv6)
  • SSH into the machine and do the following:
    • sudo su
    • passwd
      • Set root password and store to 1Password
    • passwd ec2-user
      • Set ec2-user password and store to 1Password (no idea what the AWS default for this is – maybe I’m not supposed to know!)
    • yum install php php-mysql mariadb-server
    • service mysqld start
    • mysqladmin -uroot create blog
    • mysql_secure_Installation
    • Store root MySQL credentials in 1Password
    • Create separate MySQL user for WP
      • mysql -uroot -p
      • CREATE USER wordpress IDENTIFIED BY ‘PASSWORD’;
      • GRANT ALL ON blog.* TO ‘wordpress’;
    • cd /var/www/html
    • wget http://wordpress.org/latest.tar.gz
    • tar -xzvf latest.tar.gz
    • mv wordpress blog
      • No real need to name it blog, but the post I followed did this. I also stuck with having wp installed in a subdir rather than the root for Apache.
    • vim .htaccess
      • Copy contents from below
    • chown -R apache:apache *
    • cd blog
    • mv wp-config-sample.php wp-config.php
    • vim wp-config.php and set
      • DB_NAME
      • DB_USER
      • DB_PASSWORD
    • service httpd start
    • Make sure Apache and MariahDB start on boot
      • chkconfig mariadb on
      • chkconfig mysqld on
  • Setup WordPress
  • Setup DNS
    • I already had my modelephant.net domain registered with GoDaddy
    • Login to GoDaddy and point A record to the IP of the EC2 instance
    • Wait an hour for caches to clear (TTL for my existing A record was 1hr)
    • Login to WordPress admin
    • Go to Settings->General and set
      • WordPress Address (URL): http://modelephant.net/blog
      • Site Address (URL): http://modelephant.net
      • N.B. The site address is now pointing at the root directory. A few more steps are necessary to get the site serving from the root directory instead of blog
  • Serve from root directory (SSH again)
    • sudo su
    • cd /var/www/html
    • mv blog/index.php .
    • vim index.php
      • Change the location of the require to ‘/blog/wp-blog-header.php’
  • Test services start on boot (SSH again)
    • shutdown -r now

.htaccess

.htaccess is a configuration file for Apache. It doesn’t configure Apache itself, but Apache will read it whenever it serves requests from the directory this file is in (or a subdirectory). I set my config as follows:

BEGIN WordPress
 RewriteEngine On
 RewriteBase /
 RewriteRule ^index.php$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule . /index.php [R,L]
 Options -Indexes
END WordPress

This is slightly non-standard. I made two modifications to the standard WP one:

  • Options -Indexes
    • Prevents users from listing directories when the enter a URL for a directory – I personally think that looks sloppy
  • RewriteRule . /index.php [R,L]
    • Changed the Rewrite Rule for files which don’t exist to do a redirect rather than just serve the index.php. This is nicer as it means the user doesn’t have invalid URLs in their address bar.

What’s Next?

  • Add SSL (will only do that if I actually get traction on the blog this time 😀)
  • Create regular backups of DB and uploaded files
    • Probably just backup the /var/www/html directory
    • (Maybe) Move the DB onto a separate host
  • Create regular job to auto-apply updates on the machine

Leave a Reply

Your email address will not be published. Required fields are marked *