nginx
安装命令
brew install 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服务
关于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文件,文件里面随意写些合法的验证代码即可。
说一个自己的坑 ,因为本来自己的机子上有php 版本是7.1,为了使用其他功能 花了两个小时升级到7.4(网速太慢)
在 配置 php-fpm 时 ,照搬上面的 发现NGINX 就是不支持php ,花了一天时间 想明白了 自己的php 不是在这个目录下。
搞明白以后 长舒了一口气。
#user nobody;
#user root;
worker_processes 1;
error_log /usr/local/var/log/nginx/error.log;
pid /usr/local/var/run/nginx.pid;
#pid logs/nginx.pid;
events {
#use epoll; 事件驱动模型
worker_connections 1024; # 最大连接数
}
http {
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /usr/local/var/log/nginx/access.log;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65; #链接超时时间
#gzip on;
server {
listen 80;#端口
server_name site1.com;#你的站点域名/ip
root /data/site1/public; #你的站点目录,绝对路径即可
index index.php index.html index.htm;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 80;
server_name lepai.larance.com;
root /Users/larance/PhpstormProjects/ms-lepai-article;
index index.html index.htm index.php;
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
:wqlocation / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /50x.html {
root html;
}
#
location ~ \.php$ {
root /Users/larance/PhpstormProjects/ms-lepai-article;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
include servers/*;
}