Host Multiple WordPress websites in a single Ubuntu instance of AWS

MSP Medium
9 min readNov 2, 2023

In this tutorial, we will follow a step-by-step procedure to be able to host multiple WordPress websites within a single AWS instance. We will be using virtual hosts for this particular task completion.

Step1: Set up an Ubuntu instance and host a WordPress site in it

We will start from AWS console here. From the services, select EC2 and launch an instance with following specifications: Ubuntu AMI, t2.micro instance type, a key pair, a security group which allows SSH, HTTP, and HTTPS traffic from internet. For this specific task I will be using User data and write bash script to set up the first WordPress site. If you want to learn to host WordPress site using commands then you can follow this link. Then, launch the instance, wait for it to run and after few minutes the WordPress site should be available to install and accessible from the instance’s public IP address.

Bash Script for WordPress.

#!/bin/bash
# Update and upgrade packages
sudo apt update -y && sudo apt upgrade -y
# Install Apache, MySQL, and PHP
sudo apt install -y apache2 mysql-server php libapache2-mod-php php-mysql
# Change the Apache document root directory to /var/www/html/wordpress
sudo sed -i "s/DocumentRoot.*$/DocumentRoot \/var\/www\/html\/wordpress/g" /etc/apache2/sites-available/000-default.conf
# Change the Apache directory settings to allow overrides
sudo sed -i "s/AllowOverride.*$/AllowOverride All/g" /etc/apache2/apache2.conf
# Create a WordPress database and user
sudo mysql -e "CREATE DATABASE wordpress; \
CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'password'; \
GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'@'localhost'; \
FLUSH PRIVILEGES;"
# Download and extract the latest version of WordPress
cd /tmp && curl -LO https://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz
sudo mv wordpress /var/www/html/
# Set the correct permissions for the WordPress files
sudo chown -R www-data:www-data /var/www/html/wordpress
sudo chmod -R 755 /var/www/html/wordpress
# Create a wp-config.php file from the sample file
cd /var/www/html/wordpress
sudo mv wp-config-sample.php wp-config.php
sudo sed -i "s/database_name_here/wordpress/" wp-config.php
sudo sed -i "s/username_here/wordpressuser/" wp-config.php
sudo sed -i "s/password_here/password/" wp-config.php
# Restart Apache
sudo systemctl restart apache2

Step 2: Create a new database in MySql and a user with the privilege to access that database for the second WordPress site

One of the first things to consider while hosting multiple WordPress sites in a single instance is to make sure the individual site use their database. The bash script we used only created a user and database for the WordPress site. We can create another database and user in bash script too by modifying the lines of code that was used for the former site. But for the sake of learning let’s do it using the commands.

First, we need to SSH into the instance using Putty. For hostname we use the username and DNS of the instance and access via port 22 for SSH connection. Next, we add the private key created earlier and connect to the instance.

For accessing the already installed MySQL database we use

sudo mysql
To create database, user, and grant the privilege of the database to the user:
CREATE DATABASE wordpress2;
CREATE USER 'wordpressuser2'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON wordpress2.* TO 'wordpressuser2'@'localhost';
FLUSH PRIVILEGES;
exit;

To exit mysql in terminal.

To check

SHOW DATABASES;
SELECT user FROM mysql.user;

Step 3: Set up files for the second WordPress in the instance

To set up the second WordPress site we can download the new files using wget from the official WordPress site. The simpler way that we followed here is just to copy the previously downloaded files to a new directory.

Step 4: Configure the second database and user for the second WordPress site

Next, we just need to edit wp-config.php file to update the names of database and user and also the password for that user so that the second WordPress site can access the newly created database for its operations.

Step 5: Set up virtual host files for both WordPress sites

Up next, we set up a configuration file for each of the WordPress site in /etc/apace2/sites-available directory. Then, we activate those configuration files and disable the default one.

<VirtualHost *:80>
ServerAdmin admin@wordpress2.com
ServerName w2.sanjeebnepal.com.np
ServerAlias www.wordpress2.com
DocumentRoot /var/www/html/wordpress2
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin admin@wordpress1.com
ServerName w1.sanjeebnepal.com.np
ServerAlias www.wordpress1.com
DocumentRoot /var/www/html/wordpress1
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 6: Set up DNS in Cloudflare

Subsequently, I used Cloudflare service and my preregistered domain for pointing to my sites. We need to copy the public IP address of the instance and create two A records each pointing to the subdomains we have put in the virtual hosts configurations.

Step 7: Install WordPress on both sites

This is an easy step. We access both sites by putting in subdomains in the browser. Then we put in the site name, set usernames and passwords and install WordPress.

Step 8: Install plugins in WordPress to be able to edit the files

Here, we notice that we are not able to edit the WordPress site. This is because the browser is communicating to the Cloudflare service using HTTPS protocol while the WordPress in HTTP protocol. One option here is to set up theSSL setting to HTTP in Cloudflare so there is communication using HTTP on both sides. However, this is not secure and it is better practice to make use of HTTPS protocol.

Thus, a better option is to install an SSL plugin in the WordPress site and that will make it possible for HTTPS communication. One simple option is Flexible SSL for Cloudflare. I here used this option for the first WordPress site.

However, for the second site, various issues arose. First, we tried to install another option for Flexible SSL, Really Simple SSL. The first issue that arose is the installation asked for FTP credentials. To bypass this, we add this line of PHP code in wp-config.php

define(‘FS_METHOD’, ‘direct’);

After reloading the apache web server we are still unable to install the plugin. This is because we had everything properly set up for initial WordPress site. But we just created a new directory for the second one. So the root user still has the permissions of the directory. To solve this we give the directory modification permissions wo www-data user and user group. This allows the WordPress site to make changes to the directory.

sudo chown -R www-data:www-data /var/www/html/wordpress2

Now we can install the plugin in the second WordPress site too.

Next, we need to add another line of code to wp-config.php to activate the Really Simple SSL. And enjoy the output.

Author: Sanjeeb Nepal

https://www.linkedin.com/in/sanjeebnepal/

--

--