1. 安裝 Homebrew
首先 homebrew是什麼?它是Mac中的一款軟件包管理工具,經過brew能夠很方便的在Mac中安裝軟件或者是卸載軟件。不瞭解的同窗看以看官網(https://brew.sh/index_zh-cn.html), 而後在咱們命令行中複製以下命令:php
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
運行,以下所示:html
安裝成功後的話,咱們能夠使用命令 brew update 更新下;以下命令:nginx
有關brew經常使用的指令以下:git
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 listgithub
2. brew安裝nginxruby
1. 使用brew安裝nginx,以下命令所示:服務器
brew install nginx
以下圖所示:session
2. 查看nginx的配置信息,以下命令:app
brew info nginx
如上面的截圖,From:xxx 這樣的,是nginx的來源,Docroot默認爲 /usr/local/var/www, 在/usr/local/etc/nginx/nginx.conf 配置文件中默認的端口爲8080, 且nginx將在/usr/local/etc/nginx/servers 目錄中加載全部文件。而且咱們能夠經過最簡單的命令'nginx' 來啓動nginx.curl
3. 查看nginx安裝目錄, 以下命令:
open /usr/local/etc/nginx/
以下所示:
打開nginx目錄後,能夠看到咱們上面的使用 brew info nginx 查看信息所說的 server目錄以及nginx.conf的配置文件,那麼咱們的nginx被安裝到什麼地方呢?咱們從上面的截圖能夠看到,是在 這個目錄下 /usr/local/Cellar/nginx,執行以下命令能夠查看到:
open /usr/local/Cellar/nginx
會打包目錄,以下所示:
進入上面的 1.15.5文件後,以下所示:
在該目錄下能夠看到一個名字爲html的快捷方式的文件夾,進入該目錄後,它有兩個文件50.html和index.html,以下圖所示:
其實它是指向的就是 /usr/local/var/wwww目錄的,爲何這麼說,咱們來看下進入該命令後,查看下面有哪些文件就能夠看到,以下圖:
2. 啓動nginx服務,以下命令:
brew services start nginx // 重啓的命令是: brew services restart nginx
以下圖所示:
重啓後,咱們驗證下,由於nginx默認的端口號是8080,所以咱們頁面訪問 http://localhost:8080 便可,看到以下信息:
若是成功的話,通常都是 歡迎的界面(index.html頁面我本身改過),下面咱們繼續查看下nginx.conf 配置信息,使用以下命令:
cat /usr/local/etc/nginx/nginx.conf (或者使用 sudo open /usr/local/etc/nginx/nginx.conf -a 'sublime text' 使用編輯器sublime打開)。
以下配置信息:
#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.15.5
2. nginx啓動:
2.1. 在終端輸入 ps -ef|grep nginx 命令看是否有啓動,以下:
2.2. 驗證配置文件是否正確,所以在啓動nginx以前,咱們能夠先運行下以下命令:
sudo /usr/local/Cellar/nginx/1.15.5/bin/nginx -t -c /usr/local/etc/nginx/nginx.conf
注意:必定要注意路徑是不是本身的安裝路徑。這邊個人nginx是1.15.5版本的。
若是出現以下信息,說明配置文件正確。
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
重啓nginx有以下幾種方法:
2.3. 經過brew,brew services start nginx(啓動nginx) brew services restart nginx(重啓命令), 以下所示:
2.4. 先進入bin目錄:cd /usr/local/Cellar/nginx/1.15.5/bin/, 而後再執行:./nginx -s reload, 以下所示:
2.5. 根據進程號重啓,執行命令 kill -HUP 進程號 以下所示:
3. nginx中止
終端輸入ps -ef|grep nginx獲取到nginx的進程號, 注意是找到「nginx:master」的那個進程號
其中,master process nginx表示第一個進程是主進程。worker process表示爲子進程。72是主進程號。234是子進程號。
kill -QUIT 72 (從容的中止,即不會馬上中止)
Kill -TERM 72 (馬上中止)
Kill -INT 72 (和上面同樣,也是馬上中止)