How did I do?*

Set up a WordPress website in AWS using the LAMP stack

Introduction

In this tutorial we’ll be launching a new EC2 instance using AWS, then installing and configuring the LAMP software stack with WordPress. By the end of this tutorial you will have a publicly-accessible WordPress website, optionally secured, and routed through a custom domain.

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.

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

  • Access to an AWS account with administrative permissions
  • Familiarity with AWS, Ubuntu/Linux, PHP and the LAMP stack is beneficial but not required

Launch a new EC2 instance

Log into AWS and use the search bar to navigate to EC2, select Instances from the sidebar, then click Launch Instance.

Search for "Ubuntu" and select the instance entitled "Ubuntu Server 20.04 LTS (HVM)". Select the "t3.micro" instance type and proceed until you reach "Step 6: Configure Security Group".

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.

Press Review and Launch, then Launch.

EC2 security is managed using key pair files in .pem format. 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 based on the configuration or purpose of the server it will apply to (e.g. ‘Linux-WordPress’, or ‘Personal-Web’).

Return to your list of instances, select the one you’ve just created – the status will likely still be "pending", then under the Description tab make a note of the "Public DNS (IPv4)".

Connect to your new instance using PuTTY

PuTTY is a Windows application which lets you manage connections to servers using Secure Shell (SSH). Download PuTTY from https://putty.org/ and install it using the installation wizard.

In order to connect using PuTTY, we need to generate a .ppk version of the .pem file we downloaded earlier - this can be done using PuTTYgen, which is included in the installation.

Open PuTTYgen, press Load, select "All Files (*.*)", navigate to the .pem file associated with your new instance and press "Open". Press "OK" on the popup notification and hit Save private key, add a passphrase if needed, otherwise press Yes to proceed without. Save your new file using the same name, but with the ".ppk" extension, and close PuTTYgen.

Next, open PuTTY, and under "Session", in the "Host Name" field, paste the "Public DNS (IPv4)" value you copied from AWS earlier, prefix it with "ubuntu@", which is the default username for Ubuntu instances), and ensure the "Port" is 22.

Under "Connection", change the "Seconds…" value to anything other than zero, to this stop your SSH connection from timing out due to inactivity, I use "180".

Under SSH > Auth, hit "Browse" and select your key file with the .ppk extension, then 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.

Finally, hit Open.

Install Apache, MySQL and PHP

Once connected, run the following commands to prepare the instance

# Update the package list
sudo apt update

# Download the latest packages
sudo apt-get upgrade

then run these commands to install and start Apache, which is the reverse proxy we will be using to serve web requests:

# 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

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.

Next, we will install the database software needed for WordPress (MySQL), enter the following commands:

# 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

Finally, we need to install the PHP runtime, and optionally create a file to test it's all working:

# 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

# Add the following PHP snippet to the file and press CTRL + O, then Enter to save, then CTRL + X to exit
<?php
  phpinfo();
?>

# Restart the web server
sudo systemctl restart apache2

Open your web browser and paste the "Public DNS (IPv4)" value into the address bar, followed by "/info.php" - you should see lots of information relating to PHP installation.

Download WordPress files

Next we need to download the WordPress files and adjust the directory structure and permissions 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

#Give Apache permission to access this directory:
sudo chown -R www-data:www-data /var/www/example-website

Create a MySQL database for WordPress

Since WordPress websites, and CMS websites in general, store pretty much everything from configuration to content in a database, we need to create a database within MySQL for our new website.

# 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

We'll now pre-configure the WordPress installation to use these new 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 the Apache Virtual Host

Apache uses a configuration file for each website being hosted. If you're familiar with the IIS UI, this step is similar to adding and configuring an AppPool and website on the server.

# 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

# 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>

And finally, we'll check the configuration syntax is correct and enable the website:

# 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

Install WordPress

Open up a web browser and navigate to ~/wp-install - you should be presented with the installation page.

Fill in Database Name, Username and Password with your MySQL credentials. Leave Database Host and Table Prefix as the defaults, and press Submit.

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.

And that's it for the essentials! The steps which follow in the next article, Register a domain and secure your WordPress website with a free SSL certificate, are optional, but important if you wish to make your website publicly accessible and appear more professional.