Step by Step guide on hosting multiple WordPress websites on AWS EC2 Ubuntu instance


In this article we will guide you on how to host multiple wordpress websites on an AWS EC2 instance. We will use AWS free tier t2 micro instance with the OS Ubuntu. These instruction are not limited to AWS EC2 instances. Most of the instructions can be applied to any Ubuntu Virtual machine with any service provider. Before we start please go ahead and created you free tier AWS account and secure it with MFA (Multi Factor Authentication).


Creating an EC2 Instance

When you login to AWS Management Console please select the region closest to your location from the drop down menu at the top right hand. I have selected the region Sydney which is the closest to my location. Now you can either type EC2 in the find services textbox or navigate to All Services > Compute and select EC2. This will navigate to EC2 dashboard. Click on the Launch Instance button under the Create Instance heading.

Choose and AMI

We have selected the latest Ubuntu Image for this example. Next, select the t2.micro free tier instance.

Instance Type

Next, we will select the protect against accidental termination and leave all the other default options under the Configure Instance panel.

Configure Instance

We can increase the default storage size up to 30GB on Free tier.

Storage

Next we can create some useful tags to identify our Web Server instance as below.

Tags

We will create a new security group. Provide a name, description and add the rules to allow access from SSH and HTTP as below. If you want you can restrict access to selected IP addresses only for security but for this example we will leave it open to world.

Security Group

You will be asked to create a new key pair or use an existing key pair for the instance. We’ll create a new key pair by providing a name. Download the private key and Launch the instance. It will take sometime to create the instances. Make sure to keep the downloaded key pair safe that you don’t want anyone else to use the key to gain access.

Creating Key pair

If you are using Windows and Putty to connect to the server you will have to convert .pem file to a .ppk key using Putty Gen. (you have to do this conversion only if you’re using windows)Open Puttygen and load the .pem file and click Save Private Key and save the private key in .ppk format.

Creating .ppk

Now navigate to EC2 dashboard and check whether the Instance State is running. Copy the Public DNS value or IP address of the instance.

IP Address

Next, open Putty and navigate to SSH > Auth and load/browse the .ppk private key file. Go to sessions and enter the IP address or DNS as Host Name and select port 22. You can save these details by clicking on the Save button for future connections. When you click on Open it will ask whether you trust the certificate of the server. Click yes and connect to the server instance. The default user for an EC2 Ubuntu instance is Ubuntu. Let’s upgrade the server by running the below commands.

sudo apt-get update
sudo apt-get upgrade

Create a new user with admin privileges

Let’s create a new user with admin privileges for web hosting purposes by running the below commands.

sudo adduser <username>

Provide a password and fill out the other details and create the user. Add the newly created user to sudo group to provide the admin privileges and then switch to that user by running the below commands and entering the password of the new user.

sudo usermod -aG sudo <username>
su - <username>

Install and Configure LAMP on the instance

First, we’ll install Apache web server on the instance by running the below command. You can type “http://<your public ip address>” or use the DNS copied earlier to access to check whether the installation worked properly. This will display the “Apache2 Ubuntu Default Page” which can be found at “/var/www/html/index.html”.

sudo apt-get install apache2

You can also use port forwarding to check the website in your browser with below configurations on the Putty. Load the .ppk, type the IP address and port as mentioned above and navigate to SSH > Tunnels and enter the details shown in the figure below and login to the server. Now you can navigate to 127.0.0.1:8080 on your browser to check the installation and you will be presented with the same Apache2 Ubuntu Default Page.

Creating a ssh tunnel

In this article we will install MySQL on the same instance but instead of this approach we can use another RDS instance for databases which we will present in a separate article in future. Run the below command to install MySQL on the server.

sudo apt-get install mysql-server

After the installation we have to make the installation secure by executing the below command.

sudo mysql_secure_installation

You can decide to enable the “VALIDATE PASSWORD PLUGIN” and allow MYSQL to generate an error when it encounter a weak password or it is fine to leave it disabled. Then you can change the root password. Next, remove anonymous user accounts, test databases and disable remote root logins. Finally make the changes effect immediately.

Finally we have to install and configure PHP to complete the LAMP stack. Execute the below commands to install PHP and link it with Apache2 and MySQL. We will also install few other php extensions which are needed by the Wordpress to work properly.

sudo apt-get install php libapache2-mod-php php-mysql php-xml php-gd php-curl

