Mac下通過 HomeBrew 安裝和配置 Nginx

原文鏈接:https://www.jianshu.com/p/026d67cc6cb1

Nginx(音“engine x”)是一款開源的web和反向代理服務器,查看官網。具有高性能、高併發、低內存特點,另外還有一些特色的Web服務器功能,如負載均衡、緩存、訪問和帶寬控制。


MAC環境下有一個好用的軟件安裝方法,使用homebrew來安裝各個軟件既快捷又不用考慮依賴包的問題。

1、先安裝homebrew。官網。終端執行以下命令:

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

出現以下log表示安裝完成了。

Installation successful!

2、安裝Nginx,終端下執行:

brew install nginx

安裝過程中會自己安裝依賴:

$ brew install nginx
Warning: You have Xcode 8 installed without the CLT;
this causes certain builds to fail on OS X El Capitan (10.11).
Please install the CLT via:
  sudo xcode-select --install
==> Installing dependencies for nginx: openssl, pcre
==> Installing nginx dependency: openssl

安裝完成後會有以下log:

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 have launchd 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:
  nginx
==> Summary
🍺  /usr/local/Cellar/nginx/1.10.1: 7 files, 972.3K

3.鑑於安裝過程中出現的Warning,爲避免將來掉坑,運行

sudo xcode-select --install

終端會出現提示

xcode-select: note: install requested for command line developer tools

並會詢問安裝工具

xcode-select

點擊安裝,等待軟件安裝完成即可。

4.啓動nginx服務

brew services start nginx
$ brew services start nginx
==> Tapping homebrew/services
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 10 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (10/10), done.
Checking connectivity... done.
Tapped 0 formulae (36 files, 46.1K)
==> Successfully started `nginx` (label: homebrew.mxcl.nginx)

成功後,使用瀏覽器打開 http://localhost:8080 或者 http://localhost

如果打開頁面如下,證明安裝完成,可以使用了。

5. 關閉nginx服務

brew services stop nginx

6. nginx文件目錄

6.1 nginx安裝文件目錄
/usr/local/Cellar/nginx
6.2 nginx配置文件目錄
/usr/local/etc/nginx
6.3 配置文件
/usr/local/etc/nginx/nginx.conf
6.4 系統hosts位置
/private/etc/hosts

7. nginx常用命令

用 homebrew 啓動和關閉 nginx 的命令和 nginx 自帶的命令是重複的,建議使用 nginx 自帶的命令,比較簡短。

nginx  #啓動nginx
nginx -s reopen  #重啓
nginx -s quit  #安全關閉
nginx -s stop  #快速停止
nginx -s reload  #重新加載配置
nginx -V #查看版本,以及配置文件地址
nginx -v #查看版本
nginx -h #幫助
$ nginx -h #幫助
nginx version: nginx/1.10.1
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/Cellar/nginx/1.10.1/)
  -c filename   : set configuration file (default: /usr/local/etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

8.卸載nginx

brew uninstall nginx



 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章