Linux安裝二進制PHP7.2

經過性能評測,能夠看出PHP7對性能進行了較大的優化,相比與PHP5.x50%-150%的性能提高,所以,爲了提高咱們服務的響應速度,下降機器負載,須要進行版本升級。php

由於對二進制比較熟悉,因此沒有用yum的方式進行安裝,採用的二進制安裝方式比較靈活,可是由於第一次安裝PHP的高版本,也引入了不少的問題,總而言之,就是在錯誤中不斷摸索錯誤,最終找到一個還能用的道路。html

下載PHP7.2

官方下載地址:nginx

wget http://cn2.php.net/get/php-7.2.13.tar.bz2/from/this/mirror -O php-7.2.13.tar.bz2
tar -xjvf php-7.2.13.tar.bz2
// 用於後面編譯的生成代碼目錄
mkdir php7
cd php-7.2.13

配置PHP

PHP編譯前提供了大量的參數進行配置,包括支持的擴展、執行用戶等,能夠查看參數列表web

咱們進行最簡單的配置,只支持php-fpm管理,由於咱們的PHP是配合Ngnix來進行服務,所以還要指定執行的用戶:php7

./configure --prefix=/home/work/lnmp/php7 --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx
個人第一次編譯報錯: configure: error: OpenSSL version 1.0.1 or greater required.

解決這個問題,須要首先看本身的openssl的版本信息:less

$ openssl version
OpenSSL 1.0.0-fips 29 Mar 2010

所以更新openssl版本:socket

wget https://www.openssl.org/source/openssl-1.1.0j.tar.gz
tar -xzvf openssl-1.1.0j.tar.gz
cd openssl-1.1.0j
./config --prefix=/usr/local/ssl shared zlib-dynamic
make
make install
mv /usr/bin/openssl /usr/bin/openssl1.0.0
ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl

安裝完畢再次配置依然報相同錯誤,所以咱們須要手動指定openssl的位置:php-fpm

// 查看指定openssl的參數
$./configure --help | grep openssl
  --with-openssl=DIR      Include OpenSSL support (requires OpenSSL >= 1.0.1)
  --with-openssl-dir=DIR  FTP: openssl install prefix
  --with-openssl-dir=DIR  SNMP: openssl install prefix

$ ./configure --prefix=/home/work/lnmp/php7 --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-openssl=/usr/bin/openssl

安裝

make && make install 性能

啓動

由於我是升級,因此原有Nginx和代碼以及配置文件都是OK的狀態,可能在這個階段你會遇到不一樣的問題,這個得結合你的狀況進行解決。優化

cd php7
// 複製php.ini和php-fpm.conf到etc/目錄下,這個過程你也能夠本身配置啊
// 生成兩個目錄用於日誌和sock文件保存
mkdir log
mkdir run
sbin/php-fpm -c etc/php.ini -y etc/php-fpm.conf -p .

啓動成功,訪問URL,報錯:502 Bad Gateway

502 Bad Gateway

根據nginx的訪問日誌能夠看出:

$ cat error.log
2018/12/14 10:54:18 [crit] 6260#0: *206 open() "./run/factcgi_temp/0000000015" failed (13: Permission denied) while reading upstream, client: 172.24.162.178, se
rver: , request: "GET /oss/index.php HTTP/1.1", upstream: "fastcgi://unix:run/phpfpm.sock:", host: "xx.xx.
com"

查閱【資料1】【資料2】能夠知道,在PHP老版本里,有一個bug,任何可以鏈接socket文件的用戶能夠經過它執行任何命令,特別是在Ubuntu系統裏容許www-data用戶執行任何代碼。所以最新版本里修復了這個錯誤,但也致使咱們出現了502的問題,所以咱們須要配套升級咱們的配置文件:

// 在nginx.conf頭部添加執行用戶
user www www;
// 在php-fpm.conf裏放棄註釋這3行
; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions.
; Default Values: user and group are set as the running user
;                 mode is set to 0666
listen.owner = www
listen.group = www
listen.mode = 0660

重啓nginxphp-fpm進程,依然報錯:

nginx: [emerg] getpwnam("www") failed

由於咱們沒有加上這個用戶:

useradd -r www

搞定,重啓nginxphp-fpm進程,服務正常。

總結

使用二進制來安裝PHP7.2,在編譯的時候按需加載擴展,若是有問題,咱們能夠從新編譯,也能夠動態擴展。過程比較簡單,但個人服務並無正常服務,由於使用的Yii2.0不可以完美兼容PHP7,我還得對Yii2.0進行升級,以及對自身的代碼進行升級。

參考資料

  1. PHP7.2下載地址:http://php.net/downloads.php
  2. PHP的性能演進:http://www.laruence.com/2016/...
  3. OpenSSl downloads:https://www.openssl.org/source/
  4. OpenSSL 安裝、介紹:https://www.jianshu.com/p/291...
  5. Centos7 安裝 PHP7最新版:https://www.jianshu.com/p/246...
  6. CentOS 7 Linux 安裝PHP7.2 - 編譯安裝:https://blog.csdn.net/ai_zxc/...
  7. nginx error connect to php5-fpm.sock failed (13: Permission denied):https://stackoverflow.com/que...
  8. nginx安裝 nginx: [emerg] getpwnam(「www」) failed 錯誤:https://blog.csdn.net/justdoi...
相關文章
相關標籤/搜索