Ubuntu Server 14.04 LTS中安装最新版的PHP7.1

由于PHP7.x相对于PHP5.x在性能上有数倍的提升,因此我也趁此将服务器的PHP配置升级一下。

Ubuntu Server 14.04 LTS官方安装包源中默认的是稳定版的PHP5.x系列,如果想要安装最新的PHP7.x需要使用第三方安装包源。

我们这里使用Ubuntu的ppa第三方源ondrej/php,依次执行以下命令即可:

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

之后使用

apt-cache search php7

命令即可查看当前安装包源中的PHP7安装包列表,我服务器上的结果如下:

john@iZwz9jc60a2zsthscfsb8uZ:~$ apt-cache search php7
php-radius - radius client library for PHP
php-http - PECL HTTP module for PHP Extended HTTP Support
php-uploadprogress - file upload progress tracking extension for PHP
php-yaml - YAML-1.1 parser and emitter for PHP
php-mongodb - MongoDB driver for PHP
php7.0-common - documentation, examples and common module for PHP
libapache2-mod-php7.0 - server-side, HTML-embedded scripting language (Apache 2 module)
php7.0-cgi - server-side, HTML-embedded scripting language (CGI binary)
php7.0-cli - command-line interpreter for the PHP scripting language
php7.0-phpdbg - server-side, HTML-embedded scripting language (PHPDBG binary)
php7.0-fpm - server-side, HTML-embedded scripting language (FPM-CGI binary)
libphp7.0-embed - HTML-embedded scripting language (Embedded SAPI library)
php7.0-dev - Files for PHP7.0 module development
php7.0-curl - CURL module for PHP
php7.0-enchant - Enchant module for PHP
php7.0-gd - GD module for PHP
php7.0-gmp - GMP module for PHP
php7.0-imap - IMAP module for PHP
php7.0-interbase - Interbase module for PHP
php7.0-intl - Internationalisation module for PHP
php7.0-ldap - LDAP module for PHP
php7.0-mcrypt - libmcrypt module for PHP
php7.0-readline - readline module for PHP
php7.0-odbc - ODBC module for PHP
php7.0-pgsql - PostgreSQL module for PHP
php7.0-pspell - pspell module for PHP
php7.0-recode - recode module for PHP
php7.0-snmp - SNMP module for PHP
php7.0-tidy - tidy module for PHP
php7.0-xmlrpc - XMLRPC-EPI module for PHP
php7.0-xsl - XSL module for PHP (dummy)
php7.0 - server-side, HTML-embedded scripting language (metapackage)
php7.0-json - JSON module for PHP
php-all-dev - package depending on all supported PHP development packages
php7.0-sybase - Sybase module for PHP
php7.0-sqlite3 - SQLite3 module for PHP
php7.0-mysql - MySQL module for PHP
php7.0-opcache - Zend OpCache module for PHP
php-apcu - APC User Cache for PHP
php-xdebug - Xdebug Module for PHP
php-imagick - Provides a wrapper to the ImageMagick library
php-ssh2 - Bindings for the libssh2 library
php-redis - PHP extension for interfacing with Redis
php-memcached - memcached extension module for PHP, uses libmemcached
php-apcu-bc - APCu Backwards Compatibility Module
php-amqp - AMQP extension for PHP
php7.0-bz2 - bzip2 module for PHP
php-rrd - PHP bindings to rrd tool system
php-uuid - PHP UUID extension
php-memcache - memcache extension module for PHP
php-gmagick - Provides a wrapper to the GraphicsMagick library
php-smbclient - PHP wrapper for libsmbclient
php-zmq - ZeroMQ messaging bindings for PHP
php-igbinary - igbinary PHP serializer
php-msgpack - PHP extension for interfacing with MessagePack
php-geoip - GeoIP module for PHP
php7.0-bcmath - Bcmath module for PHP
php7.0-mbstring - MBSTRING module for PHP
php7.0-soap - SOAP module for PHP
php7.0-xml - DOM, SimpleXML, WDDX, XML, and XSL module for PHP
php7.0-zip - Zip module for PHP
php-tideways - Tideways PHP Profiler Extension
php-yac - YAC (Yet Another Cache) for PHP
php-mailparse - Email message manipulation for PHP
php-libsodium - PHP wrapper for the Sodium cryptographic library
php-oauth - OAuth 1.0 consumer and provider extension
php-gnupg - PHP wrapper around the gpgme library
php-propro - propro module for PHP
php-raphf - raphf module for PHP
php-solr - PHP extension for communicating with Apache Solr server
php-stomp - Streaming Text Oriented Messaging Protocol (STOMP) client module for PHP
php-gearman - PHP wrapper to libgearman
php7.0-dba - DBA module for PHP
libapache2-mod-php7.1 - server-side, HTML-embedded scripting language (Apache 2 module)
libphp7.1-embed - HTML-embedded scripting language (Embedded SAPI library)
php7.1-cgi - server-side, HTML-embedded scripting language (CGI binary)
php7.1-cli - command-line interpreter for the PHP scripting language
php7.1-dev - Files for PHP7.1 module development
php7.1-fpm - server-side, HTML-embedded scripting language (FPM-CGI binary)
php7.1-phpdbg - server-side, HTML-embedded scripting language (PHPDBG binary)
php7.1-bcmath - Bcmath module for PHP
php7.1-bz2 - bzip2 module for PHP
php7.1-common - documentation, examples and common module for PHP
php7.1-curl - CURL module for PHP
php7.1-dba - DBA module for PHP
php7.1-enchant - Enchant module for PHP
php7.1-gd - GD module for PHP
php7.1-gmp - GMP module for PHP
php7.1-imap - IMAP module for PHP
php7.1-interbase - Interbase module for PHP
php7.1-intl - Internationalisation module for PHP
php7.1-json - JSON module for PHP
php7.1-ldap - LDAP module for PHP
php7.1-mbstring - MBSTRING module for PHP
php7.1-mcrypt - libmcrypt module for PHP
php7.1-mysql - MySQL module for PHP
php7.1-odbc - ODBC module for PHP
php7.1-pgsql - PostgreSQL module for PHP
php7.1-pspell - pspell module for PHP
php7.1-readline - readline module for PHP
php7.1-recode - recode module for PHP
php7.1-snmp - SNMP module for PHP
php7.1-soap - SOAP module for PHP
php7.1-sqlite3 - SQLite3 module for PHP
php7.1-sybase - Sybase module for PHP
php7.1-tidy - tidy module for PHP
php7.1-xml - DOM, SimpleXML, WDDX, XML, and XSL module for PHP
php7.1-xmlrpc - XMLRPC-EPI module for PHP
php7.1-zip - Zip module for PHP
php7.1-opcache - Zend OpCache module for PHP
php7.1 - server-side, HTML-embedded scripting language (metapackage)
php7.1-xsl - XSL module for PHP (dummy)
php-phalcon - full-stack PHP framework delivered as a C-extension
php-ds - PHP extension providing efficient data structures for PHP 7

