80端口占用问题

问题背景

使用vite将项目启动配置到80端口的时候发现80端口被占用。

通过浏览器访问该页面时显示 it works .响应头的Server中显示由apache提供服务

问题排查

通过端口命令,查找占用端口的进程

sudo lsof -i:80
COMMAND  PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
httpd    145 root    4u  IPv6 0x86fb7c76ad702387      0t0  TCP *:http (LISTEN)
httpd   1214 _www    4u  IPv6 0x86fb7c76ad702387      0t0  TCP *:http (LISTEN)
httpd   3143 _www    4u  IPv6 0x86fb7c76ad702387      0t0  TCP *:http (LISTEN)
httpd   3144 _www    4u  IPv6 0x86fb7c76ad702387      0t0  TCP *:http (LISTEN)
httpd   3145 _www    4u  IPv6 0x86fb7c76ad702387      0t0  TCP *:http (LISTEN)

尝试通过命令停用服务释放端口占用,显示该服务未启用

/opt/homebrew/opt/httpd/bin/httpd -k stop
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using admindeMacBook-Pro-7.local. Set the 'ServerName' directive globally to suppress this message
httpd (pid 5691?) not running

查看apache的index.html路径,并将文本内容改为 it ,访问本机环回地址仍显示it works ,初步推断本地存在不止一个apache。

通过监视器查询与端口关联进程145获得信息,父进程为launched,用户为root。

由于该服务被kill掉以后会被立即重启,因此需要从守护服务当中关闭掉该服务。

在守护进程相关目录下进行httpd服务查询。

与守护进程相关目录为:

TypeLocationRun on behalf of
User Agents~/Library/LaunchAgentsCurrently logged in user
Global Agents/Library/LaunchAgentsCurrently logged in user
Global Daemons/Library/LaunchDaemonsroot or the user specified with the key UserName
System Agents/System/Library/LaunchAgentsCurrently logged in user
System Daemons/System/Library/LaunchDaemonsroot or the user specified with the key UserName

查找系统守护进程目录下的httpd服务,并移除该服务。

/System/Library/LaunchDaemons sudo launchctl list  | grep httpd
11344	0	org.apache.httpd

查看该服务相关配置:

cat org.apache.httpd.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Disabled</key>
	<true/>
	<key>Label</key>
	<string>org.apache.httpd</string>
	<key>EnvironmentVariables</key>
	<dict>
		<key>XPC_SERVICES_UNAVAILABLE</key>
		<string>1</string>
		<key>OBJC_DISABLE_INITIALIZE_FORK_SAFETY</key>
		<string>YES</string>
	</dict>
	<key>ProgramArguments</key>
	<array>
		<string>/usr/sbin/httpd-wrapper</string>
		<string>-D</string>
		<string>FOREGROUND</string>
	</array>
	<key>KeepAlive</key>
	<true/>
</dict>
</plist>

删除掉该守护服务后,80端口被释放。

/System/Library/LaunchDaemons sudo launchctl remove org.apache.httpd

至此,问题解决。

问题结论

由于系统守护进程中包含了一个httpd服务,导致该服务会自启动并占用80端口。

参考资料
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值