Ubuntu php multi-version coexistence switch

When doing development, because the PHP version developed locally is inconsistent with the PHP version published online, it is easy to find some bugs caused by the version influence after going online, but I don’t want to change the local PHP version again. There are so many versions Coexistence is easy! If necessary, switch to the specified version for testing, and it will be OK to go online if there is no problem!

The LMAP environment installation record is as follows:

sudo apt-get install -y apache2

Installation: mysql5.7 (included with ubuntu16.04)
sudo apt instal -y mysql-server mysql-client libmysqlclient-dev mysql-workbench

安装:php5.6
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install -y php5.6-common php5.6-mbstring php5.6-mcrypt php5.6-mysql php5.6-xml php5.6-gd php5.6-curl php5.6-json php5.6-fpm php5.6-zip php5.6-mcrypt libapache2-mod-php5.6


安装:php7.0
sudo apt-get install -y php7.0-common php7.0-mbstring php7.0-mcrypt php7.0-mysql php7.0-xml php7.0-gd php7.0-curl php7.0-json php7.0-fpm php7.0-zip php7.0-mcrypt libapache2-mod-php7.0


安装:php7.1
sudo apt-get install -y php7.1-common php7.1-mbstring php7.1-mcrypt php7.1-mysql php7.1-xml php7.1-gd php7.1-curl php7.1-json php7.1-fpm php7.1-zip php7.1-mcrypt libapache2-mod-php7.1

#开启重写转向
sudo a2enmod rewrite
sudo a2enmod headers至此安装完成

 

重启:sudo service apache2 restart

 

 

sudo vi .bashrc #Edit
bashrc to add custom commands to facilitate switching between different php versions
alias php56='sudo a2dismod php7.0 && sudo a2dismod php7.1 && sudo a2enmod php5.6 && sudo service apache2 restart'
alias php70= 'sudo a2dismod php5.6 && sudo a2dismod php7.1 && sudo a2enmod php7.0 && sudo service apache2 restart'
alias php71='sudo a2dismod php5.6 && sudo a2dismod php7.0 && sudo a2enmod php7.1 && sudo service apache2 restart '

After the installation is complete, restart the computer, the default is to run the 5.6 version of php (php-v under the command line shows the highest version 7.1)

There is a folder with the corresponding version number in the /etc/php directory, edit the corresponding php.ini to configure the corresponding php version, php70 can be switched to the php7 version under the command line, and others are similar

I use ubuntu16.04, and test 14.04 is also available!

--------------------Dividing line----------------------

Since there are multiple versions installed, is it possible to have different websites load different php versions?

Searching on the Internet, many of them use nginx to handle multiple sites with different php versions, but I really don't want to bother to install another nginx. Basically, they all use proxy forwarding. Apache itself has a proxy, so using Apache itself is not enough. Okay

Personal development is customary to use virtual host, so, configure the directory of virtual host

 

 

#The following configuration is placed at the end of the file, and the website behind this configuration will also reference this php version

<VirtualHost *:80>

        ServerName my.xxx.com

        DocumentRoot /var/www/html/xxx
####Load different php versions####

 

        <FilesMatch \.php$>
            SetHandler "proxy:unix:/run/php/php7.0-fpm.sock|fcgi://localhost"
        </FilesMatch>

####Load different php versions####

</VirtualHost>
When done, save!
At the same time, enable the following two modules
sudo a2enmod proxy proxy_fcgi

 

重启:sudo service apache2 restart

Note that the apahce version is only valid if the version is 2.4.10 or above. The apache of my ubuntu14.04 system is version 2.4.7, so upgrade to the latest version

 

sudo add-apt-repository ppa:ondrej/apache2
sudo apt-get update
sudo apt-get dist-upgrade

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326857961&siteId=291194637