大网数据广告活动
服务器运维问答

Deactivate default ports of systemd socket activation

· 52CE 我爱测

I have an Apache Server running on CentOS 8.2, whose sockets are managed by systemd socket activation. But I don't want it to listen on the default port 80, because I want to run a reverse proxy with Nginx at this port. Here's th…

问题原文

原帖链接 · user2009388 · CC BY-SA 4.0

I have an Apache Server running on CentOS 8.2, whose sockets are managed by systemd socket activation. But I don't want it to listen on the default port 80, because I want to run a reverse proxy with Nginx at this port. Here's the current systemd socket configuration:

root@server6:~# systemctl status httpd.socket
● httpd.socket - Apache httpd Server Socket
   Loaded: loaded (/usr/lib/systemd/system/httpd.socket; enabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/httpd.socket.d
           └─20-redir8016.conf, 30-listen8017ssl.conf, override.conf
   Active: active (running) since Fri 2020-11-20 14:45:13 UTC; 1 weeks 0 days ago
     Docs: man:httpd.socket(8)
   Listen: [::]:80 (Stream)
           [::]:8001 (Stream)
           [::]:8002 (Stream)
           [::]:8003 (Stream)
    Tasks: 0 (limit: 820374)
   Memory: 0B
   CGroup: /system.slice/httpd.socket

As you can see, port 80 is enabled. This is by default. The other ports were added deliebrately. The manpage of httpd.socket(8) only describes how to add another port, not how to remove the default ones. Port 80 is not configured in any configuration file in /etc/httpd.

How can I deactivate port 80 here? Where is the default configuration stored?

已采纳解答

原帖链接 · meuh · CC BY-SA 4.0

You can list the files being used by systemd for the socket by using:

$ systemctl cat httpd.socket

and on my system this shows (some lines removed to keep it short):

# /usr/lib/systemd/system/httpd.socket
[Unit]
Description=Apache httpd Server Socket
[Socket]
ListenStream=80
[Install]
WantedBy=sockets.target

# /usr/lib/systemd/system/httpd.socket.d/10-listen443.conf
[Socket]
ListenStream=443

You can see that 2 named files are used (on my system) to listen on ports 80 and 443. You can use sudo systemctl edit httpd.socket to add more configuration. To actually cancel an existing configuration, you need to use the standard systemd method of entering an empty value for the keyword, i.e. in this case: ListenStream=, followed by any wanted additional values:

[Socket]
# cancel existing port 80 (and 443)
ListenStream=
ListenStream=8001
ListenStream=8002
ListenStream=8003

man systemd.syntax (or man systemd.unit in older versions) says

Various settings are allowed to be specified more than once, in which case the interpretation depends on the setting. Often, multiple settings form a list, and setting to an empty value "resets", which means that previous assignments are ignored.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-11-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。