We have to enable Apache rewrite module to enable clean URLs via Wordpress permalinks. First we will check all the module that are enabled using the below command.

apache2ctl -M

We can enable the rewrite module using below command. Expires and Headers are two other useful modules you can enable to control the cache. We are not going to dive into caching in this article, so it is beyond the scope of this article.

sudo a2enmod rewrite

Set up Virtual Hosts for multiple Wordpress sites

First, we need to create the folder structure for the two sites we are going to host in this server as below. For simplicity, we assume you already have two domains but you don’t need two domains instead you can use subdomains or change the host files to point to different website which is not covered in this article.

sudo mkdir -p /var/www/<domain1>/public_html
sudo mkdir -p /var/www/<domain2>/public_html

After setting up the directories we need to change the ownership of those folders to our new user using the below commands.

sudo chown -R $USER:$USER /var/www/<domain1>/public_html
sudo chown -R $USER:$USER /var/www/<domain2>/public_html

Next we have to create new Virtual Host files for our domains in the server. We will copy the default file and change it as below.

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/<domain1>.conf

Below record is very important because it grants the permission to overwrite the default .htaccess rules using our own .htaccess which is located at the root directory of our website and enable clean urls. We can also change the /etc/apache2/apache2.conf file to achieve the same results but we prefer to do it on this file instead.

<Directory /var/www/<domain1>/public_html/>
 Options Indexes FollowSymLinks MultiViews
 AllowOverride All
 Require all granted
 </Directory>

Next add the below record on the file and save it. You can use vi or nano to edit the file.

ServerAdmin info@<domain1>
 ServerName <domain1>
 ServerAlias www.<domain1>
 DocumentRoot /var/www/<domain1>/public_html

Follow the above steps and create another virtual host file for the other domain.

Install Wordpress

First, let’s create a new database and a database user for our wordpress site and grant the privileges on our new database to the new user. Let’s login as the root user to MySQL using the below command.

sudo mysql -u root -p

Let’s create a new database.

create database wp_db;

Now we can create the user and grant the permissions needed as below.

CREATE USER ‘wp_user’@’localhost’ IDENTIFIED BY ‘<pasword>’;
 GRANT ALL PRIVILEGES ON wp_db.* TO ‘wp_user’@’localhost’;
 FLUSH PRIVILEGES;

Use the exit command to exit from mysql. Login using the new user to check whether everything has worked fine as we intended.

mysql -u wp_user -p

Run the below command to see the database we created.

show databases;

Exit from MySQL command line and now we have to download the latest version of Wordpress using the below command. Replace the version number with the latest version.

wget https://en-au.wordpress.org/wordpress-5.0.3-en_AU.tar.gz

Then we have to extract the files to previously created directory located at /var/www/<domain>/public_html/.

tar -xvzf wordpress-5.0.3-en_AU.tar.gz -C /var/www/<domain>public_html/--strip-components 1

Follow the above steps to create a new database and a user for the other wordpress site and extract the wordpress files to correct directory of the other site.

We can enable the sites using below commands.

sudo a2ensite <domain1>.conf

We can now disable the default using below command.

sudo a2dissite 000-default.conf

If you have configured the DNS records correctly now you should be able to navigate to your domain address and see the wordpress installation screen.

Before proceeding let’s secure the connection using Let’s encrypt free SSL certificates. Use the below commands to secure our connection. Provide a valid email address and select the option redirect all http to https option when prompted. You can check the security using the links provided at the end of the installation.

sudo add-apt-repository ppa:certbot/certbot
 sudo apt install python-certbot-apache 
 sudo certbot — apache -d <domain1> -d www.<domain1> -d <domain2> -d www.<domain2>

Finally, now you are ready to proceed with the installation with the wordpress website. Provide the details requested and create the wp-config.php file at the root folder with the given details. Run the below commands to secure the files and folders.

find /var/www/<domain1>/public_html -type d -exec chmod 755 {} \;
find /var/www/<domain1>/public_html -type f -exec chmod 644 {} \;

Add the below record to wp-config.php file to enable updates and plugin installations.

define(‘FS_METHOD’,’direct’);

Change the ownership of the wp-content directory to www-data to enable updates and plugin installation.

sudo chown -R www-data:www-data /var/www/<domain1>/public_html/wp-content

Follow the same steps to secure the files and folders and enable the updates and plugin installations for the other site. Now it’s all yours and it is up to you to use your creativity to come up with gorgeous Wordpress websites.