Let’s install a webserver on Linux that you can use to host your website. We will set up an Nginx powered containerized web stack with MySQL database and trusted https certificates via lets-encrypt. The container image we will use will run out-of-the-box. But you also can fully customize it to host multiple websites, operate as a reverse proxy, and much more.
Prerequisites for your webserver
Before we start to set up our web server you should have a domain ready that points to the public IP address of your webserver. Your web server must be accessible via port 80 and 443 from the public internet. You should also create an A record for the hostname “www” that points to your web server, as well. The last step is not mandatory, but it’s recommended because we will use the “www” as a subdomain in our web server configuration, later.
For this example, I will use Ubuntu 20.04 LTS as my Linux distribution. But you can choose almost every other Linux distribution that supports docker. You only need to check out the docker installation guide for your distro.
Start installing docker and docker-compose on Linux
First, we need to install docker on our Linux webserver. If you have trouble or want to use another distro/operating system, you can also refer to the official docker documentation: https://docs.docker.com/install/
1. (Optional) Remove old docker installation
It is recommended, to first uninstall old docker installations. If you already have Docker set up and running correctly or never used it before, you can skip this step.
sudo apt remove docker docker-engine docker.io containerd runc
2. Install docker repository
sudo apt update sudo apt 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 update
3. Install docker
sudo apt-get install docker-ce docker-ce-cli containerd.io
4. Check if docker was installed successfully
If everything was successful you can test docker, with this simple command
sudo docker run hello-world
Install docker-compose on Ubuntu 20.04 LTS Linux
1. Check latest stable release
Docker-compose has no installation package for apt. You may check the latest stable release version first on the official website
2. Download docker-compose
Download the latest version (in this case it is 1.25.5, this may change whenever you read this tutorial!)
sudo curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
3. Make directory executable
sudo chmod +x /usr/local/bin/docker-compose
4. Check if docker-compose is working correctly
If everything was successful you can test docker-compose with this command.
sudo docker-compose --version
Install linuxserver.io SWAG webserver stack
Next, we need to create a docker-compose file to easily manage our web server stack containers. I’m using the docker image and template from https://linuxserver.io. The guys from linuxserver.io are enthusiasts and manage docker images for the community.
First, I will create a new folder in the /opt directory called /opt/webserver_swag. Then, I create a docker-compose.yaml file in this directory. You should also change the ownership of this folder to your administrative Linux user. Be aware, you should not use the root user.
sudo mkdir /opt/webserver_swag sudo chown christian:christian /opt/webserver_swag vim /opt/webserver_swag/docker-compose.yaml
In this file you can use the following template, please refer to the official documentation https://docs.linuxserver.io/general/swag
--- version: "2" services: swag: image: linuxserver/swag container_name: swag cap_add: - NET_ADMIN environment: - PUID=1001 - PGID=1001 - TZ=Europe/Berlin - URL=do-test-1.the-digital-life.com - SUBDOMAINS=www - VALIDATION=http volumes: - /opt/webserver_swag/config:/config ports: - 443:443 - 80:80 #optional restart: unless-stopped
To start your webserver just execute the following command, while you’re in the /opt/webserver_swag folder.
docker-compose up -d
Add a database to the webserver
docker-compose.yaml (with database)
--- version: "2" services: mariadb: image: linuxserver/mariadb container_name: mariadb environment: - PUID=1001 - PGID=1001 - MYSQL_ROOT_PASSWORD=mariadbpassword - TZ=Europe/Berlin - MYSQL_DATABASE=WP_database - MYSQL_USER=WP_dbuser - MYSQL_PASSWORD=WP_dbpassword volumes: - /opt/webserver_swag/config/mariadb:/config restart: unless-stopped swag: image: linuxserver/swag container_name: swag cap_add: - NET_ADMIN environment: - PUID=1001 - PGID=1001 - TZ=Europe/Berlin - URL=do-test-1.the-digital-life.com - SUBDOMAINS= - VALIDATION=http volumes: - /opt/webserver_swag/config:/config ports: - 443:443 - 80:80 #optional depends_on: - mariadb restart: unless-stopped
More about docker and docker-compose
If you want to learn more about docker and docker-compose, here is a link to my YouTube video about docker: