Apache 配置 PHP 一般會預設用 mod_php 的方法安裝, 安裝 Nginx 便會使用 PHP-FPM。但如果在 Apache 不想使用預設的 prefork 作為 MPM (通常基於效能因素), 想使用 worker 或 event, 便需要使用 PHP-FPM 的方法安裝 PHP。
本文會示範在 CentOS 7 安裝 Apache 及 PHP-FPM 的方法。
首先用 yum 安裝 httpd 及 php-fpm:
[root@opencli ~]# yum install httpd httpd-tools mod_ssl php-fpm
Apache 預設使用 mod_mpm_prefork, 改用 mod_mpm_event, 開啟 /etc/httpd/conf.modules.d/00-mpm.conf:
[root@opencli ~]# vi /etc/httpd/conf.modules.d/00-mpm.conf
將 mpm_prefork_module 一行, 在前面加上註解:
# LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
然後將 mpm_event_module 一行前的註解刪除:
LoadModule mpm_worker_module modules/mod_mpm_worker.so
修改好上面的設定後, 現在設定 PHP-FPM, 將所有 PHP 的請求傳送給 PHP-FPM:
[root@opencli ~]# vi /etc/httpd/conf.d/php.conf
以下是 php.conf 的內容:
# Tell the PHP interpreter to handle files with a .php extension.
# Proxy declaration
# we must declare a parameter in here (doesn't matter which) or it'll not register the proxy ahead of time
ProxySet disablereuse=off
# Redirect to the proxy
SetHandler proxy:fcgi://php-fpm
#
# Allow php to handle Multiviews
#
AddType text/html .php
#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php
#
# Uncomment the following lines to allow PHP to pretty-print .phps
# files as PHP source code:
#
#
# SetHandler application/x-httpd-php-source
#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Tell the PHP interpreter to handle files with a .php extension.
# Proxy declaration
# we must declare a parameter in here (doesn't matter which) or it'll not register the proxy ahead of time
ProxySetdisablereuse=off
# Redirect to the proxy
SetHandlerproxy:fcgi://php-fpm
#
# Allow php to handle Multiviews
#
AddTypetext/html.php
#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndexindex.php
#
# Uncomment the following lines to allow PHP to pretty-print .phps
# files as PHP source code:
#
#
# SetHandler application/x-httpd-php-source
#
預設的 php-fpm 設定檔會使用 TCP 連線, 為了有更佳的效能, 改用 socket, 開啟 PHP-FPM 設定檔 /etc/php-fpm.d/www.conf:
[root@opencli ~]# vi /etc/php-fpm.d/www.conf
以下是需要修改的內容:
; listen = 127.0.0.1:9000
listen = /var/run/php-fpm/default.sock
1
2
;listen=127.0.0.1:9000
listen=/var/run/php-fpm/default.sock
listen.allowed_clients = 127.0.0.1
listen.owner = apache
listen.group = apache
listen.mode = 0660
user = apache
group = apache
1
2
3
4
5
6
listen.allowed_clients=127.0.0.1
listen.owner=apache
listen.group=apache
listen.mode=0660
user=apache
group=apache
設定完成後, 現在啟動 httpd 及 php-fpm, 並設定開機自動啟動:
[root@opencli ~]# systemctl enable php-fpm
[root@opencli ~]# systemctl enable httpd
[root@opencli ~]# systemctl start php-fpm
[root@opencli ~]# systemctl start httpd
現在需要設定 firewalld, 開放 port 80 及 443 對外連線:
[root@opencli ~]# firewall-cmd –zone=public –permanent –add-service=http
[root@opencli ~]# firewall-cmd –zone=public –permanent –add-service=https
[root@opencli ~]# firewall-cmd –reload
最後可以測試一下 PHP 是否可以透過 PHP-FPM 正常執行, 執行以下指令建立 PHP 測試頁面:
[root@opencli ~]# echo “<?php phpinfo(); ?>” > /var/www/html/info.php
用瀏覽器開啟上面的頁面 (http://xxx.xxx.xxx.xxx/info.php), 如果可以看到 phpinfo 的畫面, 並看到 Server API 是 “FPM/FastCGI”, 便表示安裝成功了:
你可能感興趣的內容: