/ARTICLES

Grav-PHP-Nginx with Docker

Grav-PHP-Nginx with Docker

When we talk about Content Management Systems, major names comes to our minds like Drupal, Joomla or WordPress, but if we want a modern, simple but also very powerful CMS, Grav is the new comer to this game.

Now, If we pair Grav CMS with Docker and Nginx, we have a very light and efficient team for the win in a small footprint.

You can find my Dockerfile ready to build on my Github account.

You can get a Docker image ready to run in my DockerHub account (ahumaro/grav-php-nginx)

One process per container or not

In theory, the use of Docker and containers encourage the one process per container philosophy but, from the point of view of software as a service, maybe we can focus this article with the philosophy of one service per container.

Dockerfile step-by-step

Lets try it. Lets create a new Docker container and inside it, lets build our CMS stack with Nginx, PHP and Grav in a few easy steps. All of this builded automatically by docker and guided by a Dokerfile.

Step 0: Phusion as a base container
FROM phusion/baseimage:0.9.16

With this Dockerfile instruction, Phusion will be the base image for this container. Version 0.9.16 is based on Ubuntu 14.04 LTS

Usually, a docker container is based in a Linux distro, it depends on your needs but, sometimes, the distro is too fat, not very friendly nor efficient living in a container. That’s why solutions like Phusion comes in handy due to its extra utilities and its lightweight footprint. For more information: Phusion Github.

Step 1: Install core packages
#Install core packages
RUN apt-get update -q
RUN apt-get upgrade -y -q
RUN apt-get install -y -q php5 php5-cli php5-fpm php5-gd php5-curl php5-apcu ca-certificates nginx git-core
RUN apt-get clean -q && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

Instructions to install the core packages for the container like nginx, php and git

Step 2: Get latest Grav from Git and install it
#Get Grav
RUN rm -fR /usr/share/nginx/html/
RUN git clone https://github.com/getgrav/grav.git /usr/share/nginx/html/

#Install Grav
WORKDIR /usr/share/nginx/html/
RUN bin/composer.phar self-update
RUN bin/grav install
RUN chown www-data:www-data .
RUN chown -R www-data:www-data *
RUN find . -type f | xargs chmod 664
RUN find . -type d | xargs chmod 775
RUN find . -type d | xargs chmod +s
RUN umask 0002

Instructions to checkout Grav from Github and then run installation scripts. Also assign permissions for the Grav folders.

Step 3: Setup Grav inside Nginx
#Configure Nginx - enable gzip
RUN sed -i 's|# gzip_types|  gzip_types|' /etc/nginx/nginx.conf

#Setup Grav configuration for Nginx
RUN touch /etc/nginx/grav_conf.sh
RUN chmod +x /etc/nginx/grav_conf.sh
RUN echo '#!/bin/bash \n\
    echo "" > /etc/nginx/sites-available/default \n\
    ok="0" \n\
    while IFS="" read line \n\
    do \n\
        if [ "$line" = "    server {" ]; then \n\
            ok="1" \n\
        fi \n\
        if [ "$line" = "}" ]; then \n\
            ok="0" \n\
        fi \n\
        if [ "$ok" = "1" ]; then \n\
            echo "$line" >> /etc/nginx/sites-available/default \n\
        fi \n\
    done < /usr/share/nginx/html/nginx.conf' >> /etc/nginx/grav_conf.sh
RUN /etc/nginx/grav_conf.sh
RUN sed -i \
        -e 's|root   html|root   /usr/share/nginx/html|' \
        -e 's|127.0.0.1:9000;|unix:/var/run/php5-fpm.sock;|' \
    /etc/nginx/sites-available/default

The previous instructions look a little trickier but in fact they do a very easy task: put the Grav configuration instructions inside Nginx.

Grav comes with a Nginx configuration file in the root of the installation and the previous script reads this configuration and writes it inside Nginx available sites.

Step 4: Setup daemons to run at container startup

