How can I install phalcon (or any custom PHP module/extension) on my shared cPanel web host? [closed]

Solution 1:

Your web host may install custom extensions for you, it can't hurt to ask.

But most, generally, will recommend you upgrade to a VPS which allows you full root access, the ability to install anything you want, and less risk that you will negatively impact performance for other users of the server. VPSs can require extra administration effort and technical knowledge unless you opt for a "Managed VPS" which usually adds a significant monthly cost.

And so, assuming my host would not want to enable a custom extension for me, I set out to discover if I could do it on my own. I found a way of doing so without even needing SSH or command-line access.

Step 1: Obtain phalcon.so

You need to obtain the correct build of phalcon.so for your server environment and version of PHP.

cPanel generally runs on CentOS, Red Hat Enterprise Linux (RHEL), or CloudLinux. The phalcon site offers some binary versions for download, but sadly nothing for CentOS/RHEL.

Here are the files I compiled for CentOS 64bit with PHP 5.3 and 5.4. You're welcome to try that if you like until others make binaries available, or see below for instructions on how to compile your own.

https://dl.dropboxusercontent.com/u/9911892/phalcon/1.3.2/PHP5.3/phalcon.so https://dl.dropboxusercontent.com/u/9911892/phalcon/1.3.2/PHP5.4/phalcon.so

Upload this via FTP to your home directory.

Step 2: Enable the extension

You need to add the following line to php.ini:

extension = /home/__USERNAME__/phalcon.so

(Replace __USERNAME__ with your actual cPanel username)

I'm not sure whether all cPanel hosts are like this, but my host VentraIP allows you to put a custom php.ini or .user.ini file in your public_html folder which overrides the default PHP settings.

You can get a hint at whether this is possible by uploading <?php phpinfo(); to a file on your server and call it from your browser. Hopefully you'll have a line like the one below...

custom PHP.ini file

Otherwise you might have to use .user.ini files which are shown like this...

.user.ini file PHP

Note that with .user.ini files, these are commonly cached by the server for 300 seconds, so if you make a change to one, it may take up to 5 minutes for the change to take effect.

Step 3: Verify that it works

Refresh your phpinfo() page in the browser. If phalcon has loaded correctly, you will find an additional section displayed that looks like this:

phalcon phpinfo extension enabled

If you've got this far, hooray!

Not working?

Enable error logging by adding error_log = /home/__USERNAME__/error.log to your php.ini or .user.ini and refresh the phpinfo() file. Then check the log or any obvious errors. If you're not getting any error and phalcon is not showing up, then it's likely that your server is not reading your ini file correctly. Is it definitely in your public_html folder? Have you tried both php.ini and .user.ini formats?

On my first try I started getting this error appearing in my logs:
PHP Warning: PHP Startup: Unable to load dynamic library '/home/yumpstag/phalcon.so' - /home/yumpstag/phalcon.so: undefined symbol: output_globals in Unknown on line 0

I found out that I had the wrong version of phalcon.so (built for PHP 5.3 instead of 5.4) and so had to recompile it.

How to compile phalcon.so (or another extension) yourself

Chances are, you won't be able to run a compiler on your cPanel server without root access, so you'll need to find a similar server to do this on. Here's what I did:

  • Downloaded a CentOS 6 VirtualBox image from here (or see the full list here)
  • Boot it up in VirtualBox
  • Login with root / reverse
  • (If some keyboard characters aren't showing up right, type loadkeys us)

The next step is to ensure you have the correct major version of PHP installed, this needs to match what you're using on your cPanel web host. If you want a different version of PHP other than the default that CentOS supports (PHP 5.3 at the time of writing), try this:

sudo rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
sudo yum install php54w-devel        # FOR PHP 5.4 ONLY

(Otherwise, to go with the CentOS default version:)

sudo yum install php-devel           # FOR PHP 5.3 ONLY

Then install the remaining prerequisites:

sudo yum install php-mysql gcc libtool git

Fetch phalcon and compile:

git clone --depth=1 git://github.com/phalcon/cphalcon.git
cd cphalcon/build
sudo ./install

Once it has finished, it will tell you where the phalcon.so file is. You can then upload that via FTP to your cPanel home directory.

sudo yum install ftp
ftp myhostname.com
put /usr/lib64/php/modules/phalcon.so /phalcon.so

Then go back to steps 2 and 3 to complete the process.

Hope that helps some others. Please comment below if you have any additional tips to offer.