HomeLinuxDockerizing WordPress with Nginx and PHP-FPM on Ubuntu 20.04
Docker-Compose is a command line tool for defining and managing multi-container docker containers as if they were a single service. Compose is written in python and can be installed with the Python pip command. With compose, we can run multiple docker containers just with a single command. It allows you to create a container as a service, great for your development, testing and staging environment.
In this tutorial, I will guide you step-by-step to use docker-compose. We will deploy ‘WordPress‘ with Nginx, MySQL, and PHP-FPM. Each service has its own container, and we will use images from the docker hub registry. I will show you how to create containers from docker images and manage all containers with docker-compose.
How To Deploy NGINX With Docker On Ubuntu LinuxDocker is an open platform for building, shipping, running, and orchestrating distributed applications.NGINX i. This confirms that Docker Compose has been successfully installed on your system. Step 2: Creating the docker-compose.yml File. To help you understand how to work with docker-compose.yml, we will configure a web server environment using the official Nginx image from Docker Hub. The next dependency to install is NGINX. If your instance of Ubuntu Server 18.04 already has Apache installed, it will interfere with the installation of NGINX. To get around this, stop and disable it with the following commands: sudo systemctl stop apache2. Sudo systemctl disable apache2. With Apache out of the way, install NGINX with the. If you have Internet access, NGINX Controller will install Docker for you as part of the installation process. If you prefer to install Docker on the host yourself, install the following: Docker Community Edition (CE) 18.09; Containerd.io 1.2.10; If you are using Ubuntu-20.04 and want to install Docker on your own, choose the following versions. Install Nginx (Virtual Hosts) on Ubuntu 16.04. Sudo ufw status Status: inactive Inactive is ok. Sudo apt-get update sudo apt-get install curl sudo apt-get install nginx for check. Curl -4 icanhazip.com out:xx.xx.xx.xxx= (.Ip from the server where you want to install Nginx) Go to your Browser Firefox for example and go to.
We will start from scratch, by installing docker and docker compose manually with the apt command.
Before we begin, update the Ubuntu repository and install latest updates:
By default, docker is available in the Ubuntu repository, so we can continue to install it right away:
When the installation is done, start docker and add it to start automatically at boot time:
Docker Install Nginx Ubuntu Download
Now test your docker installation with the command below:
You will see hello-world from docker.
Docker Compose is a single binary file. The installation is straightforward. We’ll download the file to a directory that is in the system PATH and make it executable.
The Docker Compose package is available in the official Ubuntu 20.04 repositories, but it may not always be the latest version.
At the time of writing this article, the latest stable version of Docker Compose is
. Before downloading the Compose binary, visit the Compose repository release page on GitHub and check if there is a new version available for download.
to download the Compose file into the
Once the download is complete, apply executable permissions to the file:
To verify that the installation was successful, run the following command which will print the Compose version:
The output will look something like this:
That’s it! Docker Compose has been installed on your Ubuntu machine, and you can start using it.
or you can install via command line:
then you can change the version
Step 1. Starting up nginx-proxy to hook Docker and Nginx together
To get started, let’s start up the
nginx-proxy container. This can be accomplished either by a single
docker command, or using
docker-compose. Let’s cover both.
To get started, create a Docker network
Before we get started, either way, we need to first create a Docker network that we will use to bridge all of these containers together.
From now on, we need to ensure that we’re always adding new containers to the
nginx-proxy Docker network.
Installing nginx-proxy with Docker
Installing nginx-proxy with docker-compose
First, create a new
docker-compose.yml file in the directory of your choosing (one titled
nginx-proxy is a good idea), and copy in the following text:
And then run the following
docker-compose command to get started.
How nginx-proxy works to host multiple websites
As you can see from the code in both options, the container listens on port 80 and exposes the same port inside of the container. That allows all incoming traffic to flow though
You might be wondering what the
/var/run/docker.sock:/tmp/docker.sock line accomplishes. Essentially, this gives any container access to the host’s Docker socket, which contains information about a variety of Docker events, such as creating a new container, or shutting one down.
This means that every time you add a container,
nginx-proxy sees the event through the socket, automatically creates the configuration file needed to route traffic, and restarts
nginx to make the changes available immediately.
nginx-proxy looks for containers with the
VIRTUAL_HOST variable enabled, so that’s critical to our operations moving forward.
Also important to note is the
--net nginx-proxy line in the Docker command, and the
networks: default: external: name: nginx-proxy block in the
docker-compose.yml file. These establish that all containers will communicate over that Docker network.
Step 2. Adding a container to the proxy
Now that we have
nginx-proxy running, we can start adding new containers, which will be automatically picked up and configured for. Because we covered it in the last Docker tutorial, and since it’s an easy implementation to try out, let’s use WordPress as an example.
Starting a WordPress container with a basic configuration is quite easy.
Take note of a few elements here.
--expose 80 will allow traffic to flow into the container on port 80.
--net nginx-proxy ensures we’re using the Docker network we created earlier. Finally,
-e VIRTUAL_HOST=blog.DOMAIN.TLD flags
nginx-proxy to create the proxy information and direct traffic arriving to the correct domain.
Start by creating a ‘docker-compose.yml’ file in an empty directory and copying in the following.
Again, take note of the
environment: VIRTUAL_HOST options within the file. Also, the
networks option at the bottom is necessary to allow this container to “speak” with
Also, take note of the line that begins with
db_node_domain. If you are going to host multiple WordPress blogs using this method, you need to create unique database names for each. You should replace
db_node_domain with your preferring naming scheme. You also need to update two other fields with this chosen name. First:
Once the configuration file is set up, you can run the
docker-compose up -d command. As long as your DNS is set up to route traffic properly, things should work correctly.
From here, you can start up any number of additional WordPress site—or any type of service, for that matter—and have them be automatically added to the
nginx-proxy network. This Docker and Nginx configuration is pretty infinitely extensible, limited only by the VPS resources available to you.
Additional resources to host multiple websites
Of course, be sure to check out the extensive documentation for
nginx-proxy to learn more about how you can configure some more complex proxies between Docker and Nginx, such as those using SSL, with multiple ports, or multiple networks.
Docker Install Nginx Ubuntu Free
We haven’t tested it out yet, but there’s a “companion” to
letsencrypt-nginx-proxy-companion that allows for the creation/renewal of Let’s Encrypt certificates automatically directly alongside the proxy itself.
Just another example of the really cool things that you can do with Docker!
Thanks much to our sharp-eyed reader John! He pointed out how we can improve our docker-compose files by creating unique database names for each instance of WordPress!
Comments are closed.