Nginx for Mac install、start、stop 、reload实践

原创 陈宝佳  2018-06-25 19:15  阅读 418 次 评论 0 条
行云管家,新手有礼

1、Nginx Install

执行如下brew命令:(brew 又叫Homebrew,brew类似CentOS系统下的Yum软件包管理工具,能在Mac中方便的安装软件或者卸载软件 )

brew search nginx 			# brew 查询Nginx

brew install nginx			# brew 安装Nginx

安装完成后,可在终端输通过 brew list nginx 来显示nginx安装路径:

  • 通过brew install安装应用会在/usr/local/Cellar/目录下;
  • 某些应用会自动创建软链接放在 /usr/bin 或者 /usr/sbin,同时也可能将整个文件夹放在/usr/local
  • 可以使用 brew list 软件名 (比如 brew list Nginx)确定Nginx安装位置。
# brew list nginx 来显示安装路径

MacBookPro:~ mac$ brew list nginx		

/usr/local/Cellar/nginx/1.15.0/.bottle/etc/ (15 files)

/usr/local/Cellar/nginx/1.15.0/bin/nginx

/usr/local/Cellar/nginx/1.15.0/homebrew.mxcl.nginx.plist

/usr/local/Cellar/nginx/1.15.0/html -> ../../../var/www		# 服务器默认路径

/usr/local/Cellar/nginx/1.15.0/share/man/man8/nginx.8



# nginx 安装路径

MacBookPro:1.15.0 mac$ ll /usr/local/Cellar/nginx/1.15.0/	
total 600
-rw-r--r--  1 mac  admin  288216  6  5 21:47 CHANGES
-rw-r--r--  1 mac  admin     695  6 25 18:18 INSTALL_RECEIPT.json
-rw-r--r--  1 mac  admin    1397  6  5 21:47 LICENSE
-rw-r--r--  1 mac  admin      49  6  5 21:47 README
drwxr-xr-x  3 mac  admin      96  6  5 21:47 bin
-rw-r--r--  1 mac  admin     571  6 25 18:18 homebrew.mxcl.nginx.plist
lrwxr-xr-x  1 mac  admin      16  6 25 18:18 html -> ../../../var/www
drwxr-xr-x  3 mac  admin      96  6  5 21:47 share


# Nginx www目录
MacBookPro:~ mac$ cd /usr/local/Cellar/nginx/1.15.0/
MacBookPro:1.15.0 mac$ ll ../../../var/www			# www服务默认路径
total 16
-rw-r--r--  1 mac  admin  537  6  5 21:47 50x.html
-rw-r--r--  1 mac  admin  612  6  5 21:47 index.html
MacBookPro:1.15.0 mac$ pwd
/usr/local/Cellar/nginx/1.15.0


# 寻找Nginx 配置文件
MacBookPro:1.15.0 mac$ find /usr/ -name "nginx.conf"
/usr//local/etc/nginx/nginx.conf					# nginx配置文件路径

更改默认端口8080为80端口

MacBookPro:1.15.0 mac$ vim /usr/local/etc/nginx/nginx.conf

Nginx Start

在终端中输入

MacBookPro:1.15.0 mac$ ps -ef|grep nginx|grep -v grep

如果执行的结果是

  501 57599     1   0  6:24下午 ??         0:00.01 nginx: master process nginx
  501 57672 57599   0  6:29下午 ??         0:00.00 nginx: worker process
    0 57704     1   0  6:30下午 ??         0:00.00 nginx: master process nginx
   -2 57705 57704   0  6:30下午 ??         0:00.00 nginx: worker process

表示已启动成功,如果不是上图结果,在终端中执行

MacBookPro:1.15.0 mac$ nginx  

# 

MacBookPro:1.15.0 mac$ /usr/local/Cellar/nginx/1.15.0/bin/nginx -c /usr/local/etc/nginx/nginx.conf # -c 指定启动配置文件

