程序员

MAC安装Nginx 以及使用中遇到的坑

2018-09-19  本文已影响856人  Hello_Kugou

一.安装

brew install nginx

  1. 安装完之后,可以在终端看到路径信息
    /usr/local/etc/nginx/nginx.conf (配置文件路径) /usr/local/var/www (服务器默认路径) /usr/local/Cellar/nginx/1.8.0 (安装路径)

  2. 如果是macOS 1.12以上的系统,在安装过程中可能会出现”warning”,说是不支持该版本的操作系统,可以暂时先忽略它。

二. 启动

  1. 在终端输入 ps -ef|grep nginx看是否有启动
  2. 如果没有启动执行以下命令启动/usr/local/Cellar/nginx/1.8.0/bin/nginx -c /usr/local/etc/nginx/nginx.conf 一定要注意路径是否是自己的安装路径
  3. 这时候如果成功访问localhost:8080,说明成功安装和启动好了。

三. 停止

  1. 终端输入ps -ef|grep nginx获取到nginx的进程号, 注意是找到“nginx:master”的那个进程号
  2. kill -QUIT 15800 (从容的停止,即不会立刻停止)
    Kill -TERM 15800 (立刻停止)
    Kill -INT 15800 (和上面一样,也是立刻停止)

四、重启

  1. 如果配置文件错误,则将启动失败,所以在启动nginx之前,需要先验证在配置文件的正确性,如下表示配置文件正确
    promote:bin yangqianhua$ /usr/local/Cellar/nginx/1.8.0/bin/nginx -t -c /usr/local/etc/nginx/nginx.conf
    nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
  2. 重启有两种方法

Nginx 遇到的坑

nginx: [alert] could not open error log file: open() "/usr/local/var/log/nginx/error.log" failed (13: Permission denied) 2018/09/18 16:53:12 [emerg] 9734#0: open() "/usr/local/Cellar/nginx/1.15.3/logs/error.log" failed (2: No such file or directory)

操作 cd /usr/local/Cellar/nginx/1.15.3/ 新建logs目录添加error.log文件, 执行sudo nginx 出现如下错误

nginx: [warn] 1024 worker_connections exceed open file resource limit: 256 localhost:bin hello_xie$ nginx: [warn] 1024 worker_connections exceed open file resource limit: 256 -bash: nginx:: command not found localhost:bin hello_xie$ nginx: [warn] 1024 worker_connections exceed open file resource limit: 256 -bash: nginx:: command not found

nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

另一种解决方案(stackoverflow)

到 nginx.conf 文件添加 worker_rlimit_nofile 1024;这种入侵性更小, 推荐使用

Mac 403 Forbidden

要在Nginx.conf文件 最开始的地方加权限
user root owner 然后重启或者重新加载Nginx

上一篇下一篇

猜你喜欢

热点阅读