WordPress is a popular PHP application. It is used over many sites available on the Internet. Our task is to deploy a WordPress website on Amazon EC2 Instance. WordPress needs MySQL Database so we will use Amazon RDS for it.
Creating MySQL Database with Amazon RDS
- Go to Amazon RDS in AWS Console.
- Choose MySQL as Database Engine as WordPress uses MySQL Database Engine.
- Choose your desired template in the creation wizard (Use free-tier if you want to just test the deployment).
- Specify authentication settings like username and password and also give database name.
- Next, Choose VPC in which you want to deploy your application.
In this, we created a fully-managed MySQL database using Amazon RDS. Now, we will create an Amazon EC2 instance for running our WordPress site.
Creating an EC2 instance
- Choose Machine Image (Prefer Amazon Linux 2 AMI (HVM)).
- Choose the desired instance type.
- Now, Configure a Security Group allowing SSH from Your IP and HTTP from anywhere.
- Get a key pair used for SSH and Launch.
We have successfully launched our EC2 instance. In the next module, we will configure our RDS database to work with our EC2 instance.
Configuring RDS Database
- Allow EC2 to access RDS by Changing Security Group of RDS.
- SSH your EC2 Instance using command “ssh -i <path/to/pem/file> ec2-user@<publicIpAddress>”.
- Install MySQL on your EC2 Instance by using the command “sudo yum install -y mysql”.
- Set Environment variable for MySQL host using export “MYSQL_HOST=<rds-endpoint>”.
- Connect to Database by “mysql — user=<user> — password=<password> <database-name>”.
- Finally, create a database user for your WordPress application and give it permission to access the database. Run these commands in the terminal, “CREATE USER ‘<user-name>’ IDENTIFIED BY ‘<password>’;
GRANT ALL PRIVILEGES ON <database-name>.* TO <user-name>;
Note — This user is different from the user we gave during MySQL Engine Creation.
We learned how to configure network and password security for our RDS database. Our EC2 instance now has network access to our RDS database. Further, we created a database user to be used by your WordPress application.
Now, we will configure our EC2 instance to run the WordPress application.
Configuring WordPress on EC2
- Install Apache Web Server on EC2 by using, “sudo yum install -y httpd”.
- Download and Extract WordPress application by using following commands,
tar -xzf latest.tar.gz”.
- Change into the wordpress directory and create a copy of the default config file using the following commands:
cp wp-config-sample.php wp-config.php”.
- Move WordPress application files into the /var/www/html directory used by Apache using “sudo cp -r ./* /var/www/html/”.
- Open the wp-config file using “vim wp-config” or any other desired editor.
- Edit the following lines and enter values we got:
“// ** MySQL settings — You can get this info from your web host ** //
/** The name of the database for WordPress */
define( ‘DB_NAME’, ‘database_name_here’ );
/** MySQL database username */
define( ‘DB_USER’, ‘username_here’ );
/** MySQL database password */
define( ‘DB_PASSWORD’, ‘password_here’ );
/** MySQL hostname */
define( ‘DB_HOST’, ‘localhost’ );”
- Install the WordPress Deployments using, “sudo amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2”
- Start the service by using the command, “systemctl start httpd”.
Now, Click on the Public IP and You must be seeing the WordPress welcome page.