Creating and Hosting a Wordpress Site (Using AWS EC2 & RDS)

November 2, 2017
aws ec2 howto rds wordpress

Introduction

If you’d like to skip this introduction please click here.

So, I was presented to AWS (Amazon Web Services) in one of our post-graduation courses (Langara College). Basically, it is a set of cloud services like computing, storage, internet of things, artificial intelligence, etc. I’ve made a quick research about these services and realized it is possible to combine two of them to create and host a Wordpress site, one is called EC2 (Elastic Compute Cloud) which is a virtual server, the other is called RDS (Relational Database Service) which is a database manager.

AWS offers a starting experience package called Free Tier, where they give you access to use many services for free within a year (see their site here), but we are considering the creation of the website using their default URL, so if you’d like to buy a domain AWS offers this service as well on Route 53 for about C$12 depending on which name you want and around C$0.54 per month (maintenance). The price rises considerably after the one year trial (around C$20 per month), but this is logical because we’re paying for a virtual server and a database management services, and a Wordpress site is only a small portion of the real potential for these two services (see pricing of EC2) (see pricing of RDS).

In the next chapters, I’ll explain how to create these services and host a Wordpress site. So let’s go to the solution!

Contents

1. AWS Free Tier & Creating an Account

When we open the AWS landing page (free tier session), the company highlight its main services eligible for the year free experience, and we can already see the two services we’re going to use for the tutorial. Both services, EC2 and RDS, have 750 hours per month free access each (within a year), that means we can use one EC2 instance and one RDS instance free (24h x 31days = 744h).

The first thing we need to do is create an AWS account, so go to AWS website and follow their steps.

2. Hands On

2.1. Security Groups

In AWS console main page, you can see a service search field, type “EC2” and click on the first search result.

aws console search ec2

On the left-side menu bar, click on the security groups option.

We’ll create two groups, server-tier and db-tier, the first one will be responsible to allow only you to access the server through SSH, and everyone to access the website through HTTP, the second one will be responsible to allow only you and the server to access the database.

Create a new group:

aws security group server-tier

Copy the Group ID just created, you’ll use it for the second group.

Create another group:

aws security group db-tier

2.2. Creating RDS Instance

Back to the AWS console main page, type “RDS” and click on the first search result.

aws console search rds

On the RDS console main page, click on Get Started Now to create a new instance. Check the option Only enable options eligible for RDS Free Usage Tier to secure the usage of the free tier.

aws free tier checkbox

Choose the option MySQL and click “Next”.

In the Step 1, at the Settings section, give a name for the instance and a username and password. The rest of the options we can leave as it is.

rds creation step 1

In the Step 2, select yes for the Public accessibility option, then choose db-tier for the VPC security groups option and leave only this security group (delete any other associated).

rds creation step 2-1

Give a name for your first database of this instance, and leave the rest of the options as it is.

rds creation step 2-2

Click on Launch DB instance to initiace the creation process. You can follow the status by clicking Instances on the left-side menu bar.

2.2.1. Checking Connection

We can try connecting to the instance as soon as it’s finished the creation process. I’ll use MySQL Workbench, just install from their website.

For the hostname, use the endpoint created with the RDS instance.

rds endpoint

And you can check your databases created by fetching the command show databases;.

rds connected with mysql workbench

2.3. Creating EC2 Instance

Back to the AWS console main page, type “EC2” and click on the first search result.

aws console search ec2

Click on Launch Instance to start the creation process.

In the Step 1, check the Free tier only option. Let’s choose the first option here (Amazon Linux AMI 2017.09.0 (HVM), SSD Volume Type).

ec2 creation step 1

In the Step 2, check the Free tier eligible option and click Next: Configure Instance Details.

For the Steps 3, 4, and 5, leave the options as they are and click Next.

In the Step 6, select the previously created server-tier security group. Click on Review and Launch.


ec2 creation step 6


Review your options and click on Launch. A pop-up window is shown asking you to use or create a key pair for secure connection, so create a new key pair and download it, and click on Launch Instances.

2.3.1. Connecting Via SSH

By finishing the EC2 instance creation process, click on Connect to get the guide for SSH connection.


ec2 instance created


Follow their guide and connect to the EC2 instance.

ec2 connected

2.3.2. Checking Connection To RDS

