How to Host WordPress on Amazon EC2

AWS EC2 WordPress website tutorial

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

In this tutorial, learn how to setup and host a WordPress website on an EC2 instance. At the time of this writing, this server instance is part of the AWS 12 Months Free Tier which means that there will be no cost for the first year.

1. Get Started with EC2

Go to https://aws.amazon.com/ec2/ and click the orange Get started with Amazon EC2 button. Sign up for an AWS account if you don’t already have one.

Otherwise, navigate to the EC2 Dashboard and click on Launch instance. Search for and select Ubuntu Server 20.04. Feel free to select any instance type, but to stay within the Free Tier, select t2.micro.

Continue through the setup keeping the defaults except for the following options:

  • Add Storage: 30 GB
  • Configure Security Group: HTTP from Anywhere
  • Configure Security Group: HTTPS from Anywhere

Click on the launch button, and in the prompt select Create a new key pair called awsec2 and download the key pair. It will be a pem file.

Finally click on the blue Launch Instance button.

A few moments later, the Instance state column will say running which means it’s online and you can proceed to the next step.

2. Link Domain Name

If you have a domain name, you can link it to your EC2 instance by creating a DNS record.

Go to your registrar and find the DNS settings of your domain name. Create an A record that points to the IP address of your EC2 instance and another A record for the www version of your website that points to the same IP address of your EC2 instance.

If you’re not familiar with this process, learn more about DNS A records here.

3. Login to EC2 via SSH

If you are on Mac or Linux, you can use Terminal to login via SSH and Windows users can either use Command Prompt or Putty to login. Here is an example of the SSH command to login to your EC2 server.

chmod 400 awsec2.pem
ssh -i awsec2.pem ubuntu@IP

If you configured your DNS settings in the previous step, you can also use your domain name instead of your instance IP address in the command above.

4. Update System and Install LEMP Packages

Execute the following to upgrade Ubuntu server packages.

sudo apt update
sudo apt upgrade

Use the apt package manager to install PHP, MariaDB, and the Nginx web server.

sudo apt install nginx mariadb-server php-fpm php-mysql

5. Install WordPress

After logging in to your server as described above, execute the following commands to install WordPress on Ubuntu.

cd /var/www
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xzvf latest.tar.gz
sudo rm latest.tar.gz
sudo chown -R www-data:www-data wordpress
sudo find wordpress/ -type d -exec chmod 755 {} \;
sudo find wordpress/ -type f -exec chmod 644 {} \;

6. Setup the Database

Secure your MariaDB installation by adding a password and disabling other features. When prompted, answer Y.

sudo mysql_secure_installation

Access the MariaDB console with the password that you just created.

sudo mysql -u root -p

Within the MariaDB console, create a database for WordPress. Please choose your own database name, user name, and a password.

create database example_db default character set utf8 collate utf8_unicode_ci;
create user 'example_user'@'localhost' identified by 'example_pw';
grant all privileges on example_db.* TO 'example_user'@'localhost';
flush privileges;
exit

7. Configure Nginx Web Server

Navigate to the directory which contains configuration files for the Nginx web server, and create a new configuration file with the text editor of your choice. In this example, the text editor is vim.

cd /etc/nginx/sites-available/
sudo vim wordpress.conf

Use this configuration as a template for your website. Please change the server_name and make sure that the php-handler socket exists (you may have a different version of PHP installed).

upstream php-handler {
        server unix:/var/run/php/php7.4-fpm.sock;
}
server {
        listen 80;
        server_name netwits.io www.netwits.io;
        root /var/www/wordpress;
        index index.php;

        location / {
                try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass php-handler;
        }
}

Make a symbolic link to tell Nginx about your website, and apply the changes by restarting the web server.

sudo ln -s /etc/nginx/sites-available/wordpress.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

8. Finish WordPress Install

Assuming that DNS propagation has finished, you can now access your website via your domain name in a web browser. You will be prompted to finish the WordPress installation, part of which is entering the database name, user, and password that you created earlier.

WordPress database setup

Upon completion of the installation, you can access your WordPress administrator dashboard at http://example.com/wp-admin/ where example.com is your domain name.

9. Install PHP Packages Required by WordPress

From your WordPress administrator dashboard, navigate to Tools > Site Health > Status and you may see a critical issue that says “One or more required modules are missing”.

To fix this, go back to your EC2 instance’s console window and install these packages.

sudo apt install php-curl php-dom php-mbstring php-imagick php-zip php-gd

10. Install an SSL Certificate for HTTPS

Secure your website with an SSL certificate from Let’s Encrypt. To do this, execute the following commands.

sudo apt install snapd
sudo snap install core; snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot --nginx

Finally back in your WordPress administrator dashboard, go to Settings > General and change the WordPress Address and Site Address to start with https.

Next Steps

Now that you’re all set up a fresh install of WordPress on your EC2 instance, check out my list of 15 important things to do after installing WordPress next.

Also for your convenience, the following is a detailed video walkthrough of the steps in this tutorial.

YouTube video

Facebook
Twitter
Pinterest
LinkedIn
Reddit

Meet Tony

Tony Teaches Tech headshot

With a strong background in computer science and enterprise web development, Tony is determined to demystify the web. Discover why Tony quit his job to pursue this mission. You can join the Tony Teaches Tech community here.

One Response

  1. I loved the process man i went through yours but i am getting an error at wordpress php can you help there i havent bought any domain yet

Leave a Reply

Your email address will not be published. Required fields are marked *