Nginx 安装 启动 停止 重启

本文介绍如何在MacOS系统上安装Nginx,并提供了启动、停止及重启Nginx的方法。通过Homebrew安装Nginx,设置默认网站路径及端口等关键步骤。同时提供验证配置文件正确性的指令。

一、安装(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 进程号。

转载于:https://www.cnblogs.com/pinweyshg/p/8820091.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值