Set up a WordPress website in AWS using the LAMP stack

8th Nov 2020 AWS, Hosting, WordPress

Finding these articles useful? Help support more articles like this

This article forms part of the WordPress Setup Series:

By the end of this tutorial series you will have a publicly-accessible, secure WordPress website routing through your own domain.

In Part 1 of this tutorial series we’ll be launching a new EC2 instance using AWS, then installing and configuring the LAMP software stack with WordPress.

A website hosted using the LAMP stack consists of:

  • Linux: the operating system being run on the server. In this tutorial we’ll be using the Ubuntu distribution. The principles will be similar for other distributions, but some aspects may differ slightly
  • Apache: the web server which will be running our website, responsible for handling HTTP requests and responses
  • MySQL: the database software used by WordPress to store website data
  • PHP: the programming language which WordPress is written in, and is required for the website to function.

Important: We’ll be making use of resources from AWS’s “free tier” in this tutorial. Please ensure you’re familiar with the AWS pricing structure before continuing if you intend to use resources beyond those required for this tutorial.

AWS has a selection of server images available in the Marketplace which come with pre-installed services and configurations for WordPress websites. Some of these are free, others require a recurring payment for software on top of any AWS resources used. By the end of this tutorial you should have the knowledge necessary to create your own image which can be re-used time and time again.

Assumptions

Steps

  1. Launch a new EC2 instance
  2. Connect to your new instance using PuTTY
  3. Install Apache, MySQL and PHP
  4. Install WordPress
  5. Create a MySQL database for WordPress
  6. Configure WordPress database credentials
  7. Configure Apache Virtual Host
  8. Set up WordPress
  9. Optional (Database GUI): Install phpMyAdmin

Launch a new EC2 instance

  1. On AWS, navigate to Services > EC2 > Instances
  2. Click ‘Launch Instance’
  3. Search for ‘Ubuntu’ and select the instance entitled ‘Ubuntu Server 20.04 LTS (HVM)’
  4. Select the ‘t2.micro’ instance type
  5. Proceed until you reach ‘Step 6: Configure Security Group’
  6. The default security group for this instance only permits SSH access (port 22), however we’ll also be needing HTTP (80). Press ‘Add Rule’, select ‘HTTP’ as the type, and ‘My IP’ as the source – you can amend this security group to accept incoming HTTP requests from ‘Anywhere’ when your website is ready to be seen by the public
  7. Press ‘Review and Launch’, review your settings and make any necessary adjustments
  8. Press ‘Launch’
  9. Create a new key pair and download the .pem file. Call the key file whatever you like as long as it makes sense to you – I name mine after the configuration or purpose of the server it will apply to (e.g. ‘Linux-WordPress’, or ‘Personal-Web’)
  10. Return to your list of instances, select the one you’ve just created – the status will likely still be ‘pending’ – under the ‘Description’ tab you will find the ‘Public DNS (IPv4)’, copy value this into Notepad.

Connect to your new instance using PuTTY

  1. Download PuTTY from https://putty.org/ and install it following the installation wizard
  2. Open PuTTYgen
  3. Press ‘Load’, select ‘All Files (*.*), navigate to the .pem file associated with your new instance and press ‘Open’
  4. Press ‘OK’ on the popup notification and hit ‘Save private key’, add a passphrase if needed, otherwise press ‘Yes’ to proceed without
  5. Save your new file using the same name, but with the ‘.ppk’ extension, and close PuTTYgen
  6. Open PuTTY
  7. Under ‘Session’, in the ‘Host Name’ field, paste the value you copied for the ‘Public DNS (IPv4)’, and prefix it with ‘ubuntu@’ (the default username), ensure the ‘Port’ is 22
  8. Under ‘Connection’, change the ‘Seconds…’ value to anything other than zero, this stops your SSH connection from timing out due to inactivity, I use ‘180’
  9. Under SSH > Auth, hit ‘Browse’ and select your key file with the PPK extension
  10. Go back to ‘Session’, select ‘Default Settings’ under ‘Saved Sessions’ and press ‘Save’ so you don’t have to re-do this next time you want to connect
  11. Press ‘Open’

Install Apache, MySQL and PHP

  1. Once connected, run the following commands:
# Update the package list
sudo apt update

# Download the latest packages
sudo apt-get upgrade

# Install Apache
sudo apt-get install apache2

# Start the Apache service and enable automatic startup
systemctl start apache2
systemctl enable apache2

