• Home
    • English
    • 中文
  • About Us
  • Services
    • SEO Services
    • Website Design Service
  • Projects
  • Docs
  • Blog
    • Affiliate
    • Ecommerce
    • Frontend
    • linux
      • nginx
    • PHP
      • Magento
      • wordpress
    • Python
    • SEO
    • Web
  • Contact Us

Subscribe to Updates

Get the latest creative news from FooBar about art, design and business.

What's Hot

Design a plugin for wordpress woocommerce to display a tab to show attachment download

2024-04-06

TranslatePress v2.6.9 – WordPress Translation Plugin

2023-12-25

A Linux batch script converting pictures to webp format

2023-07-10
Facebook Twitter Instagram
  • 中文
  • English
Facebook Twitter Instagram Pinterest VKontakte
Weilai Tech Weilai Tech
  • Home
    • English
    • 中文
  • About Us
  • Services
    • SEO Services
    • Website Design Service
  • Projects
  • Docs
  • Blog
    • Affiliate
    • Ecommerce
    • Frontend
    • linux
      • nginx
    • PHP
      • Magento
      • wordpress
    • Python
    • SEO
    • Web
  • Contact Us
Weilai Tech Weilai Tech
Home»virtualization»Running the Apache HTTP Server with PHP inside Docker
virtualization

Running the Apache HTTP Server with PHP inside Docker

OxfordBy Oxford2023-05-15No Comments5 Mins Read
Facebook Twitter Pinterest LinkedIn Tumblr Email
Share
Facebook Twitter LinkedIn Pinterest Email

Running the Apache HTTP Server with PHP inside Docker

Running the Apache HTTP Server (“httpd”) with PHP inside Docker is easy. So easy that, in case you’re using httpd and PHP, you actually have no reasons not to do it. Here’s a short guide about how to do that.

https://nelkinda.com/blog/apache-php-in-docker/

Table of Contents

  • 1 A Vanilla Prototype
    • 1.1 Things that didn’t work for me
  • 2 The Dockerfile
    • 2.1 Explanation
  • 3 The docker commands
  • 4 The Makefile
  • 5 Appendix
    • 5.1 Table of Abbreviations
    • 5.2 Glossary
    • 5.3 References
Figure -1: Docker + Apache + PHP

1 A Vanilla Prototype

To run the Apache HTTP Server with PHP in Docker on your, say, htdocs/ directory, on port 8082, you can run the following command:

docker run -d -p 8082:80 --mount type=bind,source="$(pwd)/htdocs",target=/var/www/html php:apache
Listing 1-1: Command to run the Apache HTTP Server with PHP in a Docker container

That’s it! If you try this, and it works for you, you’re done!

The command does the following:

  • If the Docker image php:apache is not present in your machine’s local Docker registry, it will be downloaded from Docker hub.
  • It creates a new container based on the image php:apache.
  • It maps port 80 from the container to port 8082 on your host machine.
  • It mounts the directory htdocs/ from your host machine to /var/www/html in the container.

If you’re lucky, you can now access http://localhost:8082/ and see your web pages hosted from an Apache HTTP Server with PHP in a Docker container.

1.1 Things that didn’t work for me

For me, this didn’t work, for the following reasons:

  • I override a lot of configuration in .htaccess.
  • I need a modified PHP configuration.
  • I use Apache HTTP Server modules which are not enabled by default.

So I created my own Dockerfile to cope with the changes that I need.

2 The Dockerfile

The standard image does not serve all purposes. Things you might want to change are:

  • Enable additional Apache HTTP Server modules.
  • Allow overriding specific directives in .htaccess.
  • Add a PHP configuration.
  1. FROM php:apache
  2. SHELL [“/bin/bash”, “-c”]
  3. RUN ln –s ../mods–available/{expires,headers,rewrite}.load /etc/apache2/mods–enabled/
  4. RUN sed –e ‘/<Directory \/var\/www\/>/,/<\/Directory>/s/AllowOverride None/AllowOverride All/’ –i /etc/apache2/apache2.conf
  5. COPY php.ini /usr/local/etc/php/
Listing 2-1: Dockerfile for building a slightly modified Apache PHP image.

2.1 Explanation

  • The FROM php:apache selects the latest version of PHP with the latest version of the Apache HTTP Server. I do not use this image in production, otherwise I would pin down the version.
  • The SHELL line changes the shell command for the subsequent RUN commands to be /bin/bash -c instead of /bin/sh -c. I do this to get brace expansion.
  • The RUN ln command enables the Apache HTTP Server modules. I could’ve used RUN a2enmod instead, but since I know what I’m doing, ln is simpler and faster for me than a2enmod. There’s a caveat: If the way how Debian handles Apache HTTP Server modules changes, my Dockerfile might fail. If I would use a2enmod, my Dockerfile would still work. Refer to [man:a2enmod] for more information about a2enmod.
  • The RUN sed command replaces AllowOverride None with AllowOverride All for the directory /var/www/. I have a lot of configuration in my .htaccess file which needs this.
  • The COPY command copies my php.ini file to the desired location in the docker image. My PHP configuration has a lot of modifications.

For more information what these Dockerfile commands mean and how they work, refer to [Dockerfile].

3 The docker commands

To use and run this image, you need a few docker commands.

