背景:本人博客自2014年上線以來,一直使用阿里云
ECS
最低配的實例,由于最近阿里云ECS
進行了升級遷移,原來的低配實例已經不存在了,升級后實例的配置有所提升,當然價格更高了,為了更好的發揮服務器性能,所以就想利用空閑時間對整站進行升級,包含阿里云ecs更換系統盤
,MySQL 5.7.19 編譯安裝與配置
,Nginx 1.12.1 編譯安裝與配置
,PHP 7.1.9 編譯安裝與配置
等。
服務器環境
CentOS 6.3 64位 全新純凈的系統
/1核1GB
/經典網絡 1MB
進入PHP官網下載頁面,如果你需要下載php-7.1.9.tar.gz
版本,請點擊此處
進入/usr/local/src
目錄,一般我喜歡把下載的文件放在此目錄,根據自己的喜好設定
[root@iZ2864f6btwZ src]# cd /usr/local/src
使用wget下載php文件,如果wget沒有安裝,yum -y install wget
即可安裝
[root@iZ2864f6btwZ src]# wget http://cn2.php.net/distributions/php-7.1.9.tar.gz
安裝編譯所需的常用組件和依賴包 [ 參考于網絡博客 ]
[root@iZ2864f6btwZ src]# yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel libmcrypt mcrypt mhash
創建php用戶組和用戶,用來運行php, -g
指定用戶組, -r
創建系統用戶
[root@iZ2864f6btwZ src]# groupadd php
[root@iZ2864f6btwZ src]# useradd -r -g php -s /bin/false -M php
解壓php,進入 php-7.1.9 目錄
[root@iZ2864f6btwZ src]# tar zxvf php-7.1.9.tar.gz && cd php-7.1.9
注意:
部分源碼執行./buildconf
提示如下信息,說明你下載的是發行版本,不需要再執行此命令生成配置文件[root@iZ2864f6btwZ php-7.1.9]# ./buildconf You should not run buildconf in a release package. use buildconf --force to override this check.
生成Makefile文件,所有擴展參考于網絡博客,可根據個人需要安裝相應的擴展
[root@iZ2864f6btwZ php-7.1.9]# ./configure \
--prefix=/usr/local/php \
--exec-prefix=/usr/local/php \
--bindir=/usr/local/php/bin \
--sbindir=/usr/local/php/sbin \
--includedir=/usr/local/php/include \
--libdir=/usr/local/php/lib/php \
--mandir=/usr/local/php/php/man \
--with-config-file-path=/usr/local/php/etc \
--with-mysql-sock=/var/run/mysql/mysql.sock \
--with-mcrypt=/usr/include \
--with-mhash \
--with-openssl \
--with-mysql=shared,mysqlnd \
--with-mysqli=shared,mysqlnd \
--with-pdo-mysql=shared,mysqlnd \
--with-gd \
--with-iconv \
--with-zlib \
--enable-zip \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-xml \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--enable-gd-native-ttf \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg-dir \
--with-freetype-dir \
--enable-opcache \
--enable-fpm \
--enable-fastcgi \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--without-gdbm \
注意:
php-fpm用戶和用戶組必須與nginx用戶和用戶對應--with-fpm-user=nginx \ --with-fpm-group=nginx \
編譯并安裝
[root@iZ2864f6btwZ php-7.1.9]# make && make install
注意:
檢查安裝后的目錄和文件,查看相應的擴展文件是否存在[root@iZ2864f6btwZ php-7.1.9]# cd /usr/local/php/lib/php/extensions/no-debug-non-zts-20160303/ [root@iZ2864f6btwZ no-debug-non-zts-20160303]# ll total 7608 -rwxr-xr-x 1 root root 1375334 Sep 24 23:56 mysqli.a -rwxr-xr-x 1 root root 648840 Sep 24 23:56 mysqli.so -rwxr-xr-x 1 root root 3391640 Sep 24 23:56 opcache.a -rwxr-xr-x 1 root root 1631520 Sep 24 23:56 opcache.so -rwxr-xr-x 1 root root 497702 Sep 24 23:56 pdo_mysql.a -rwxr-xr-x 1 root root 229568 Sep 24 23:56 pdo_mysql.so [root@iZ2864f6btwZ no-debug-non-zts-20160303]#
將 php 添加到環境變量,新建/etc/profile.d/php7.sh
文件,在php7.sh
文件中添加export PATH=/usr/local/php/bin:/usr/local/php/sbin:$PATH
[root@iZ2864f6btwZ no-debug-non-zts-20160303]# vim /etc/profile.d/php7.sh
[root@iZ2864f6btwZ no-debug-non-zts-20160303]# source /etc/profile.d/php7.sh
查看PHP版本
[root@iZ2864f6btwZ no-debug-non-zts-20160303]# php -v
PHP 7.1.9 (cli) (built: Sep 24 2017 23:55:54) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
[root@iZ2864f6btwZ no-debug-non-zts-20160303]#
配置php.ini
文件,從php源碼拷貝php.ini-production
到/usr/local/php/etc/
目錄,編譯php.ini
文件,一般只需要設置擴展目錄及相應的擴展和時區即可
[root@iZ2864f6btwZ etc]# cp /usr/local/src/php-7.1.9/php.ini-production /usr/local/php/etc/php.ini
[root@iZ2864f6btwZ etc]# vim /usr/local/php/etc/php.ini
...
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20160303"
zend_extension=opcache.so
extension=mysqli.so
extension=pdo_mysql.so
...
date.timezone = PRC
...
[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1
...
設置php-fpm.conf
文件,此文件是php-fpm運行配置文件,先將php-fpm
腳本文件拷貝到/ect/init.d/
目錄并給出執行權限,并添加到開機自動啟動,編譯php-fpm.conf
,設置好擴展配置文件的路徑和錯誤日志保存路徑
[root@iZ2864f6btwZ etc]# cp /usr/local/src/php-7.1.9/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@iZ2864f6btwZ etc]# chmod +x /etc/init.d/php-fpm
[root@iZ2864f6btwZ etc]# chkconfig --add php-fpm
[root@iZ2864f6btwZ etc]# chkconfig php-fpm on
[root@iZ2864f6btwZ etc]#
[root@iZ2864f6btwZ etc]# cp php-fpm.conf.default php-fpm.conf
[root@iZ2864f6btwZ etc]# vim php-fpm.conf
...
error_log = /var/log/php-fpm/error.log
....
include=/usr/local/php/etc/php-fpm.d/*.conf
...
設置www.conf
文件,此文件是php-fpm運行的擴展配置文件
[root@iZ2864f6btwZ etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf
[root@iZ2864f6btwZ etc]# vim php-fpm.d/www.conf
...
user = nginx
group = nginx
...
listen = /var/run/php-fpm/php-fpm.sock
...
slowlog = /var/log/php-fpm/$pool.log.slow
; The timeout for serving a single request after which a PHP backtrace will be
; dumped to the 'slowlog' file. A value of '0s' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
request_slowlog_timeout = 1m
...
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
...
注意:
此配置文件參考于網絡博客,請務必注意以上user
,group
,listen
,slowlog
,request_slowlog_timeout
,php_value
等位置的配置,listen= /var/run/php-fpm/php-fpm.sock
是根據個人喜愛來設定的,你也可以配置成listen = 127.0.0.1:9000
,只不過到時候nginx.conf
配置文件里面的fastcgi_pass
指定的監聽方式不同,后續步驟整合php和nginx將會提及到。
設置php-fpm運行目錄及相關目錄和權限
[root@iZ2864f6btwZ etc]# mkdir -p /var/log/php-fpm /var/run/php-fpm
[root@iZ2864f6btwZ etc]# chown nginx:nginx -R /var/run/php-fpm/
[root@iZ2864f6btwZ etc]# ll -d /var/run/php-fpm/
drwxr-xr-x 2 nginx nginx 40 Sep 25 10:30 /var/run/php-fpm/
[root@iZ2864f6btwZ etc]#
注意:
由于之前我們在www.conf
中使用的的user
和group
來運行php-fpm的,所以/var/run/php-fpm
運行時的目錄一定要把擁有者改變為相應的用戶和組,即nginx
用戶和nginx
組,否則啟動php-fpm時會報錯。
在 /etc/init.d/php-fpm
中添加如下代碼,讓php-fpm運行目錄自動創建并修改權限
...
php_fpm_log="/var/log/php-fpm"
php_fpm_run="/var/run/php-fpm"
if [ ! -d $php_fpm_log ];then
mkdir -p $php_fpm_log
chown -R nginx.nginx $php_fpm_run
fi
if [ ! -d $php_fpm_run ];then
mkdir -p $php_fpm_run
chown -R nginx.nginx $php_fpm_run
fi
chown -R nginx:nginx /var/run/php-fpm/
...
設置session的存儲目錄
[root@iZ2864f6btwZ etc]# mkdir -p /var/lib/php/session && chown nginx.nginx -R !$
mkdir -p /var/lib/php/session && chown nginx.nginx -R /var/run/php-fpm/
[root@iZ2864f6btwZ etc]#
注意:
上述命令中!$
為簡寫,意為上次使用的路徑
檢測php-fpm配置啟動php-fpm
進程
[root@iZ2864f6btwZ etc]# php-fpm -t
[25-Sep-2017 10:58:58] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful
[root@iZ2864f6btwZ etc]# service php-fpm start
Starting php-fpm done
[root@iZ2864f6btwZ etc]#
注意:
啟動php-fpm進程后,最好檢查一下php-fpm.sock
的所有者和權限,如果不是nginx更換成nginx[root@iZ2864f6btwZ etc]# chown nginx.nginx -R /var/run/php-fpm/php-fpm.sock [root@iZ2864f6btwZ etc]# ll !$ total 0 srw-rw---- 1 root root 0 Sep 25 13:40 /var/run/php-fpm/php-fpm.sock
配置nginx.conf
文件,添加index.php
索引,并添加php腳本解析引擎
...
location / {
root html;
index index.php index.html index.htm;
autoindex on;
}
...
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
...
include conf.d/*.conf;
...
注意:
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
配置信息必須與www.conf
設置的 listen 保持一致,fastcgi_param 路徑中根據/scripts
替換為$document_root
,不然無法找到php腳本文件。
重啟nginx服務器和php-fpm
[root@iZ2864f6btwZ conf]# service nginx restart
Restarting nginx (via systemctl): [ OK ]
[root@iZ2864f6btwZ conf]#
[root@iZ2864f6btwZ conf]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@iZ2864f6btwZ conf]#
注意:
如果你在啟動 nginx 的時候遇到如下錯誤,請仔細檢查你的nginx.conf
文件是否某個位置少寫了分號或是配置文件語法是否有誤[root@iZ2864f6btwZ conf]# service nginx restart Starting nginx (via systemctl): Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details. [FAILED]
測試訪問php文件是否正常,在網站根目錄新建info.php
文件,在瀏覽器地址欄輸入 http://服務器ip/info.php
[root@iZ2864f6btwZ etc]# cd /usr/local/nginx/html/
[root@iZ2864f6btwZ html]# ll
total 8
-rw-r--r-- 1 nginx nginx 537 Sep 24 15:07 50x.html
-rw-r--r-- 1 nginx nginx 612 Sep 24 15:07 index.html
[root@iZ2864f6btwZ html]# vim info.php
<?php phpinfo();
結尾:
至此,php 7.1.9 編譯安裝與配置全部完成,有問題的朋友請給我留言,如有毛病,歡迎指正。