Skip to content

Instantly share code, notes, and snippets.

Last active Aug 4, 2021
What would you like to do?
Dockerfile for a dev web server with PHP/Apache
# To create the docker image :
# cd <this file directory>
# docker build -t apache-php-dev .
# Start image :
# docker run -d -p 80:80 \
# -v /home/user/html:/var/www/html \
# -v /home/user/subdomain1:/var/www/subdomain1 \
# -v /home/user/subdomain2:/var/www/subdomain2 \
# apache-php-dev
# Open browser :
# http://localhost (/home/user/html)
# http://subdomain1.localhost (/home/user/subdomain1)
# http://subdomain2.localhost (/home/user/subdomain2)
# <!> if you use a localdomain like mydomain.lan
# you may need to add in your /etc/hosts :
# mydomain.lan
# subdomain1.mydomain.lan
# To cancel restart always :
# docker ps (get the NAMES)
# docker update --restart=no <NAME>
FROM ubuntu:20.04
LABEL maintainer=""
LABEL description="Apache / PHP for dev"
RUN apt-get -y update && apt-get install -y \
apache2 \
php7.4 \
libapache2-mod-php7.4 \
php7.4-bcmath \
php7.4-gd \
php7.4-json \
php7.4-sqlite \
php7.4-mysql \
php7.4-curl \
php7.4-xml \
php7.4-mbstring \
php7.4-zip \
mcrypt \
RUN apt-get install locales
RUN locale-gen fr_FR.UTF-8
RUN locale-gen en_US.UTF-8
RUN locale-gen de_DE.UTF-8
# config PHP
# we want a dev server which shows PHP errors
RUN sed -i -e 's/^error_reporting\s*=.*/error_reporting = E_ALL/' /etc/php/7.4/apache2/php.ini
RUN sed -i -e 's/^display_errors\s*=.*/display_errors = On/' /etc/php/7.4/apache2/php.ini
RUN sed -i -e 's/^zlib.output_compression\s*=.*/zlib.output_compression = Off/' /etc/php/7.4/apache2/php.ini
# to be able to use "nano" with shell on "docker exec -it [CONTAINER ID] bash"
ENV TERM xterm
# Apache conf
# allow .htaccess with RewriteEngine
RUN a2enmod rewrite
# to see live logs we do : docker logs -f [CONTAINER ID]
# without the following line we get "AH00558: apache2: Could not reliably determine the server's fully qualified domain name"
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf
# autorise .htaccess files
RUN sed -i '/<Directory \/var\/www\/>/,/<\/Directory>/ s/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf
# for production :
# RUN echo "ServerTokens Prod\n" >> /etc/apache2/apache2.conf
# RUN echo "ServerSignature Off\n" >> /etc/apache2/apache2.conf
#RUN chown -R www-data:www-data /var/www
#RUN chmod 755 -R /var/www
RUN chgrp -R www-data /var/www
RUN find /var/www -type d -exec chmod 775 {} +
RUN find /var/www -type f -exec chmod 664 {} +
# Virtual hosts if needed
# RUN echo "<VirtualHost *:80>\n ServerName subdomain1.localhost\n DocumentRoot /var/www/subdomain1\n</VirtualHost>\n" > /etc/apache2/sites-available/subdomain1.conf
# RUN echo "<VirtualHost *:80>\n ServerName subdomain2.localhost\n DocumentRoot /var/www/subdomain2\n</VirtualHost>\n" > /etc/apache2/sites-available/subdomain2.conf
# RUN a2ensite subdomain1
# RUN a2ensite subdomain2
# start Apache2 on image start
CMD ["/usr/sbin/apache2ctl","-DFOREGROUND"]
# NB : si update image and commit, do :
# docker commit -m "changes" --change "ENV TERM xterm" --change "CMD /usr/sbin/apache2ctl -DFOREGROUND" dc5239032732 apache-php-dev

This comment has been minimized.

Copy link

@lunfardo lunfardo commented Jan 31, 2019

why not use APACHE_DOCUMENT_ROOT instead of /var/www?


This comment has been minimized.

Copy link
Owner Author

@Tazeg Tazeg commented Oct 15, 2019

yes, why not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment