Back

MacOS X 下配置Nginx & PHP

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

0x1 Nginx安装

通过Homebrew安装

$ brew search nginx
$ brew install nginx

然后通过

$ brew info nginx 

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

Docroot is: /usr/local/var/www

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

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

通过

$ nginx

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

0x2 PHP

采用了系统的php

$ 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

$ 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,然后再根据需要改动配置。

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

再执行php-fpm

$ 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 改为和当前用户一样。

; 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(当前用户组)

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

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

执行 php-fpm,再次报错:

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

$ 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

$ sudo pkill php-fpm

最后

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

location / {
    root   html;
    index  index.html index.htm index.php;
}

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

server {
    listen       80;
    server_name  localhost;

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

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

所以完整php配置为

# 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

$ sudo nginx -s reload

0x5 BrBrBr

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

Licensed under CC BY-NC-SA 4.0

I am looking for some guys who have a strong interest in CTFs to build a team focused on international CTFs that are on the ctftime.org, if anyone is interested in this idea you can take a look at here: Advertisements


想了解更多有意思的国际赛 CTF 中 Web 知识技巧,欢迎加入我的 知识星球 ; 另外我正在召集一群小伙伴组建一支专注国际 CTF 的队伍,如果有感兴趣的小伙伴也可在 International CTF Team 查看详情


comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy