Instant PHP Frameworks Using LfPHP and lfphp-get

When developing customer code, I lost count of how many times I had to dig around to find the instructions for installing this or that PHP framework.  In most cases all I really want or need to do is to install the skeleton application in order to do some quick testing on a proposed feature.  Yet another problem is that more often than not, the version of PHP running on the customer server is not the same as the version running on my own development computer.  This often results in another agonizing round of testing and debugging before I can get my code to run properly on the customer’s website.

So … given this long preamble, by this point you’re asking, is there a magic carpet someplace?  Can’t I just get on the carpet, and whoosh … it magically transports me to Working Code Land?  Ah ha,  I thought you’d never ask!  Enter the Linux for PHP Docker image.

 

What in Heck is Linux for PHP?

Linux for PHP (LfPHP) is a Docker image developed by my partner, Andrew Caya.  It’s based on ASCLinux, an ultra-fast streamlined version of Linux that includes only the bare minimum number of packages needed to run a headless server.  LfPHP is expressly designed from the ground-up as a Docker image.  In addition, LfPHP contains a full LAMP stack including:

  • Linux
  • Apache
  • MariaDB (MySQL), and
  • PHP

And that’s not all folks (now I’m feeling like a presenter on the Shopping Channel!) … also included in this incredible image is nginx, MongoDB, Python and Perl.  So, in a certain sense, LfPHP is a LANMMPPP  stack.  For more information, including a link to the documentation and downloads, go to the LfPHP website.

 

So What Does This Have to Do with Frameworks?

Now you’re probably wondering: what does this have to do with frameworks?  In order to answer this question, I need to introduce you to an amazing utility included with all versions of LfPHP.  That tool is called lfphp-get, a package manager specifically designed for Docker and the Cloud environment.  It’s extremely simple to use (and to extend), non-distribution dependent, and replaces top-heavy package managers such as aptitude or yum.   lfphp-get uses an extremely clever and extensible mechanism that performs streamlined minimal installations of a bewildering variety of packages and technologies.

For example, let’s say that we want to download and run the PHP 7.4 non-threadsafe version of LfPHP.  Have a look at the following command:

docker run -it asclinux/linuxforphp-8.2-ultimate:7.4-nts /bin/bash

From the prompt that opens following this command, ask for help on lfphp-get using the --help option:

After you enter lfphp-get --list the LfPHP website is queried to obtain the list of available packages.  The reasoning behind this is twofold.  First of all, you are assured to get the latest list.  If the list were hard-coded into the LfPHP image itself, it would quickly fall out of date.  The second advantage is that it allows parallel development of both LfPHP itself, as well as its available packages.  In other words, development of additional add-on packages can occur in parallel with the development of the LfPHP image itself.

IMPORTANT: the lfphp-get utility is embedded in LfPHP and itself could get out of date.  Accordingly, before any important operation, it’s best to run lfphp-get --self-update.  Here is a screenshot of the tail end of the self-update process:

Okay … now that I’m done patting Andrew on the back for a job well done, let’s have a look at one package in particular that stands out: php-frameworks.

 

What’s the Big Deal About the php-frameworks Package?

The php-frameworks package, as its name implies, gives you access to the most well-known PHP frameworks.  If you just run lfphp-get php-frameworks and do not specify a particular one, you get a list of frameworks the utility is able to install:

The first prompt asks you for the name of your new project.  This can be any string suitable for a sub-directory name, as that is what happens next: a sub-directory under /srv/tempo is created.  After that, don’t be surprised to see Composer kick in, as most frameworks these days are installed using a composer create-project installer script:

At the very end of the installation process, lfphp-get shows you the generated database connection information.  It also automatically starts running the new application using the built-in PHP development server:

You can now hop over to the browser on your host computer (i.e. the computer upon which you are running Docker), and see your new framework-based website right away.  Don’t forget to run ifconfig to find out the IP address chosen for your container!

docker exec CONTAINER_ID /bin/bash -c "ifconfig"

Here is how the new default Laravel website appears:

Automate, Automate, Automate

If you’re a real nut on automation (and well you should be!), the entire process, start to finish, can be automated by supplying the appropriate command line options as you run the LfPHP Docker image.  The CLI options for lfphp-get when php-frameworks has been selected, are as follows:

lfphp-get php-frameworks < zf | ze | symfony | laravel | cakephp | slim | lightmvc | lightmvc-swoole | laminas | mezzio > < project_name >

The first argument is the type of framework you wish to install.  The second argument is the name of your project.  As mentioned above, the project_name must be a suitable sub-directory name, meaning alpha-numeric with no special characters aside from underscore or dash (preferably no spaces!).  As an example, let’s choose Symfony, with a project name of php_cl. Once your choice is made, the appropriate custom installer script kicks in and installs the project skeleton and framework itself.

You can run the command from inside the running container as follows:

lfphp-get php-frameworks symfony php_cl

Alternatively, you can run the command as an add-on to the initial Docker run:

docker run -it asclinux/linuxforphp-8.2-ultimate:7.4-nts /bin/bash -c "lfphp-get php-frameworks symfony php_cl"

The script then does its magic, and in the end runs the build-in PHP development server.  After running ifconfig in a separate terminal to determine the IP address, here is the resulting screen:

And, as the saying goes, that’s all Folks!

 

Summary

Linux for PHP (LfPHP) is a fantastic aid for developers who need a full LAMP stack with access to different versions of PHP.  LfPHP includes a utility lfphp-get that provides flexible, extensible, up-to-date, package management.  One of the less well-known packages is php-frameworks.  By supplying the name of the framework and project name, you are able to instantly build development websites for any of the frameworks listed. Not only does it save lots of time, but you have the added advantage of being able to choose the version of PHP which will ultimately be used on the customer’s website.

As always dear readers, thanks for your attention, and please do feel free to browse this website and sign up for some of our courses.