The goal of this project is to build an accessible website for everyone using the cloud. I will use the following technologies to accomplish this task.
- WordPress - The most popular CMS on the internet.
- AWS EC2 - The server hosting the website.
- AWS RDS - The database host for the WordPress MySQL.
Creating the Database
On the AWS dashboard, search for RDS and continue to the RDS dashboard. Click on the 'Create database' button.
Use the standard option and select MySQL database engine.
I have selected the free tier template to avoid charges on my account. You can select any of the others depending on your use case. Note that this will affect the default configuration of your database instance.
In the settings section, enter the DB instance name and Master credentials.
Select your database instance configuration.
Select the storage type and size of the database instance.
Next, enter network and connectivity settings.
In the additional configuration section, enter a database name. This automatically creates a database when the database instance is launched.
The final step is to click the 'Create database' button.
After your RDS instance has been created, you should then see it running.
Provisioning the server
Good job! We will now create an EC2 instance that will host the WordPress application. All packages and dependencies required to get the CMS running and communicating to the RDS instance will also be installed. They are:
- Apache (httpd) web server
- PHP
- MySQL
Now, go to the EC2 instance dashboard and click the 'Launch instance' button. Enter your preferred server name.
Next, select your operating system image. I have used Amazon Linux 2 here. Note that for another AMI the installation process might be slightly different.
Now select the instance type. Since we are in the free tier, I have selected the eligible t2.micro.
Create a key-pair. They are used for making secure connection from a client.
Next, configure your network settings and security group information. I have only allowed SSH (port 22) and HTTP (port 80) to communicate to the server. And for the secure connection I have selected only my IP address to have access, which will be done using the key pair I created above. You can click on 'Edit' to make more changes, also security groups can always be modified later on.
To provision the server, go to the 'Advanced details' section and modify the 'User Data' with a script that installs & starts an Apache server.
User data script:
#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
That is the last configuration for now. You can now launch the EC2 instance by clicking the 'Launch instance' button in the summary section.
You should now see the instance running.
Visit the EC2 public IP address and you should see a sample Apache page.
RDS Connection Configurations
After setting up both the EC2 web server and RDS database instances. We now want both of them to communicate directly. We would do this by modifying the security group of the RDS instance.
Go back to the RDS instance page. Click on the WordPress database to view the details page. In the 'connectivity & security' tab, click the link to the default security group.
Under 'Inbound rules' tab, select 'Edit inbound rules'
Modify the only inbound rule to allow MYSQL/Aurora type connection. In the source, search for the security group of your EC2 instance and select it. This will allow inbound MySQL connection from the WordPress EC2 instance. Save the rules.
EC2 MySQL configuration
To connect the EC2 instance, copy the public ipv4 address of the WordPress server in the EC2 instances page. You will also need a CLI tool and the key you previously created and downloaded. Remember that the server allows SSH connection from only your IP address, so as long as your IP address has not changed you should be fine. If it has, modify your EC2 instance inbound rule
I will be using Git Bash on Windows. Feel free to use any other command line tool.
Now connect to the WordPress locally using the following.
$ ssh -i wordpress-key.pem ec2-user@<ipv4 address>
Next, install MySQL on the WordPress server.
$ sudo yum install -y mysql
When the installation is completed, modify the MySQL Host with the endpoint address of your RDS instance. Get the endpoint address from your RDS instance details page. It will look something like this.
$ export MYSQL_HOST=wordpress.cdjuc7xlsvu.us-east-1.rds.amazonaws.com
Remember the Master user name and password you created earlier, you'll need to use them to establish connect with the RDS wordpress database that was created earlier.
$ mysql --user=admin --password=adminadmin wordpress
Now create a normal database user with credentials that will have access to the wordpress database.
CREATE USER 'wordpress' IDENTIFIED BY 'wp12345';
GRANT ALL PRIVILEGES ON wordpress.* TO wordpress;
FLUSH PRIVILEGES;
Exit
Note: Always use better passwords/credentials to secure your systems
Download and Configure WordPress
Download the latest WordPress package from the official source.
$ wget https://wordpress.org/latest.tar.gz
Uncompress the downloaded archive.
$ tar -xzf latest.tar.gz
This creates a wordpress directory, change directory to the wordpress directory and duplicate the config file.
$ cd wordpress
$ cp wp-config-sample.php wp-config.php
Next, use a text editor to modify the config file.
$ nano wp-config.php
Edit the default configuration with their respective values.
Next, update the key and salts with unique values found here. Then save and exit the file.
Update Apache Web Server with WordPress files
Install WordPress PHP dependencies.
$ sudo amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
Copy WordPress files to Apache root directory.
$ sudo cp -r /home/ec2-user/wordpress/* /var/www/html/
That's it. Restart Apache service to update.
$ sudo service httpd restart
Reload the WordPress website. Also, do the preliminary setup to get the full experience.
Mission accomplished! What's next? Use this setup to host your website. Or terminate resources to avoid charges. Be aware that RDS snapshots may incur charges to your account.
Thanks for reading.
Top comments (0)