In the EC2 instance, update the modules using the command below.

> sudo yum update -y

Install the MySQL client.

> sudo yum install mysql -y

Connect to the RDS instance using the endpoint created and the username and password.

> mysql -h ec2-rds-wordpress.cllwpsr5wpkr.us-west-2.rds.amazonaws.com -u admin -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 32
Server version: 5.6.37-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Type a command to test the connection.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| innodb             |
| my_site_1          |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

Type exit to close the connection.

2.4. Creating And Hosting Wordpress Site

To create and host a Wordpress site, we need basically three “things”: an Apache server, PHP, and MySQL. The MySQL database we already have from the RDS instance create in the previous chapters, let’s install Apache and PHP.

In the EC2 instance, type the following command to install Apache, PHP, and the communication driver to PHP/MySQL.

> sudo yum install -y httpd24 php70 php70-mysqlnd

Start the Apache service and test it in a web browser (using the EC2 endpoint).

> sudo service httpd start

ec2 apache test webpage

Now, stop the Apache service.

> sudo service httpd stop

Add this user (EC2-USER) to apache group.

> sudo usermod -a -G apache ec2-user
> groups
ec2-user wheel

Log out of EC2 and log in again to make effect.

> groups
ec2-user wheel apache

Change the group ownership of /var/www and its contents to apache group.

> sudo chown -R ec2-user:apache /var/www

Add to the group write permissions and set group ID on future subdirectories and files.

> sudo chmod 2775 /var/www
> find /var/www -type d -exec sudo chmod 2775 {} \;
> find /var/www -type f -exec sudo chmod 2775 {} \;

Download Wordpress from its website.

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

Unzip the file.

> tar -xzf latest.tar.gz

Create the wp-config.php file from sample provided and edit it.

> cd wordpress
> cp wp-config-sample.php wp-config.php
> vim wp-config.php

Edit the file according to the RDS instance created. Also, generate the authentication keys from the website specified in the comments and paste in the file.

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'my_site_1');

/** MySQL database username */
define('DB_USER', 'admin');

/** MySQL database password */
define('DB_PASSWORD', 'your_password');

/** MySQL hostname */
define('DB_HOST', 'ec2-rds-wordpress.cllwpsr5wpkr.us-west-2.rds.amazonaws.com');

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

/**#@+
 * Authentication Unique Keys and Salts.
 *
 * Change these to different unique phrases!
 * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
 * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
 *
 * @since 2.6.0
 */
define('AUTH_KEY',         'generate from website above');
define('SECURE_AUTH_KEY',  'generate from website above');
define('LOGGED_IN_KEY',    'generate from website above');
define('NONCE_KEY',        'generate from website above');
define('AUTH_SALT',        'generate from website above');
define('SECURE_AUTH_SALT', 'generate from website above');
define('LOGGED_IN_SALT',   'generate from website above');
define('NONCE_SALT',       'generate from website above');

/**#@-*/

Go outsite the wordpress directory and copy all the content of the wordpress directory to /var/www/html/.

> cp -r wordpress/* /var/www/html/

To allow permalinks feature change the file in /etc/httpd/conf/httpd.conf.

> sudo vim /etc/httpd/conf/httpd.conf

Inside the tag <Directory "/var/www/html"> change the AllowOverride to All.

<Directory "/var/www/html">
    ...
    AllowOverride All
    ...
</Directory>

Start the Apache service.

> sudo service httpd start

To start the apache service every time you initiate EC2.

> sudo chkconfig httpd on

Finally, check your website (on EC2 endpoint)

wordpress created

And it’s working! YEAHHH!

3. Conclusion

AWS offers some good services for a year for free, and if you’d like just to create and host a Wordpress site you can create yours for a very low cost monthly (considering you want to use your domain). After the free one year experience, the price will rise consistently and you might want to evaluate other options like Hostgator or GoDaddy. Another possible option is to install MySQL Server inside the EC2 instance, so you don’t need the RDS for this case, and the final cost would drop considerably.

4. Resources

[Android] Creating Custom Login Screen for AWS Mobile Hub

November 20, 2017
aws android howto login mobilehub

Serving Dynamic Web Pages Using HapiJS

October 11, 2017
dynamic html hapijs howto nodejs

Calling an API on Android Studio

September 19, 2017
android api howto java