Updated: November 2020

Overview

This post is the third part of a 6-post series with step-by-step procedures that I use to setup a simple WordPress website on AWS. In this post, a LAMP stack is setup on the AWS instance. LAMP is an acronym for Linux, Apache, MySQL, and PHP.

Note: The original version for this series was written in 2016 with EC2 servers running Ubuntu 16.04. This series has been completely updated in late 2020. An example website, Seattle Hobbies, will be used throughout this series. The development of the Seattle Hobbies website assumes a simple low-maintenance website with low visitor count with no auto-scaling or redundancy. As such, implementation and configuration are easy. I will most likely seek the support of web-dev experts if I want to establish a sophisticated website that handles commerce or significant traffic with lots of site visitors.

Installing Apache Web Server Package:

If not logged in, then log into AWS SSH as user2 (e.g., alternate user from previous post) via a command line interface (e.g., terminal on the Mac or PuTTY on Windows) as described in AWS WordPress 2: Improve Security.

To install the Apache web server package on the EC2 instance, enter the following command:

sudo apt install apache2

To verify if the installation was successful, enter either the elastic IP address (e.g., http://44.242.122.237) or the website name (e.g., http://seattlehobbies.com) into a web browser and see if this is the response:

Installing MySQL Server Package:

To install the MySQL server package on the EC2 instance, enter the following command.

sudo apt-get install mysql-server

To perform the MySQL security installation, enter the following command:

sudo mysql_secure_installation

Note that a database password will need to be created during this step. Here are the replies to the series of questions requested during My SQL setup:

Would you like to setup validate password plugin? n
Remove anonymous users? y
Disallow root login remotely? y
Remove test database and access to it? y
Reload privilege tables now? y

Installing PHP7:

To install the PHP7 and Apache PHP module, enter the following command:

sudo apt-get -y install php7.4 libapache2-mod-php7.4

Then, restart Apache:

sudo systemctl restart apache2

To test the PHP installation, a small PHP file ‘info.php’ will be created and placed in the default website directory ‘/var/www/html’:

sudo nano /var/www/html/info.php

Add the following line in the nano editor:

<?php phpinfo(); ?>

Enter ctrl-x, type y, and return to save.

Change the ownership of this file:

sudo chown www-data:www-data /var/www/html/info.php

Time to check PHP operation. In a browser window, enter either the elastic IP address (e.g., http://44.242.122.237/info.php) or the website name (e.g., http://seattlehobbies.com/info.php). The PHP file will display a ton of information about the current PHP installation.

Scrolling down through the PHP information reveals that MySQL is not listed meaning that MySQL support in PHP isn’t installed yet. The php7.4-mysql package needs to be installed. In addition, other useful PHP modules can be installed at this point:

sudo apt-get -y install php7.4-mysql php7.4-cgi php7.4-cli php7.4-curl php7.4-json

sudo apt-get -y install php7.4-gd php-imagick php7.4-mbstring php7.4-intl php7.4-pspell

sudo apt-get -y install php7.4-imap php7.4-tidy php7.4-xmlrpc php7.4-xsl php7.4-opcache php7.4-zip

sudo apt-get -y install php-memcache php7.4-sqlite3 php7.4-common php-memcached

Then, restart Apache:

sudo systemctl restart apache2

Reloading the browser window with either the elastic IP address (e.g., http://44.242.122.237/info.php) or the website name (e.g., http://seattlehobbies.com/info.php) will now include PHP information about mysql and mysqlnd and several other packages.

Installing APCu PHP cache to speed up PHP:

APCu speeds up PHP pages by caching and optimizing PHP intermediate code. To install APCu, enter the following command:

sudo apt-get -y install php-apcu

Then, restart Apache:

sudo systemctl restart apache2

Reloading the browser window with either the elastic IP address (e.g., http://44.242.122.237/info.php) or the website name (e.g., http://seattlehobbies.com/info.php) will now include PHP information about APCu.

Note: Since the ‘info.php’ displays sensitive server details about the EC2 instance, it is important to delete this file now that PHP operation is verified.

sudo rm -f /var/www/html/info.php

That’s it. The LAMP stack is installed.