docker build -t my/apache-php .
This command builds your Apache PHP Docker image from the Dockerfile and stores it in your local registry under the name my/apache-php.
docker run --name apache -d 8082:80 --mount type=bind,source="$(pwd)"/htdocs,target=/var/www/html my/apache-php
This command runs your docker image my/apache-php in a docker container. The Docker container is named apache for further reference. Port 80 inside the container, which is where the Apache HTTP Server is running, is mapped to port 8082 on the host. So you can access this with http://localhost:8082/. The option --mount type=bind,source="$(pwd)"/htdocs,target=/var/www/html creates a bind mount of the htdocs directory of the host to /var/www/html in the container. If your web pages are in a different directory, adjust the source path accordingly.
docker stop apache
Stops your docker container.
docker rm apache
Removes your docker container.
docker container logs apache
Prints the logs of the Apache HTTP Server inside the container.
docker exec -it apache bash
Logs into the Apache PHP container using a bash shell.
docker ps -f name=apache
Shows the status of the Apache PHP container.

4 The Makefile

The Makefile serves as a more convenient way to wrap the docker commands needed to deal with the container. The things you want to do:

  • (Create and) start the container.
  • Stop (and remove) the container.
  • Print the logs of the container.
  • Login to the container.

So, the Makefile offers the following commands:

make start
Builds and starts the container. Docker is incremental. In case the Dockerfile was not changed, the container image will actually not be rebuilt. The Apache HTTP Server will be bound to port 8082. If you want a different port, for example, port 9000, you can run it like this: make start PORT=9000.
make stop
Stops and removes the container. Because the container does not store any data, it can safely be removed.
make logs
Prints the logs of the container, in case you want to inspect the logs.
make login
Login to the container with a root bash, in case you want to inspect something inside the container.
  1. PORT?=8082
  2.  
  3. .PHONY: start
  4. start:
  5. docker build –t nelkinda/apache–php .
  6. docker run —name apache –d –p $(PORT):80 —mount type=bind,source=“$(CURDIR)”/../htdocs,target=/var/www/html nelkinda/apache–php
  7.  
  8. .PHONY: stop
  9. stop:
  10. docker stop apache
  11. docker rm apache
  12.  
  13. .PHONY: logs
  14. logs:
  15. docker container logs apache
  16.  
  17. .PHONY: login
  18. login:
  19. docker exec –it apache bash
  20.  
  21. .PHONY: status
  22. status:
  23. docker ps –f name=apache
Listing 4-1: Makefile for wrapping control of the Apache PHP docker container.

5 Appendix

5.1 Table of Abbreviations

Abbreviation Long Form
HTTP Hypertext Transfer Protocol
PHP PHP: Hypertext Preprocessor (old: Personal Home Page)

5.2 Glossary

Apache HTTP Server
A web server developed by the Apache Software Foundation. It is one of the most popular web servers.
Docker
Provides operating-system-level virtualization known as containerization on Linux.
Dockerfile
A file describing the automated creation of a Docker image.
Makefile
A file describing the automation of (not only) software construction run by a tool called make.
PHP
A programming language originally designed to create web pages dynamically on a web server.

5.3 References

[Dockerfile]
Dockerfile reference (URL: https://docs.docker.com/engine/reference/builder/), on Docker Inc. Docker Documentation
[man:a2enmod]
Debian System Manager’s Manual, a2enmod(8) (URL: https://manpages.debian.org/jessie/apache2/a2enmod.8.en.html), Daniel Stone on Debian Manpages
Author: Christian Hujer

The Docker logo is a trademark of Docker, Inc. and used according to its Docker Legal Terms. The Powered By Apache logo is a trademark of the Apache Foundation and is used according to its Press Kit. The PHP logo is designed by Colin Viebrock under the Creative Commons Attribution-Share Alike 4.0 International on PHP Download Logos and Icons.

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Avatar photo
Oxford

Related Posts

how to use docker to run php5.6 plus apache

2023-05-15

Compile a Jekyll project without installing Jekyll or Ruby by using Docker

2022-01-07

run jekyll on docker

2022-01-06

Add and Use ISO Library Storage Repository in Xen XCP-ng

2021-12-26
Recent Posts
  • Design a plugin for wordpress woocommerce to display a tab to show attachment download
  • TranslatePress v2.6.9 – WordPress Translation Plugin
  • A Linux batch script converting pictures to webp format
  • Hearing aid listed company official website SEO case
  • how to use docker to run php5.6 plus apache
May 2023
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
293031  
« Mar   Jul »
Tags
app branding design digital Docly docs etc faq fix github Helpdesk Image issue magento Manual marketing memecached Photography planing seo sequrity tips Travel ui/ux web WordPress 爬虫
Editors Picks
About Us

Guangzhou Weilai Technology is a foreign trade integrated marketing service provider focusing on Google as the drainage center and marketing self-built website as the carrier.

Email Us: [email protected]
Contact: +86 18676917505

Facebook Pinterest YouTube LinkedIn
Recent Posts
  • Design a plugin for wordpress woocommerce to display a tab to show attachment download
  • TranslatePress v2.6.9 – WordPress Translation Plugin
  • A Linux batch script converting pictures to webp format
  • Hearing aid listed company official website SEO case
  • how to use docker to run php5.6 plus apache
From Flickr
Website Design Case
© 2024 Copyright by Guangzhou Weilai Technology Co.,Ltd..
  • Home
  • About Us
  • SEO Services
  • Website Design Service
  • Projects
  • Blog
  • Contact Us

Type above and press Enter to search. Press Esc to cancel.