Apache

Docker & Mac OS – Setting up a php development container

It has cost me quite a while to set this up. So I just want to share a simple Tutorial to set up a php development environment with docker on Mac OS.

Goals:

What we want to achieve is, that we have an environment, in which we can develop any php-app and – more important – edit it on the fly with our IDE. For instance, I use PhpStorm, where I have a project folder. And whenever I edit and save something in the folder, I want that theses changes appear in the browser directly.

For me, It was quite a hassle to set this up, so here I want to share my learnings.

Prerequisites:

  • First, you should have understood the basic concepts of docker and you should also have gone through the Getting Started. For example, you have to know:
    • what is an image and a container,
    • what the Dockerfile is good for and
    • what are the basic commants of docker (docker ps, docker-machine, etc.)
  • Secondly, you should have installed boot2docker

Basic setup of development environment:

So you want to set up a php-development-environment in docker? Do the following steps:

  1. Create a directory on your Mac: Your workspace, where you you want to edit your web app. I work with PhpStorm, so I will create a new project, which creates a new folder with the path
  2. Create files and folder:
    • The Dockerfile
    • A Shell script run.sh
    • Create a src-folder. Within that src-folder create two test-files: index.php and phpinfo.php.So finally your directory structure looks like:
  3. Put the following content into your Dockerfile:

    I won’t go into detail, but the code is mainly coming form Github (thanks!). In the following the most important concepts:

    • Name your main image-source (ubuntu):  FROM ubuntu:trusty
    • Starting apache and php:  RUN apt-get update && ...
    • And last but not least, you should add the project source into your working directory in the container. This is done via:  ADD src/ /app
  4. In the run.sh file put the following content:
  5. Put some example content into your php files:
    • index.php
    • phpinfo.php

That’s it. Your development environment is set. Now you just have to do the build and run process and map your src-folder with the app folder in your container. That way changes are seen directly on the fly.

Build and run your php-app with docker:

  1. You have created the folder structure as explained above, so now open your terminal and go to the project folder. Im my case that is:
  2. Make sure that boot2docker is running.

    If boot2docker is already up, then you should see something like. If you don’t know about boot2docker, google and install it.
  3. Do the build-process to create a new docker-image:

    Explanation:

    • docker build  says, that you want to build a new image
    • With  -t you’re stating, that you want to define the image name. After that state any user name and an image name that you like.
    • The Dot in the end is telling docker, that you want to build the image from the directory that your’re currently in. So that’s why you have to be in your project folder
  4. Run the image to get a new docker-container.

    This is the most tricky part, because you have to be aware of the different options:

    • docker run is the opening command, of course
    • -d run in the background
    • -p 80:80 is the port-mapping: Here the 80 port of your browser (on the mac os) is mapped to the port 80 of the container. 80 is the standard apache port, that’s why.
    • -v /Users/tobi/Documents/Docker/my-project-foler/src:/app Similar to the ADD-Statement in the Dockerfile, the v-option tells docker, to mount the project folder (on the Mac) to the app-folder (in the container). This way, when your container is running, changes in your project folder are directly passed to the container.
    • --name mycontainer  gives a name to your created container
    • The last thing is to name the image:  my-user-name/my-image-nameAfter that command, you can test, if your container is running. With using
      docker ps you should see something like:
  5. You’re almost ready to test your php web-app, but first you have to find out the ip-adress of boot2docker:

    If boot2docker is up, you should see the ip, for example:
  6. Now you’re finally ready to test your php-files in the browser. Go to your browser and type in the following url:

    If you see the content of your index-file (“Hello World! Thank god it’s friday!” as stated in the first part) you can be sure, that the build process was successful.
    You can also test your php-version with:
  7. Next, you have to test, if the mapping of your project folder to your container-folder is working correctly (in docker that is called data-volumes, and is explained in detailed here)
    • Edit the index.php file in your project folder. For example put in the following content:
    • Go to your browser, type in the url (or update the page):

      If you see the changes in the browser, you’re all set.

Conclusion

That’s it. By following that guide you should have been able to set up your dev-environment. Hopefully you could follow the guide and it helped you.

mod_rewrite (Apache2-Server)

Dieser Artikel widmet sich dem Thema mod_rewrite, zu dem vielleicht bei dem ein oder anderen hin und wieder die selben Fragen auftauchen.

Was ist “mod rewrite” oder mod_rewrite?

Erstens: mod_rewrite ist eine Einstellung des Apache-Servers. D.h. mod_rewrite ist entweder an (enabled) oder aus (disabled).

Zweitens: Wenn mod_rewrite an ist, so können URLs umgeleitet werden. D.h. die folgende URL …

 

… wird durch die Einstellungen von mod_rewrite umgeformt zu:

Wie stelle ich fest, ob mod_rewrite aktiv ist?

Ganz einfach: Erstelle eine leere php-Datei mit dem Inhalt:

Rufe diese auf dem Browser auf und suche mit der Suchfunktion des Browsers nach “mod_rewrite”. Wenn du einen Eintrag findest, wie im Screenshot dargestellt, so ist mod_rewrite an.

mod_rewrite_enabled_phpinfoEine andere Möglichkeit ist, einfach in der Shell den Befehl php -info einzugeben und dann zu schauen, ob mod_rewrite unter der Rubrik “Loaded Modules” auftaucht.

Wie aktiviere ich mod_rewrite, wenn es nicht aktiv ist?

Ich möchte hier zwei Möglichkeiten vorstellen, mod_rewrite zu aktivieren. (Es gibt noch eine dritte, sehr einfache Variante, die manchmal am Effektivsten ist: Nämlich einfach den Hoster anzufragen, ob er mod_rewrite aktivieren kann. Manchmal ist dies auch nötig, denn bei bestimmten Hostern sind die Rechte für die Apache-Cofing-Datei beschränkt und man hat gar keine andere Möglichkeit)

1. Die schnelle Variante, mod_rewrite zu aktivieren:

Gebe in der Shell von Linux einfach folgenden Befehl ein:

Dann musst Du noch den Apache-Server neustarten:

Falls dann in der Shell folgendes auftreten sollte …

… versuche es mit Möglichkeit 2:

2. mod_rewrite in apache-Config-Datei anschalten

Das ist etwas kniffliger: zuerst musst du die Datei apache2.config finden, was allerdings sehr einfach über folgenden Shell-Befehl geht:

Hierdurch findest du Einträge wie:

Dann gibst du folgenden Befehl ein:

Und du solltest als Ausgabe sehen, wo die HTTPD_ROOT-Directory ist und wie das SERVER_CONFIG_FILE heißt:

nun weißt du, wie der Pfad zur Datei lautet und kannst diese mit beispielsweise nano (oder alternativ auch per FTP) editieren:

Folgende Zeile muss in der Datei enthalten sein:

Nun muss nur noch der Apache-Server neu gestartet werden …

Und mod_rewrite sollte aktiviert sein.