Mac M1 安装 nginx

2,195 阅读2分钟

Mac M1 安装 nginx

1. 前言

哈喽,大家好啊!想必大家对 nginx 已经非常熟悉了,此处不再赘述。下面跟大家分享一个非常简单的 nginx 安装方法,就是 brew 安装啦。

2. 安装

➜  ~ brew install nginx
Running `brew update --auto-update`...
Error: Failed to download https://formulae.brew.sh/api/formula.jws.json!
==> Downloading https://formulae.brew.sh/api/formula.jws.json
curl: (35) Recv failure: Connection reset by peer

Warning: formula.jws.json: update failed, falling back to cached version.
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
==> New Formulae
lpeg                                                                  ssocr                                                                 svlint
==> New Casks
dadroit-json-viewer                                 huiontablet                                         nordic-nrf-command-line-tools                       synology-photo-station-uploader
eobcanka                                            jabra-direct                                        philips-hue-sync                                    synology-surveillance-station-client
fractal-bot                                         lg-onscreen-control                                 qflipper                                            synologyassistant
hp-easy-start                                       linn-konfig                                         synology-cloud-station-backup                       wacom-tablet

You have 1 outdated formula installed.

==> Fetching nginx
==> Downloading https://ghcr.io/v2/homebrew/core/nginx/manifests/1.23.4
Already downloaded: /Users/username/Library/Caches/Homebrew/downloads/a0e240157ec5079b89a98743836191aa6c5feaffbd02eae69a28ec457d777df1--nginx-1.23.4.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/nginx/blobs/sha256:e619828547bb5fe50761d5dcfb0271478ed734e576e2a589f9f1eaea0a3076a3
Already downloaded: /Users/username/Library/Caches/Homebrew/downloads/e36014648b45296ade9bcf124c853c2164592f34913115121e4b90e2bab7cc4b--nginx--1.23.4.arm64_ventura.bottle.tar.gz
==> Pouring nginx--1.23.4.arm64_ventura.bottle.tar.gz
==> Caveats
**Docroot is: /opt/homebrew/var/www**

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

nginx will load all files in **/opt/homebrew/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:
  **/opt/homebrew/opt/nginx/bin/nginx -g daemon off**;
==> Summary
🍺  **/opt/homebrew/Cellar/nginx/1.23.4**: 26 files, 2.2MB
==> Running `brew cleanup nginx`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

注意看,安装提示里会告诉我们一些非常有用的信息。加粗标记的地方啦。

3. 修改端口

使用 lsof -i :8080 查看下端口占用情况发现 8082 可用。

➜  ~ lsof -i :8080
COMMAND   PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
java    70090 youngx    8u  IPv6 0xdba0ddd207bcac25      0t0  TCP *:http-alt (LISTEN)
➜  ~ lsof -i :8082

修改下 niginx 配置 vim /opt/homebrew/etc/nginx/nginx.conf,改成 8082 端口。这里将 root 对应的文件夹进行修改是为了 前端工程化之 CI/CD 实践 做准备。

➜  ~ vim /opt/homebrew/etc/nginx/nginx.conf
.....
listen       8082;
server_name  localhost;

#charset koi8-r;

#access_log  logs/host.access.log  main;

location / {
    #root   html
    root   /Users/username/apps/app-cicd; #html
    index  index.html index.htm;
}
.....

4. 启动

brew services start nginx 启动 nginx。

➜  ~ brew services start nginx
==> Successfully started `nginx` (label: homebrew.mxcl.nginx)
➜  ~ nginx -s reload

打开 http://localhost:8082/,熟悉的页面出现了。

Untitled.png

5. 管理 nginx

brew 方式管理 nginx。

brew services start nginx
brew services stop nginx
brew services restart nginx

nginx 常用命令。

# stop — fast shutdown
# quit — graceful shutdown
# reload — reloading the configuration file
# reopen — reopening the log filesn

nginx -s stop

6. 参考资料

nginx.org/en/docs/