This guide provides step-by-step instructions on how to deploy a WordPress website on an Amazon EC2 instance using Amazon RDS as the database. By following these steps, you can set up a scalable and reliable WordPress site in the AWS cloud environment.
Before starting the deployment process, make sure you have the following prerequisites:
- An AWS account with appropriate permissions to create and manage EC2 instances and Amazon RDS.
- Basic knowledge of AWS services such as EC2, RDS, and security groups.
- Familiarity with SSH and command line interface (CLI) tools.
- A domain name registered with a DNS provider (optional but recommended for a production environment).
- Log in to the AWS Management Console.
- Open the EC2 service and click on "Launch Instance".
- Select an Amazon Machine Image (AMI) , which is Amazon Linux 2 , if you selected any other AMI the installation commands to setup the server are different.
- Choose an instance type based on your website's expected traffic and resource needs.
- Configure the instance details, such as the number of instances, network settings, and storage options.
- Set up security groups to control inbound and outbound traffic to the EC2 instance. Allow HTTP (port 80) and SSH (port 22) access, at a minimum.
- Review your configuration and launch the instance.
- Create or select an existing key pair for SSH access to the EC2 instance.
-
Open the Amazon RDS service from the AWS Management Console.
-
Click on "Create database" and select the database engine of your choice (e.g., MySQL, MariaDB).
-
Choose a database deployment option, such as "Standard Create" or "Easy Create" based on your needs.
-
Configure the database settings, including instance specifications, storage, and connectivity options.
- In connectivity options , make sure the same VPC id has been mentioned for the RDS instance as well the EC2 instance you want to connect it to.
- Assign the same security group created for the EC2 instance which allows SSH,HTTPS and HTTP access to the instance , to the VPC of the RDS instance also
- This is done while creating and configuring the database
- Click on "connect to EC2" and select the EC2 instance to connect it to, to create a connection between them , which creates the appropriate security groups for RDS and EC2 on port 3306.
-
Set up the database authentication credentials and choose an appropriate database name.
-
Configure additional options as needed, such as backups, monitoring, and maintenance settings.
-
Review your configuration and create the database instance.
- Once the EC2 instance is launched, note down its public IP address or public DNS name.
- Open a terminal or SSH client and connect to the EC2 instance using the SSH key pair you specified during instance launch.
ssh -i path/to/key.pem ec2-user@<instance-public-ip>
These below commands are to install LAMP stack on EC2 instance for AMAZON LINUX 2 AMI ONLY
- Update the system packages:
sudo yum update -y
- Install Apache web server:
sudo yum install httpd -y
- Start the Apache web server:
sudo systemctl start httpd
- Enable Apache to start on system boot:
sudo systemctl enable httpd
- Install MySQL server and client:
sudo yum install mariadb-server -y
- Start the MySQL server:
sudo systemctl start mariadb
- Secure the MySQL installation:
sudo mysql_secure_installation
Follow the prompts to set a root password, remove anonymous users, disallow remote root login, remove the test database, and reload privilege tables.
- Install PHP and required modules:
sudo amazon-linux-extras install php7.4 -y sudo yum install php-mysqlnd -y
- Restart Apache to apply the changes:
sudo systemctl restart httpd
I just copied the commands to download the CLI from https://wp-cli.org/de/#installing:
$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
$ php wp-cli.phar — info
$ chmod +x wp-cli.phar
$ sudo mv wp-cli.phar /usr/local/bin/wp
Now, check if the cli is available:
$ wp — info
Navigate to /var/www/html and run
$ cd /var/www/html
$ wp core download.
If you’re now typing your IP address of the instance in your browser, you should see the WordPress installation guide.
To check if the connection can be made to the RDS database created, follow these steps:
-
Verify RDS Database Endpoint: Ensure that you have the correct endpoint (DNS name) of your RDS database. You can find this information in the Amazon RDS dashboard under "Connectivity & security" for your specific database instance.
-
Use MySQL Client: You can use the MySQL command-line client on your EC2 instance (where you have installed the LAMP stack and WordPress) to test the connection to the RDS database.
Open a terminal or SSH into your EC2 instance and enter the following command:
mysql -u your_database_username -p -h your_rds_endpoint
Replace
your_database_username
with the username you created for the RDS database andyour_rds_endpoint
with the endpoint you obtained from the RDS dashboard. -
Enter Password: After running the command, you will be prompted to enter the password for the database user. Enter the password and press Enter.
If the connection is successful, you will be logged into the MySQL prompt, and you should see something like this:
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is XXXXXXXX Server version: X.X.X RDS XXXXXXXX:XXXXXXXXX ... mysql>
If the connection fails, you may see an error message indicating the reason for the failure
-
if the connection is successful run the following command to Create a database in RDS
CREATE DATABASE your_database_name;
In Place of "your_database_name" add a name for your database , dont forget the Colon at the end.
- Open a web browser and enter the public IP address or DNS name of your EC2 instance.
http://<instance-public-ip>/wp-admin/install.php
![[Pasted image 20230719182515.png]]
- For your database name , mentioned the name of the database you have just created in RDS above.
- Mention the database Username and password setup at the beginning of the RDS setup
- For the Database host , enter the RDS Instance Endpoint which looks something like this (wordpress.c2isuhOIEmks.ap-south-1.rds.amazonaws.com)