httpd+php结合的方式:
module: php
fastcgi : php-fpm
一、安装
yum install php-fpm -y
二、修改配置
cat /etc/ hp-fpm.conf
include=/etc/php-fpm.d/*.conf #这行表示子配置文件也可以存放在这里
/etc/php-fpm.d
#在这个子目录里,只有一个文件,这个文件比较重要,
/etc/php-fpm.d/www.conf
#这个也是配置文件,
listen = 127.0.0.1:9000
#这行表示监听的端口在本机的127.0.0.1:9000这个端口。如果要跨网络访问的话,需要更改IP地址
listen.allowed_clients = 127.0.0.1
#这里也是只允许自己连,跨网络自己修改IP地址
; static - a fixed number (pm.max_children) of child processes;
#这个表示静态的,定了多少个就不改了
; dynamic - the number of child processes are set dynamically based on the
#这个是动态的,
; pm.max_children
#这个表示最大多少
; pm.start_servers
#这个表示刚开始多少
; pm.min_spare_servers
#这个表示最小空闲进程
; pm.max_spare_servers
#最大空闲进程
#默认是动态的,都可以自己调,修改后面的数字即可
;pm.status_path = /status
#状态页面
ping.path = /ping
ping.response = pong
测试服务器是否是好的
三、开启服务
systemctl start php-fpm
服务启动了,Apache是它的客户端
四、修改Apache 配置,
#不修改的话,这Apache和php-fpm是两个毫不相干的服务,在Apache上加以配置,让他能够收到PHP请求,不是自己处理而是转发给PHP-fpm 来处理
1 vim /etc/httpd/conf.d/fcgi.conf
2
3 DirectoryIndex index.php
4 #支持首页是index.php
5 ProxyRequests Off
6 #启用代理请求,off
7 ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/$1
8 #如果收到php后缀的请求,就交给本机的fastcgi协议的9000端口,通过这个来访问
五、编译安装httpd,点击此处查看编译安装htppd
在本机上的数据库不需要走9000端口,可以直接走本地的socket文件
参看:http://httpd.apache.org/docs/2.4/mod/mod_proxy_fcgi.html
注意:在HTTPD服务器上必须启用proxy_fcgi_module模块,充当PHP客户端
六、启用fcgi模块
LoadModule proxy_module modules/mod_proxy.so
#开启代理功能,
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
#取消注释即可
DirectoryIndex index.html
#搜索到这行,将提替换成
DirectoryIndex index.php index.html
#默认这个只支持index.html 让他支持index.php ,并优先寻找index.php
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
#Apache默认是识别不了PHP后缀的,在最后添加这两行让他能至此PHP后缀的
ProxyRequests Off
#将这个关闭
ProxyPassMatch "^/(.*\.php(/.*)?)$" "unix:/var/run/php5-fpm.sock|fcgi://localhost//app/httpd24/htdocs/"
#使用套接字的话,fcgi服务器和Apache必须在同一台服务器上,这里的/app/httpd24/htdocs/是我编译安装的路径,默认应该是/var/www/html/
这里如果使用的是本地的9000端口的话,上面最后一行就用这行,修改下路径就好了
ProxyPassMatch "^/.*\.php(/.*)?$" "fcgi://localhost:9000/var/www/" enablereuse=on
七、修改php-fpm的配置文件,启用本地套接字连接
vim /etc/php-fpm.d/www.conf
;listen = 127.0.0.1:9000
#在原来的注释这行下面添加一行,必须和httpd中的配置文件一样
listen=/var/run/php5-fpm.sock
listen.owner = apache
listen.group = apache
listen.mode = 0666
#将这三行取消注释,并修改默认用户为apache
八、重启两个服务
systemctl restart php-fpm
#重启php-fpm 服务
apachectl start
#由于是编译安装的,不能通过systemctl 管控。不过这个apachectl 是Apache自带的
将Apache的路径添加到PATH变量下
echo ‘PATH=/app/httpd24/bin:$PATH‘ > /etc/profile.d/httpd24.sh
. !$
九、如果需要systemctl 管理httpd的话,需要做如下操作
1、/etc/sysconfig/httpd ,这个可以不要,但有总比没有好,yum安装的有这个,自己编译的没有这个,没有的话注释掉下面第二步的这个EnvironmentFile=/etc/sysconfig/httpd
cat /etc/sysconfig/httpd
#
# This file can be used to set additional environment variables for
# the httpd process, or pass additional options to the httpd
# executable.
#
# Note: With previous versions of httpd, the MPM could be changed by
# editing an "HTTPD" variable here. With the current version, that
# variable is now ignored. The MPM is a loadable module, and the
# choice of MPM can be changed by editing the configuration file
# /etc/httpd/conf.modules.d/00-mpm.conf.
#
#
# To pass additional options (for instance, -D definitions) to the
# httpd binary at startup, set OPTIONS here.
#
#OPTIONS=
#
# This setting ensures the httpd process is started in the "C" locale
# by default. (Some modules will not behave correctly if
# case-sensitive string comparisons are performed in a different
# locale.)
#
LANG=C
2、
cat /usr/lib/systemd/system/httpd24.service
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=forking
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/app/httpd24/bin/httpd $OPTIONS -k start
ExecReload=/app/httpd24/bin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
将这两个配置文件添加到对应的路径,cat 后面是绝对路径,
此时通过systemctl start httpd24 就可以开启和关闭了
原文:https://www.cnblogs.com/alexlv/p/14621021.html