# Ensure the service is running
systemctl status apache2
  1. At this stage you can verify that everything is working by pasting the ‘Public DNS IPv4’ value into your browser – you should be presented with the Apache welcome page
  2. Enter the following commands to install MySQL:
# Install MySQL
sudo apt-get install mysql-server

# Perform basic database security configuration
sudo mysql_secure_installation

# You should then receive the following prompts:
MySQL admin password: # enter over as none has been set yet
Update password plugin: Y
Password policy: # whatever level you deem suitable, 0 or 1 should suffice for a practice account
Root password: # enter something memorable or press 'N' to skip
Remove anonymous users: Y
Disallow remote root login: Y
Remove test database: Y

# Start the MySQL service and enable automatic startup
sudo systemctl start mysql
sudo systemctl enable mysql
  1. Install PHP and create a test file:
# Install PHP (including required packages for WordPress)
sudo apt-get install php php-mysql libapache2-mod-php

# Navigate to the default web directory
cd /var/www/html

# Create and open a test file using the nano text editor
sudo nano info.php
  1. Add the following PHP snippet to the file and press CTRL + O, then Enter to save, then CTRL + X to exit
<?php
phpinfo();
?>
  1. Run the following command to restart the web server:
sudo systemctl restart apache2
  1. Open your web browser and paste the ‘Public DNS (IPv4)’ value into the address bar, followed by ‘/info.php’. If PHP was installed successfully you should see a page of information relating to the PHP configuration.

Install WordPress

  1. Remove the test files and install WordPress using the following commands:
# Navigate up one directory
cd ..

# Delete the 'html' folder
sudo rm -rv html

# Fetch the latest WordPress tar file
sudo wget -c http://wordpress.org/latest.tar.gz

# Extract the tar file into the current directory
tar -xzvf latest.tar.gz

# Rename the extracted 'wordpress' folder to the name of your website
sudo mv wordpress example-website
  1. Give the Apache user (‘www-data’) ownership of this directory:
sudo chown -R www-data:www-data /var/www/example-website

Create a MySQL database for WordPress

# Log into MySQL
sudo mysql -u root -p <the password you chose earlier>

# Create a new database for your website
CREATE DATABASE example-website;

# Create a new user for this database
CREATE USER 'example-website-user' IDENTIFIED BY 'password';

# Give the new user full permissions for the new database and exit
GRANT ALL PRIVILEGES ON example-website.* TO 'example-website-user';

FLUSH PRIVILEGES;

quit

Configure WordPress database credentials

# Navigate to your website's directory
cd /var/www/example-website

# Rename the sample configuration file
sudo mv wp-config-sample.php wp-config.php

# Open the renamed configuration file in the nano text editor
sudo nano wp-config.php

# Update the following fields with your new database credentials
define( 'DB_NAME', 'example-website' );
define( 'DB_USER', 'example-website-user' );
define( 'DB_PASSWORD', 'password' );
define( 'DB_HOST', 'localhost' );

# Save and exit by pressing CTRL + O, ENTER, CTRL + X

Configure Apache Virtual Host

  1. Create a new configuration file for your website
# Navigate to the sites-available directory
cd /etc/apache2/sites-available

# Make a copy .conf file from the default
sudo cp default-000.conf example-website.conf

# Open the new .conf file in the nano text editor
sudo nano website-name.conf
  1. Add/amend the following fields (NB. use the ‘Public DNS (IPv4)’ value for web addresses if you skipped the domain setup step)
<VirtualHost *:80>
    ServerName example-website.com
    ServerAlias example-website.com *.example-website.com
    ServerAdmin you@example-website.com
    DocumentRoot /var/www/example-website

    <Directory /var/www/example-website/public_html>
    Options Indexes FollowSymLinks
    Require all granted
    AllowOverride all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
  1. Save, exit and enable required modules
# Save and exit
CTRL + O, ENTER, CTRL + X

# Check configuration syntax
sudo apachectl configtest

# Enable the website
sudo a2ensite website-name.conf

# Enable the URL rewriting module
sudo a2enmod rewrite

# Restart Apache
sudo systemctl restart apache2

Set up WordPress

  1. Open your web browser and navigate to http://<your-domain-or-public-dns>/wp-install – you should be presenting with a page similar to below
  1. Fill in Database Name, Username and Password with your MySQL credentials. Leave Database Host and Table Prefix as the defaults, and press ‘Submit’
  2. After a few moments, and assuming there were no issues with any of the previous steps, you should be presented with the default WordPress dashboard