近期终于忍痛将所有的代码从SAE切换到了个人的vps,部署过程中发现,ubuntu 16默认支持的PHP版本为PHP 7,但是由于本人的PHP写的比较早,用到了mysql相关的内容,因此为了降低成本,不得不安装PHP 5.X系列。经过一番折腾终于解决问题,这里附上对应的过程。
服务器:
$ cat /proc/version
Linux version 4.8.3-x86_64-linode76 (maker@build) (gcc version 4.7.2 (Debian 4.7.2-5) ) #1 SMP Thu Oct 20 19:05:39 EDT 2016
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.1 LTS
Release: 16.04
Codename: xenial
nginx版本:
$ nginx -v
nginx version: nginx/1.10.1
最终安装PHP版本:
$ /usr/bin/php5.6 -v
PHP 5.6.28-1+deb.sury.org~xenial+1 (cli)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
with Xdebug v2.4.1, Copyright (c) 2002-2016, by Derick Rethans
$ /usr/bin/php7.0 -v
PHP 7.0.13-1+deb.sury.org~xenial+1 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.13-1+deb.sury.org~xenial+1, Copyright (c) 1999-2016, by Zend Technologies
with Xdebug v2.4.1, Copyright (c) 2002-2016, by Derick Rethans
卸载所有相关模块
$ sudo dpkg -l | grep php | awk '{print $2}' | sudo xargs apt-get remove
删除关联
$ sudo find /etc -name "*php*" | xargs rm -rf
清除残留信息
$ sudo dpkg -l |grep ^rc|awk '{print $2}' | sudo xargs dpkg -P
安装PHP及相关依赖
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update
$ sudo apt-get install php7.0 php5.6 php-pear php-xdebug php5.6-xml php5.6-json php5.6-mbstring php5.6-mysql php5.6-readline php-gettext libapache2-mod-php5.6 libapache2-mod-php7.0 php-memcache memcached php-memcached
切换PHP版本
$ sudo a2dismod php7.0 ; sudo a2enmod php5.6 ;
这一步主要是修改nginx的配置中关于server相关配置中的fastcgi_pass,需要结合服务器的配置做对应的修改。某个host的配置修改后如下:
server {
listen 80;
listen [::]:80;
server_name example.com;
root /example/com;
index index.html index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
fastcgi_param SCRIPT_FILENAME /Users/zixie/web/show/1/$fastcgi_script_name;
}
}
部分设备可能文件目录略有不同,可以通过find来找到对应的文件目录
$ sudo find / -name php5.6-fpm.sock
/run/php/php5.6-fpm.sock
修改配置完成以后,重启php-fpm,命令如下
$ sudo service /usr/sbin/php-fpm5.6 restart
部分设备可能安装目录略有不同,可以通过whereis来找到对应的安装目录
$ whereis php-fpm
php-fpm: /usr/sbin/php-fpm5.6
当php-fpm重启以后,就可以重启nginx,nginx重启以后,即可生效
$ sudo systemctl restart nginx
当所有模块都已经OK以后,可以通过一个简单的PHP文件来检查模块安装是否OK,配置是否正确。参考如下:
<?php
ini_set('display_errors', true);
error_reporting(E_ERROR);
// 测试mem
$memLink = new Memcache;
$memLink->connect('127.0.0.1', '11211');
$memLink->set("testkey","testValue",0,3600*24);
var_dump($memLink->get("testkey"));
//测试mysql
$link = mysql_connect('localhost', 'user', 'password');
mysql_select_db('dbname', $link);
mysql_query("set names utf8");
$query = "select * from t_table";
$result = mysql_query($query,$link);
printf("Select returned %d rows.\n", mysql_num_rows($result));
//测试XML
$note=<<<XML
<note>
<to>temp@bihe0832.com</to>
<from>blog@bihe0832.com/from>
<heading>Test</heading>
<body>This is a test e-mail</body>
</note>
XML;
$xml=simplexml_load_string($note);
print_r($xml);
//测试json
$testArray = array();
$testArray["name"] = "zixie";
$testArray["mail"] = "temp@bihe0832.com";
var_dump($testArray);
$testJson = json_encode($testArray);
var_dump($testJson);
?>
以上代码对应github地址:https://github.com/bihe0832/MyWeb/blob/master/testPHP/index.php
命令行查看
$ php -i | grep "php.ini"
Configuration File (php.ini) Path => /etc/php/5.6/cli
Loaded Configuration File => /etc/php/5.6/cli/php.ini
$ php -i | grep "display_error"
display_errors => Off => Off
xdebug.force_display_errors => Off => Off