centos7 源碼編譯安裝 php

準備工做

  • 下載 PHP 源碼包並解壓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 源碼包目錄html

    $ cd php-7.2.19

配置和構建 PHP

  • 經常使用配置項及其說明 若是看着麻煩能夠直接看下面的總結步驟mysql

    --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 支持
  • 一些不經常使用的選項nginx

    --enable-debug 開啓 debug 模式
  • 執行 configure 配置 若是看着麻煩能夠直接看下面的總結步驟sql

    $ ./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
  • 安裝總結vim

    $ 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 # 查看狀態
相關文章
相關標籤/搜索