命令即可启动Nginx。

这时候如果成功访问127.0.0.1,说明成功安装和启动好了。

Nginx stop

法一:kill 命令

在终端中输入 ps -ef|grep nginx |grep -v grep|awk '{print $2}' 获取到nginx的进程号。

MacBookPro:1.15.0 mac$ sudo kill -9 `ps -ef|grep nginx  |grep -v grep|awk '{print $2}'`
Password:
MacBookPro:1.15.0 mac$ ps -ef|grep nginx  |grep -v grep

在终端中输入以下几种命令都可以停止

kill -QUIT ps -ef|grep nginx |grep -v grep|awk '{print $2}' (从容的停止,即不会立刻停止)

kill -TERM ps -ef|grep nginx |grep -v grep|awk '{print $2}'(立刻停止)

kill -INT ps -ef|grep nginx |grep -v grep|awk '{print $2}'(和上面一样,也是立刻停止)

法二:Nginx -s stop

MacBookPro:1.15.0 mac$ ps -ef|grep nginx  |grep -v grep

    0 58154     1   0  7:01下午 ??         0:00.00 nginx: master process nginx

   -2 58155 58154   0  7:01下午 ??         0:00.00 nginx: worker process

MacBookPro:1.15.0 mac$ sudo nginx -s stop

MacBookPro:1.15.0 mac$ ps -ef|grep nginx  |grep -v grep

Nginx reload

如果配置文件错误,则将启动失败,在启动nginx之前,需要先验证在配置文件的正确性,如下表示配置文件正确

法一:

MacBookPro:1.15.0 mac$ sudo nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok	# OK 表示没问题。
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

法二:

MacBookPro:1.15.0 mac$ sudo /usr/local/Cellar/nginx/1.15.0/bin/nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok	# OK 表示没问题
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

在终端输入输入如下命令即可重启


MacBookPro:1.15.0 mac$ sudo nginx -s reload

# 

MacBookPro:1.15.0 mac$ sudo /usr/local/Cellar/nginx/1.15.0/bin/nginx -s reload

#  根据进程号重启,执行命令 kill -HUP 进程号

MacBookPro:1.15.0 mac$ sudo kill -HUP `ps -ef|grep nginx  |grep -v grep|grep master|awk '{print $2}'`

FAQ

Mac启动Nginx报错 nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)

MacBookPro:~ mac$ lsof -i :80

MacBookPro:~ mac$ nginx 

nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)

MacBookPro:~ mac$ lsof -i :80

MacBookPro:~ mac$ sudo nginx	# 原因:1024以下端口启动时需要root权限,所以sudo nginx 即可解决。

Password:

MacBookPro:~ mac$ lsof -i :80

COMMAND   PID USER   FD   TYPE            DEVICE SIZE/OFF NODE NAME

Google    574  mac  241u  IPv4 0x7f3f9f18ce44b15      0t0  TCP localhost:54784->localhost:http (CLOSE_WAIT)

Google    574  mac  247u  IPv4 0x7f3f9f18c9581b5      0t0  TCP localhost:54785->localhost:http (CLOSE_WAIT)

Google    574  mac  248u  IPv4 0x7f3f9f17cc5c855      0t0  TCP localhost:54790->localhost:http (CLOSE_WAIT)

Google    574  mac  250u  IPv4 0x7f3f9f18813b1b5      0t0  TCP localhost:54791->localhost:http (CLOSE_WAIT)

Google    574  mac  334u  IPv4 0x7f3f9f186ab4855      0t0  TCP 192.168.5.56:54650->121.9.246.112:http (CLOSED)

(完)

本文地址:https://www.cloudbility.com/club/7496.html
关注我们:请关注一下我们的微信公众号:扫描二维码,公众号:cloudbility
版权声明:本文为原创文章,版权归 陈宝佳 所有,欢迎分享本文,转载请保留出处!

发表评论


表情