billysoftacademy.com

Learn how to install WordPress using Docker on Ubuntu 22.04 LTS

Introduction

WordPress is a well-known content management system that many users opt for to create their websites. It is a free and open-source platform. However, setting up WordPress directly on a Linux Ubuntu Server can be a time-consuming process. Luckily, there is an open-source containerization application called Docker that simplifies the installation process with just a few commands. In this tutorial, we will show you how to install WordPress using Docker on Ubuntu. This installation can be done on either Ubuntu 20.04 or Ubuntu 22.04.

Requirements

The following is a list of items needed to complete the installation successfully:
1) Linux Ubuntu Server 20.04 or 22.04 LTS
2) An SSH client such as Putty
3) A fully qualified domain name pointing to the IP address of the Ubuntu Server
4) A stable internet connection
5) A basic understanding of Linux commands

Overview

The following is a basic overview of the steps covered in this tutorial:
1) Download and install Docker and needed dependencies
2) Create a MariaDB container.
3) Create a WordPress container.
4) Configure NGINX as a reverse proxy server
5) Complete the WordPress post installation setup wizard.

Step 1: Download and install Docker and needed dependencies

The first step is downloading and installing some important system dependencies. You can easily install them by running these commands:

apt-get update -y
apt-get install mariadb-client apt-transport-https ca-certificates curl gnupg-agent software-properties-common -y

Once the installation is complete, you can move on to the next step.

By default, the most recent Docker version is not available in the Ubuntu 20.04 default repository. Therefore, it is recommended to add the Docker official repository to your system. To do this, begin by downloading and adding the GPG key using the following command:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
Afterwards, add the Docker repository to your system using the following command:
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Once the repository has been added, you can install Docker and Docker Compose by using the following command:
apt-get install docker-ce docker-ce-cli containerd.io -y
After installing both packages, check the version of Docker that is installed by using the following command:
docker --version

Step 2: Create a Mariadb container

To begin with, you need to download the MariaDB image from the Docker repository. Below is the command to download the image:
docker pull mariadb
Once the download is complete, proceed with creating a directory structure for WordPress on your server. Here are the commands to do so:
mkdir ~/wordpress
mkdir -p ~/wordpress/database
mkdir -p ~/wordpress/html
Next, create a MariaDB container with the name “wordpressdb” by running the following command:
docker run -e MYSQL_ROOT_PASSWORD=root-password -e MYSQL_USER=wpuser -e MYSQL_PASSWORD=password -e MYSQL_DATABASE=wpdb -v /root/wordpress/database:/var/lib/mysql --name wordpressdb -d mariadb
Now, check the IP address of your MariaDB container with the following command:
docker inspect -f '{{ .NetworkSettings.IPAddress }}' wordpressdb
On executing the above command, you would see the MariaDB container IP address in the following output:
172.17.0.2
After that, connect to your MariaDB container using the database user and password:
mysql -u wpuser -h 172.17.0.2 -p
Now, verify the database with the following command:
show databases;
On executing the above command, you would see your wpdb database in the following output:
+--------------------+
| Database |
+--------------------+
| information_schema |
| wpdb |
+--------------------+
2 rows in set (0.001 sec)
Finally, exit from the MariaDB shell with the following command:
EXIT;

Step 3: Create a WordPress container

The next step is downloading the latest WordPress image from the Docker repository, execute the following command to start the download:

docker pull wordpress:latest

Once executed, the output should display a list of numbers and letters. After downloading the image, use the following command to create a new WordPress container named wpcontainer:

docker run -e WORDPRESS_DB_USER=wpuser -e WORDPRESS_DB_PASSWORD=password -e WORDPRESS_DB_NAME=wpdb -p 8081:80 -v /root/wordpress/html:/var/www/html --link wordpressdb:mysql --name wpcontainer -d wordpress

This command will expose port 80 on the container to port 8081 on the host machine.

To verify whether the WordPress container has been successfully created, use the following command:

curl -I localhost:8081

The output should display detailed information about the server and WordPress installation.

Step 4: Configure NGINX as a reverse proxy server

To access your WordPress using port 80 or port 443, you will need to install and configure Nginx as a reverse proxy for the WordPress container.  You can also use OpenLiteSpeed or LiteSpeed Enterprise however the revece proxy configuration may be different. To begin, install the Nginx web server by running the command below:
apt-get install nginx -y
After installation, create a new Nginx virtual host configuration file using the following command:
nano /etc/nginx/sites-available/wordpress 
Add the following lines to the file:
server {
listen 80;
server_name wp.example.com;
location / {
proxy_pass http://localhost:8081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Save and exit the file. Then, activate the virtual host using the command below:
ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
Finally, restart the Nginx service with the following command to apply the changes:
systemctl restart nginx

Step 5: Complete the WordPress post installation setup wizard

Open your preferred web browser and enter https://example.com in the address bar (replace example.com with your domain name). This action will redirect you to the WordPress installation wizard.

Next, select your preferred language and click on the “Continue” button. Once the page loads, you will see the installation setup screen.

Provide your site’s name, admin username, email, and password on the setup page. Afterward, click on the “Install WordPress” button. The installation process will take a few moments to complete. Upon completion, the page will show you the following screen:

Input your admin username and password into the fields provided, then click on the “Log In” button. You will be taken to the WordPress dashboard, which will appear on the following screen.

Conclusion

In this tutorial, we have successfully demonstrated how to install WordPress on Ubuntu Server 20.04 or 22.04 using Docker. Throughout the process, we walked you through downloading and installing essential dependencies, creating MariaDB and WordPress containers, configuring NGINX as a reverse proxy, and finally, completing the WordPress post-installation setup wizard. By following these steps, you can now have a fully functional WordPress website running on your Ubuntu server.

Here are some additional takeaways:

1. Using Docker simplifies the WordPress installation process by minimizing manual configuration and streamlining the overall setup.
2. This method enables easy scaling and management of your WordPress website, allowing you to add and remove resources as needed.
3. Implementing Nginx as a reverse proxy enhances security and performance by handling external traffic and routing it efficiently to the WordPress container.

We encourage you to experiment and customize your WordPress website to your liking. This guide provides a solid foundation for building a secure and robust web presence using the power of Docker and WordPress on Ubuntu Server.

Scroll to Top