我已经在我的Ubuntu 16.04上安装了php 5.6和php 7.1
我知道用Apache作为我的网络服务器,我可以做到
a2enmod php5.6 #to enable php5
a2enmod php7.1 #to enable php7
当我在Apache模块中禁用php7.1并启用php 5.6时,Apache识别出了这个变化,并按预期使用php 5.6解释器。
但是当我从命令行运行内部php web服务器时:
php -S localhost:8888
PHP使用PHP 7处理请求。那么如何在命令行中在php 5.6和php 7.1之间切换呢?
我做了一个bash脚本在Ubuntu上的不同PHP版本之间切换。
希望它能帮助到别人。
剧本如下:
(保存在/usr/local/bin/sphp.sh,不要忘记用命令sudo chmod +x /usr/local/bin/sphp.sh)
#!/bin/bash
# Usage
if [ $# -ne 1 ]; then
echo "Usage: sphp [phpversion]"
echo "Example: sphp 7.2"
exit 1
fi
currentversion="`php -r \"error_reporting(0); echo str_replace('.', '', substr(phpversion(), 0, 3));\"`"
newversion="$1"
majorOld=${currentversion:0:1}
minorOld=${currentversion:1:1}
majorNew=${newversion:0:1}
minorNew=${newversion:2:1}
if [ $? -eq 0 ]; then
if [ "${newversion}" == "${currentversion}" ]; then
echo "PHP version [${newversion}] is already being used"
exit 1
fi
echo "PHP version [$newversion] found"
echo "Switching from [php${currentversion}] to [php${newversion}] ... "
printf "a2dismod php$majorOld.$minorOld ... "
sudo a2dismod "php${majorOld}.${minorOld}"
printf "[OK] and "
printf "a2enmod php${newversion} ... "
sudo a2enmod "php${majorNew}.${minorNew}"
printf "[OK]\n"
printf "update-alternatives ... "
sudo update-alternatives --set php "/usr/bin/php${majorNew}.${minorNew}"
printf "[OK]\n"
sudo service apache2 restart
printf "[OK] apache2 restarted\n"
else
echo "PHP version $majorNew.$minorNew was not found."
echo "Try \`sudo apt install php@${newversion}\` first."
exit 1
fi
echo "DONE!"
您可以创建一个脚本来切换版本:sudo nano switch_php
然后输入这个:
#!/bin/sh
#!/bin/bash
echo "Switching to PHP$1..."
case $1 in
"7")
sudo a2dismod php5.6
sudo a2enmod php7.0
sudo service apache2 restart
sudo ln -sfn /usr/bin/php7.0 /etc/alternatives/php;;
"5.6")
sudo a2dismod php7.0
sudo a2enmod php5.6
sudo service apache2 restart
sudo ln -sfn /usr/bin/php5.6 /etc/alternatives/php;;
esac
echo "Current version: $( php -v | head -n 1 | cut -c-7 )"
退出并保存
使其可执行:sudo chmod +x switch_php
要执行脚本,只需输入./switch_php [VERSION_NUMBER],其中参数为7或5.6
就是这样,您现在可以轻松地从PHP7切换到phph5.6 !