DEV Community

Cover image for Deploy WordPress on EC2
On-cloud7
On-cloud7

Posted on

Deploy WordPress on EC2

•Wordpress
WordPress is a highly popular content management system (CMS). It is most commonly used for blogs but can also be used for running e-commerce sites, message boards, company websites, Portfolio Websites and many other popular use cases. In this guide, you will learn how to set up a WordPress site to run a blog.
•AWS Services services that use in this project
An Amazon EC2 instance to install and host the WordPress application.

Implementation
Here are the steps on how to deploy WordPress on an EC2 instance in AWS:

  1. Create an EC2 Instance: 2.Navigate to the EC2 service: Click on "Services" and select "EC2"

Image description

3.Launch a new instance: Click on "Launch Instances"

Image description

Image description

4.Choose an AMI: Select a suitable Amazon Machine Image (AMI).

Image description

5.Choosing an instance type:

Scroll down to select an EC2 instance type. An instance type is a particular configuration of CPU, memory (RAM), storage, and network capacity.
AWS has a huge selection of instance types that cover many different workloads. Some are geared toward memory-intensive workloads, such as databases and caches, while others are aimed at compute-heavy workloads, such as image processing or video encoding.
Amazon EC2 allows you to run 750 hours per month of a t2.micro instance under the AWS Free Tier.

**Select the t2.micro instance.

Image description

  1. Configuring an SSH key: You will see a details page on how to configure a key pair for your instance. You will use the key pair to SSH into your instance, which will give you the ability to run commands on your server. a.Open the key pair (login) section and choose Create new key pair for your instance.

Image description

b.You will return to Key pair (login) Section and you will find that Key has been selected.

  1. Configuring a security group and launching your instance: You need to configure a security group before launching your instance. Security groups are networking rules that describe the kind of network traffic that is allowed to your EC2 instance. You want to allow traffic to your instance:

•SSH traffic from all IP addresses so you can use the SSH protocol to log in to your EC2 instance and configure WordPress.

•HTTPS traffic from all IP addresses so that users can view your WordPress site Secured.

•HTTP traffic from all IP addresses so that users can view your WordPress site.

a. To configure this, select Allow SSH traffic from Anywhere and select Allow HTTPS & HTTP traffic from the internet

Image description

  1. Configuring storage on instance.

Image description

  1. Launch: It is now time to launch your EC2 instance. a. Choose the Launch instance button to create your EC2 instance. b. Wait for instance to launch.

c. You have successfully launched your EC2 instance. Click on Open address to view website.

Image description

Image description

  1. Allocate elastic IP : a.Click on Ec2 dashboard b. Click on Allocate Elastic IP

Image description
c.To connect elastic IP click on the action and click on associate elastic IP addresses.

Image description
d.After that choose instance and click on associate.

Image description

  1. Connect to Your EC2 Instance: •Use SSH to connect: Use a terminal or an SSH client like PuTTY to connect to your EC2 instance using its public IP address and the key pair you created earlier.

All the commands that are executed :

  1. Install Apache server on Ubuntu
    sudo apt install apache2

  2. Install php runtime and php mysql connector
    sudo apt install php libapache2-mod-php php-mysql

  3. Install MySQL server
    sudo apt install mysql-server

  4. Login to MySQL server
    sudo mysql -u root

  5. Change authentication plugin to mysql_native_password (change the password to something strong)
    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'Testpassword@123';

  6. Create a new database user for wordpress (change the password to something strong)
    CREATE USER 'wp_user'@localhost IDENTIFIED BY 'Testpassword@123';

  7. Create a database for wordpress
    CREATE DATABASE wp;

  8. Grant all privilges on the database 'wp' to the newly created user
    GRANT ALL PRIVILEGES ON wp.* TO 'wp_user'@localhost;

  9. Download wordpress
    cd /tmp
    wget https://wordpress.org/latest.tar.gz

  10. Unzip
    tar -xvf latest.tar.gz

  11. Move wordpress folder to apache document root
    sudo mv wordpress/ /var/www/html

Image description

Image description

  1. copy and paste the code in the terminal.

  2. Command to restart/reload apache server
    sudo systemctl restart apache2
    OR
    sudo systemctl reload apache2

  3. Install certbot
    sudo apt-get update
    sudo apt install certbot python3-certbot-apache

  4. Request and install ssl on your site with certbot
    sudo certbot --apache

Image description

Top comments (6)

Collapse
 
ryanlwh profile image
Ryan Lee • Edited

Nice tutorial. Be aware that the resources of the micro instance are limited. If you were to run this configuration in production, the machine would crash indefinitely even with a small audience (I learned it the hard way). In that case, it is better to deploy MySQL separately on another instance, or the RDS free tier.

Collapse
 
oncloud7 profile image
On-cloud7

Thanks for the heads up! You're absolutely right, micro instances are fantastic for testing purposes but can struggle in production. Running MySQL on a separate instance, either another EC2 instance or the RDS free tier, is a much better approach for handling a real audience.

Collapse
 
nickor profile image
Nick

Thanks for this! I've always wanted to host my WordPress sites on AWS EC2 but since I've never administered my own server before I'm scared that something would go wrong and my sites will be offline for days while I'm trying to find a solution. There is no tech support to help.

Collapse
 
oncloud7 profile image
On-cloud7

You're welcome! I understand your concerns about hosting your WordPress sites on AWS EC2. It's a great platform for scalability and control, but managing your own server can be daunting, especially with no dedicated tech support.

Collapse
 
anurag_vishwakarma profile image
Anurag Vishwakarma

Try via docker compose it will hardly take 2 min to run a WordPress container.

Collapse
 
oncloud7 profile image
On-cloud7

That's an excellent suggestion!will definitely try on docker