准備工作
-
下載 PHP 源碼包並解壓
$ wget https://www.php.net/distributions/php-7.2.19.tar.bz2 $ yum -y install bzip2 # 如果有可以不用安裝 $ tar -jxvf php-7.2.19.tar.bz2
-
進入 PHP 源碼包目錄
$ cd php-7.2.19
配置和構建 PHP
-
常用配置項及其說明 如果看着麻煩可以直接看下面的總結步驟
--prefix=/usr/local/php7 # 配置安裝目錄 --with-config-file-path=/usr/local/php7 # 配置文件 php.ini 的路徑 --enable-sockets # 開啟 socket --enable-fpm # 啟用 fpm 擴展 --enable-cli # 啟用 命令行模式 (從 php 4.3.0 之后這個模塊默認開啟所以可以不用再加此命令) --enable-mbstring # 啟用 mbstring 庫 --enable-pcntl # 啟用 pcntl (僅 CLI / CGI) --enable-soap # 啟用 soap --enable-opcache # 開啟 opcache 緩存 --disable-fileinfo # 禁用 fileinfo (由於 5.3+ 之后已經不再持續維護了,但默認是開啟的,所以還是禁止了吧)(1G以下內存服務器直接關了吧) --disable-rpath #禁用在搜索路徑中傳遞其他運行庫。 --with-mysqli # 啟用 mysqli 擴展 --with-pdo-mysql # 啟用 pdo 擴展 --with-iconv-dir # 啟用 XMLRPC-EPI 字符編碼轉換 擴展 --with-openssl # 啟用 openssl 擴展 (需要 openssl openssl-devel) --with-fpm-user=www #設定 fpm 所屬的用戶 --with-fpm-group=www #設定 fpm 所屬的組別 --with-curl # 啟用 curl 擴展 --with-mhash # 開啟 mhash 基於離散數學原理的不可逆向的php加密方式擴展庫 # GD --with-gd # 啟用 GD 圖片操作 擴展 --with-jpeg-dir # 開啟對 jpeg 圖片的支持 (需要 libjpeg) --with-png-dir # 開啟對 png 圖片支持 (需要 libpng) --with-freetype-dir # 開啟 freetype # 壓縮 --enable-zip # 啟用 zip --with-zlib # 啟用對 zlib 支持 # xml --enable-simplexml # 啟用對 simplexml 支持 --with-libxml-dir # 啟用對 libxml2 支持
-
一些不常用的選項
--enable-debug 開啟 debug 模式
-
執行 configure 配置 如果看着麻煩可以直接看下面的總結步驟
$ ./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php7 --enable-sockets --enable-fpm --enable-cli --enable-mbstring --enable-pcntl --enable-soap --enable-opcache --disable-fileinfo --disable-rpath --with-mysqli --with-pdo-mysql --with-iconv-dir --with-openssl --with-fpm-user=www --with-fpm-group=www --with-curl --with-mhash --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-zip --with-zlib --enable-simplexml --with-libxml-dir === 第 [1] 嘗試 === ... configure: error: libxml2 not found. Please check your libxml2 installation. # 安裝 libxml2 庫 $ yum -y install libxml2 libxml2-devel === 第 [2] 嘗試 === ... configure: error: Cannot find OpenSSL\'s <evp.h> # 安裝 openssl 庫 $ yum -y install openssl openssl-devel === 第 [3] 嘗試 === ... checking for cURL 7.10.5 or greater... configure: error: cURL version 7.10.5 or later is required to compile php with cURL support # 安裝 curl 庫 $ yum -y install libcurl libcurl-devel === 第 [4] 嘗試 === ... configure: error: jpeglib.h not found. # 安裝 libjpeg 順便 把 libpng 也裝上 $ yum -y install libjpeg libjpeg-devel libpng libpng-devel === 第 [5] 嘗試 === ... configure: error: freetype-config not found. # 安裝 freetype 庫 $ yum -y install freetype freetype-devel === 第 [6] 嘗試 === +--------------------------------------------------------------------+ | License: | | This software is subject to the PHP License, available in this | | distribution in the file LICENSE. By continuing this installation | | process, you are bound by the terms of this license agreement. | | If you do not agree with the terms of this license, you must abort | | the installation process at this point. | +--------------------------------------------------------------------+ Thank you for using PHP. ... # 成功 $ make && make install
-
安裝總結
$ wget https://www.php.net/distributions/php-7.2.19.tar.bz2 $ yum -y install bzip2 # 如果有可以不用安裝 $ tar -jxvf php-7.2.19.tar.bz2 $ cd php-7.2.19 $ yum -y install libxml2 libxml2-devel openssl openssl-devel libcurl libcurl-devel install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel # **此處的 —prefix 安裝目錄可以不和我一樣** $ ./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php7 --enable-sockets --enable-fpm --enable-cli --enable-mbstring --enable-pcntl --enable-soap --enable-opcache --disable-fileinfo --disable-rpath --with-mysqli --with-pdo-mysql --with-iconv-dir --with-openssl --with-fpm-user=www --with-fpm-group=www --with-curl --with-mhash --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-zip --with-zlib --enable-simplexml --with-libxml-dir $ make && make install
配置 nginx 支持
$ cp php-7.2.19/php.ini-production /usr/local/php/php.ini # 復制 php.ini 文件到目錄
$ vi /usr/local/nginx/conf/nginx.conf
...
http {
...
server {
...
# 把 [1] 換成 [2]
# 讓 nginx 支持 php 文件
#[1]
location / {
root html;
index index.html index.htm;
}
#[2]
location / {
root html;
index index.php index.html index.htm;
}
...
# 把 [1] 換成 [2]
# 配置監聽 php 端口
# 注意: 要把 /scripts 換成 $document_root
#[1]
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
#[2]
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
...
}
# 重啟 php-fpm
$ pkill -9 php-fpm
$ /usr/local/php7/sbin/php-fpm
配置更簡單的啟動命令
# 復制啟動腳本到 init.d 目錄
$ cp php-7.2.19/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# 增加執行權限
$ chmod +x /etc/init.d/php-fpm
# 配置 php-fpm 文件
$ cd /usr/local/php7/etc/
$ cp php-fpm.conf.default php-fpm.conf
# 進入 php-fpm.conf , 並去除 pid = run/php-fpm.pid 的注釋
$ vim php-fpm.conf
===
...
[global]
; Pid file
; Note: the default prefix is /usr/local/php7/var
; Default Value: none
# 取消下面的注釋
pid = run/php-fpm.pid
...
===
# 復制 www.conf 文件
$ cp php-fpm.d/www.conf.default php-fpm.d/www.conf
# 重啟 php 服務器
$ pkill -9 php-fpm
$ /usr/local/nginx/php7/sbin/php-fpm
# 測試
$ /etc/init.d/php-fpm stop # 停止服務
Gracefully shutting down php-fpm . done
$ /etc/init.d/php-fpm start # 啟動服務
Starting php-fpm done
$ /etc/init.d/php-fpm restart # 重啟服務
Gracefully shutting down php-fpm . done
Starting php-fpm done
- /etc/init.d/php-fpm stop # 停止服務
- /etc/init.d/php-fpm start # 啟動服務
- /etc/init.d/php-fpm restart # 重啟服務
CentOS 7 配置自啟動
# 啟動(因為上面已經配置好啟動腳本了,並且放到了 /etc/init.d 中所以下面命令是可以用的)
# 在 centos7 中 systemctl 是找 /etc/init.d 中的腳本
$ systemctl start php-fpm # 啟動
$ systemctl enable php-fpm # 自啟動
php-fpm.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig php-fpm on # 這里他提示我 php-fpm 並不是系統服務所以不能使用 systemctl 推薦我使用 /sbin/chkconfig php-fpm on 來實現自啟動
$ /sbin/chkconfig php-fpm on 再次重啟即可完成自啟動
# 注: 如果不想配置 php-fpm 服務器化到此已經可以結束了,如果想看服務化可以看以下操作
Centos 7 服務化 php-fpm
# 在 centos 7 之后我們可以使用 systemctl 更好的管理系統服務
# 所以我們也要讓 php-fpm 支持
# 因為 php 7.2 源碼包里面含有 systemctl 所需要的腳本文件
# 我們只要復制過去即可,我們來開始配置
# 進入下載的 php源碼包
$ cd php-7.2.19/sapi/fpm
# 復制其中的 php-fpm.service 到 /usr/lib/systemd/system/
$ cp php-fpm.service /usr/lib/systemd/system/
# 再次使用 systemctl enable php-fpm 進行配置自啟動
$ systemctl enable php-fpm
# 重啟測試一下看看自己服務器的 php-fpm 是否成功運行
- systemctl enable xxxxxx # 配置自啟動
- systemctl stop xxxxx # 停止服務
- systemctl start xxxx # 開啟服務
- systemctl status xxxx # 查看狀態