DEV Community

Muhammad Muzammil
Muhammad Muzammil

Posted on

Deploy WordPress on AWS EC2: Quick and Easy Setup!

Image description

It is essential to understand the importance of writing this blog and to grasp the significance of deploying WordPress on AWS.

In 2024, over 810 million websites are using WordPress, accounting for 43.2% of all live websites, essentially 2 out of every 5 sites you visit. WordPress holds a 63.3% market share in the CMS market and powers over a third of the world’s top websites. Impressively, more than 500 new sites are built daily on the platform. WordPress’s growth has grown substantially, from just 12% of all sites in 2011 to now powering 36% of the top 1 million websites, including major names like The New York Times, Bloomberg, and Nike.

Image description

Setting up a LAMP (Linux, Apache, MySQL/ MariaDB, PHP) server on an AWS EC2 instance is essential for hosting dynamic websites. In this guide, I will explain the process of launching an EC2 instance, installing the necessary components, and configuring a WordPress site.

Launch an EC2 Instance

Here are the basic steps to launch an EC2 instance on AWS

Login to the AWS Management Console

Go to the AWS Management Console.

Image description

Sign in using your AWS account credentials.

Navigate to EC2 Dashboard

In the AWS Management Console, search for “EC2” in the search bar and select “EC2” from the results.

Image description

This will take you to the EC2 Dashboard.

Image description

Launch Instance

Click on the “Launch Instance” button on the EC2 Dashboard.

Name of the instance

Write the name of your choice for the instance you are going to launch in the name field.

Image description

Choose an Amazon Machine Image (AMI)

Select an AMI (e.g., Amazon Linux 2, Ubuntu, etc.) based on your requirements.

Image description

AMIs are pre-configured templates that provide the operating system and software stack.

I am choosing Ubuntu for this you can choose any AMI according to your use case.

Choose an Instance Type

Select an instance type based on the CPU, memory, and networking capacity you need.

Image description

For example, t2.micro is a common choice for a small application.

Choose or Create a Key Pair

Select an existing key pair or create a new one to securely connect to your instance.

Image description

Download the private key file (.pem file) and store it securely.

Or also you can move with “Proceed without a key pair” but it is not recommended.

Configure Security Group

A Security Group acts as a virtual firewall for your instance.

Configure the security group to allow the necessary ports (e.g., SSH, HTTP, etc.).

For now, you have to do this type of configuration that you can see below

Image description

You can leave the rest of the configuration as Default.

Add Storage

Configure the storage attached to your instance. By default, an EBS (Elastic Block Store) volume is provided.

You can increase the storage size or add additional volumes if needed.

Review and Launch

Review all your configurations.

Click on “Launch instance” to start your instance.

After a few minutes, your instance will be up and running.

You can see your instance in the instances you can see below.

Image description

Access the Instance

Select the instance and click on the “Connect” button then you will reach this screen that you can see below.

Image description

Then click on the Connect button to connect to your server.

Image description

Update the Server

It’s crucial to keep your server up to date. Run the following command to update and upgrade the packages:

sudo apt update && sudo apt upgrade -y

Set Up the LAMP Server

Install Apache

Install Apache, the web server that will serve your website:

sudo apt install apache2 -y

Ensure Apache is running by checking its status:

sudo systemctl status apache2

If Apache isn’t running, verify that port 80 is open in your EC2 security group.

Start the Apache service:

sudo systemctl start apache2

Enable Apache to Start on the Boot

Ensure Apache starts automatically if the server reboots:

sudo systemctl enable apache2

Install MySql

Install MySql Server and Client

MySql will serve as your database server:

sudo apt install mysql-server mysql-client -y

Start and Check MySql Status

Start the MySql service:

sudo systemctl start mysql

To check the status of MySql:

sudo systemctl status mysql

Setup the MYSQL Server

Set up the root password and secure your database by running:

sudo mysql_secure_installation

Follow the prompts to remove anonymous users, disallow remote login, and remove the test database.

Install PHP

Install PHP and necessary extensions to work with Apache and MySql:

sudo apt install php php-mysql php-gd php-cli php-common -y

Prepare for WordPress Installation

Install required packages:

sudo apt install wget unzip -y

Download and Set Up WordPress

Download WordPress

Download the latest version of WordPress from the official site:

sudo wget https://wordpress.org/latest.zip

Unzip the Downloaded File

Unzip the WordPress package:

sudo unzip latest.zip

Copy WordPress Files

Move the WordPress files to the Apache root directory:

sudo cp -r wordpress/* /var/www/html/

Set Permissions and Clean-Up

Change Ownership of WordPress Files

Ensure that the web server owns the WordPress files:

sudo chown www-data:www-data -R /var/www/html/

Remove the Default Apache Index File

sudo rm -rf /var/www/html/index.html

Configure the Database for WordPress

Log into MySql

Log into MySql as root:

sudo mysql -u root -p

Create a WordPress Database and User

Run the following commands to create a database and user for WordPress:

CREATE DATABASE wordpress;
CREATE USER ‘wpadmin’@’%’ IDENTIFIED BY ‘wpadminpass’;
GRANT ALL PRIVILEGES ON wordpress.* TO ‘wpadmin’@’%’;
EXIT;

Complete WordPress Installation

Visit your server’s public IP address in your web browser to complete the WordPress installation. Enter the database details, site title, and admin credentials.

Once these steps are complete, you’ll be redirected to the WordPress dashboard, and your WordPress site will be live.

Top comments (0)