Tom Jones

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 Free Tier

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

  • 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

1# Update the package list
2sudo apt update
3
4# Download the latest packages
5sudo 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:

1# Install Apache
2sudo apt-get install apache2
3
4# Start the Apache service and enable automatic startup
5systemctl start apache2
6systemctl enable apache2
7
8# Ensure the service is running
9systemctl 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:

1# Install MySQL
2sudo apt-get install mysql-server
3
4# Perform basic database security configuration
5sudo mysql_secure_installation
6
7# You should then receive the following prompts:
8- MySQL admin password: enter over as none has been set yet
9- Update password plugin: Y
10- Password policy: whatever level you deem suitable, 0 or 1 should suffice for a practice account
11- Root password: enter something memorable or press 'N' to skip
12- Remove anonymous users: Y
13- Disallow remote root login: Y
14- Remove test database: Y
15
16# Start the MySQL service and enable automatic startup
17sudo systemctl start mysql
18sudo systemctl enable mysql

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

1# Install PHP (including required packages for WordPress)
2sudo apt-get install php php-mysql libapache2-mod-php
3
4# Navigate to the default web directory
5cd /var/www/html
6
7# Create and open a test file using the nano text editor
8sudo nano info.php
9
10# Add the following PHP snippet to the file and press CTRL + O, then Enter to save, then CTRL + X to exit
11<?php
12 phpinfo();
13?>
14
15# Restart the web server
16sudo 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:

1# Navigate up one directory
2cd ..
3
4# Delete the 'html' folder
5sudo rm -rv html
6
7# Fetch the latest WordPress tar file
8http://wordpress.sudo wget -c org/latest.tar.gz
9
10# Extract the tar file into the current directory
11tar -xzvf latest.tar.gz
12
13# Rename the extracted "wordpress" folder to the name of your website
14sudo mv wordpress example-website
15
16#Give Apache permission to access this directory:
17sudo 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.

1# Log into MySQL
2sudo mysql -u root -p <the password you chose earlier>
3
4# Create a new database for your website
5CREATE DATABASE example-website;
6
7# Create a new user for this database
8CREATE USER 'example-website-user' IDENTIFIED BY 'password';
9
10# Give the new user full permissions for the new database and exit
11GRANT ALL PRIVILEGES ON example-website.* TO 'example-website-user';
12
13FLUSH PRIVILEGES;
14
15quit

We'll now pre-configure the WordPress installation to use these new database credentials:

1# Navigate to your website's directory
2cd /var/www/example-website
3
4# Rename the sample configuration file
5sudo mv wp-config-sample.php wp-config.php
6
7# Open the renamed configuration file in the nano text editor
8sudo nano wp-config.php
9
10# Update the following fields with your new database credentials
11define( 'DB_NAME', 'example-website' );
12define( 'DB_USER', 'example-website-user' );
13define( 'DB_PASSWORD', 'password' );
14define( 'DB_HOST', 'localhost' );
15
16# 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.

1# Navigate to the sites-available directory
2cd /etc/apache2/sites-available
3
4# Make a copy .conf file from the default
5sudo cp default-000.conf example-website.conf
6
7# Open the new .conf file in the nano text editor
8sudo nano website-name.conf
9
10# Add/amend the following fields (NB. use the "Public DNS (IPv4)" value for web addresses if you skipped the domain setup step)
11<VirtualHost *:80>
12 ServerName example-website.com
13 ServerAlias example-website.com *.example-website.com
14erverAdmin you@example-website. Scom
15 DocumentRoot /var/www/example-website
16
17 <Directory /var/www/example-website/public_html>
18 Options Indexes FollowSymLinks
19 Require all granted
20 AllowOverride all
21 </Directory>
22
23 ErrorLog ${APACHE_LOG_DIR}/error.log
24 CustomLog ${APACHE_LOG_DIR}/access.log combined
25</VirtualHost>

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

1# Check configuration syntax
2sudo apachectl configtest
3
4# Enable the website
5sudo a2ensite website-name.conf
6
7# Enable the URL rewriting module
8sudo a2enmod rewrite
9
10# Restart Apache
11sudo 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.