How to create an Amazon EC2 instance and install PHP, MySQL and WordPress on AWS


Here’s a guide I wrote (primarily for myself) with details everything you need to do to spin up an Amazon EC2 VPS instance and install and configure a WordPress to run a website on it.

NB – Since I wrote this guide, I have since terminated my Amazon AWS account as I found it to have hidden costs and didn’t fancy receiving a shock hosting bill if I got a traffic spike, so have since switch my web hosting to a Digital Ocean VPS running a Linux Ubuntu 14.04 install.

Here’s my guide anyway as it may come in handy for someone as I spent many days and hours labouring over getting WordPress installed on an AWS EC2 instance, enjoy!

Create a new EC2 instance (virtual server)

  • Login to AWS
  • Click on EC2 in the dashboard
  • Click on instances
  • Click launch instance
  • Click Amazon Linux AMI (most updated and community supported package).  Leave 64-bit clicked
  • Select t1.micro instance
  • Configure instance details – enter 1 instance and leave the other entries as default.
  • Add storage – Leave all defaults
  • Tag Instance – Leave all defaults
  • Configure security groups – Click this in the left hand nav
    1. click ‘create security group’ and give it a name and description
    2. click the ‘inbound’ tab (should already be selected)
    3. click add rule
    4. select SSH and source as ‘anywhere’ unless you want to lock it down to a single IP address, in which case enter that.
    5. click add rule
    6. select HTTP and leave defaults
    7. click add rule
    8. select HTTPS and leave defaults
    9. click create
  • Review Instance and click launch
  • Public and Private Key screen –
    1. select ‘create a new key pair’, give it a name and
    2. click the download key pair button which will save your private key file on your computer.
    3. click launch button
  • Click view instances buttonGo back into the Instances page and you should see your new instance initialising.  At this point rename the instance to something relevant to you (this won’t be used anywhere, its just a name for you.
  • Install PHP & MySQL

  • Lock down access to your private key. The server will not allow you to have access with your private key with it’s current default permissions as it’s permissions are open by default and you will get a message saying “warning: unprotected private key file”.  To change to more secure permissions type the following command:
    • **chmod 400 ****
  • SSH onto the server.
    1. Open Terminal
    2. Open Finder and locate the folder where your private key file (created in step 12 above) is located.  You can drag and drop the private key file from finder into Terminal in step C below to create the path to private key file.
    3. Enter the following command to gain access to the instance/server.  
      ssh -i ec2-user@</b> </li> </ol> </li>
    4. sudo su (puts you into root environment so you don’t need to type sudo before every command.
    5. Install PHP & MySQL
    6. * ****yum install php php-mysql**** * **yum install mysql-server**
    7. Start MySQL service
    8. ****service mysqld start****
    9. Create MySQL database
    10. * ****mysqladmin -uroot create ****
    11. Secure the database installation
    12. * ****mysql\_secure\_installation****
    13. Press enter for none when asked for current password as none is set yet
    14. Enter the root password
    15. Remove anonymous users, select Yes
    16. Disallow remote root login, select Yes
    17. Remove test database, select Yes
    18. Reload privilege tables, select Yes
    19. Start Apache service
    20. * ****service httpd start**** ## Test the site is running OK Your web server should now be setup with PHP, MySQL and Apache should both now be running and ready to go.  To test the site is serving web pages go back to the instances page in the the management console and look for the section that say “Public DNS”.  This is your current domain name URL for your site.  Cut and paste this into a web browser and if this installation has been performed correctly you should be served something that looks like the image below: ## Install WordPress
    21. Switch to public_html folder on the server cd /var/www/html
    22. Download WordPress repository wget http://wordpress.org/latest.tar.gz
    23. Unpack the repository tar -xzvf latest.tar.gz
    24. OPTIONAL STEP – Move the installation from wordpress folder into a new directory of your choice, or if you prefer, the root folder. mv wordpress </b> </li> ## Connect WordPress with MySQL database
    25. Switch to folder where wordpress is installed cd </b> </li>
    26. Rename sample config file to actual config file used by WordPress mv wp-config-sample.php wp-config.php
    27. Edit the config file with the database details created previously using the built in visual editor vi wp-config.php
    28. Navigate with the cursor to where it says ‘database_name_here’, hit i to change to text edit mode and change this to </b> created previously.   </li>
    29. Change the username to root
    30. Change the MySQL password to the one you changed it to earlier.
    31. Press escape
    32. Save your change and quit the vi editor with :wq
    33. Restart Apache to see changes service httpd restart
    34. Go to a web browser enter your domain name with /wordpress at the end and you should see the WordPress installation setup screen
      1. /wordpress</b> </li> </ol> </li>
      2. Done.  Rock & Roll!
      3.  
  • Contact Me