AWS Linux

Setup a Load Balanced WordPress Website on AWS EC2 – Part 1

Disclosure: This post may contain affiliate links, which means we may receive a commission if you click a link and purchase something that we recommended.

Pinterest LinkedIn Tumblr

Setup a Load Balanced WordPress Website on AWS EC2. Learn how to setup your WordPress application to handle high traffic with auto-scaling capabilities on AWS with Elastic Load Balancing.

In this full guide you will install WordPress, configure your website to use AWS S3 for media files, create Amazon Machine Image, setup template, create a launch configuration, configure auto-scaling group to manage live traffic and setup RDS for your database.


If you have all the above mentioned required prerequisites in place, you can proceed to setup Elastic Load Balancing.

Make sure you have added the security group of your EC2 instance to the security group of RDS for a successful connection from EC2 to RDS.

Initial Server Setup

SSH to the VM Instance and start by updating and upgrading the packages.

sudo apt update
sudo apt upgrade

Install Apache2 for WordPress

Install Apache2 using the following command.

sudo apt install apache2

This will install apache2 and all required dependencies.

Configure Firewall

Now you can set up Uncomplicated Firewall (UFW) with Apache to allow public access on default web ports for HTTP , HTTPS and SSH

sudo ufw app list

You will see all listed applications.

Available applications:
Apache Full
Apache Secure
  • Apache: This profile opens port 80 (normal, unencrypted web traffic)
  • Apache Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
  • Apache Secure: This profile opens only port 443 (TLS/SSL encrypted traffic)
  • OpenSSH: This profile opens port 22 for SSH access.

Now we will enable Apache Full.

sudo ufw allow OpenSSH
sudo ufw allow 'Apache Full'

Enable UFW using the following command

sudo ufw enable

Install PHP 8 and Extensions

Add the ondrej/php which has PHP 8 package and other required PHP extensions.

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update

Once you have added the PPA you can install PHP 8.

sudo apt install php8.0 libapache2-mod-php8.0 php8.0-common php8.0-mysql php8.0-xml php8.0-xmlrpc php8.0-curl php8.0-gd php8.0-imagick php8.0-cli php8.0-dev php8.0-imap php8.0-mbstring php8.0-opcache php8.0-soap php8.0-zip php8.0-intl php8.0-bcmath unzip mariadb-client -y

Configure PHP 8

Now we configure PHP for Web Applications by changing some values in php.ini file.

For PHP 8 with Apache the php.ini location will be in following directory.

sudo nano /etc/php/8.0/apache2/php.ini

Hit F6 for search inside the editor and update the following values for better performance.

upload_max_filesize = 32M 
post_max_size = 48M
memory_limit = 256M
max_execution_time = 600
max_input_vars = 3000
max_input_time = 1000

Once you have installed and confogured your PHP settings you need to restart your Apache for the changes to take effect.

sudo service apache2 restart

Configure Website Directories

Once you have installed PHP 8 and Apache you can proceed to setup directories.

Your website will be located in the home directory and have the following structure.

Replace yourdomainname with your original domain name without the extension.

 -- yourdomainname
 ---- public

The public directory is your website’s root directory.

Now we create these directories and set correct permissions

You need to SSH into your VM Instance and run these commands

mkdir -p /var/www/html/yourdomainname/public
sudo chmod -R 755 /var/www/html/yourdomainname
sudo chown -R www-data:www-data /var/www/html/yourdomainname

Configure Virtual Hosts for WordPress

Create a new configuration file for your website in the sites-available directory.

sudo nano /etc/apache2/sites-available/yourdomainname.conf

Copy and paste the following configuration, ensure that you change the server_name, error_log and root directives to match your domain name. Hit CTRL+X followed by Y to save the changes.

<VirtualHost *:80>
      ServerAdmin [email protected]

      DocumentRoot /var/www/html/yourdomainname/public

      <Directory /var/www/html/yourdomainname/public>
           Options Indexes FollowSymLinks
           AllowOverride All
           Require all granted

      <Directory /usr/share/phpmyadmin>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted

      ErrorLog ${APACHE_LOG_DIR}/yourdomainname.com_error.log
      CustomLog ${APACHE_LOG_DIR}/yourdomainname.com_access.log combined

To enable this newly created website configuration, symlink the file that you just created into the sites-enabled directory.

