本次安裝基于系統(tǒng)CentOS Linux release 7.2.1511 (Core)
1.環(huán)境配置
安裝前的環(huán)境準(zhǔn)備:
##安裝make,已安裝,可省略
$yum -y install gcc automake autoconf libtool make
##安裝gcc g++ glibc庫(kù)
$yum -y install gcc gcc-c++ glibc
##安裝所需的包
$yum -y install libmcrypt-devel mhash-devel libxslt-devel
libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel
zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel
ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel
krb5 krb5-devel libidn libidn-devel openssl openssl-devel
安裝的包有些多,以上安裝可分批或者一個(gè)一個(gè)進(jìn)行,如yum -y install libmcrypt-devel,一般來(lái)說(shuō)均可安裝成功,如果失敗可多試幾次。
2.php安裝
下載源碼編譯安裝
##下載源碼
$wget http://am1.php.net/distributions/php-7.2.2.tar.gz
##解壓
$tar -zxvf php-7.2.2.tar.gz
##創(chuàng)建安裝目錄
$mkdir php7
$mv php-7.2.2 php7
$cd php7/php-7.2.2/
## 編譯前配置
$./configure --prefix=/home/test/php7 --with-config-file-path=/home/test/php7/etc --with-curl --with-freetype-dir --with-gd \
--with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex \
--with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --with-bz2 \
--with-mhash --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex \
--enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm \
--enable-xml --enable-zip --with-ldap
##編譯時(shí)間略長(zhǎng),耐心等待
$make
##安裝
$make install
配置php相關(guān)文件
##php.ini,編譯配置時(shí)配在php7/etc目錄下
$cp php-7.2.2/php.ini-development ../etc/php.ini
##php-fpm.conf
$cp ../etc/php-fpm.conf.default ../etc/php-fpm.conf
##www.conf
$cp ../etc/php-fpm.d/www.conf.default ../etc/php-fpm.d/www.conf
修改配置
- php.ini
原cgi.force_redirect = 1被注釋,需要放開(kāi)且修改為cgi.force_redirect = 0 - php-fpm.conf
修改錯(cuò)誤日志路徑error_log = log/php-fpm.log -
www.conf
修改用戶和用戶組選項(xiàng)user = nginx、group = nginx
設(shè)置php環(huán)境變量
$vim /etc/profile
##加上下面這句話
export PATH=/home/test/php7/bin:/home/test/php7/sbin:$PATH
$source /etc/profile
$php -v
PHP 7.2.2 (cli) (built: Feb 10 2018 16:13:50) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
啟動(dòng)php
$/home/test/php7/sbin/php-fpm
##檢查進(jìn)程是否啟動(dòng)
$ps -ef | grep php
root 41963 1 0 18:04 ? 00:00:00 php-fpm: master process (/home/test/php7/etc/php-fpm.conf)
nginx 41964 41963 0 18:04 ? 00:00:00 php-fpm: pool www
nginx 41965 41963 0 18:04 ? 00:00:00 php-fpm: pool www
##檢查端口9000是否活著
$netstat -anp | grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 41963/php-fpm: mast
3.修改nginx配置
Nginx安裝參考:Nginx源碼安裝
找到nginx配置nginx.conf,修改如下,修改后重啟nginx:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
##添加index.php
index index.php index.html index.htm;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
##注意這里有關(guān)php的location注釋要放開(kāi)
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
##注意腳本目錄路徑使用絕對(duì)路徑更清晰
fastcgi_param SCRIPT_FILENAME /home/test/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
4.測(cè)試
在nginx/html目錄下建文件index.php,文件內(nèi)容如下
<?php
phpinfo();
?>
訪問(wèn) http://IP/index.php (IP為服務(wù)器的ip地址)可得如下頁(yè)面即是成功

php.png
注意:nginx的用戶對(duì)html目錄必須有讀寫(xiě)權(quán)限;最好nginx,php的用戶保持統(tǒng)一