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 list2sudo apt update34# Download the latest packages5sudo 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 Apache2sudo apt-get install apache234# Start the Apache service and enable automatic startup5systemctl start apache26systemctl enable apache278# Ensure the service is running9systemctl 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 MySQL2sudo apt-get install mysql-server34# Perform basic database security configuration5sudo mysql_secure_installation67# You should then receive the following prompts:8- MySQL admin password: enter over as none has been set yet9- Update password plugin: Y10- Password policy: whatever level you deem suitable, 0 or 1 should suffice for a practice account11- Root password: enter something memorable or press 'N' to skip12- Remove anonymous users: Y13- Disallow remote root login: Y14- Remove test database: Y1516# Start the MySQL service and enable automatic startup17sudo systemctl start mysql18sudo 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-php34# Navigate to the default web directory5cd /var/www/html67# Create and open a test file using the nano text editor8sudo nano info.php910# Add the following PHP snippet to the file and press CTRL + O, then Enter to save, then CTRL + X to exit11<?php12 phpinfo();13?>1415# Restart the web server16sudo 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 directory2cd ..34# Delete the 'html' folder5sudo rm -rv html67# Fetch the latest WordPress tar file8http://wordpress.sudo wget -c org/latest.tar.gz910# Extract the tar file into the current directory11tar -xzvf latest.tar.gz1213# Rename the extracted "wordpress" folder to the name of your website14sudo mv wordpress example-website1516#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 MySQL2sudo mysql -u root -p <the password you chose earlier>34# Create a new database for your website5CREATE DATABASE example-website;67# Create a new user for this database8CREATE USER 'example-website-user' IDENTIFIED BY 'password';910# Give the new user full permissions for the new database and exit11GRANT ALL PRIVILEGES ON example-website.* TO 'example-website-user';1213FLUSH PRIVILEGES;1415quit
We'll now pre-configure the WordPress installation to use these new database credentials:
1# Navigate to your website's directory2cd /var/www/example-website34# Rename the sample configuration file5sudo mv wp-config-sample.php wp-config.php67# Open the renamed configuration file in the nano text editor8sudo nano wp-config.php910# Update the following fields with your new database credentials11define( 'DB_NAME', 'example-website' );12define( 'DB_USER', 'example-website-user' );13define( 'DB_PASSWORD', 'password' );14define( 'DB_HOST', 'localhost' );1516# 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 directory2cd /etc/apache2/sites-available34# Make a copy .conf file from the default5sudo cp default-000.conf example-website.conf67# Open the new .conf file in the nano text editor8sudo nano website-name.conf910# 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.com13 ServerAlias example-website.com *.example-website.com14erverAdmin you@example-website. Scom15 DocumentRoot /var/www/example-website1617 <Directory /var/www/example-website/public_html>18 Options Indexes FollowSymLinks19 Require all granted20 AllowOverride all21 </Directory>2223 ErrorLog ${APACHE_LOG_DIR}/error.log24 CustomLog ${APACHE_LOG_DIR}/access.log combined25</VirtualHost>
And finally, we'll check the configuration syntax is correct and enable the website:
1# Check configuration syntax2sudo apachectl configtest34# Enable the website5sudo a2ensite website-name.conf67# Enable the URL rewriting module8sudo a2enmod rewrite910# Restart Apache11sudo 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.
