How to setup Phalcon framework in a Docker container

Views: 12
0 0
Read Time:3 Minute, 46 Second

How to setup Phlacon framework in a Docker container? This is how we did it.

Not sure if everyone of you is aware but its innovative architecture makes this framework as the fastest PHP framework ever built. More information here but the long story short is that (from the documentation):

Phalcon is an open source full stack framework for PHP, written as a C-extension. Phalcon is optimized for high performance. Its unique architecture allows the framework to always be memory resident, offering its functionality whenever it’s needed, without expensive file stats and file reads that traditional PHP frameworks employ.

I personally started to use Phalcon since its version 1.x and in one instant I forgot about all the others (Laravel, CodeIgnitor, Cake, Zend etc were the major competitor of those times).

For the simplicity of this setup we are going with the ubuntu image and Apache, but just in case you want more information on how to setup phalcon from scratch this is the link to follow https://docs.phalcon.io/4.0/en/installation or https://docs.phalcon.io/3.4/en/installation

The below docker file is going through few steps

  • first we install the dependencies needed for cloning and compiling Phalcon locally
  • then we install Apache and PHP needed files
  • configure the phalcon.ini and install xdebug
  • after that we start the removal of all the libraries not needed anymore
  • start the Apache service
FROM ubuntu:18.04 as intermediate
# install the dependencies
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && \
    apt-get install -y \
        zlib1g-dev \
        libxml2-dev\
        locales \
        g++ \
        git \
        libpcre3-dev \
        make \
        php \
        php-dev \
        re2c \
        wget 
RUN locale-gen en_US.UTF-8 && \
        export LANG=en_US.UTF-8
        
RUN git clone --depth 1 -b v3.4.5 git://github.com/phalcon/cphalcon.git

WORKDIR cphalcon/build
RUN ./install && \
    apt-get install -y --allow-unauthenticated \
        apache2 \
        iputils-ping \
        cron \
        curl \
        libc6-i386 \
        mcrypt \
        php \
        php-apcu \
        php-curl \
        php-mbstring \
        php-mysql \
        php-soap \
        php-xml \
        unzip \
        zip && \
        apt-get autoremove -y && \
        apt-get clean && \
        rm -rf /var/lib/apt/lists/* && \
        echo "extension=phalcon.so" > /etc/php/7.2/mods-available/phalcon.ini && \
        pecl install xdebug && \
        echo "zend_extension=\"/usr/lib/php/20170718/xdebug.so\"" > /etc/php/7.2/mods-available/xdebug.ini && \
        cp /etc/php/7.2/mods-available/xdebug.ini /etc/php/7.2/apache2/conf.d/20-xdebug.ini && \
        phpenmod phalcon && phpenmod mbstring && \
        a2enmod rewrite && \
        apt-get purge -y \
        zlib1g-dev \
        libxml2-dev\
        locales \
        g++ \
        git \
        libpcre3-dev \
        make \
        re2c \
        wget && \
        rm -rf /cphalcon && \
        mkdir /app

WORKDIR /app
EXPOSE 80
CMD service apache2 start && \
        service cron start && \
    /bin/bash
CMD ["apache2ctl", "-D", "FOREGROUND"]

All you need to do is to create a folder i.e. phalcon_test and the Dockerfile in it with the above script. After that run

# from /you/path/to/phalcon_test 
docker build -t phalcon .

Create a file called index.php in your working directory, i.e. /home/your_user/work/phalcon_app/application

<?php
    echo phpinfo();
?>

Then spin up the container with this command

docker run -v /home/your_user/work/phalcon_app/application/:/var/www/html -p 8080:80 --name phalcon_test_1 phalcon &

At this stage you should have a working docker container with Apache/PHP/Phalcon running, and you can verify this by going to your browser to “http://localhost:8080 and we should be able to see something like this

phpinfo result

In particular you should look on that page for the “phalcon” setup

phalcon php framework setup verification

This means that our setup is complete and that the php phalcon extension has been correctly loaded.

This is the end of this part and I hope will help you kick start your understanding of how this framework works.

In the next few weeks we are going to create a small project using this amazing framework.

Link to the new version

Link to the dev environment with Apache, PHP7 and Phalcon 4

Other articles of the PHP series

Learning PHP – Introduction

Learning PHP – print and echo

Learning PHP – The variables

Learning PHP – if else

Learning PHP – Arrays

Learning PHP – The loops

Learning PHP – The Functions

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %
Previous post Mouse mover in python
Next post How to extract metadata from images
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x