一、安装(Mac OS)
执行 brew search nginx 和 brew install nginx 命令:
$ brew search nginx ==> Searching local taps... nginx ==> Searching taps on GitHub... ==> Searching blacklisted, migrated and deleted formulae...
$ brew install nginx ==> Installing dependencies for nginx: openssl, pcre ==> Installing nginx dependency: openssl ==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2o_1.high_sierr ######################################################################## 100.0% ==> Pouring openssl-1.0.2o_1.high_sierra.bottle.tar.gz ==> Caveats A CA file has been bootstrapped using certificates from the SystemRoots keychain. To add additional certificates (e.g. the certificates added in the System keychain), place .pem files in /usr/local/etc/openssl/certs and run /usr/local/opt/openssl/bin/c_rehash This formula is keg-only, which means it was not symlinked into /usr/local, because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries. If you need to have this software first in your PATH run: echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile For compilers to find this software you may need to set: LDFLAGS: -L/usr/local/opt/openssl/lib CPPFLAGS: -I/usr/local/opt/openssl/include ==> Summary ? /usr/local/Cellar/openssl/1.0.2o_1: 1,791 files, 12.3MB ==> Installing nginx dependency: pcre ==> Downloading https://homebrew.bintray.com/bottles/pcre-8.42.high_sierra.bottl ######################################################################## 100.0% ==> Pouring pcre-8.42.high_sierra.bottle.tar.gz ? /usr/local/Cellar/pcre/8.42: 204 files, 5.3MB ==> Installing nginx ==> Downloading https://homebrew.bintray.com/bottles/nginx-1.13.11.high_sierra.b ######################################################################## 100.0% ==> Pouring nginx-1.13.11.high_sierra.bottle.tar.gz ==> 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 ==> Summary ? /usr/local/Cellar/nginx/1.13.11: 23 files, 1.4MB
安装完以后,可以在终端输出的信息里看到一些配置路径:
配置文件路径: /usr/local/etc/nginx/nginx.conf
服务器默认路径: /usr/local/var/www
安装路径: /usr/local/Cellar/nginx/1.13.11
二、启动
执行 ps -ef|grep nginx 命令:(直接执行 nginx 也可以 命令行看上去没效果,但是已经启动)
$ ps -ef|grep nginx 501 3178 1 0 1:38下午 ?? 0:00.00 nginx: master process nginx 501 3179 3178 0 1:38下午 ?? 0:00.00 nginx: worker process 501 3183 1595 0 1:39下午 ttys039 0:00.00 grep nginx
表示已启动成功,如果不是上面结果,在终端中执行 /usr/local/Cellar/nginx/1.13.11/bin/nginx -c /usr/local/etc/nginx/nginx.conf 命令即可启动nginx。
这时候如果成功访问localhost:8080,说明成功安装和启动好了。
三、停止
输入 启动命令(ps -ef|grep nginx) 获取到nginx的进程号,注意是找到“nginx:master”的那个进程号,如上是 3178。
以下几种命令都可以停止:
kill -QUIT 3178 (从容的停止,即不会立刻停止)
Kill -TERM 3178 (立刻停止)
Kill -INT 3178 (立刻停止)
四、重启
如果配置文件错误,则将启动失败,所以在启动nginx之前,需要先验证在配置文件的正确性,如下表示配置文件正确。
$ /usr/local/Cellar/nginx/1.13.11/bin/nginx -t -c /usr/local/etc/nginx/nginx.conf 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
重启有两种方法:
1. $ /usr/local/Cellar/nginx/1.13.11/bin/nginx -s reload 或者 $ cd /usr/local/Cellar/nginx/1.13.11/bin $ ./nginx -s reload
2. 根据进程号重启,执行命令 kill -HUP 进程号。