sudo a2ensite yourdomainname.conf

Check your configuration and restart Apache for the changes to take effect

sudo systemctl restart apache2

Install PhpMyAdmin (Optional)

Now you can install PhpMyAdmin which helps you to create database and assign a user to it in a user friendly way.

Download PhpMyAdmin and configure it using the following commands.

sudo wget -P /usr/share
sudo unzip /usr/share/ -d /usr/share
sudo mv /usr/share/phpMyAdmin-5.1-all-languages /usr/share/phpmyadmin
sudo rm -f /usr/share/
sudo mkdir /usr/share/phpmyadmin/tmp
sudo chmod -R 777 /usr/share/phpmyadmin/tmp
sudo cp /usr/share/phpmyadmin/ /usr/share/phpmyadmin/

Edit the file

sudo nano /usr/share/phpmyadmin/

Add the following below the /* Authentication type */.

$cfg['Servers'][$i]['auth_type'] = 'cookie';
/* Server parameters */
$cfg['Servers'][$i]['host'] = 'RDS_DATABASE_ENDPOINT';
$cfg['Servers'][$i]['compress'] = false;
$cfg['Servers'][$i]['AllowNoPassword'] = false;
$cfg['Servers'][$i]['verbose'] = 'Amazon RDS';
$cfg['Servers'][$i]['port'] = '3306';
$cfg['Servers'][$i]['socket'] = '';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['TempDir'] = '/usr/share/phpmyadmin/tmp';

Enter the blowfish secret.

$cfg['blowfish_secret'] = 'Some_Secret_Password_54_characters';

Hit CTRL+X followed by Y and ENTER to save and exit the file.

Create Database

Now you can login to your RDS instance with PhpMyAdmin ( using the master username and password you configured while creating the instance.

Create a new database and an user associated with that database.

Download WordPress

Now that our server software is configured, we can download and set up WordPress.

It is always recommended to get the latest version of WordPress from their website.

cd /var/www/html/yourdomainname/public
curl -LO

Once WordPress is downloaded you need to extract it using the following command.

sudo tar xzvf latest.tar.gz

Now, you can copy the entire contents of the directory into our document root.

sudo cp -a /var/www/html/yourdomainname/public/wordpress/. var/www/html/yourdomainname/public

Next cleanup your root directory by deleting the wordpress folder and the downloaded tar file.

sudo rm -r /var/www/html/yourdomainname/public/wordpress
sudo rm -f /var/www/html/yourdomainname/public/latest.tar.gz 

Setup correct permissions for the root folder. Don’t forget to replace the yourdomainname with your domain name you used.

sudo chmod -R 755 /var/www/html/yourdomainname
sudo chown -R www-data:www-data /var/www/html/yourdomainname

Install WordPress

Now visit your website in the browser and select the language you would like to use and click continue.

You will be prompted to enter your database, user, password, and hostname.

Enter the database name you created using PhpMyAdmin and the user assigned with the database with the password. Finally, enter the RDS endpoint as the hostname.

Now you can run the installation.

Once the installation is complete we need to set the method that WordPress should use to write to the file system. Since we’ve given the web server permission to write where it needs to, we can explicitly set the file system method to “direct”. Failure to set this with our current settings would result in WordPress prompting for FTP credentials when we perform some actions like WordPress update, plugin updates, file upload, etc. This setting can be added below the database connection settings in the configuration file.

sudo nano /var/www/html/yourdomainname/public/wp-config.php

Find the line define('DB_PASSWORD', 'password'); and paste the following line below it.

define('FS_METHOD', 'direct');

Hit Ctrl+X and Y to save your configuration file.

Become a Certified AWS Professional with this easy to learn course now.

That’s for the part 1. Now you have created a Instance in EC2 and RDS and installed WordPress.

In the next part you can configure AWS S3, configure WordPress to use S3 for media library, request AWS managed SSL, create load balancer, auto scaling group and point your domain to Load balancer.

You can see the next part of this full tutorial here.


  1. At the “Create Database” step I am unable to load my PhpMyAdmin without modifying the site config file to include a “Alias “/phpmyadmin” “/usr/share/phpmyadmin” line. Even after that and installing WordPress I am unable to load the homepage of my site. Is there something I am missing? Is anyone else experiencing this?

    • I am also having this issue. PHP My Admin is not working following the steps as described.

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.