Hosting WordPress site with AWS EC2 and RDS
For this example we are using Amazon Linux 2 AMI (AMI2) instance and Amazon Relational Database Service(RDS) to setup MYSQL database. To make things easy and fast we will use a bootstrap script also known as a bash script in the launch configuration of EC2 instance.
Step 01 — Creating EC2 Instance.
Login in to your AWS management console and navigate to compute services and select EC2 which is also known as the backbone of AWS. Select your region from the top right hand corner of management console in this case I have selected ap-southeast-2 (Sydney). Select Amazon Linux 2 AMI instance and follow the configurations below.
on the next screen choose the instance type as t2.micro instance and go to configure instance details section.
In the configuration screen click on the box on prevent against accidental termination this ensures that you will not accidentally delete your production instance. In order to terminate you have to modify and disable termination protection. Leave the other configurations as default.
scroll down to the page an expand on advance details and attach the bash script. I will explain what this script is doing when creating our instance. Download the bootstrap script from the link below.
This script will install Apache and php-mysql and then navigate to html directory and download WordPress and unzip all contents and copy all contents to /var/www/html directory. Then it performs some cleanup to remove zip file and WordPress install file. chmod -R 755 wp-content gives owner the read/write and execute permissions while read and execute permissions to others. And it will create .htaccess file and create wp-config.php file.
If you do not include this script you have to manually do this after creating the instance.(in that case download the script and open with your favorite text editor and follow the commands after ssh in to your instance)
After attaching the bash script navigate to next section to configure storage, For this example we will go with 10GB and no need to add additional volumes keep the rest as default and go to next sections to add TAGS.
Add tags that you can identify your instances, tags basically allows customers to assign metadata to AWS resources. Refer to this AWS documentation to get an idea on how to use tags.
Assign your own metadata tags to each Amazon EC2 resource to manage your instances, images, and other resources.docs.aws.amazon.com
For this example we will create a new security group ‘WPSecurity’ and we will add SSH and HTTP on port 22 and 80. This allows web traffic to come in to the instance from port 80 and port 22 allows you to SSH in to your instance.(you can restrict ssh access to a given IP but for this we will keep it open)
Press review and launch and then you will be prompt with a screen to select or create the key pair that you need to SSH in to your instance. Make sure that you download the key pair and keep it safe. The key pair will be in .pem format, if you’re using windows and using putty to SSH you need to convert .pem file to .ppk using puttygen. After downloading the key pair select the tick box to acknowledge and click on Launch Instance.
It will take some time to create the instance once it finish you can see your instance in the EC2 dashboard notice the IP address next to public DNS we will need that to ssh in to the instance to do the rest of the configuration.
Open a putty terminal and ssh in to your instance use your IP address to the ec2 instance as host and then add your key pair in ssh>auth and save the session for future use and then open the connection. Default user for AMI2 instance is ec2-user.
After you ssh in to the instance you can confirm whether your bootstrap script has worked by using cd /var/www/html and if you can see the WordPress files. check the .htaccess file as well. This is a hidden file so run ls -al command to see all the files.
Now we will setup our RDS instance and after setting that up we will come back to ec2 instance to do the rest of the configuration.
Step 02— Creating RDS Instance.
In your AWS management console go to services >>database >>RDS and select MYSQL
in the next screen keep the default configurations and on the settings section enter db instance identifier and master username and password and confirm the password. For the password do not include ‘@’ and ‘/’ characters.
In the next section network and security use the default configurations and select yes on public accessibility for this example but as for the best practice we can restrict its accessibility only for instances in the VPC- virtual private cloud.
In the next section define a database name and keep the rest as default and finally launch the database instance.
It will take sometime to create the db instance so be patient refreshing it every second will not make the process faster :) After your instance finish creating then click on the instance scroll down and then click on the security groups inbound rules. This will open a new tab ec2 security groups section. In here edit the inbound rule on MYSQL/Aurora to 0.0.0.0/0 and click save.
Now we are in the last section it's time to go back to the ec2 instance.
Step 03 — Edit wp-config.php file to connect the RDS database
Navigate in to your html directory by cd /var/www/html and run
sudo nano wp-config.php
this will open wp-config file for you to edit. replace the database name, database user ,password and host from local to endpoint. you can get the RDS endpoint from clicking on your instance in RDS dashboard.
And now it's time to test your connection run sudo service httpd start to start apache service.
and then open your browser and go to your instance IP address. Then you will see the installation page.
create your site title and username and password for your WordPress site. After you log in you can see the dashboard and you can customize and build your site according to your needs. Now you have your website hosted on AWS EC2 instance which uses RDS MYSQL instance as your database.
If you want to know how to setup multiple WordPress websites in one AWS instance go to this link.