Mac 自带 php-fpm,在终端执行 php-fpm,会报如下错误:

ERROR: failed to open configuration file '/private/etc/php-fpm.conf': No such file or directory (2)
ERROR: failed to load configuration file '/private/etc/php-fpm.conf'
ERROR: FPM initialization failed

错误信息显示,不能打开配置文件,cd /private/etc,发现没有 php-fpm.conf 文件,但是有 php-fpm.conf.default 文件。这个文件是默认配置,我们可以复制一份,改名为 php-fpm.conf,然后再根据需要改动配置。

sudo cp /etc/php-fpm.d/www.conf.default /etc/php-fpm.d/www.conf
sudo cp /etc/php-fpm.conf.default /etc/php-fpm.conf
sudo cp /etc/php.ini.default /etc/php.ini

执行 php-fpm,再次报错:

ERROR: failed to open error_log (/usr/var/log/php-fpm.log): No such file or directory (2)
ERROR: failed to post process the configuration
ERROR: FPM initialization failed

错误信息显示,不能打开错误日志文件。cd /usr/var/log 发现根本没有这个目录,甚至连 var 目录都没有,加上为了避免权限问题,干脆配置到 /usr/local/var/log 目录。首先手动建立这些目录

mkdir /usr/local/var/log
mkdir /usr/local/var/run

然后修改 php-fpm.conf 以下内容:

error_log = /usr/local/var/log/php-fpm.log
pid = /usr/local/var/run/php-fpm.pid

执行 php-fpm,再次报错:

NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root
NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root

于是 sudo php-fpm,再次报错:

ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (48)
ERROR: FPM initialization failed

sudo killall php-fpm

然后通过运行 php-fpm 命令即可正常启动 php-fpm

最后为了方便启动管理,我们给 php-fpm 启动、关闭、重启建个别名,方便以后操作

vim ~/.bash_profile

alias php-start="sudo php-fpm"
alias php-stop="kill -INT `cat /usr/local/var/run/php-fpm.pid`"
alias php-restart="kill -USR2 `cat /usr/local/var/run/php-fpm.pid`"

标签: php, macOS

添加新评论