Hi there, this will be an absurdly extensive “how-to” configure an AWS EC2 Instance to host a WordPress site. Before I move forward I want to point out that I will be using OS X as my dev-environment, if you’re using a Windows machine; no worries here’s some love. The following is a pretty straight forward YouTube tutorial that will cover the same but won’t be in-depth as the following.

Let me answer what many of you may be thinking, why host a WordPress site on AWS? I mean who needs Amazon’s vast infrastructure to just host a blog, right? Well let me answer both questions with, why not?! Sure, we can go the DigitalOcean route and setup WordPress there, but it won’t be as fun. Ha. If any of you in the near future find yourself in a scenario where you need to launch a WordPress site or blog in-minutes and at a low cost or often for free (free-tier). Then this setup guide will still be here. Enjoy:

Let’s begin by logging into our AWS account, if you don’t have an account this will be a great time to create a new one.

Now, go ahead and enter your e-mail address and password (if you have an existing account), if you don’t have an existing account, then select “I am a new user” and follow the steps to create your AWS account.

After logging in or creating a new account, you should be redirected to the main AWS dashboard. The dashboard showcases all available “Amazon Web Services”, in this setup guide we’ll only focus on EC2. Also, you have the ability to select a “region” which I find pretty damn awesome, you can select the physical location where your WordPress installation will be hosted. Although I’m located in Southern California I always prefer to select the US West (Oregon) region. That is my personal preference but I would recommend you to select a region that is closest to your location. FYI: All West Coast regions have a history of less down-time.

Note: For security reasons I removed my name from my AWS dashboard.

After selecting your preferred region click on EC2:

You will be redirected to the EC2 Dashboard.

Now click on Launch Instance:

Note: This setup guide will be based on the Amazon AMI 64-bit, you are free to select any other Linux version but some commands that I’m about to showcase may not work.

Click select on Amazon Linux AMI 64-bit.

Click on – Next: Configure Instance Details:

Let’s select the general purpose t2.micro “free tier eligible”:

Click on – Next Configure Instance Details:

Note: As noted before I removed my Instance network ip for security reasons.

The only configuration that will be made here is under Auto-assign Public IP select enable.

Click on – Next Add Storage:

No configurations will be made here.

Click on – Next: Tag Instance

Note: A tag helps you manage your Instances, images, and other Amazon EC2 resources, you can assign your own metadata to each resource in the form of tags.

To keep it simple as possible leave the Key as default (Name) and let’s name the Value (wordpress) all lowercase. Again, you can name them whatever you want but for the sake of this setup guide I want to keep it simple.

Click on – Next: Configure Security Group

Note: If this is your first time creating a security group, go ahead and read some documentation on it. Don’t be lazy!

If you don’t have a security group already assigned, let’s select “create a new security group”. If you already have an assigned security group, click on “select an existing security group” and choose it from the menu.

For those that want to create a new security group let’s name it: Web Services. Remember you can name it whatever you want. Also, give it a brief description or leave the default text.

Now let’s make one more configuration, under Type let’s add a rule:

Click on – Add Rule

And select HTTP.

Note: If you wan’t to add an SSL certificate you can add another rule and select HTTPS.

Click on – Review and Launch

Let’s do a final review of all configurations and settings before launching the Instance.

If everything looks good click: Launch

Now, you will be prompted to select a key pair (private & public), if you don’t have one go ahead and select “create a new pair key”. For those who already have a pair key select it from the menu.

For those that selected to create a new key pair, let’s name it wordpress and select: Download Key Pair

Note: I strongly recommend you to make a copy of your wordpress.pem file and keep it in a secure place, or just uploaded to your Dropbox and back it up; yeah it’s not secure but you won’t be able to download the file from amazon again.

After selecting to download your .pem file go ahead and Launch Instance.

Congratulations if this is your first time launching an EC2 Instance. Now let’s make it WordPress friendly!

Let’s wait about 5-10 minutes in order for the Instance to be 100% setup.

