I have little knowledge of how to configure Apache
and the server. Currently, my server is running a version of PHP 5.X
I would like to upgrade to PHP 7
but there would be a lot of projects that would need to be upgraded.
So here's my question :
Can I specify a version of PHP for a specific folder on my server? For example, I have 20 projects in /var/www/html
All are working on PHP 5.
For my new project, could I force the directory "project 21" to use PHP7
(while the others still use PHP5
)?
Thanks.
It's a bit old question, but here is how to do it, you need to use php-fpm version and fast CGI to do it.
You can do it for example with virtual host config. This example is fitting if you have a root and inside the root multiple websites (from a browser you reach it like this: www.domain.com/site1) this is from my dev environment, so the settings are not fully fit for production:
<VirtualHost *:8080>
#ServerName www.example.com
<Directory /home/my_user/environment>
Options Indexes FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
AllowOverride All
Require all granted
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler "proxy:unix:/run/php/php7.3-fpm.sock|fcgi://localhost"
</FilesMatch>
</Directory>
<Directory /home/my_user/environment/site1>
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler "proxy:unix:/run/php/php5.6-fpm.sock|fcgi://localhost"
</FilesMatch>
</Directory>
<Directory /home/my_user/environment/site2>
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost"
</FilesMatch>
</Directory>
ServerAdmin webmaster@localhost
DocumentRoot /home/my_user/environment
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
AccessFileName .htaccess
</VirtualHost>
Or if every site has its own domain you can use multiple vhosts:
<VirtualHost *:8080>
#ServerName www.example.com
<Directory /home/my_user/environment>
Options Indexes FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
AllowOverride All
Require all granted
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler "proxy:unix:/run/php/php7.3-fpm.sock|fcgi://localhost"
</FilesMatch>
</Directory>
ServerAdmin webmaster@localhost
DocumentRoot /home/my_user/environment
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
AccessFileName .htaccess
</VirtualHost>
<VirtualHost *:8080>
ServerName site1.localhost
DocumentRoot "/home/my_user/environment/site1"
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler "proxy:unix:/run/php/php5.6-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>
<VirtualHost *:8080>
ServerName site2.localhost
DocumentRoot "/home/my_user/environment/site2"
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>
In both examples, the default PHP is 7.3, while site1 runs from 5.6 and site2 from 7.2.
The difference between the two is that you have a domain that only points to that website or it points to multiple and just the route determinate which website should be displayed.
Side note, if you have multiple virtual hosts files, apache will choose the most specific one, that's what we using out in the second example.
This config represents ubuntu/debian systems, i.e. centos the sockets placed in a different location.
All used PHP version config needs to be enabled via a2enconfig command before you can use it.* EDIT: *Actually not. I have all disabled and just the vhost determinate which PHP needs to run on which directory/site. I had to do this because I have a simple script that changes the default PHP version.
But it also possible without a virtual host, in this case, you need something like this between the config files:
<Directory /home/my_user/environment>
Options Indexes FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
AllowOverride All
Require all granted
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler "proxy:unix:/run/php/php7.3-fpm.sock|fcgi://localhost"
</FilesMatch>
</Directory>
<Directory /home/my_user/environment/site1>
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler "proxy:unix:/run/php/php5.6-fpm.sock|fcgi://localhost"
</FilesMatch>
</Directory>
<Directory /home/my_user/environment/site2>
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost"
</FilesMatch>
</Directory>
Some other notes that my apache root is in my user's home directory/environment. So I can easily access my web server files. For this I running apache as my user and group to prevent file permission issues.
You may also need to change the port where the apache runs (in my example 8080).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With