Skip to main content

Newsletter

ads

Docker image running Apache, PHP-FPM on Ubuntu

This one took me a bit so thought I'd share. The goal here is to run an local development environment that is most similar to what I know and have running in production.

 

Although Alpine Linux seems to be the best practice these days I know Ubuntu well so I am willing to sacrifice memory for expediency. 

 

The trickiest part of this is getting php-fpm to stay alive in the docker container.  I simply could not get it to start and stay alive. Since CMD override each other -- only one CMD per Dockerfile, we can combine the commands into the same line with && using the shell form of CMD 

FROM ubuntu:latest
#the following ARG turns off the questions normally asked for location and timezone for Apache
ENV DEBIAN_FRONTEND=noninteractive

#install all the tools you might want to use in your container
#probably should change to apt-get install -y --no-install-recommends
RUN apt update
RUN apt install -y nano
RUN apt install -y apache2
RUN apt install libapache2-mod-fcgid
RUN a2enmod proxy
RUN a2enmod proxy_fcgi

# Install PHP-fpm &onfigure Apache to use our PHP-FPM socket for all PHP files
RUN apt install -y php7.4-fpm
RUN a2enconf php7.4-fpm
RUN php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer 
EXPOSE 80
# Now start the server
# Start PHP-FPM worker service and run Apache in foreground
CMD service php7.4-fpm start && /usr/sbin/apache2ctl -D FOREGROUND

#set working directory to where Apache serves files
WORKDIR /var/www/html
RUN rm index.html
#write a php file to demonstratre it is working.
RUN echo "<?php phpinfo() ?>" >> index.php

Run the following the the same directory as the Dockerfile above

docker build -t apachephp .

docker run --name php -p 8080:80 -d apachephp