mac安装nginx+phpnginx
安装命令
brew install nginx
nginx 默认监听 8080 端口,启动成功之后,在浏览器访问 http://localhost:8080 可以看到 nginx 的欢迎界面。
默认服务路径,项目根目录
/usr/local/var/www
配置文件路径
nginx安装好后,只有对应的默认配置文件。
/usr/local/etc/nginx/nginx.conf.default
需要手动拷贝一份为nginx.conf。
执行命令
sudo cp /usr/local/etc/nginx/nginx.conf.default /usr/local/etc/nginx/nginx.conf
这样就有默认配置文件了。
/usr/local/etc/nginx/nginx.conf
!!!服务启动命令!!!
brew services start nginx
sudo nginx #启动nginx服务 sudo nginx -s reload #重新载入配置文件 sudo nginx -s stop #停止nginx服务
!!!注意注意注意!!!
nginx 已经跑起来了,但是目前还不能解析 php,我们需要利用php-fpm解析。php-fpm 和 php 一起安装,我们先去安装 php,再做相关配置。
php
关于php-fpm
Mac上默认安装了php和php-fpm,所以暂没使用brew进行php安装。但需要注意的几个点:
一、php-fpm对应的配置文件只有默认的。
/private/etc/php-fpm.conf.default
二、拷贝配置文件
sudo /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf
三、修改配置
修改error_log 错误日志路径,否则php-fpm会启动失败。
error_log = /usr/local/var/log/php-fpm.log
修改nginx配置
当nginx和php-fpm都按照上述配置完毕后,再进行nginx的配置:
一、找到server中的下面代码,添加index.php。
location / {
root html;
index index.html index.htm index.php;
}
二、在找到开启FastCGI server
# 下面是针对Apache服务
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# 这里是针对nginx服务
# 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 /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
需要把1修改为2;
1
fastcgi_param SCRIPT_FILENAME/scripts$fastcgi_script_name;
2
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
三、启动nginx和php-fpm
sudo nginx -s reload sudo php-fpm
四、验证php服务
在/usr/local/var/www下添加index.php文件,文件里面随意写些合法的验证代码即可。
在浏览器里面运行localhost:8080,如果返回预期,则说明nginx和php-fpm配置OK,可以进行后续开发啦。
添加自己的servers配置
touch jasper.conf
添加servers配置(配置基本核心的即可)
server {
listen 8081;
index index.php index.html index.htm;
server_name jasper.tme.com;
root /Users/songzeyu/workspace/code/;
# charset utf - 8;
access_log /usr/local/etc/nginx/log/localhost.access.log;
error_log /usr/local/etc/nginx/log/localhost.error.log;
location / {
# if (!-f $request_filename) {
# rewrite ^ /(.*)$ /index.php last;
#}
index index.php;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location =
/50x.html {
}
#proxy the php scripts to php-fpm
location ~ .*\.(php)?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
proxy_buffering off;
}
location ~ /\.ht {
deny all;
}
}
添加hosts配置
sodo vi /private/etc/hosts
# 添加一行
127.0.0.1 jasper.tme.com
然后就可以在/Users/songzeyu/workspace/code/目录进行开发
非特殊说明,本博所有文章均为博主原创。
如若转载,请注明出处:https://www.95app.top/mac-phpnginx/