Mac Nginx 配置

1、配置准备工作

  • 1)配置 Nginx 准备工作

    • 下载相关软件

      • Nginx 官网

      • 其它下载地址

  • 2)配置注意事项

    • 提前下载好相关软件,且安装目录最好安装在全英文路径下。如果路径有中文名,那么可能会出现一些莫名其妙的问题。
  • 3)示例配置环境

    • macOS 11.6.4

2、配置 Nginx

  • 1)安装 Nginx

    • Homebrew 使用以下的命令安装。

      1
      2
      # 安装 Nginx
      $ brew install nginx
    • nginx 默认安装到 /usr/local/Cellar/ 目录下。

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      ==> nginx
      Docroot is: /usr/local/var/www

      The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
      nginx can run without sudo.

      nginx will load all files in /usr/local/etc/nginx/servers/.

      To restart nginx after an upgrade:
      brew services restart nginx
      Or, if you don't want/need a background service you can just run:
      /usr/local/opt/nginx/bin/nginx -g daemon off;
  • 2)配置环境变量

    • 打开终端输入以下命令,打开 .bash_profile 文件

      1
      $ vim ~/.bash_profile
    • 在此文件中添加设置环境变量的命令

      1
      2
      3
      # 1.21.6_1 为安装的 nginx 版本号
      export NGINX_HOME=/usr/local/Cellar/nginx/1.21.6_1
      export PATH=$PATH:$NGINX_HOME/bin
    • 添加之后保存并退出,在终端执行以下命令使配置生效

      1
      $ source ~/.bash_profile
  • 3)查看配置是否生效

    • 在终端输入以下命令

      1
      2
      3
      4
      5
      # 启动 nginx
      $ nginx

      # 也可以使用下面的命令启动,但是配置文件 nginx.conf 修改后用这个命令执行不生效,故不建议使用。
      $ brew services start nginx
    • 查看 nginx 是否启动成功

      • 打开浏览器,然后网址输入 http://localhost:8080/ ,如果出现如下界面,则说明启动成功。

      • 端口号是在配置文件 nginx.conf 里面配置的,默认端口是 8080 ,配置文件的位置 /usr/local/etc/nginx

    • 查看 nginx 版本

      1
      $ nginx -v
  • 4)关闭 Nginx

    • 关闭 nginx

      1
      $ nginx -s stop
    • 重新加载 nginx

      1
      $ nginx -s reload
文章目录
  1. 1. 1、配置准备工作
  2. 2. 2、配置 Nginx
隐藏目录