How to Install WordPress on AWS EC2 Instance

How to Install WordPress on AWS EC2 Instance

Now that we have created an EC2 instance in my earlier post How to Install WordPress on AWS EC2 Instance, let’s go ahead and install a WordPress software and start building a simple site. To keep our setup very simple, we will do without any VPC or complex security group rules.

What we will do here.

  • Create an Application Tier
    • We will install the apache httpd,WordPress software,php & mysql for php.
  • Create a Database Tier
    • Setup Mysql DB using Amazon RDS, DB tier for the WordPress site.

We can custom install a WordPress on top of the EC2 instance which we have already lanuched as part of How to Launch my First EC2 instance – Part1. As well we can use Amazon’s Lightsail a fully managed service to build/launch a WordPress site.

Now we will start setting up WordPress on top of the EC2 instance.

Note: Whenever you stop and start the EC2 instance, a new public ip will assigned, SSH using the same and connect to instance.

1) Setting up an Application tier (EC2 instance setup):

SSH into your instance and switch to root user and issue below commands in sequence,

Commands Description
Sudo su switch to root user
yum update -y Updates all software patches on the instance.
sudo yum install -y httpd24 php70 mysql56-server php70-mysqlnd Installs Apache server,pho,mysql
cd /var/www/html Go to this directory to download
wordpress software
wget https://wordpress.org/latest.tar.gz Download wordPress s/w
tar -xzf latest.tar.gz Extract the contents
cp -r wordpress/* /var/www/html/ copy the wordPress directory under this directory.
rm -rf wordpress remove the extracted content.
*** This step is optional to save space.
rm -rf latest.tar.gz remove the downloaded zip file.
chmod -R 755 wp-content change the permission of wp-content directory
chown -R apache:apache wp-content make ‘apache’ as the owner of wp-content.
yum -y install php php-mysql Install php software for WordPress.
As well install php-sql.
yum install mysql-server mysql-client install mysql server client aswell.
service httpd start Start the Apache server
chkconfig httpd on Always turn on Apache server.
Save & Exit. Save & Exit.
How to install WordPress on AWS EC2 Instance

How to build WordPress on AWS EC2 Instance

Now, we have successfully configured our EC2 instance(Application tier) with all necessary tools & software’s required to work on WordPress site.

2) Setting up a Database Tier using RDS:

Database -> RDS -> Instances -> Mysql (make sure you click the free tier checkbox checked)

  1. Create a New Database

AWS RDS Setup

2.  We will go with all the default setup’s as provided by AWS, including VPC, Security Group.

AWS RDS Setup

AWS RDS Setup for WordPress AWS RDS for WordPress

Once DB is created We will be doing below steps to complete your Setup.

  1. Edit the already created Security Group, to allow all traffic. This is not advisable, but we will do this to make our access between application tier and database tier simple.
  2. Modify our DB and attach the existing Security Group, which was originally created during EC2 instance creation. Again this is not advisable.
  3. Use our EC2 IP to configure WordPress site.
  4. In a real world scenario, we will create a VPC and place the Application tier inside a Public Subnet & DB tier in a Private Subnet.As this particular post is for beginners we will try to do as simple as possible.

Editing the Security Group.

  1. Go to Ec2 Instance and click on the security group, to edit the access.

 Modify the RDS Security Group to add the above Security Group.

Go to RDS Console -> Database -> particularDB and click on modify and add the security group.

Now copy the EC2 instance’s IP and paste in the web browser to setup WordPress site, as below,

url = ‘<<IP>>/wp-admin/options.php’

Click on Let’s go! and enter the DB details. You can get all the required details from RDS console and copy your DB endpoint to paste in Database host.

After clicking on submit a wp-config.php content will be displayed. It is very important to copy the entire content of the text and paste in a notepad. SSH to EC2 instance and go to /var/www/html/ and create a new file wp-config.php and paste this content, save and exit. Then press on ‘Run the installation’ only after the file is successfully saved.

You will be taken to ‘WordPress Welcome’ page. Provide your Site Tile,username & password to connect to the WordPress site and then click on Install, which will take you to Success page with login details.

Enter the above Created Username & Password to login into your WordPress site.

Click on Vist Site to see your WordPress site with admin rights to edit your content.

Hwo to Install WordPress on EC2


Well done !!! now you have created your own WordPress Site.

Common Errors and Fixes on How Install WordPress on AWS EC2

Though it looks easy to install, configure and manage WordPress, there are quite few errors which will cause a concern. Below are some errors and resolutions which i have come across.

1) ‘Error establishing database connection‘ . – This either means that the username and password information in your wp-config.php file is incorrect or we can’t contact the database server at localhost. This could mean your host’s database server is down.

Resolution:

This is mostly to do with DB connection.

  • Could be because restrictions at Security Group. Try giving ‘Allow All’ and check if that works, then slowly add other Access types and remove ‘Allow All’.
  • Proper Security Group not attached, modify the one at RDS console and add the correct Security Group.
  • DB itself is down, restart it.
  • wp-config.php file already exists with incorrect DB details, replace the db details there.

2) Migrating EC2 instance from one IP to another IP. A very common issue we may face is when we Stop and Start our EC2 instance. And if we have not assigned a Static IP, everytime a new IP will be assigned, due to this our DB will not be able to connect back to Application server. For this case follow the below,

  1. Advice you to take back up of your wp-config.php file and your Database before proceeding.
  2. ssh -> go to ‘/var/www/html/’ open wp-config.php. And add a new row  define(‘RELOCATE’,true).
  3.  Log into mysql db and update the below tables,
    1. update wp_options
      set option_value = ‘http://34.205.48.202‘ — replace this with your new IP
      where option_name = ‘home’;
    2. update wp_options
      set option_value = ‘http://34.205.48.202‘ –replace this with your new IP
      where option_name = ‘siteurl’;
    3. Or update across
      1. update wp_options set option_value = replace(option_value, ‘<<your old ip>>’, ‘<<your new ip>>’);

3) While installing a plugin, if it asks for FTP password, edit the wp-config.php file and add

define('FS_METHOD','direct');

4) Some times your permalinks may not work. Only you home page works and rest of the pages breaks. It could be because of broken permalinks. Try below to fix the same,

Go to : /etc/httpd/config

Edit httpd.comfig and change below

Options Indexes FollowSymLinks
AllowOverride All
Require all granted

You may also like...

2 Responses

  1. October 19, 2018

    […] part of How to Setup WordPress on AWS EC2 instance – Part1,we have already configure our Application Tier as well Database […]

  2. October 19, 2018

    […] we have created our own WordPress Site as part of How to Setup WordPress on AWS EC2 instance – Part1 & How to Setup WordPress on AWS EC2 instance – Part2 . With this we can launch our own […]