相关命令

# 启动
sudo nginx
# 关闭 nginx
sudo nginx -s stop
# 重启
sudo nginx -s reload

# 查看进程
ps -ef | grep nginx

目录

<aside> 📃 目录

</aside>

参考:https://segmentfault.com/a/1190000012953480

安装

解决报错

  1. sudo nginx -t报错

    解决nginx报错 nginx: [emerg] could not build server_names_hash, you should increase server_nam es_hash_bucket_size: 32 nginx: configuration file xxxx/conf/nginx.conf test failed

    该报错产生的原因主要是因为Nginx中的server配置中server_name的定义值过长产生的。

    解决方法

    Nginxhttp字段内添加如下代码,放大默认bucket_size

    http {
            server_names_hash_bucket_size 64;
            .....
        }
    # 注意:如果已经存在该字段信息,需要加大后面的数值。且数值必须是32的倍数
    
  2. sudo nginx -s reload报错

    nginx: [error] invalid PID number "" in "/opt/homebrew/var/run/nginx.pid”

    nginx: [alert] kill(3771, 1) failed (3: No such process)

    nginx: [error] open() "/opt/homebrew/var/run/nginx.pid" failed (2: No such file or directory)

    切换到nginx配置文件的路径

    cd /usr/local/etc/nginx/
    # 或者直接带这路径执行
    sudo nginx -c /usr/local/etc/nginx/nginx.conf
    
    # M1,使用的是
    cd /opt/homebrew/etc/nginx
    # 当然也可以直接带着路径
    sudo nginx -c /opt/homebrew/etc/nginx/nginx.conf
    

    nginx.conf文件的路径可以从nginx -t的返回中找到。

    Untitled

  3. 如果报错

    Untitled

    端口占用,可能是apache

    sudo apachectl stop
    

    再次执行

    sudo nginx -c /opt/homebrew/etc/nginx/nginx.conf
    

    然后再执行重启

    sudo nginx -s reload
    
  4. 没有 log 目录

    nginx: [emerg] open() "/opt/homebrew/Cellar/nginx/1.25.1_1/logs/error.log" failed (2: No such file or directory)

    mkdir /opt/homebrew/Cellar/nginx/1.25.1_1/logs
    

解决用户权限问题

  1. 查看进程执行的用户

    ps aux | grep nginx
    

    Untitled

  2. nginx 默认是用户是 nobody

    打开 nginx.conf

    vim /usr/local/etc/nginx/nginx.conf
    # M1的是在 /opt 下
    vim /opt/homebrew/etc/nginx/nginx.conf
    

    第一行添加

    user xuweidong staff;
    # user 用户名 
    

    Untitled

    然后重新加载 nginx.conf 即可

    sudo nginx -t
    sudo nginx -s reload
    

Nginx An error occurred报错~

Untitled

PHP 意外终止了,brew 更新一下 PHP 就好了