> For the complete documentation index, see [llms.txt](https://yuhongli.gitbook.io/nginx-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yuhongli.gitbook.io/nginx-docs/macos-an-zhuang-nginx.md).

# MacOS安装Nginx

#### 1、**安装 Homebrew**

homebrew是什么？它是Mac中的一款软件包管理工具，通过brew可以很方便的在Mac中安装软件或者是卸载软件。不了解的同学看以看官网([brew.sh/index\_zh-cn…](https://link.juejin.cn/?target=https%3A%2F%2Fbrew.sh%2Findex_zh-cn.html)), 然后在我们命令行中复制如下命令:

```java
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
```

安装成功后的话，我们可以使用命令 “brew update”更新下；如下命令：

```java
brew update
```

**有关brew常用的指令如下：**

1. brew搜索软件命令： **brew search nginx**\\
2. brew安装软件命令： **brew install nginx**\\
3. brew卸载软件命令: **brew uninstall nginx**\\
4. brew升级命令： **sudo brew update**\\
5. 查看安装信息(比如查看安装目录等) **sudo brew info nginx**\\
6. 查看已经安装的软件：**brew list**

#### **2、brew安装nginx**

2.1、使用brew安装nginx，如下命令所示：

```java
brew install nginx
```

如下图所示： ![WX20230306-151521@2x.png](https://cdn.nlark.com/yuque/0/2023/png/1212576/1678086932663-02d6a8c5-060d-454c-bc94-b09aa158312d.png#averageHue=%23f1f0f0\&clientId=uf4044d22-6181-4\&from=drop\&id=u9bd22864\&name=WX20230306-151521%402x.png\&originHeight=1008\&originWidth=1320\&originalType=binary\&ratio=1\&rotation=0\&showTitle=false\&size=200824\&status=done\&style=none\&taskId=ubc9326d7-345c-4365-9037-560ea16bb55\&title=)

2.2、查看nginx的配置信息，如下命令：

```java
brew info nginx
```

![WX20230306-151929@2x.png](https://cdn.nlark.com/yuque/0/2023/png/1212576/1678087200958-855e0bd2-ee16-4b0b-a656-0adbca1fe906.png#averageHue=%23f1eded\&clientId=uf4044d22-6181-4\&from=drop\&id=u3c0c4217\&name=WX20230306-151929%402x.png\&originHeight=812\&originWidth=1290\&originalType=binary\&ratio=1\&rotation=0\&showTitle=false\&size=171086\&status=done\&style=none\&taskId=u2e556db7-2bbd-4a9d-be9f-a684ce06b34\&title=)

标记1: nginx的安装目录 标记2: nginx的安装来源 标记3: 根目录 标记4: nginx的配置文件和默认端口8080 标记5: 在/usr/local/etc/nginx/servers 目录中加载所有文件。并且我们可以通过最简单的命令'nginx' 来启动nginx

2.3、查看nginx安装目录， 如下命令：

```java
open /usr/local/etc/nginx/
```

![WX20230306-152437@2x.png](https://cdn.nlark.com/yuque/0/2023/png/1212576/1678087495501-c411c3e2-6292-47c6-8661-fb25cdb27a6c.png#averageHue=%23f8f7f7\&clientId=uf4044d22-6181-4\&from=drop\&id=u32af6377\&name=WX20230306-152437%402x.png\&originHeight=994\&originWidth=1560\&originalType=binary\&ratio=1\&rotation=0\&showTitle=false\&size=534832\&status=done\&style=none\&taskId=u1f461b0a-c430-4b49-aaee-dbf6bdfa1a9\&title=)

打开nginx目录后，可以看到我们上面的使用 brew info nginx 查看信息所说的 server目录以及nginx.conf的配置文件， 那么我们的nginx被安装到什么地方呢？我们从上面的截图可以看到，是在 这个目录下 **/usr/local/Cellar/nginx**，执行如下命令可以查看到：

```java
open /usr/local/Cellar/nginx
```

会打包目录，如下图所示： ![WX20230306-152557@2x.png](https://cdn.nlark.com/yuque/0/2023/png/1212576/1678087572881-3640c04b-a5df-44c9-a552-4fe55b9afa41.png#averageHue=%23fbfbfb\&clientId=uf4044d22-6181-4\&from=drop\&id=u505a59ba\&name=WX20230306-152557%402x.png\&originHeight=858\&originWidth=1536\&originalType=binary\&ratio=1\&rotation=0\&showTitle=false\&size=414127\&status=done\&style=none\&taskId=u34f7d2b0-4a98-48d0-bb7b-83c07056898\&title=)

进入上面的 1.23.3文件后，如下图所示： ![WX20230306-152706@2x.png](https://cdn.nlark.com/yuque/0/2023/png/1212576/1678087633235-65fbcb51-4b66-402d-bdad-76509b1c01c4.png#averageHue=%23f8f8f8\&clientId=uf4044d22-6181-4\&from=drop\&id=u55082f69\&name=WX20230306-152706%402x.png\&originHeight=872\&originWidth=1540\&originalType=binary\&ratio=1\&rotation=0\&showTitle=false\&size=189199\&status=done\&style=none\&taskId=u166faf6d-233a-4a80-a519-ac94048c9d7\&title=)

在该目录下可以看到一个名字为html的快捷方式的文件夹，进入该目录后，它有两个文件50.html和index.html，如下图所示：

![WX20230306-152735@2x.png](https://cdn.nlark.com/yuque/0/2023/png/1212576/1678087659401-516c7b98-830a-4356-b8b9-060f3d487d22.png#averageHue=%23fbfbfb\&clientId=uf4044d22-6181-4\&from=drop\&id=uc1401a1e\&name=WX20230306-152735%402x.png\&originHeight=850\&originWidth=1532\&originalType=binary\&ratio=1\&rotation=0\&showTitle=false\&size=377313\&status=done\&style=none\&taskId=u06a60358-16b5-47d2-99f6-77db5e1e34a\&title=)

其实它是指向的就是 /usr/local/var/wwww目录的

#### 3、**启动nginx服务，如下命令：**

```java
brew services start nginx // 重启的命令是: brew services restart nginx
```

![WX20230306-153111@2x.png](https://cdn.nlark.com/yuque/0/2023/png/1212576/1678087880913-01426aa2-c605-436f-9704-733933a8cb54.png#averageHue=%23e8e8e8\&clientId=uf4044d22-6181-4\&from=drop\&id=u4414bcaf\&name=WX20230306-153111%402x.png\&originHeight=348\&originWidth=1132\&originalType=binary\&ratio=1\&rotation=0\&showTitle=false\&size=103918\&status=done\&style=none\&taskId=u177afbcf-6278-4dcb-add7-891b0752d96\&title=)

重启后，我们验证下，因为nginx默认的端口号是8080，因此我们页面访问 [http://localhost:8080](https://link.juejin.cn/?target=http%3A%2F%2Flocalhost%3A8080) 即可，看到如下信息：

![WX20230306-153146@2x.png](https://cdn.nlark.com/yuque/0/2023/png/1212576/1678087926299-211b2d94-0c0d-4e89-b0a1-666c5849a612.png#averageHue=%23f6f6f6\&clientId=uf4044d22-6181-4\&from=drop\&id=u24e2150d\&name=WX20230306-153146%402x.png\&originHeight=862\&originWidth=1766\&originalType=binary\&ratio=1\&rotation=0\&showTitle=false\&size=125731\&status=done\&style=none\&taskId=u3f6fd546-215c-4f7f-9442-1261dc9679e\&title=)

如果成功的话，一般都是 欢迎的界面(index.html页面我自己改过)，下面我们继续查看下nginx.conf 配置信息，使用如下命令：

```java
cat /usr/local/etc/nginx/nginx.conf // 或者使用 sudo open /usr/local/etc/nginx/nginx.conf -a 'sublime text' 使用编辑器sublime打开。
```

配置信息如下：

```java

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # 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;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}
```

如上，就可以使用nginx搭建本地服务了。

### 三、**总结nginx常见的配置**

**nginx的配置文件路径：/usr/local/etc/nginx/nginx.conf** **nginx的服务器默认路径：/usr/local/var/www** **nginx的安装路径：/usr/local/Cellar/nginx/1.23.3**

#### 1、**nginx启动：**

1.1、在终端输入 ps -ef|grep nginx 命令看是否有启动，如下：

![WX20230306-153558@2x.png](https://cdn.nlark.com/yuque/0/2023/png/1212576/1678088167155-eecd7478-6ea2-4761-ac47-e9cf8be5e684.png#averageHue=%23f9f9f9\&clientId=uf4044d22-6181-4\&from=drop\&id=uc5b8c70b\&name=WX20230306-153558%402x.png\&originHeight=712\&originWidth=1164\&originalType=binary\&ratio=1\&rotation=0\&showTitle=false\&size=71514\&status=done\&style=none\&taskId=u61f06602-6867-4204-927b-9b69344c238\&title=)

master: 主进程 worker:子进程（执行进程）

1.2、验证配置文件是否正确，因此在启动nginx之前，我们可以先运行下如下命令：

```java
sudo /usr/local/Cellar/nginx/1.23.3/bin/nginx -t -c /usr/local/etc/nginx/nginx.conf 
```

![WX20230306-153813@2x.png](https://cdn.nlark.com/yuque/0/2023/png/1212576/1678088300860-1b5b4a16-5d1e-48ad-9e7e-46c163066bdb.png#averageHue=%23f0efef\&clientId=uf4044d22-6181-4\&from=drop\&id=u1f8f2808\&name=WX20230306-153813%402x.png\&originHeight=452\&originWidth=1166\&originalType=binary\&ratio=1\&rotation=0\&showTitle=false\&size=92457\&status=done\&style=none\&taskId=u562eaf49-826b-4fc2-a368-b12f9b7262d\&title=)

重启nginx有如下几种方法： 1.3、通过brew，brew services start nginx(启动nginx) brew services restart nginx(重启命令)

1.4、先进入bin目录：cd /usr/local/Cellar/nginx/1.23.3/bin/， 然后再执行：./nginx -s reload

1.5、根据进程号重启，执行命令 kill -HUP 进程号 如下所示：

```java
sudo kill -HUP 55070
```

#### 2、**nginx停止**

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