Note: If you are not comfortable using the command line interface (CLI); I don’t know what to tell you, ha. I will try to keep things simple. I am using iTerm2 as my terminal app and zsh as my shell with oh-my-zsh. You don’t need to have both iTerm2 and zsh, if you’re using the regular OS X terminal app and the bash shell it will work just fine.

Open your terminal app of choice.

We’re going to connect to our EC2 Instance via ssh.

Note: The EC2 ip address that is shown on the following screenshots has been changed for security reasons.

Let me explain what we’re about to do. First, by default when you open your terminal app you begin in your “Home” directory. Let’s go ahead and change our directory where our wordpress.pem file was dowloaded to. In my case it was downloaded to my Downloads folder. We can change our directory with the following command:

cd ~/downloads

It worked like magic! Ha. Now to be sure you’re in the right place, let’s go ahead and type:

ls

You should be able to see all the folders or files, make sure wordpress.pem is a file you can see.

Second, let’s go ahead and change the permission on our .pem file with the following command:

chmod 400 wordpress.pem

Note: My .pem file is named a bit different than yours.

Third, let’s connect to our EC2 Instance:

ssh -i wordpress.pem ec2-user@ec2-52-38-24-228.us-west-2-compute.amazonaws.com

Hit enter.

For newbies let me go ahead and break down the above command:

ssh -i = Secure Shell

wordpress.pem = Key Pair Authentication

ec2-user@ = Default AWS Linux User Account

ec2-52-38-24-228.us-west-2-compute.amazonaws.com = Refers to the public DNS name of the EC2 Instance

After connecting via SSH to our EC2 Instance let’s go ahead and do some quick updates:

Update 03/17/16: When you first connect to your EC2 Instance you should receive the following warning, when you see it type “yes”:

Let’s proceed with the updates:

sudo yum update

Hit enter.

Note: You will be required to select Yes or No during the update process, type Y when prompted.

After all updates are completed:

Let’s be safe and reboot our EC2 Instance with the following command:

sudo reboot

Hit enter.

Our EC2 Instance will reboot now, let’s wait 5-10 minutes. Also, let’s go ahead and close and reopen our terminal app to keep things refreshed.

And we’re back, ha.

You know I always find it very tedious to type:

ssh -i wordpress.pem ec2-user@ec2-52-38-24-228.us-west-2-compute.amazonaws.com

Every time I connect to any EC2 Instance, so let me share a way to make it extremely easy:

If you logged in again to your EC2 Instance after the reboot, not a problem let’s exit with:

exit

Ha.

You’re back in your “Home” directory, let’s change directories again to where your .pem file is:

cd ~/downloads

Let’s open up vim with the following command:

vim ~/.ssh/config/

Note: Newbies if you’re not familiar with vim it should become your best friend and second choice text editor.

Once vim is open, enter the following:


Host ec2
Hostname ec2-52-38-24-228.us-west-2-compute.amazonaws.com
User ec2-user
IdentityFile ~/.ssh/pixilounge-wordpress.pem

Note: To save and quit, type:

😡

Note: First line, “Host ec2”, after [Host] you can name it whatever you want but let’s keep it simple with ec2.

After creating our folder and entering our EC2 Instance credentials, let’s move our .pem file from downloads to ~/.ssh:

mv ~/Downloads/pixilounge-wordpress.pem ~/.ssh

And that is it. Now you can connect to your EC2 Instance via ssh with the following command:

ssh ec2

Now, let’s install apache, php, mysql and WordPress to make some magic. Let’s go ahead and change our default user from ec2-user to root, you can use either command:

sudo -i

or

sudo su

Apache Installation:

yum install httpd

Note: You will be required to select Yes or No during the update process, type Y when prompted.

When apache completes installing, let’s go ahead and restart it:

service httpd start

Now, let’s test it, head over to your EC2 dashboard on your web browser and copy your Instance public ip.

Open a new browser tab or window and enter your EC2 Instance public ip:

