sudo /usr/local/bin/openresty
      
      nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
     
但是,通过命令:sudo netstat -n|grep 80 以及 ps -A|grep nginx 都找不到是哪个程序占用了80端口。
     后续探索的过程:
     
     1、lsof -P -itcp:80
     
     -P是显示端口号,而不是程序名. -i 后面跟”协议@server:port”
     
     找不到。
     
     2、sudo lsof -P -itcp:80 ,结果如下:
     
COMMAND    PID       USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
httpd       82       root    4u  IPv6 0x3ef15ffdc5dd853f      0t0  TCP *:80 (LISTEN)
httpd      268       _www    4u  IPv6 0x3ef15ffdc5dd853f      0t0  TCP *:80 (LISTEN)
SogouServ  368 xushengbin   20u  IPv4 0x3ef15ffdcda6971f      0t0  TCP 10.10.2.251:50215->180.149.156.142:80 (ESTABLISHED)
SogouServ  368 xushengbin   23u  IPv4 0x3ef15ffdcda6971f      0t0  TCP 10.10.2.251:50215->180.149.156.142:80 (ESTABLISHED)
httpd      658       _www    4u  IPv6 0x3ef15ffdc5dd853f      0t0  TCP *:80 (LISTEN)
httpd      659       _www    4u  IPv6 0x3ef15ffdc5dd853f      0t0  TCP *:80 (LISTEN)
httpd      660       _www    4u  IPv6 0x3ef15ffdc5dd853f      0t0  TCP *:80 (LISTEN)
httpd      662       _www    4u  IPv6 0x3ef15ffdc5dd853f      0t0  TCP *:80 (LISTEN)
httpd      663       _www    4u  IPv6 0x3ef15ffdc5dd853f      0t0  TCP *:80 (LISTEN)
httpd      664       _www    4u  IPv6 0x3ef15ffdc5dd853f      0t0  TCP *:80 (LISTEN)
     找到了。是mac自带的apache程序,自动启动了。
     
     3、也可以通过pstree,显示所有的进程:
     
|-+= 00082 root /usr/sbin/httpd -D FOREGROUND
 | |--- 00268 _www /usr/sbin/httpd -D FOREGROUND
 | |--- 00658 _www /usr/sbin/httpd -D FOREGROUND
 | |--- 00659 _www /usr/sbin/httpd -D FOREGROUND
 | |--- 00660 _www /usr/sbin/httpd -D FOREGROUND
 | |--- 00662 _www /usr/sbin/httpd -D FOREGROUND
 | |--- 00663 _www /usr/sbin/httpd -D FOREGROUND
 | \--- 00664 _www /usr/sbin/httpd -D FOREGROUND
      总结:
      
      1、现在也不明白,为啥”sudo netstat -n|grep 80″ 搜不到80端口对应的进程。
      
      2、lsof,加上sudo,才能显示所有用户打开的文件。
      
      3、ps -A|grep nginx, 占用80端口的程序不叫nginx,而是apache,所以搜不到
      
      4、安装完nginx,涉及到另外一个问题,怎样把brew install安装的程序,开机启动:
      
      目前了解到的有launchctl 和homebrew services两种方式:
      
      下面是官方对homebrew services的介绍:
      
      Integrates Homebrew formulae with MacOS X’s launchctl manager.
     
说明homebrew services是对launchctl的整合。那么最好就用homebrew services来控制mac下软件的开机启动。
     具体命令如下:
     
     比如我想把openresty开机启动:
     
     执行命令 brew info openresty
     
     输出结果:
     
To have launchd start homebrew/nginx/openresty now and restart at login:
brew services start homebrew/nginx/openresty
     接下来执行brew services start homebrew/nginx/openresty
     
     应该就把openresty加入开机启动项了。
     
     可通过brew services list 查看所有的开机启动项(这里面包括了通过launchctl load添加的开机启动项),输出结果如下:
     
memcached started xushengbin /Users/xushengbin/Library/LaunchAgents/homebrew.mxcl.memcached.plist
mysql     stopped
openresty started xushengbin /Users/xushengbin/Library/LaunchAgents/homebrew.mxcl.openresty.plist
php53     stopped
php56     stopped
php70     started xushengbin /Users/xushengbin/Library/LaunchAgents/homebrew.mxcl.php70.plist
redis     started xushengbin /Users/xushengbin/Library/LaunchAgents/homebrew.mxcl.redis.plist                  
      
       关机,重新启动,发动openresty并没有自动启动。想一想,应该是openresty占用80端口,必须是root账号启动,当前登录账号无法启动,因此,改为
       
       sudo brew services start homebrew/nginx/openresty
       
       就可以了。
       
       brew services start 和sudo brew services start 区别在于:
       
       前者是start service at login后者是start service at boot
      
     
      另外,如果想要清楚无效的开机启动项,可以用命令:
      
      brew services cleanup
     
上面的brew services
      参考文档:
      
      
       Starting and Stopping Background Services with Homebrew
      
      
      
       GitHub – Homebrew/homebrew-services: Starts Homebrew formulae’s plists with launchctl
      
     
附launchctl用法:
     运行 launchctl list 显示当前的启动脚本。
     
     sudo launchctl unload [path/to/script] 停止正在运行的启动脚本,再加上 -w 选项即可去除开机启动。
    
 
