MacOS X 下配置Nginx & PHP

  1. 1. 0x1 Nginx安装
  2. 2. 0x2 PHP
  3. 3. 0x3 php-fpm
  4. 4. 0x4 配置Nginx.conf
  5. 5. 0x5 BrBrBr

也有一段时间没写博客了,说好的技术博主呢,hhhhh。其实一些小错误感觉没什么好写的。但是配置环境的坑我踩了好久了。趁这次重装了Mac,刚好记录记录配置Nginx,虽然前面写过一篇配置,但是Mac上还是有比较多的坑的。也不知道啥时候Mac才自带Nginx呀。个人喜欢Homebrew,没用Macport,所以下文都是采用的Homebrew。

0x1 Nginx安装

通过Homebrew安装

1
2
$ brew search nginx
$ brew install nginx

然后通过

1
$ brew info nginx

你可以看到关于nginx的各种信息

1
2
3
4
5
Docroot is: /usr/local/var/www

Nginx.conf : /usr/local/etc/nginx/nginx.conf

Log : /usr/local/var/log/nginx

通过

1
$ nginx

即可启动nginx,访问localhost:8080便可看到Nginx欢迎界面

0x2 PHP

采用了系统的php

1
2
3
4
$ php -v
PHP 5.6.30 (cli) (built: Oct 29 2017 20:30:32)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

想用php7也可以通过brew进行安装使用

0x3 php-fpm

按照上面流程走的话,如果在Nginx网站目录下创建一个index.php,内容为phpinfo,会发现访问index.php会出现直接下载,那便是因为还没有启动php-fpm以及配置nginx.conf的原因。

Mac自带php-fpm,但是这里还是有比较多坑的。
参考来自: Mac 配置 php-fpm

直接运行php-fpm启动php-fpm

1
2
3
$ php-fpm
[04-Mar-2018 00:35:04] ERROR: failed to open configuration file '/private/etc/php-fpm.conf': No such file or directory (2)
[04-Mar-2018 00:35:04] ERROR: failed to load configuration file '/private/etc/php-fpm.conf'

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

1
$ sudo cp /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf

再执行php-fpm

1
2
3
4
$ 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目录。

修改php-fpm.conferror_log 配置为 /usr/local/var/log/php-fpm.log,并把 user 和 group 改为和当前用户一样。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
; Error log file
; If it's set to "syslog", log is sent to syslogd instead of being written
; in a local file.
; Note: the default prefix is /usr/var
; Default Value: log/php-fpm.log
error_log = /usr/local/var/log/php-fpm.log

...

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
user = xxx(当前用户)
group = xxx(当前用户组)

怎么查看当前用户以及所属组呢?参考链接

1
2
3
4
$ who   //查看当前登陆的用户
$ finger //用于查找并显示用户信息
$ finger xxx //查看用户的信息
$ groups xxx //查看用户所属组别

执行 php-fpm,再次报错:

1
2
3
$ 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,再次报错:

1
2
3
$ 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

编辑php-fpm.conf,修改listen127.0.0.1:9999
具体也可以参考Mac如何查看端口占用

其实后来通过检查端口,发现当时占用的也是php-fpm,也有点疑惑。

可以用过sudo pkill php-fpm关闭php-fpm

1
$ sudo pkill php-fpm

最后

1
2
php-fpm -t
[04-Mar-2018 01:56:01] NOTICE: configuration file /private/etc/php-fpm.conf test is successful

0x4 配置Nginx.conf

nginx.conf的配置还是相当重要的,毕竟是nginx的配置文件

网上也挺多参考链接

首先,找到serverlocation配置,给index加一个index.php

1
2
3
4
location / {
root html;
index index.html index.htm index.php;
}

然后配置端口,我改成了80端口,但是每次启动都需要sudo

1
2
3
server {
listen 80;
server_name localhost;

接着去掉php相关的注释,并修改 fastcgi_param 参数

1
2
- fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
+ fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

所以完整php配置为

1
2
3
4
5
6
7
8
9
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
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;
}

重启Nginx

1
$ sudo nginx -s reload

0x5 BrBrBr

好啦,可以开心地进行开发啦!


Article Author: Zeddy

Article Link: https://blog.zeddyu.info/2018/03/04/MacOS-X-%E4%B8%8B%E9%85%8D%E7%BD%AENginx-amp-PHP/index.html

Copyright Notice: With the exception of the special statement at the beginning of the article, all articles can be reprinted in accordance with the CC BY 4.0 agreement with the author's permission.

博客自动化及一些坑 服务器配置的那些事--基于LNMP

Comments