DEV Community

Cover image for Hosting a WordPress Website on AWS
Paul Aderoju
Paul Aderoju

Posted on • Edited on

Hosting a WordPress Website on AWS

Hi.

In this tutorial, we are going to learn how to host a WordPress website on AWS.

Requirements

  • An AWS Account;

  • An active domain name.

STEP 1: Create an AWS EC2 Instance

  • In Your AWS Management Console, Search for EC2 in the search bar, and click on the first option that appears. Then click on the Launch Instance button.

AWS EC2

  • Fill out the fields like this:
  • Give your EC2 instance a name and select the Ubuntu AMI.

IEC2

  • Select the t2.micro(free tier) instance type and create a keypair or use an already existing one.

T2 micro

  • Create a new security group, and allow SSH, HTTPS and HTTP traffic from the Internet.

security

  • Configure your storage. 8GB is enough for this project. Launch your instance.

storage

  • Allocate an Elastic IP and Associate it with your instance. An Elastic IP doesn't change unless you explicitly release it, making it useful for scenarios where you need a consistent IP address for your resource even if the resource is stopped and started.

Elastic IP

Elastic IP

STEP 2: SSH into your EC2 Instance

Securely login to your EC2 Instance. You can do that using a SSH Client or EC2 Instant Connect.

STEP 3: Update and Upgrade the local package index on your system

Run the command:

sudo apt update -y
sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

STEP 4: Install Apache2, PHP, MySQL

Run the command:

sudo apt install apache2 -y
sudo apt install apache2 -y
sudo apt install mysql-server -y
Enter fullscreen mode Exit fullscreen mode

STEP 5: Change Mysql authentication plugin to a mysql native password

sudo mysql -u root
Enter fullscreen mode Exit fullscreen mode
  1. Change authentication plugin

    ALTER USER 'root'@localhost IDENTIFIED WITH mysql_native_password BY 'Testpassword@123';
    
  2. Create a seperate user other than root

    CREATE USER 'wp_user'@localhost IDENTIFIED BY 'Testpassword@123';
    
  3. Create a Database for Wordpress. We'll call this database wp.

    CREATE DATABASE wp;
    
  4. Assign all priviledges on the newly cretaed DB to the wp_user user;

    GRANT ALL PRIVILEGES ON wp.* TO 'wp_user'@localhost;
    

Exit MySQL by pressing Ctrl + d

STEP 6: Download and Unzip Wordpress

  1. cd to the /tmp directory

    cd /tmp
    
  2. Download wordpress

    wget https://wordpress.org/latest.tar.gz
    
  3. Unzip Wordpress. This will create a new folder called wordpress.

    tar -xvf latest.tar.gz 
    
  4. Move wordpress folder to document root of Apache2

    sudo mv wordpress/ /var/www/html/
    

STEP 7: Create a wp-config.php file from wordpress GUI

  • In the browser, go to http://{ip_address}/wordpress

wp-config.php

  • Fill out the form with the information we used to create MySQL earlier, and submit:
    DATABASE Name: wp
    Usename: wp_user
    password: Testpassword@123
    DATABASE host: localhost
    table prefix: wp_
Enter fullscreen mode Exit fullscreen mode

wordpress sql

It will throw an error saying Unable to write to wp-config.php file. Follow theses instructions to correct error.

  • Go to the /var/www/html/wordpress directory:

    cd /var/www/html/wordpress
    
  • Create a wp-config.php file and copy instructions from earlier error.

    sudo vi wp-config.php
    
  • Reload the browser and run installation.

  • Fill the form on the next page.

Go back to http://{ip_address}/wordpress and see the wordpress sites working

wordpress

STEP 8: Modifying the Apache 000-default.conf file

  1. Go to Apache2 config folder

    cd /etc/apache2/sites-available/
    
  2. Open up sudo 000-default.conf file

    sudo vi 000-default.conf 
    

Change DocumentRoot /var/www/html/ to:

 DocumentRoot /var/www/html/wordpress
Enter fullscreen mode Exit fullscreen mode

Add:

Alias '/wordpress/' '/var/www/html/wordpress/'
Enter fullscreen mode Exit fullscreen mode

/var/www/html/wordpress

  1. Restart apache2
sudo systemctl restart apache2 
Enter fullscreen mode Exit fullscreen mode

STEP 9: Configuring your domain

  • Create an A Type DNS record. Copy your ip address from your ec2 and paste it to the new A type, set TTL to 60.

DNS

  • Add ServerName and ServerAlias to 000-default.conf file
ServerName paulboye.live
ServerAlias www.paulboye.live
Enter fullscreen mode Exit fullscreen mode
  • Restart apache2
sudo systemctl restart apache2 
Enter fullscreen mode Exit fullscreen mode
  • Login to wordpress admin: http://{ip_address}/wordpress/wp-admin Settings -> General ->
WordPress Address (URL): http://{your-domain-name}
Site Address (URL): http://{your-domain-name}
Enter fullscreen mode Exit fullscreen mode

Click on Save Changes

Notice that the url of your wordpress site will have changed.

STEP 10: Install SSL Certificate

  • Install CertBot
sudo apt install certbot python3-certbot-apache -y
Enter fullscreen mode Exit fullscreen mode
  • Run Certbot and Follow prompts
sudo certbot --apache
Enter fullscreen mode Exit fullscreen mode

certboi

Your Wordpress website is now up and running and has SSL certificates installed.

SSL

Top comments (2)

Collapse
 
fredlag profile image
fredlag

Thanks for the sharing what's the coast of this installation ?

Collapse
 
pauladeroju profile image
Paul Aderoju

Hey Fred. Thanks for your comment. I utilized a free tier EC2 instance, which is running for free. However, you might accrue some costs with the elastic IP address. You can use the AWS Pricing Calculator to estimate the cost for the requirements of your project.