WordPress Installation using Amazon Web Services

So to set up a few sites using WordPress I decided to set up a new virtual server using Amazon Web Services.  I provisioned an EC2 t2.small Ubuntu instance that I would use the run everything on.  Later on I’ll probably distribute the load using RDS to move the MySQL database off of the host.  I used multiple reference guides to set everything, Apache HTTPD, MySQL, and Varnish so I thought I would put together a quick article that shows all the steps in one clean location.  Total Time: Under 1 Hour.

Apache HTTPD

Installing Apache HTTP is pretty straightforward and requires just one command:

sudo apt-get install -y apache2

After Ubuntu installs Apache HTTPD the ports.conf file needs to be updated since Varnish will be using port 80. Apache HTTPD needs be modified to use a different port. Open /etc/apache2/ports.conf and update any references to port 80 to 8080 and 443 to 8443. These ports are normally reserved for Tomcat, but since we’re not installing Tomcat on this box, it shouldn’t make any difference.

My ports.conf ends up looking like this:

Listen 8080

<IfModule ssl_module>
        Listen 8443
</IfModule>

<IfModule mod_gnutls.c>
        Listen 8443
</IfModule>

After this the final Apache HTTPD task for now is to turn on mod_rewrite.

sudo a2enmod rewrite

Varnish

Before we install Varnish we need to install an Ubuntu package:

sudo apt-get install apt-transport-https

The Varnish WordPress article says to do the following:

sudo echo "deb https://repo.varnish-cache.org/ubuntu/ precise varnish-4.0" >> /etc/apt/sources.list.d/varnish-cache.list

I had trouble with this and opened up /etc/apt/sources.list.d/varnish-cache.list using nano and then added the “deb https://repo.varnish-cache.org/ubuntu/ precise varnish-4.0” (Note: Do not type in the quotes!) by hand.
After this performing the following will install Varnish. The update is required to pick up the new source added in previous step.

sudo apt-get update
sudo apt-get install -y varnish

After the Ubuntu installation finishes we need to configure Varnish to use port 80. We can open up the /etc/default/varnish file and find the DAEMON_OPTS that is not commented with a #. For me this was the second one in the configuration file.  The DAEMON_OPTS should look like this:

DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"

The Varnish WordPress article then takes a few steps to build out the default.vcl file. I’ll simplify the steps here. Open up /etc/varnish/default.vcl and make sure it has all of the following:

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

acl purge {
  "localhost";
}

sub vcl_recv {
    if (req.method == "PURGE") {
      if(!client.ip ~ purge) {
        return (synth(405, "Not allowed."));
      }
      if (req.http.X-Purge-Method == "regex") {
        ban("req.url ~ " + req.url + " && req.http.host ~ " + req.http.host);
        return (synth(200, "Banned."));
      } else {
        return (purge);
      }
    }
    if (req.url ~ "wp-admin|wp-login") {
      return (pass);
    }
    set req.http.cookie = regsuball(req.http.cookie, "wp-settings-\d+=[^;]+(; )?", "");
    set req.http.cookie = regsuball(req.http.cookie, "wp-settings-time-\d+=[^;]+(; )?", "");
    set req.http.cookie = regsuball(req.http.cookie, "wordpress_test_cookie=[^;]+(; )?", "");
    if (req.http.cookie == "") {
      unset req.http.cookie;
    }
}

sub vcl_backend_response {
    if (beresp.ttl == 120s) {
      set beresp.ttl = 1h;
    }
}

sub vcl_deliver {
}

This will set up Varnish to grab the information from Apache HTTPD and cache it while also setting up the purging.

MySQL

You can install MySQL on Ubuntu using the following command:

sudo apt-get install -y mysql-server

After the installation create a new schema for WordPress in the MySQL CLI

create schema wordpress

I tend to use one schema for all my WordPress instances and rely on the namespacing (XX_) to separate tables for each WordPress instance.

PHP

PHP needs to be installed on the server. This involves multiple packages:

sudo apt-get install php5
sudo apt-get install php-mysql
sudo apt-get install libapache2-mod-php5
sudo apt-get install php5-mysql

WordPress

WordPress can be downloaded from https://wordpress.org/ and then unzipped.

wget https://wordpress.org/latest.tar.gz
tar zxvf latest.tar.gz

After WordPress is expanded we can set up the application we want to build

cp -r wordpress/ /var/www/test.com

So we don’t run into any issues with permissions we need to change the permissions for /var/www. This will allow WordPress to write the wp-config.php and handle uploads appropriately.

sudo chown -R www-data /var/www

Since we’re setting up multiple WordPress instances on this server, we need to create a site config. We’ll copy the default one and then make the appropriate changes.

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/test.com.conf

After copying the file the contents of /etc/apache2/sites-available/test.com.conf should be modified to look like the following

<VirtualHost *:8080>
    ServerName test.com
    ServerAlias www.test.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/test.com

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

After the conf is modified we can enable it

sudo a2ensite test.com.conf

After the site is enabled we can restart Apache HTTPD and Varnish.

sudo service apache2 restart
sudo service varnish reload

Typing in varnishlog and hitting the website at https://www.test.com should now generate logs of the varnish caching process and should take you to the WordPress install screen.

Have any questions, comments, or concerns? Contact Me