Because we are using Phusion as the base image for the container, this image comes with a special init system (/sbin/my_init) that solves the PID 1 zombie reaping problem and also allows us to configure services to start automatically with ease.

#Configure Nginx daemon
RUN mkdir /etc/service/nginx
ADD build/daemons/nginx.sh /etc/service/nginx/run
RUN chmod +x /etc/service/nginx/run

#Configure PHP-FPM daemon
RUN mkdir /etc/service/php-fpm
ADD build/daemons/php-fpm.sh /etc/service/php-fpm/run
RUN chmod +x /etc/service/php-fpm/run
Step 5: Configure SSH service
#Enable SSH
RUN rm -f /etc/service/sshd/down
RUN /etc/my_init.d/00_regen_ssh_host_keys.sh
Step 6: Expose ports and volumes
#Expose ports
EXPOSE 80 22

#Expose volumes
VOLUME ["/usr/share/nginx/html", "/etc/nginx", "/root/.ssh"]

Build the Docker image

Now we are ready to build the image:

docker build -t="ahumaro/grav-php-nginx:0.0.1" .

Run the container

Run the container using:

root [ ~ ]# docker run -d -p 8080:80 -p 2222:22 ahumaro/grav-php-nginx:0.0.1

Here you can see how you can redirect the ports. For example, port 22 of the container will be redirected to port 2222 in the host. To access your container make a ssh connection to port 2222 on your host.

Test the final product

The new container is up and running. Nginx and Grav is ready to handle new requests so lets try it. Open a browser and point it to the host machine with port 8080, this will redirect the request to port 80 on the container where Nginx-Grav is waiting. You will get the following result:

Grav-Nginx with Docker

Next steps and recommendations

Now you are ready to run but don’t forget there are many more possibilities that you can explore. For example:

  • How to find the exposed volumes on the host is very easy, first find the ID of the running container with the following command:

    root [ ~ ]# docker ps
    CONTAINER ID        IMAGE                           COMMAND             CREATED             STATUS              PORTS                                        NAMES
    560abd21e5a4        ahumaro/grav-php-nginx:latest   "/sbin/my_init"     16 minutes ago      Up 16 minutes       0.0.0.0:2223->22/tcp, 0.0.0.0:8080->80/tcp   nostalgic_babbage

    With the ID, inspect the container and look for the section Volumes

    root [ ~ ]# docker inspect 560abd21e5a4
    ...
    "Volumes": {
          "/etc/nginx": "/var/lib/docker/vfs/dir/7e618a0800605659352f5f69ab2c58eee555a7f725a3ef46f016e2d269d1e11e",
          "/root/.ssh": "/var/lib/docker/vfs/dir/24db3321bd511e2134dd3f6487707e62accd2bcbf2cfd299773aba9c7cb79a18",
          "/usr/share/nginx/html": "/var/lib/docker/vfs/dir/5c927c2485052a57e70ea66bda9911479e581eceb891f5ea12a38f061a209591"
    ...

    Now you can see the mappings and you can access those folders from the host, for example, you can insert your public key inside SSH user config folder of the container.

    cat my_ssh_key.pub > /var/lib/docker/vfs/dir/24db3321bd511e2134dd3f6487707e62accd2bcbf2cfd299773aba9c7cb79a18/authorized_keys

    And test a connection to the container:

    ssh -i my_ssh_key -p 2222 root@ip-to-your-host
  • For the management of the Grav CMS content, there are many strategies. You can access the root folder from the volumes exposed, or access directly to the container via SSH. The way I recommend is to synchronize the user folder of Grav (/usr/share/nginx/html/user) to a Git repository and just pull new changes inside it.

  • Don’t forget you can run your containers on the cloud with services like Google Cloud Run, Google Compute Engine, AWS or Digital Ocean.

  • Remember that you can find my Grav-PHP-Nginx Dockerfile on GitHub ready to build or, if you are in a hurry, just get my pre-built Docker image ready to run at Docker Hub.

Keep exploring and enjoy!

Ahumaro Mendoza
ahumaro@ahumaro.com
www.ahumaro.com