Install Drush and Composer for Global Server Access EA4

Author:
phil
Created:
Wednesday, August 24th, 2016
Last Updated:
Wednesday, August 24th, 2016

Disclaimer: Accessing the information on this page means you agree to the Sites Terms of Service


I use cPanel (centos) with the new EasyApache 4 and a multi-php environment. I struggled with the absolutely horrible documentation offered by "GetComposer" for installing composer and had to muck my way through it. This post is to be read "as-is". I'm simply blogging it in case I end up moving servers or have to re-do something later. The info might be slightly incomplete due to problems I ran into after initially writing the post. I updated it as best as I could. Also, your system might be slightly different. This is for a hosting platform that you have full access to. If you're on a shared host, take a look at my shared hosting write-up which is kinda outdated now that the .phar files are available for use.

‹rant› Why the top level PHP experts are all clamoring to Composer which has the absolute worst installation documentation write up of any garage open source project, is beyond me. Their documentation is out of date, incomplete and flat out sucks when it comes to installing composer on a unix style system. At least the Windows folks have an exe to run to install it for you...
‹/rant›


Several thoughts before we start...

Make sure EA4 has php-cli enabled for the various versions of PHP you want to use. EA4 has a different approach to php-cli than it did in EA3. In EA3, you physically called on php-cli where-as EA4 requires you to call the specific php binary located in /opt/cpanel/ea-phpxx/root/usr/bin/php (where xx = php version. ex. php54, php55 etc). Supposedly, calling on /usr/bin/php is supposed to automatically use whatever handler you're trying to use, but I couldn't get it to work right. I still had to call on the one in /opt/cpanel... etc

The new EA4 extensions aren't very info friendly on the GUI screen. Some of them are straight forward, but others don't give you a clue as to what they are. I ended up enabling the php-process extension (sp?) because it deals with pctnl. I don't know if this needs to be enabled or not. If you're unsure, you can always issue /opt/cpanel/ea-phpxx/root/usr/bin/php -m to check the loaded modules. (again, change xx for the version you want checked)

Drush update will NOT work until you explicitly tell your profile to point at a specific php binary located in /opt/cpanel/ea-phpxx/root/usr/bin/php (xx = php version #). See .bash_profile below for details. This has to do with the whole php-cli thing mentioned above.


Initially, login as root (sudo the following commands etc blah blah blah)

Composer Setup

Note: Newer versions of cPanel and EA4, come with composer already available. You can find it at:

/opt/cpanel/composer/bin

Simply add an alias to it in your .bashrc file:

alias composer='/opt/cpanel/composer/bin/composer'

(see .bash_profile instructions below if you get errors. Errors mean composer probably isn't using the right version of php)

If you don't have composer already installed, you can follow the instructions below to install it manually.

cd
cd tmp

(mkdir ~/tmp if it doesn't exist)

wget --no-check-certificate https://getcomposer.org/installer
mv installer composer-setup.php
php ./composer-setup.php
mv ./composer.phar /usr/local/bin/composer

Composer global installation complete

Drush Global Setup

Note: If you want to control the version of Drush per account, you would be better off to run composer global require drush/drush for each respective account. Skip this section and see later section Drush Per Account Setup

cd
cd tmp

(should still be in tmp anyway...)

wget --no-check-certificate https://s3.amazonaws.com/files.drush.org/drush.phar
chmod +x ./drush.phar
mv ./drush.phar /usr/local/bin/drush

Drush global installation complete

Testing both Composer and Drush

Because of the new EA4 (EasyApache4) ability to use multiple versions of PHP, invoking the standard php from /usr/bin/php will cause all sorts of errors. You must specify the specific php binary from it's installed location (I just use PHP 5.6):

PHP 5.4
/opt/cpanel/ea-php54/root/usr/bin/php

PHP 5.5
/opt/cpanel/ea-php55/root/usr/bin/php

PHP 5.6
/opt/cpanel/ea-php56/root/usr/bin/php

PHP 7.0
/opt/cpanel/ea-php70/root/usr/bin/php

Composer

/opt/cpanel/ea-php56/root/usr/bin/php /usr/local/bin/composer

This should output the list of composer commands

Drush

/opt/cpanel/ea-php56/root/usr/bin/php /usr/local/bin/drush status

This should list the status report of Drush.

Setting it up per server user account

Finally, we need to setup .bash_profile to point to the correct php binary and then add the necessary aliases to the .bashrc file for each user that will need access to it. If you want to set it up globally, look at the stack exchange regarding /etc/profile.d however note that it is recommended to call the php version that is being used per account. (If multiple sites are on the same account, each with different versions of PHP, I would assume use the lowest version of PHP?)

su - [user]

.bash_profile
First we need to manually tell your profile where to use PHP from:

vi .bash_profile

Insert the following into the .bash_profile (change your desired ea-phpxx version)

export PHP_PATH=/opt/cpanel/ea-php54/root/usr/bin
export PATH="$PHP_PATH:$PATH"

It should look something like this when you're finished:

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PHP_PATH=/opt/cpanel/ea-php54/root/usr/bin
export PATH="$PHP_PATH:$PATH"

Exit the .bash_profile file (:qw) and commit the changes:

source .bash_profile

NOTE: you might have to exit your session and log back in for it to work. You can use:

echo $PATH

or

which php

To check the version of PHP your terminal will use. There's also php -v to check the version (should show (cli))

.bashrc
Next, we need to add the aliases:

vi .bashrc

Paste the following into the .bashrc file:

alias composer='/usr/local/bin/composer'
alias drush='/usr/local/bin/drush'

Exit the .bashrc file (:qw) and commit the changes:

source .bashrc

Check the respective commands as a stand-alone command to test and it should be good.

composer
drush status

Initialize Drush

The final step for drush is to initialize it.

drush init

When the prompt asks you if you want to update the .bashrc profile, choose 'y'.

Commit the changes:

source .bashrc

Finally, go into .drush and chmod the drush script files:

cd .drush
chmod +x drush.*.sh

Test drush with drush status

Upgrade Composer

Upgrading composer is simple (assuming you have the alias setup for root / sudo)

composer selfupdate

Upgrade Drush

For the global version of Drush, I honestly have no idea... Since we installed it globally and not through composer, it isn't as easy to update. All I can suggest at this point is to blow away the drush file in /usr/local/bin, and follow the steps above to re-download it and insert it back into /usr/local/bin.

Drush Per Account Setup

If you would rather setup Drush per account, skip the global steps above and instead, navigate to the respective account you want to install Drush into. (This will have to be done manually for each and every account)

This step REQUIRES that composer is properly working.

From the root of your user account, type:

composer global require drush/drush

This will install drush to ~/.composer/vendor/drush/drush

.bash_profile
First we need to manually tell your profile where to use PHP from:

vi .bash_profile

Insert the following into the .bash_profile (change your desired ea-phpxx version)

export PHP_PATH=/opt/cpanel/ea-php54/root/usr/bin
export PATH="$PHP_PATH:$PATH"

It should look something like this when you're finished:

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PHP_PATH=/opt/cpanel/ea-php54/root/usr/bin
export PATH="$PHP_PATH:$PATH"

Exit the .bash_profile file (:qw) and commit the changes:

source .bash_profile

NOTE: you might have to exit your session and log back in for it to work. You can use:

echo $PATH

or

which php

To check the version of PHP your terminal will use. There's also php -v to check the version (should show (cli))

.bashrc
Next, edit the .bashrc file (hopefully you went ahead and copied the whole alias from above to create an alias for composer). We need to point drush to look in the vendor directory. Update your .bashrc to look something like:

alias drush='~/.composer/vendor/drush/drush/drush'

Exit the .bashrc file and commit it:

source .bashrc

Next, initialize drush by typing:

drush init

When the prompt asks you if you want to update the .bashrc profile, choose 'y'.

Commit the changes:

source .bashrc

Finally, go into .drush and chmod the drush script files:

cd .drush
chmod +x drush.*.sh

If all goes well, you should be able to test Drush with:

drush status

Later, you can update to the latest Drush with:

composer global update drush/drush

I think that was it... It seems pretty straight forward, but took me 6+ hrs to figure out that I needed to call on the proper php binary in the .bash_profile by initializing the path at session load. I tried to run the necessary binary inline with the drush alias and never did get it to work. I believe it's because the initial execution runs on the inline method but once drush begins to bootstrap, it looks to the user path directive which isn't explicitly set by default. It would then crap out. PITA.

On another thought, I ended up using the built in composer located in /opt/cpanel/composer/bin and was able to update it (sudo) without a problem. It blew down to the rest of my accounts globally. I might end up setting drush aliases up in the global /etc/profile.d area but for now, I manually set aliases for each account since there are only 4 that need it.

With this setup, the only interaction that will need to take place is updating drush in the future and making sure the .bash_profile php binary is pointing to the necessary version for the account.

Post Comment