Working with multiple PHP versions on your server

How to activate a Virtual Environment for your PHP site

Moss allows you to specify the PHP version that a website will use. Moreover, you’re free to upgrade/downgrade the PHP version of your websites at will. Hence you may have multiple sites using different PHP versions, even if they’re on the same server.

Moss ensures that the proper version of php and php-fpm is employed to serve your applications and to run your deployment scripts. But what if you need to run something right from your server? How can you make sure the PHP version of your website is being used? Easy, just activate a virtual environment for your site.

Activate your site’s environment
We’ve taken the approach followed by Python’s virtualenv to isolate the PHP environment of an application from another one. While you’re within the virtual environment of a site, any call to php or php-fpm will use the version you set up in Moss for such website.

Enter virtual environment: source the activation script at /home//sites/<site-name>/bin/activate (prompt changes)
Do stuff
Exit virtual environment: run deactivate command (prompt restores)
The following example with a PHP 5.6 site shows how it works:

user@myserver$ cd sites/mysite

user@myserver$ ls bin/
activate composer php php-fpm

user@myserver$ source bin/activate # enter virtualenv

(mysite) user@myserver$ php --version
PHP 5.6.31-6+ubuntu16.04.1+deb.sury.org+1 (cli)

(mysite) user@myserver$ php-fpm --version
PHP 5.6.31-6+ubuntu16.04.1+deb.sury.org+1 (fpm-fcgi)

(mysite) user@myserver$ deactivate # exit virtualenv

user@myserver$