There are three main ways to host your own WordPress website on AWS according to the best practices:
- Amazon Lightsail (Recommended)
- Amazon Elastic Compute Cloud (Amazon EC2)
- AWS Marketplace
There are also different ways to install WordPress on a Ubuntu machine (non-exhaustive):
- Manually set up WordPress with LAMP stack like this or this or this
- Using containers (e.g. Docker and Docker Compose) like this
- Using infrastructure as code tools (e.g. Terraform, Ansible) like this
This guide covers the steps to host a WordPress on a Amazon Elastic Compute Cloud (Amazon EC2) Ubuntu 18.04 machine using Docker Compose. You can use Docker Compose to easily run WordPress in an isolated environment built with Docker containers.
The private key (a .pem
file) that corresponds to your AWS EC2 instance is required in order to connect to your instance using SSH to perform the WordPress installation. If you do not have the private key, please check with your instance provider.
Extract the private key from the zipped file.
Note that it is not '-x', there is no space between the option '-p' and the password for the 7z
command. Replace PASSWORD
with the password for the zipped file.
$ sudo apt-get install p7zip-full
$ 7z x -pPASSWORD devops-test.pvt.zip
$ mv devops-test.pvt.key devops-test.pem
$ chmod 400 devops-test.pem
$ ssh -i devops-test.pem devops-test@ec2-54-255-184-141.ap-southeast-1.compute.amazonaws.com
If the instance already has Docker Engine and Docker Compose installed, you can skip to Setting up the WordPress project.
$ sudo apt-get update
$ sudo apt-get upgrade
This sets up the Docker repository and installs the latest version of Docker Engine and containerd.
$ sudo apt-get remove docker docker-engine docker.io containerd runc
$ sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
Verify that Docker Engine is installed correctly by running the hello-world
image.
$ sudo docker run hello-world
For more information on installing, uninstalling and upgrading Docker Engine, check out the official installation guide.
This downloads the current stable release of Docker Compose binary from the Compose repository release page on GitHub.
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.26.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
Test the installation.
$ sudo docker-compose --version
docker-compose version 1.26.0, build 1110ad01
For more information on installing, uninstalling and upgrading Docker Compose, check out the official installation guide.
$ git clone https://gitlab.com/alanwuha/wordpress.git
$ cd wordpress
Update docker-compose.yml
with the credentials (e.g. database name and passwords) specific to your environment using an editor before building the project.
If you are unsure about the necessary changes, please contact the writer of this guide for any clarification.
Run the following command from the wordpress directory.
$ sudo docker-compose up -d
This runs sudo docker-compose up
in detached mode, pulls the needed Docker images, and starts the containers.
Containers:
- WordPress
http://54.255.184.141:80
- Prometheus (metrics database)
http://54.255.184.141:9090
- Grafana (visualize metrics)
http://54.255.184.141:443
- AlertManager (alerts management)
http://54.255.184.141:9093
- port not opened - Phpmyadmin (database administration tool)
http://54.255.184.141:81
- port not opened - NodeExporter (host metrics collector)
http://54.255.184.141:9100/metrics
- MysqldExporter (database metrics collector)
http://54.255.184.141:9104/metrics
- port not opened - cAdvisor (containers metrics collector)
http://54.255.184.141:8080/metrics
- port not opened - Mysql (database)
Navigate to http://54.255.184.141:80
and complete the "famous five-minute installation" as a WordPress administrator.
Navigate to http://54.255.184.141:443
and login with user admin password admin.
Go to Configure > Data Sources
and add Prometheus as a data source. Fill in the URL input with prometheus:9090
and click on Save & Test. If successful, it should return Data source is working
.
Go to Create > Import
and import the following dashboards one by one using the steps below:
- 1860 (Node Exporter Full)
- 7362 (MySQL Overview)
- 893 (Docker and system monitoring)
Under Import via grafana.com
, fill in the dashboard id (e.g. 1860) and click Load. Select 'Prometheus' under the Prometheus dropdown and then click Import:
Remove the containers and default network, but preserve your WordPress database.
$ sudo docker-compose down
Remove the containers, default network, and the WordPress database. DO THIS WITH CAUTION AS YOUR DATA WILL BE ERASED.
$ sudo docker-compose down --volumes
$ sudo docker stop <CONTAINER ID>
$ fallocate -l 10G dummy.img
This is a standalone application with wordpress, database, and monitoring tools installed on the same AWS EC2 instance.
- Best Practices for WordPress on AWS
- Install Docker Engine on Ubuntu
- Install Docker Compose on Ubuntu
- Docker for Beginners: Full Course
- Quickstart: Compose and WordPress
- Docker image: WordPress
- Docker image: MySQL
- Docker image: PhpMyAdmin
- Docker image: Prometheus
- Docker image: Grafana
- Docker image: Node Exporter
- Docker image: Mysqld Export
- Docker image: Alertmanager
- Prometheus Docker Installation Guide
- Quick Wordpress Setup with Docker
- Monitoring Amazon EC2
- Dockprom - A monitoring solution for Docker hosts and containers
- Setting up Prometheus and Grafana for monitoring your servers
- Step-by-step guide to setting up Prometheus Alertmanager with Slack, PagerDuty, and Gmail
- Dockprom alert rules
- Save your Grafana data to Docker volume
- Docker Data Volume Container pattern