Migrating Two WordPress Websites In A Single EC2 Instance
Amazon Web Services, Work

Migrating Two WordPress Websites In A Single EC2 Instance

My client Mr. Rovetta wants to migrate two of his very low traffic website into the aws cloud because he needs cheaper alternative to his current hosting so the requirements aren’t astringent except for the fact that the WordPress system is old so we need to install PHP 5.6 for compatibility reasons.

Requirements

  • The instance has to have enough resources to run a LAMP stack
  • Should run two WordPress blogs
  • Have at least enough resources to be accessed by 10 concurrent users
  • Cheaper than his current hosting
  • No regional preference
  • PHP 5.6

Architecture

  • A single EC2 instance that can host 2 low traffic WordPress websites in a LAMP stack
  • A single Elastic IP allocated to the EC2 instance

The best solution would be to use a T2 Nano which will only cost $4.25 a month but since the client is new to Amazon and has just created his account, we can make use of the free tier program to host it in a T2 Micro which should cost $8.64 a month but can be used free for up to a year.

Setup

The EC2

I planned to use the Bitnami LAMP Stack with PHP 5.6 but that had a built in PHPMyAdmin among other things and I don’t want to bloat the system, so I opted for manually setting up a LAMP stack and securing everything myself.

So on the EC2 Management Console, we click on Launch Instance and select the Ubuntu Server 16.04 LTS (HVM), SSD Volume Type and picked the t2.micro to avail of the 1 year Free tier promo. For the instance configuration, I left everything as is except for setting the Auto-assign Public IP to Disable since we’re going to use an elastic IP and checking the Enable termination protection to avoid accidentally deleting our instance.

I allocated 8 gig of storage to the instance since it was enough to host both websites. I’ve also set the security group to correctly allow access to the Apache and MySQL ports. After launching the instance and waiting for it to be accessible, I’ve gone ahead and allocated a new elastic IP and associated it with the instance.

Apache and Firewall

So the first thing we do is SSH into our instance, update all our packages and install Apache using the terminal. Then we check if the configuration is ok and it should return a Syntax OK output.

sudo apt-get update
sudo apt-get install apache2
sudo apache2ctl configtest
Syntax OK

Next we configure the firewall to allow web traffic from Apache.

sudo ufw app list
sudo ufw app info "Apache Full"
sudo ufw allow in "Apache Full"

That should allow us to view the website if we go to http://you-elastic-ip-here/

We also shouldn’t forget to enable mod-rewrite for out .htaccess

sudo a2enmod rewrite
sudo systemctl restart apache2

PHP 5.6 + Modules

Now we must install PHP. As I’ve mentioned before, the WordPress installation is quite old so we need PHP 5.6 for it to work. PHP 5.6 itself is quite old and isn’t supported by the latest version of Ubuntu anymore, hence we have to add a repository update the packages list and install.

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php5.6

After installing PHP 5.6, we install the modules and verify our php version

sudo apt-get install php5.6-mbstring php5.6-mcrypt php5.6-mysql php5.6-xml php5.6-cli php5.6-curl
sudo php -v

You should get something that looks like the following which means you’ve got PHP 5.6 installed correctly

PHP 5.6.31-4+ubuntu16.04.1+deb.sury.org+4 (cli)

Alternatively if you don’t have any restrictions in PHP and want the latest version you can use this command

sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql php-cli php-xml php-mbstring php-curl

and you will get

PHP 7.0.22-0ubuntu0.16.04.1 (cli)

MySQL Installation and Security

Now that we’ve got PHP and Apache running it’s time to do a secure MySQL installation

sudo apt-get install mysql-server
mysql_secure_installation

The installation should ask you for a root password, what I do is I generate a secure password from PasswordsGenerator and save it with the pem file. On the security wizard, I usually remove the test database, disable anonymous and make sure that root is only accessible via localhost.

Creating Database + User Credentials and Importing the SQL Backup

After installing and securing MySQL, we create a database and user, and grant permission for that user to access the created database. This database and user will be for our WordPress installations to use.

mysql -u root -p
CREATE DATABASE dabatase_name;
CREATE USER database_user@localhost IDENTIFIED BY 'database_pass';
GRANT ALL PRIVILEGES ON dabatase_name.* TO database_user@localhost;
FLUSH PRIVILEGES;
exit

Next we import the old .sql backup file into the databases like so:

mysql -u root -p database_name < backup_name.sql

We can confirm that the database backup has been successfully imported by using a SQL GUI client like MySQL Workbench and logging into the server.

Uploading Files and Configuring vhosts

Now that we’ve got LAMP stack setup and running our next task is to create a folder for each website inside /var/www/ then, import and extract the WordPress files.

cd /var/www
sudo mkdir kilitechtrek.com
sudo mkdir summit-stories.com

We may encounter problems in uploading our zip files so it’s best to change the folder owners for now.

sudo chown -R ubuntu:ubuntu /var/www/kilitechtrek.com
sudo chown -R ubuntu:ubuntu /var/www/summit-stories.com

After uploading and extracting the files using unzip we configure the Apache vhost for each website by creating vhost configuration files in the apache2 sites-available folder for each website and pasting the following structure into the configuration files.

sudo touch /etc/apache2/sites-available/kilitechtrek.com
sudo touch /etc/apache2/sites-available/summit-stories.com

sudo nano /etc/apache2/sites-available/kilitechtrek.com

<VirtualHost *:80>
ServerAdmin nilesuan@gmail.com
ServerName kilitechtrek.com
ServerAlias www.kilitechtrek.com
DocumentRoot /var/www/kilitechtrek.com
ErrorLog /var/www/kilitechtrek.com/error.log
CustomLog /var/www/kilitechtrek.com/access.log combined
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/kilitechtrek.com>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>

If we’re all done with both configuration files, we can then tell Apache to enable the configuration and restart Apache.

sudo a2ensite kilitechtrek.com.conf
sudo a2ensite summit-stories.com.conf
sudo systemctl restart apache2

After everything, there should be a problem about WordPress not being able to update the core files, themes and plugins due to permission problems. We can solve it by changing the owner of the folders.

sudo chown -R www-data:www-data /var/www/kilitechtrek.com
sudo chown -R www-data:www-data /var/www/summit-stories.com

And we’re done. That concludes the job of migrating two WordPress websites from a different host into an aws EC2 instance.

Next Post

No Comments

Leave a Reply

%d bloggers like this: