linux系統PHP7.0編譯安裝升級及WordPress問題解決分享

來源:互聯網
上載者:User

官方給出的新特性如下:

PHP 7.0.0 comes with new version of the Zend Engine with features such as (incomplete list):
Improved performance: PHP 7 is up to twice as fast as PHP 5.6
Consistent 64-bit support
Many fatal errors are now Exceptions
Removal of old and unsupported SAPIs and extensions
The null coalescing operator (??)
Combined comparison Operator (<=>)
Return Type Declarations
Scalar Type Declarations
Anonymous Classes

至於新特性是什麼,百度一下都有大牛給出很詳細的解釋,不過我也看不太懂,但是我看懂了一條:
Improved performance: PHP 7 is up to twice as fast as PHP 5.6
PHP7的效能將是PHP5.6的2倍!
好了,其他就不用看了,單這一條就已經有升級的動力了吧!
之前就用過RC版本,效能確實提高了很多,但是在PHP7.0環境中,Begin主題存在不少問題,由於不是正式版,我也就懶得花時間去解決了。
今天官方發布了正式版,於是利用下班時間給部落格編譯安裝了PHP7.0,且一併解決了Begin主題依然存在的不相容問題,下面簡單的分享下。

一、編譯安裝


以下安裝步驟是在已有PHP5的環境下進行的,不保證能夠順利完成,僅供參考。

①、下載PHP


cd /usr/local/src
#下載安裝包
wget http://cn2.php.net/distributions/php-7.0.0.tar.gz
這是PHP官方的PHP7.0正式版的國內CDN下載地址,可以放心下載。

②、解壓編譯

基本大傢伙都已經安裝了PHP的5.6或更老的版本,所以我們可以編譯安裝到一個新的路徑。

 

#進入安裝目錄
cd php-7.0.0
 
#編譯安裝
#因為是在已經有PHP5.6的環境下安裝,下面的參數基本不會報錯,如果報錯,那麼缺少什麼就補什麼吧!
CFLAGS= CXXFLAGS= ./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php7/etc \
--with-fpm-user=www --with-fpm-group=www --enable-fpm --enable-opcache \
--disable-fileinfo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-jpeg-dir \
--with-iconv-dir=/usr/local --with-freetype-dir  --with-png-dir --with-zlib --disable-rpath \
--with-libxml-dir=/usr --enable-xml  --enable-bcmath --enable-shmop --enable-exif --with-curl \
--enable-sysvsem --enable-inline-optimization  --enable-mbregex --enable-inline-optimization \
--enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl \
--with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-ftp \
--with-gettext --enable-zip --enable-soap --disable-ipv6 --disable-debug
 
#繼續make
make ZEND_EXTRA_LIBS='-liconv'
 
#最後make install完成安裝

make install

上面的編譯安裝啟用了opcache緩衝,如果不需要可以去掉 --enable-opcache,個人推薦使用。
③、設定參數


#以下操作均在php-7.0.0這個安裝目錄下執行
php_install_dir=/usr/local/php7
cp php.ini-production $php_install_dir/etc/php.ini
Mem=`free -m | awk '/Mem:/{print $2}'`
if [ $Mem -gt 1024 -a $Mem -le 1500 ];then
    Memory_limit=192
elif [ $Mem -gt 1500 -a $Mem -le 3500 ];then
    Memory_limit=256
elif [ $Mem -gt 3500 -a $Mem -le 4500 ];then
    Memory_limit=320
elif [ $Mem -gt 4500 ];then
    Memory_limit=448
else
    Memory_limit=128
fi
sed -i "s@^memory_limit.*@memory_limit = ${Memory_limit}M@" $php_install_dir/etc/php.ini
sed -i 's@^output_buffering =@output_buffering = On\noutput_buffering =@' $php_install_dir/etc/php.ini
sed -i 's@^;cgi.fix_pathinfo.*@cgi.fix_pathinfo=0@' $php_install_dir/etc/php.ini
sed -i 's@^short_open_tag = Off@short_open_tag = On@' $php_install_dir/etc/php.ini
sed -i 's@^expose_php = On@expose_php = Off@' $php_install_dir/etc/php.ini
sed -i 's@^request_order.*@request_order = "CGP"@' $php_install_dir/etc/php.ini
sed -i 's@^;date.timezone.*@date.timezone = Asia/Shanghai@' $php_install_dir/etc/php.ini
sed -i 's@^post_max_size.*@post_max_size = 50M@' $php_install_dir/etc/php.ini
sed -i 's@^upload_max_filesize.*@upload_max_filesize = 50M@' $php_install_dir/etc/php.ini
sed -i 's@^;upload_tmp_dir.*@upload_tmp_dir = /tmp@' $php_install_dir/etc/php.ini
sed -i 's@^max_execution_time.*@max_execution_time = 600@' $php_install_dir/etc/php.ini
sed -i 's@^;realpath_cache_size.*@realpath_cache_size = 2M@' $php_install_dir/etc/php.ini
sed -i 's@^disable_functions.*@disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,fsocket,popen@' $php_install_dir/etc/php.ini
sed -i 's@^session.cookie_httponly.*@session.cookie_httponly = 1@' $php_install_dir/etc/php.ini
sed -i 's@^mysqlnd.collect_memory_statistics.*@mysqlnd.collect_memory_statistics = On@' $php_install_dir/etc/php.ini
[ -e /usr/sbin/sendmail ] && sed -i 's@^;sendmail_path.*@sendmail_path = /usr/sbin/sendmail -t -i@' $php_install_dir/etc/php.ini
#如果編譯時間去掉了 --enable-opcache,則以下包含opcache的都請忽略
sed -i 's@^\[opcache\]@[opcache]\nzend_extension=opcache.so@' $php_install_dir/etc/php.ini
sed -i 's@^;opcache.enable=.*@opcache.enable=1@' $php_install_dir/etc/php.ini
sed -i "s@^;opcache.memory_consumption.*@opcache.memory_consumption=$Memory_limit@" $php_install_dir/etc/php.ini
sed -i 's@^;opcache.interned_strings_buffer.*@opcache.interned_strings_buffer=8@' $php_install_dir/etc/php.ini
sed -i 's@^;opcache.max_accelerated_files.*@opcache.max_accelerated_files=4000@' $php_install_dir/etc/php.ini
sed -i 's@^;opcache.revalidate_freq.*@opcache.revalidate_freq=60@' $php_install_dir/etc/php.ini
sed -i 's@^;opcache.save_comments.*@opcache.save_comments=0@' $php_install_dir/etc/php.ini
sed -i 's@^;opcache.fast_shutdown.*@opcache.fast_shutdown=1@' $php_install_dir/etc/php.ini
sed -i 's@^;opcache.validate_timestamps.*@opcache.validate_timestamps=1@' $php_install_dir/etc/php.ini
sed -i 's@^;opcache.enable_cli.*@opcache.enable_cli=1@' $php_install_dir/etc/php.ini
sed -i 's@^;opcache.use_cwd.*@opcache.use_cwd=1@' $php_install_dir/etc/php.ini
sed -i 's@^opcache.max_accelerated_files.*@opcache.max_accelerated_files=100000@' $php_install_dir/etc/php.ini
sed -i 's@^;opcache.max_wasted_percentage.*@opcache.max_wasted_percentage=5@' $php_install_dir/etc/php.ini
sed -i 's@^;opcache.consistency_checks.*@opcache.consistency_checks=0@' $php_install_dir/etc/php.ini
sed -i 's@^;opcache.optimization_level.*@;opcache.optimization_level=0@' $php_install_dir/etc/php.ini
 
# php-fpm直接沿用之前的配置即可
cp -f /usr/local/php/etc/php-fpm.conf /usr/local/php7/etc/
Ps:以上參數等代碼從lnmp一鍵安裝包中提取。
④、版本替換
php 7 已經安裝到了 /usr/local/php7,為了讓2個版本暫時都存在,方便過渡,這裡我們使用軟連結搞定


#停止php-fpm
service php-fpm stop
 
#重新命名php老版本
cd /usr/local
mv /php php5
 
#建立php7的軟連結
ln -s php7 php
 
#啟動php-fpm
service php-fpm start

哦了,做完以上步驟,要是沒報錯基本就已經搞定了,執行一下php --version 應該可以看到版本資訊了


PHP 7.0.0 (cli) (built: Dec  2 2015 19:44:28) ( NTS )
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies

二、問題解決

升級到PHP7之後,網站不出意外的白屏了!但是,不必驚慌,靜下心來看看是什麼問題。
①、不相容某些PHP加密方案
直接編輯wp-config.php,插入如下內容開啟PHP的錯誤顯示:


error_reporting(E_ALL);
ini_set('display_errors', Ƈ');

保險起見,重新整理下php,因為opcache可能有緩衝:
service php-fpm reload
再次重新整理網站,發現原來是鳥哥的Begin加密帶來的錯誤,看來PHP7不支援這個加密形式。好吧,直接找鳥哥要來一份沒有加密的Begin,繼續測試。
②、重複函數衝突
替換最新未加密Begin主題之後,毫無意外,還是報錯...
看了下,原來是鳥哥把歸檔頁面自動更新的那個函數添加到了主題,和我的子主題已有函數存在衝突。
直接注釋其一,搞定!
③、評論沒有內容
解決完上面幾個問題之後,正在享受PHP7帶來的提速快感,突然發現前台和後台都不顯示評論內容了!只顯示評論作者等資訊。
突然想起之前測試PHP7 RC版本的時候就出現過,但是沒有深究直接放棄了。
 
定位方法很簡單,再次開啟PHP所有錯誤顯示,然後重新整理後台評論介面,發現有如下報錯:


原來是preg_replace函數在PHP7中已經棄用了/e 這個參數,改用了 preg_replace_callback 函數。
於是,找到錯誤所指向的檔案及函數,發現是評論貼圖功能,於是把/e 去掉或改成/i 試了下,暫時沒發現有啥問題,估計貼圖功能會有異常吧!
暫時沒空研究如何替換成 preg_replace_callback 函數,後面再具體研究學習下。
三、簡單測試

問題解決了,應該測試下效能了。
不過,一直也沒用過啥專業的效能測試工具,粗人一個,直接目測後台同一個頁面載入效果。
PHP 5.6 載入WordPress後台【所有文章】頁面,頭部響應耗時2.6s


PHP 7.0 載入WordPress後台【所有文章】頁面,頭部響應耗時1.5s


簡單目測已經高下立分!絕對不是心理作用,後台速度提升相當明顯了。
當然,部落格前台我一直是用的純靜態,所以升級php7也只是為了提升自己的後台體驗!
後面有時間,再來詳細深入的測試下php7的效能吧!

 

原文來自:http://zhangge.net/5075.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.