题记:对接项目用到了nginx才发现买了mac从没摸过nginx,记录一下在mac上安装nginx的经历!
一、使用brew安装nginx
1,安装:
brew install nginx
2,查看安装信息(经常用到, 比如查看安装目录等)
brew info nginx
说明:安装后,主要看brew把nginx安装到哪里去了,默认是安装到/usr/local/etc/nginx/nginx.conf
Action:~ wangjun$ brew info nginx
nginx: stable 1.17.9 (bottled), HEAD
HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server
https://nginx.org/
/usr/local/Cellar/nginx/1.17.9 (25 files, 2.1MB) *
Poured from bottle on 2020-07-13 at 08:58:04
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/nginx.rb
==> Dependencies
Required: openssl@1.1 ✔, pcre ✔
==> Options
--HEAD
Install HEAD version
==> Caveats
Docroot is: /usr/local/var/www
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
==> Analytics
install: 31,080 (30 days), 104,700 (90 days), 408,180 (365 days)
install-on-request: 30,338 (30 days), 101,623 (90 days), 393,792 (365 days)
build-error: 0 (30 days)
Action:~ wangjun$
这就安装成功了!
二、常用命令
1,查看nginx版本的命令
Action:~ wangjun$ nginx -v
nginx version: nginx/1.17.9
2,启动nginx服务
第一种方式:
启动nginx:sudo nginx
Action:log wangjun$ brew services start nginx
==> Successfully started `nginx` (label: homebrew.mxcl.nginx)
3,查看nginx是否启动成功
Action:~ wangjun$ brew services start nginx
Service `nginx` already started, use `brew services restart nginx` to restart.
Action:~ wangjun$ ps -ef|grep nginx
501 2968 1 0 9:09AM ?? 0:00.01 nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;
501 2971 2968 0 9:09AM ?? 0:00.03 nginx: worker process
501 11022 10470 0 9:17PM ttys002 0:00.00 grep nginx
4,日志位置
/usr/local/var/log/nginx/error.log
查看日志
tail -f *
5,关闭nginx服务
第一种方式:
关闭nginx:sudo nginx -s stop
第二种方式:
Action:log wangjun$ brew services stop nginx
Stopping `nginx`... (might take a while)
==> Successfully stopped `nginx` (label: homebrew.mxcl.nginx)
6,重启nginx服务
第一种方式:
重启nginx:sudo nginx -s reload
7,判断配置文件是否正确:
nginx -t -c /usr/local/nginx/conf/nginx.conf
或者
cd /usr/local/nginx/sbin
./nginx -t
8,也可将配置文件分离
在主配置文件中添加:include servers/*; 该语句会加载servers文件夹下所有的配置信息。
在/usr/local/etc/nginx文件夹下新增servers文件夹,