AdSense

網頁

2024/1/11

Mac Homebrew 安裝Nginx

在Mac以Homebrew安裝Nginx。


安裝環境:

  • MacBook Pro Apple M1 Pro
  • macOS Ventura 13.0.1
  • Homebrew 4.2.3


安裝

在終端機輸入brew install nginx以Homebrew安裝Nginx。

% brew install nginx
==> Downloading https://ghcr.io/v2/homebrew/core/nginx/manifests/1.25.3
#=#=-  #       #                                                                                                                                                                                                                                               #=O#-     #        #                                                                                                                                                                                                                                           -#O=- #      #          #                                                                                                                                                                                                                                      -=O#-   #        #           #                     ######################################################################################################################################################################################################################################################### 100.0%
==> Fetching dependencies for nginx: pcre2
==> Downloading https://ghcr.io/v2/homebrew/core/pcre2/manifests/10.42
######################################################################################################################################################################################################################################################### 100.0%
==> Fetching pcre2
==> Downloading https://ghcr.io/v2/homebrew/core/pcre2/blobs/sha256:8423a338c590ab1a6f265b39a9d1a67ab1361a586f0e494a8c9555cff2867536
#=#=-  #       #                                                                                                                                                                                                                                               #=O#-     #        #                                                                                                                                                                                                                                           -#O=- #      #          #                                                                                                                                                                                                                                      -=O#-   #        #           #                     ######################################################################################################################################################################################################################################################### 100.0%
==> Fetching nginx
==> Downloading https://ghcr.io/v2/homebrew/core/nginx/blobs/sha256:80881b0bc5bdc5fac40ddcaa3fc34d68a89ddd6d61505c9489e5699f26430d90
#=#=-  #       #                                                                                                                                                                                                                                               #=O#-     #        #                                                                                                                                                                                                                                           -#O=- #      #          #         ######################################################################################################################################################################################################################################################### 100.0%
==> Installing dependencies for nginx: pcre2
==> Installing nginx dependency: pcre2
==> Downloading https://ghcr.io/v2/homebrew/core/pcre2/manifests/10.42
Already downloaded: /Users/user/Library/Caches/Homebrew/downloads/6a53794fcaabc5cc5e05b19c02ca9c4c5f2cb9a4d65a5790a6841146465b040f--pcre2-10.42.bottle_manifest.json
==> Pouring pcre2--10.42.arm64_ventura.bottle.tar.gz
🍺  /opt/homebrew/Cellar/pcre2/10.42: 230 files, 6.2MB
==> Installing nginx
==> Pouring nginx--1.25.3.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 start nginx now and restart at login:
  brew services start 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.25.3: 26 files, 2.4MB
==> Running `brew cleanup nginx`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Caveats
==> nginx
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 start nginx now and restart at login:
  brew services start nginx
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/nginx/bin/nginx -g daemon\ off\;

輸入nginx -v檢視版本資訊。

% nginx -v
nginx version: nginx/1.25.3


預設配置文件 nginx.conf

預設配置文件nginx.conf位置在/opt/homebrew/etc/nginx/

% ls /opt/homebrew/etc/nginx/
fastcgi.conf		fastcgi_params		koi-utf			mime.types		nginx.conf		scgi_params		servers			uwsgi_params.default
fastcgi.conf.default	fastcgi_params.default	koi-win			mime.types.default	nginx.conf.default	scgi_params.default	uwsgi_params		win-utf

內容如下(已刪除註解)。

/opt/homebrew/etc/nginx/nginx.conf

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       8080;
        server_name  localhost;

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

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    include servers/*;
}


預設首頁

預設首頁index.html的位置在/opt/homebrew/var/www

% ls /opt/homebrew/var/www
50x.html	index.html

Homebrew啟動

輸入sudo brew services start nginx啟動Nginx服務。

% sudo brew services start nginx
Password:
Warning: Taking root:admin ownership of some nginx paths:
  /opt/homebrew/Cellar/nginx/1.25.3/bin
  /opt/homebrew/Cellar/nginx/1.25.3/bin/nginx
  /opt/homebrew/opt/nginx
  /opt/homebrew/opt/nginx/bin
  /opt/homebrew/var/homebrew/linked/nginx
This will require manual removal of these paths using `sudo rm` on
brew upgrade/reinstall/uninstall.
Warning: nginx must be run as non-root to start at user login!
==> Successfully started `nginx` (label: homebrew.mxcl.nginx)

輸入sudo brew services list查看nginx服務狀態為啟動(started)。

% sudo brew services list
Name          Status  User File
nginx         started root /Library/LaunchDaemons/homebrew.mxcl.nginx.plist


Homebrew停止

輸入sudo brew services stop nginx停止Nginx服務。

% sudo brew services stop nginx
Stopping `nginx`... (might take a while)
==> Successfully stopped `nginx` (label: homebrew.mxcl.nginx)


程序啟動

除了以Homebrew服務的方式啟動,也可以以一般程序啟動。

輸入nginx啟動Nginx。

% nginx

輸入ps aux | grep nginx檢視Nginx在Mac的運行程序,也可在macOS的活動監視器查看(Process Manager)。

% ps aux | grep nginx
user         38624   0.0  0.0 408636096   1472 s005  S+   10:22上午   0:00.00 grep nginx
user         38371   0.0  0.0 408923040   1552   ??  S    10:17上午   0:00.01 nginx: worker process
user         38370   0.0  0.0 408643488    432   ??  Ss   10:17上午   0:00.00 nginx: master process nginx


程序停止

輸入nginx -s stop停止Nginx

% nginx -s stop


首頁

啟動後在瀏覽器輸入http://localhost:8080/前往Nginx預設首頁。





沒有留言:

AdSense