Skip to content

Month: July 2021

Containerize old PHP/Laravel application with Apache and SSL

Recently I migrated my websites and some applications from 7 years old server to a new server. Then I just realize some PHP application is not working on the latest PHP 7.4, thus I have to create a simple docker container to run the app.

Specifically, it’s Invoice Ninja v3.4.1.
This application was running on my server for more than 5 years.
Because I’m too lazy to upgrade to the latest version manually, so I think letting it run in a container with a specific PHP version will be good enough for me.

Following is my Dockerfile:

# use any php version as u need
FROM php:7.0.33-apache

# install any php extension as u need
RUN docker-php-ext-install pdo_mysql mysql

# install ssl-cert for generate self-signed cert
RUN apt-get update && apt-get install -y ssl-cert

# enable apache2 mod
RUN a2enmod rewrite
RUN a2enmod ssl

# simple virtualhost definition
RUN echo '\n\
<VirtualHost *:443>\n\
  <Directory /srv>\n\
    Options Indexes FollowSymLinks\n\
    AllowOverride All\n\
    Require all granted\n\
  </Directory>\n\
  DocumentRoot /srv/public \n\
  SSLEngine on \n\
  SSLCertificateFile  /etc/ssl/certs/ssl-cert-snakeoil.pem \n\
  SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key \n\
</VirtualHost>\n'\
> /etc/apache2/sites-available/000-default.conf

WORKDIR /srv

CMD ["apache2-foreground"]

With this Dockerfile:
1. install PHP extension to run the applications.
2. it has a self-signed certificate to enable simple SSL.
3. it allows us to mount any kind of PHP application and use it immediately.

To run the application with this Dockerfile:
1. Put the Dockerfile in your application root directory.

2. Build the image:
docker build -t YOUR-APPLICATION-IMAGE:custom .

3. Run the container with the image and mount the application directory to container’s /srv:
docker run --rm --name CONTAINER_NAME -v /path/to/your/application YOUR-APPLICATION-IMAGE:custom

That’s all~
With this trick, you should be able to run any web applications that require PHP version less than 7.4

Leave a Comment