mac view port occupied

When I started nginx today, I reported the following error:

sudo /usr/local/bin/openresty
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)

However, through the commands: sudo netstat -n|grep 80 and ps -A|grep nginx, I can't find which program occupies port 80.

The process of subsequent exploration:
1. lsof -P -itcp:80
-P is to display the port number, not the program name. -i followed by "protocol@server:port" cannot be
found.
2. sudo lsof -P -itcp:80 , the results are as follows:
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)

found it. It is the apache program that comes with the mac and starts automatically.
3. You can also display all processes through 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

Summary:
1. I still don't understand why "sudo netstat -n|grep 80" can't find the process corresponding to port 80.
2. lsof, plus sudo, can display all files opened by users.
3. ps -A|grep nginx, the program occupying port 80 is not called nginx, but apache, so it cannot be found
. 4. After installing nginx, another problem is involved. How to start the program installed by brew install:
Currently There are two ways to learn about launchctl and homebrew services:
the following is the official introduction to homebrew services:
Integrates Homebrew formulae with MacOS X's launchctl manager.

Explain that homebrew services is an integration of launchctl. Then it is best to use homebrew services to control the startup of software under mac.

The specific command is as follows:
For example, I want to start openresty:
execute the command brew info openresty to
output the result:
To have launchd start homebrew/nginx/openresty now and restart at login:
brew services start homebrew/nginx/openresty
Next, executing brew services start homebrew/nginx/openresty
should add openresty to the startup items.
You can view all startup items (including those added through launchctl load) through brew services list. The output is as follows:
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                  

Shutting down, restarting, launching openresty did not start automatically. Think about it, it should be openresty occupying port 80, and it must be started by the root account. The current login account cannot be started. Therefore, you can change to
sudo brew services start homebrew/nginx/openresty
.
The difference between brew services start and sudo brew services start is:
the former is start service at login and the latter is start service at boot

In addition, if you want to clear invalid startup items, you can use the command:
brew services cleanup

brew services above


参考文档:
Starting and Stopping Background Services with Homebrew
GitHub - Homebrew/homebrew-services: Starts Homebrew formulae's plists with launchctl


With launchctl usage:

Running launchctl list displays the current launch script.
sudo launchctl unload [path/to/script] Stop the running startup script, plus the -w option to remove the startup.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324747823&siteId=291194637