Docker: A primer for PHP Developers

               Nginx can be used as web server to serve static assets (html,css and javascript files for example).

               When a user requests a page from a web server, the web server takes the request and sends an appropriate response                back to the user.

               Nginx is a very efficient open-source web server that is also used for reverse proxy, HTTP load balancing and email proxy.

               We configure Nginx in three steps:

  • Step 1: Configure a nginx configuration file. For us: default.conf
  • Step 2: Update the Dockerfile (see section 5/13. Dockerfiles) with the newly created default.conf file
  • Step 3: Rebuild the container image after our update of the Dockerfile

Step 1: default.conf
server {
    listen 80 default_server;

    root /var/www/html/public;

    index index.html index.htm index.php;

    server_name _;

    charset utf-8;

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt  { log_not_found off; access_log off; }

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }

    error_page 404 /index.php;
}

Step 2: Dockerfile
FROM ubuntu:18.04

LABEL maintainer="Kâmi Barut-Wanayo"

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update \
    && apt-get install -y gnupg tzdata \
    && echo "UTC" > /etc/timezone \
    && dpkg-reconfigure -f noninteractive tzdata

RUN apt-get update \
    && apt-get install -y curl zip unzip git supervisor sqlite3 \
       nginx php7.2-fpm php7.2-cli \
       php7.2-pgsql php7.2-sqlite3 php7.2-gd \
       php7.2-curl php7.2-memcached \
       php7.2-imap php7.2-mysql php7.2-mbstring \
       php7.2-xml php7.2-zip php7.2-bcmath php7.2-soap \
       php7.2-intl php7.2-readline php7.2-xdebug \
       php-msgpack php-igbinary \
    && php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \
    && mkdir /run/php \
    && apt-get -y autoremove \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
    && echo "daemon off;" >> /etc/nginx/nginx.conf

ADD default /etc/nginx/sites-available/default

                 Step 3: We rebuild our container image using the same command we used in section 5/13. Dockerfiles:

$ docker build -t  kbarut/app:latest -f docker/app/Dockerfile docker/app
										

Kâmi Barut-Wanayo © 2024 - 2025