Create a MySQL Database on AWS RDS and connect it to the WordPress PHP site using AWS CLI

Ayush Rawat
4 min readJun 3, 2021

The first step we have to create the security group for the MySQL Database because we have to attach it at the time of the creation of the Database at the creation of we have to only provide the group name and the description for the security group creation.

Then we have to create the MySQL database command using the following command

aws rds create-db-instance --engine mysql --engine-version <MySQL Version> --db-instance-identifier <database-instance-name> 
--allocated-storage <size> --db-instance-class <instance-class>
--vpc-security-group-ids <securitygroup-id> --master-username <db-master-username> --master-user-password <db-master-password> --no-multi-az --no-auto-minor-version-upgrade --no-publicly-accessible
--deletion-protection

Output :

Then we have to launch an instance for WordPress PHP web server in the same VPN of the database as we have launched the database in privately accessible so it can only access in by the instances in the same VPN

So first we create the security group for the instance

and the key pair using the command

as it required for launching the instances. Then the command for launching the instance is

aws ec2 run-instance --instance-type t2.micro --image-id 
ami-0a9d27a9f4f5c0efc --security-group-ids <sg-id>
--associate-public-ip-address --key-name wordpress-key
--tag-specifications ResourceType=instance,Tags=[{Key=Name,Value=wordpressfrontend}]

Output:

After launching the instance we have to connect to the instance using the SSH we can do it using the command in the following in Windows command line.

ssh -i keyname username@instanceIp

After doing ssh to that instance we have to install some software on that instance to configure the environment for WordPress PHP

  • Install httpd for using the Apache webserver
dnf install httpd -y
  • Install EPEL release for extra packages
dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y
  • Install Remi release for PHP packages
dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm -y
  • Install PHP packages using the Remi module
dnf module install php:remi-7.4
  • Install php-mysqld for connecting PHP and MySQL
dnf install php-mysqlnd -y
  • Install MySQL
dnf install mysql -y

After installing all the software download WordPress PHP from the WordPress website using the command

curl -O https://wordpress.org/latest.tar.gz

Extract this zip file

tar zxvf latest.tar.gz

then move all the content of the WordPress PHP site to the document root

mv wordpress/* /var/www/html/

change the owner of the folder to an apache user

chown -R apache:apache /var/www/html

The environment is configured now we have to connect to our MySQL database and create a database on it using the endpoint we get from the RDS as a host

So the command for connecting is

mysql -h <endpoint> -u <username> -p

then create the database by

create database <databasename>;

and exit the using

exit

as demonstrated in the images

Then finally we have to enable and start the webserver services using the commands

To start

systemctl start httpd

To enable permanent

systemctl enable httpd

Then we have to go to the instance public IP address on the browser where the webserver is started after we have to the following

Put your database data that your database name, username, password, and database host

Then put the site data your site title username, password, and email id

The site successfully started and linked with a database

Thanks for Reading

--

--