If you see the default Amazon Linux AIM Test Page, your apache configuration is working correctly.

PHP & MySQL Installation:

Let’s install PHP:

yum install php php-mysql

Note: You will be required to select Yes or No during the update process, type Y when prompted.

Now let’s install MySQL:

yum install mysql-server

Note: You will be required to select Yes or No during the update process, type Y when prompted.

When MySQL completes installing, let’s go ahead and restart it:

service mysqld start

Now let’s create “myblog”, or you can name it whatever you want:

mysqladmin -uroot create myblog

Let’s go ahead and secure MySQL:

mysql_secure_installation

Note: When prompted “Enter current password for root (enter for none)”, just hit enter. And when prompted with “Set root password?”, type Y.

Create an alphanumeric password and enter it twice:

Almost done, let’s go ahead and do some tidy-up:

Note: You will be required to select Yes or No during the update process, type Y when prompted.

WordPress Installation:

Let’s go ahead and change our directory and go into our html folder:

cd /var/www/html

Now let’s install WordPress:

wget http://wordpress.org/latest.tar.gz

After WordPress is downloaded, let’s go ahead and extract the files:

tar -xzvf latest.tar.gz

After extraction, let’s rename the blog to myblog:

mv wordpress myblog

Great, almost done. Now let’s go ahead and change directories:

cd myblog

Here, we want to rename our wp-config-sample.php to wp-config.php:

mv wp-config-sample.php wp-config.php

Now, let’s configure our wp-config.php file:

vi wp-config.php

And let’s go ahead and enter our database name and password, the same credentials we made when we setup MySQL earlier.

Once we have it configured, let’s go ahead and save it and exit:

Type – : (colon key)

:wq!

And let’s restart apache:

service httpd restart

Let’s verify that WordPress was deployed correctly by entering in your web browser your EC2 Instance public ip followed by /myblog:

If you see the WordPress “welcome” page, go ahead and enter your site title, username, password and e-mail address. You also have the option for search engine web crawlers to index your site, it’s up to you if you want to select the option or not.

Now, let’s log in:

Enter the username and password that you just created.

“Hello World!”

There you have it you just deployed WordPress on an EC2 Instance.

Let me address the “issue” of permalinks:

By default if you try to change the link structure of any other option other than the “default” option your site or blog will be riddled with 404 errors. This “issue” is due to WordPress requiring an .htaccess file, and by default when we installed apache on our EC2 Instance it does not read the .htaccess file. Good news is we can fix it, here’s how:

Let’s ssh into our EC2 Instance, open your terminal app of choice and if you configured as I suggested earlier only type:

ssh ec2

Great, now let’s go ahead and switch to our super user:

sudo -i

Let’s change directories to where the httpd/conf file is:

cd /etc/httpd/conf

Let’s go ahead and configure the conf file using vi:

Let’s arrow down until we find:


Options FollowSymLinks
AllowOverride None

And change None to All, you can switch to insert mode by pressing i on your keyboard to enable editing:

Great, let’s do one more change before we exit-out, arrow down until we find:

AllowOverride None

And change None to All.

Hit ‘esc’ on your keyboard to exit insert mode in vi, and then:

Type – : (colon key)

:wq

Now let’s restart apache:

service httpd restart

Just for fun, let’s go back to our WordPress permalink settings page again, and select any “common setting option” once you have your selection go ahead and save the settings.

You should be able to see “If your .htaccess file were writable, we could do this automatically…”, let’s go ahead and fix it:

Go ahead and copy the whole code block, and switch back to your terminal app, let’s change directories to where our root WordPress install is:

cd /var/www/html/myblog

Then let’s configure our .htaccess file:

vi .htaccess

Then paste the code block:

Type – : (colon key)

:wq

Beautiful, one last change. Let’s change the file permissions for our .htaccess:

Then let’s go back to our WordPress permalink settings page again and refresh the page and you will see the writable message is gone:

And that is it!