1、假设已经安装好php-fpm服务,并且php安装目录为 /usr/local/php,可参考:php-7.2.0源码安装及扩展安装
2、配置
2.1、cd /usr/local/php/etc
2.2、cp php-fpm.conf.default php-fpm.conf
2.3、编辑文件(vim php-fpm.conf),添加以下三行
pid = run/php-fpm.pid
user = www
group = www
2.4、新加www用户和用户组
groupadd www
useradd -g www www
3、启动
/usr/local/php/sbin/php-fpm
4、查看
ps -ef|grep php-fpm
netstat -tnl | grep 9000
5、添加到开机启动服务
5.1、在/etc/init.d中编写脚步php-fpm,内容如下
#!/bin/bash
### BEGIN INIT INFO
# Provides:php-fpm
# Required-Start: $network $remote_fs $syslog
# Required-Stop: $network $remote_fs $syslog
# Default-Start:2 3 4 5
# Default-Stop:0 1 6
# Short-Description:PHP-FPM server.
# Description:PHP is an HTML-embedded scripting language
### END INIT INFO
# config: /usr/local/php/etc/php.ini
PHP_PATH=/usr/local
DESC="php-fpm daemon"
NAME=php-fpm
# php-fpm路径
DAEMON=$PHP_PATH/php/sbin/$NAME
# 配置文件路径
CONFIGFILE=$PHP_PATH/php/etc/php-fpm.conf
# PID文件路径(在php-fpm.conf设置)
PIDFILE=$PHP_PATH/php/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
rh_start() {
$DAEMON -y $CONFIGFILE || echo -n " already running"
}
rh_stop() {
kill -QUIT `cat $PIDFILE` || echo -n " not running"
}
rh_reload() {
kill -HUP `cat $PIDFILE` || echo -n " can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
rh_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
rh_stop
echo "."
;;
reload)
echo -n "Reloading $DESC configuration..."
rh_reload
echo "reloaded."
;;
restart)
echo -n "Restarting $DESC: $NAME"
rh_stop
sleep 1
rh_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
exit 3
;;
esac
exit 0
5.2、调整脚步权限
chmod 755 php-fpm
5.3、加入到开机启动服务
update-rc.d php-fpm defaults
5.4、管理开机启动服务
sysv-rc-conf
6、常用命令
service php-fpm start启动
service php-fpm stop停止
service php-fpm restart重启
7、
5、配置nginx通过fastcgi访问php-fpm
方法一:
server{
listen 80;
server_name test.com;
index index.php;
root /var/www/test/web;
#以fastcgi配置php-fpm
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;#php-fpm运行默认端口9000
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
access_log /var/log/test/access.log;
error_log /var/log/test/error.log;
}
方法二:PHP-FPM使用Unix套接字,需要修改/usr/local/php/etc/php-fpm.d/www.conf
修改后如下:
[...]
;listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php7-fpm.sock
[...]
server{
listen 80;
server_name test.com;
index index.php;
root /var/www/test/web;
#以fastcgi配置php-fpm
location ~ .php$ {
fastcgi_pass unix:/var/run/php-fpm/php7-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
access_log /var/log/test/access.log;
error_log /var/log/test/error.log;
}