正好看到安装源中有最新的PHP7.1,因此执行以下安装命令:

sudo apt-get install php7.1-fpm php7.1-cli php7.1-common php7.1-curl php7.1-gd php7.1-mbstring php7.1-mysql php7.1-opcache php7.1

接着我们修改nginx的配置,将PHP5的unix socket修改为PHP7.1的unix socket地址,例如我的wordpress的nginx配置修改如下:

server {
    listen      80;
    root        /home/john/www/wordpress;
    index       index.html index.htm index.php;
    server_name johng.cn;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {
        expires 1d;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #fastcgi_pass            unix:/var/run/php5-fpm.sock;
        fastcgi_pass            unix:/var/run/php/php7.1-fpm.sock;
        fastcgi_index           index.php;
        include                 fastcgi_params;
    }
}

重启nginx即可生效:

sudo service nginx restart

 

 

针对于大部分PHP应用来说,以上安装的基础PHP7.1环境即可够用,但是我的应用还需要Redis扩展,但是PHP7.1的安装源中并没有提供Redis的扩展,因此需要自己进行编译安装。

首先安装PHP7.1的开发头文件及编译工具包:

sudo apt-get install php7.1-dev

接着我们需要下载redis的源码进行编译,过程如下:

cd ~
wget https://github.com/edtechd/phpredis/archive/php7.zip
unzip php7.zip
cd phpredis-php7/
phpize7.1
./configure
make
sudo make install

这样我们将会有一个redis.so扩展文件生成到 /usr/lib/php/20160303/redis.so

我们需要修改PHP7.1的 php.ini 配置文件 /etc/php/7.1/fpm/php.ini ,在文件末尾增加 extension=redis.so ,随后重启 php7.1-fpm 即可。

sudo service php7.1-fpm restart

由于PHP7.1的FPM和PHP5.x的FPM是并行运行的,因此我们只需要保留一个即可,于是关闭原有PHP5.x的FPM:

sudo service php5-fpm stop

完毕!

 

 

 

 

 

 

 

 

 

 

Leave a Reply

Your email address will not be published.