You get a bonus - 1 coin for daily activity. Now you have 1 coin

Individual PHP settings for sites

Lecture



This article describes the creation of a separate version of PHP on the account, which allows you to test or launch a new site without global PHP edits for the account as a whole.

Quite often, there may be situations when it is necessary that PHP files in a specific directory are processed by a PHP interpreter with a different version. For example, this may be required to test a new version of a site or if there are several sites on one account that require different PHP settings.

This article describes how to configure an arbitrary number of independent versions of PHP to work within a single account. The essence of the method is to use your own CGI handler (wrapper). In fact, a CGI script will be created, the only task of which will be to correctly set environment variables and launch the PHP interpreter of the required version. Then, using a single directive in the .htaccess file, the web server will be instructed that all files with the .php extension within a specific directory should be processed by this CGI script.

Consider an example of a solution to this problem: let site1.ru be required to configure script processing using PHP 5.2, while the rest of the account sites continue to work with the version specified in the hosting control panel.

  1. Log in to the hosting server using the SSH access details. To establish a connection, you can use the PUTTY program and instructions from the article SSH access, authorization by key, use SFTP.

  2. Create a file with a CGI script (native PHP interpreter):

     touch ~/site1.ru/cgi-bin/php5.2 && chmod 755 ~/site1.ru/cgi-bin/php5.2 
  3. The created file must be filled with the following text:

     #!/bin/sh export PHPRC="/home/uXXXXX/site1.ru" exec /usr/bin/php-cgi5.2 -- "$@" 

    here:

    • uXXXXX is the name of your user on the hosting server. In most cases, matches the login
    • /usr/bin/php-cgi5.2 - PHP v5.2 itself
    • PHPRC is an environment variable indicating the PHP directory with the php.ini configuration file

    The list of PHP versions installed on the server can be viewed using the following command:

     ls /usr/bin/php-cgi* 
  4. Copy the configuration file php.ini from the directory with the settings of the main version of PHP:

     cp ~/etc/php/php.ini ~/site1.ru/ 
  5. In the copied file, we need to correct the value of the parameter extension_dir - we need to replace the current value with the following:

     /usr/lib/php5.2 

    where the target directory name is determined based on the version number selected. For example, for PHP 5.3, the target directory with extensions will be php5.3 .

  6. Now it is necessary to inform the Apache web server that we will use our own wrapper for processing PHP files. Create a .htaccess file in the directory with the site name site1.ru and write the following directive into it:

     Action application/x-httpd-php "/cgi-bin/php5.2" 

    And we will create another .htaccess in the site1.ru/cgi-bin directory with the following text:

     <Files "php5.2"> SetHandler fcgid-script </Files> 
  7. Check the performance of the configuration. To do this, create in the directory ~/site1.ru/www/ file phpinfo.php with the following contents:

     <?php phpinfo(); ?> 
  8. Referring to the created file through the browser, make sure that you use your own PHP configuration with version 5.2. This means that the settings are correct.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Running server side scripts using PHP as an example (LAMP)

Terms: Running server side scripts using PHP as an example (LAMP)