Local PHP/Apache Setup 101

Created by: :: May 01, 2018 5:18:25 PM PDT (Revised May 03, 2018 12:12:39 AM PDT)

Learn the basics of containerizing your dev environment


Docker can be an extremely useful development platform for building applications that scale anywhere from a local test environment to a collaborative cloud solution between teams. The best way to get familiar with how Docker works is to start small, such as building and automating a local test environment. If you have a chance, I recommend reading about Docker and understanding some fundamental principles of containerization platforms and how Docker is different than yet another virtual environment here. Once you familiarize yourself with the general overview and are ready to start developing, I would also recommend reading through some of the documentation, starting with this.


Getting started:

This tutorial/blog post of mine will show you a basic example of how to start up a project locally on your own PC.


1) Install Docker (instructions here)

# Bash installation (Debian Jessie/Stretch)

$ sudo apt-get update 

$ sudo apt-get install \
     apt-transport-https \
     ca-certificates \
     curl \
     gnupg2 \

$ curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -

$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/debian \
   $(lsb_release -cs) \

$ sudo apt-get update

$ sudo apt-get install docker-ce

# Verify...
$ which docker


Write your first Dockerfile. This will create a new image, based on the official PHP/Apache Dockerfile:

FROM php:7.2-stretch
COPY php-site/ /var/www/html

Note: the directory "php-site," per the example above, will need to be in the same directory as your Dockerfile. There are ways to adjust the "context," but for this example, keep these together.

Now, let's build the image then run it...

$ docker build --tag sample-web-image .

You just built the image and set the context to be the current working directory (hence the last parameter being a period). This is the directory where the Dockerfile and the rest of the build reside.


Let's see the image:

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
sample-web-image    latest              1cadc7378907        28 hours ago        377MB


Great! Now let's run it...

$ docker run --detach --interactive --tty --publish 80:80 --name sample-web-container sample-web-image

$ docker ps
CONTAINER ID        IMAGE               ...        PORTS               NAMES
2bb45eee388e        sample-web-image    ...>80/tcp  sample-web-container


You should be set. Hit up localhost (port 80):

$ curl localhost
<title>403 Forbidden</title>
<p>You don't have permission to access /
on this server.<br />
<address>Apache/2.4.25 (Debian) Server at localhost Port 80</address>

Sometimes, we don't always get a happy ending.



But, the fact that Apache returns this error means there is a server now running on Localhost. Mission accomplished? Results will vary based on what is in your webroot (referenced above in the php-site/ dir).