Krishnan's Personal Website


Home | About | Blog | Interesting Reads | Tutorials | Skills | Personal Projects | Books | Fun | Connect with me


Deploying Docker Container to a LAMP Server (Ubuntu 18.04 with Apache)


Published On: Aug 15 2021
Written By: Krishnan Sethuraman
Category: DevOps


I had built this website and blog with Phalcon on a LAMP server on Ubuntu 18.04. But configuring the LAMP server to run Phalcon locally and on the server was a bit painful as it involved some additional steps. The complication would further increase if I had to move the website from one server to another. So I decided to migrate this website to Docker. This would save me time and effort spent on configuring the environment again and again. With Docker I just had to do it once. 

At the time of writing this article I’m fairly new to Docker. I struggled to find clear information on the internet that could guide me in deploying my dockerised web application to my cloud server running Ubuntu 18.04 and make it accessible with a domain (like you would normally do with a website or web app)

To follow the steps covered in this article you need to have set up Docker and configured your application Phalcon and Docker on your local machine. If not, no problem I have provided links to both below. 

Please note that I followed the Digitalocean article to set up Docker on Ubuntu 18.04. It is more descriptive than the official one (in my opinion)

How To Install and Use Docker on Ubuntu 18.04

To set up Phalcon locally with Docker please follow the below article. 

Setting up PhalconPHP with Docker

Login into your cloud server and deploy the files in your home directory. Normally we deploy the files in /var/www/html but as we are running the application with Docker it does not matter and hence you can create a directory in your home directory and deploy the files including the Dockerfile and docker-compose.yml. 

$ cd /home/ubuntu_user_name

$ mkdir PhalconApp

$ cd PhalconApp

Once you have all your files deployed on location you can now start the containers.

$ docker-compose up -d

You can check if your containers are up and running.

$ docker ps

In my case the application was running on port 8000. So I was able to access the application on both http://127.0.0.1:8000 and also on http://server_ip:8000.

The next step is to create virtual hosts so that we can make our website or web app accessible to the outside world. 

To create a virtual host you can follow the steps mentioned in the article below (things will not work but still follow the article and complete the steps).

How To Set Up Apache Virtual Hosts on Ubuntu 18.04

Once you have created the virtual host we now need to make the following changes to Apache and to the virtual host that we just created.

$ sudo a2enmod proxy

$ sudo a2enmod proxy_http

$ systemctl restart apache2

Please note that for Docker containers the DocumentRoot is irrelevant.

$ sudo nano /etc/apache2/sites-available/your_domain.conf

<VirtualHost *:80>

    ServerAdmin webmaster@localhost

    ServerName domain.com

    ServerAlias www.domain.com

    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log

    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Modify the above section as mentioned below. 

<VirtualHost *:80>

        ServerAdmin admin@example.com

        ServerName domain.com

        ServerAlias www.domain.com

        DocumentRoot /var/www

        ErrorLog logs/docker.example.com_error.log

        CustomLog logs/docker.example.com_access.log combined

        ProxyPreserveHost On

        ProxyRequests off

        <Location />

                ProxyPass http://localhost:8000/

                ProxyPassReverse http://localhost:8000/

                Order allow,deny

                Allow from all

        </Location>

</VirtualHost>

Save and exit the file and restart apache. You should now be able to access your website or web app from www.domain.com or domain.com

To make the connection as HTTPS you can use Letsencrypt. Follow the steps mentioned in the article below to enable HTTPS for the new virtual host. Please note that the DNS should be properly set up before initiating this step. 

How To Secure Apache with Lets Encrypt on Ubuntu 18.04

Restart apache one last time. 

$ sudo /etc/init.d/apache2 restart

Congratulations you have now successfully completed hosting your dockerised website or web app on a Ubuntu server made it accessible with a domain. 

Additional Info

You can login into your docker containers with the following command.

$ docker exec -it container_name /bin/bash

You can import the DB dump to the docker container with the following command.

$ docker exec -i container_name mysql -uroot -ppwd dbname < db_dump.sql