When working in the Windows environment we were able to download a version of the Phalcon PHP module which had been compiled into a Windows dll file. We then saved this dll file into the PHP extensions folder and added it to the list of extensions in php.ini. Once the server was re-started the server picked up on the module and, provided it was the correct version, the module was loaded.
It is possible to download a pre-compiled extension for Phalcon also and deploy it using apt. This is described in that section on the Phalcon website.
In order for this to work someone will have had to compile the module for the specific version of Linux and PHP that you are using and deploy it to a repository. It can be sometimes necessary to add a specific repository to the list accessed by apt to get this to work. When it works first time it's great - and it's easy. If it doesn't work it can be problematic.
In this post, rather than describe how to install the Phalcon extension using apt, I will describe how to compile and install it from source code. This has the benefit of being ultimately more flexible - when you compile source code for a platform on that platform there won't be any issues related to incorrect versions of the software.
In addition to this, although it introduces a level of complexity, it's a great learning opportunity. Being able to compile source code on Linux really opens up the world of open source software and sets the learner on a path to becoming a full-stack programmer.
Before I can go about compiling any code I need to address some memory issues. As the VM I created only has 1GB of RAM its a bit short of memory for a task like this. This can be easily remedied by the creation of a swap file. A swap file is a file on the disk that can be used by the OS to store data when it runs short of RAM.
If you are following on from previous steps you might not be in the home folder of the root directory. To change back into the home folder:
The tilde (~) here denotes the home folder. ~ on Linux always refers to a user's home folder. The home folder of the root (system admin) user is probably not the best place to be storing source code. I don't want to get diverted at this point setting up multiple users so let's just say this folder can be deleted once we've built the phalcon module from the source code.
To create the swapfile type
sudo fallocate -l 2G /swapfile
Now you must change the permissions on the file to allow only the owner of the file (root) have read and write permissions - type
sudo chmod 600 /swapfile
Next to make the file into swap space type
sudo mkswap /swapfile
Finally to turn the swap space on type
sudo swapon /swapfile
The swap file is now enabled. It should be noted that if the server is restarted for any reason the swap file will be lost. The swap file can be made permanent but I don't plan to address this issue now. If you're interested in how to do this you can read one of the many excellent guides explaining how to set up fstab.
To download and manage the source code for the PhalconPHP extension we will use git. To ensure git is installed on your server type
sudo apt install git
To "clone" the repository of source code from where it is hosted on github type
git clone https://github.com/phalcon/cphalcon.git
This clones all the code from the repository which contains all the versions and changes. To change directory into the folder you have just downloaded type:
At the time of writing the most recent version is version 4.0.0 alpha. The version used throughout learnphalcon at the moment - which would be the latest stable version at the time of writing is 3.4.2. To reset the head of the repository to point to this version of the code (i.e. to use this version of the code) type the following
git reset --hard 2f55ebb
We're now ready to build the php extension from the source code. To do this change directory into the build subfolder of the cphalcon folder as follows
Before we can initiate the build we need to install the appropriate php developer tools which will be used by the installer when compiling the code. The latest stable version of php at the time of writing is php 7.2. So I'm going to use the dev tools for php 7.2. If you're unsure which version of PHP you are using you can refer to the phpinfo.php page. To install the dev tools type:
sudo apt install php7.2-dev
Initiate the build process as follows.
The build itself will take a few minutes. While the code is compiling you will see this screen.
Once the build has completed successfully you will see the following:
There is now a file called phalcon.so present in the folder /usr/lib/php/20170718. This just happens to be the folder where library files for PHP are stored using my particular flavour/version of Linux (Ubuntu 18.04). Users using a different flavour/version of Linux may end up with modules stored in a different folder.
Now that the code has been compiled we need to make a change to php.ini(as we did on Windows with phalcon.dll) to ensure that PHP picks up on the new module. Making this change to the php settings file is handled slightly differently on Linux so I'll deal with it in the next post.