Apache Listen: Binding to IP Addresses and Ports

When the Apache HTTPD web server starts, it must bind to a certain IP address and port on the machine so that it can start accepting (or listening) to incoming HTTP requests. How does it know which network interface and port to bind on ? This must be specified in the main server configuration using the Listen directive. If it is not put in the config file, the server will fail to start.

Basic Usage

This is how the usage syntax for Listen looks like:

Listen [IP-address:]portnumber [protocol]

Listen tells httpd to bind to the specified IP Address (optional) and port to accept incoming client requests. By default, new installations of the server should already have a Listen 80 in the main server config which basically instructs Apache to listen on all available network interfaces on port 80. Let’s quickly see how it can be used:

# Bind to all IP address (or network interfaces), on port 80 (http)
Listen 80

# Bind to all IP interfaces, on port 443 (https, TLS)
Listen 443

# Accept connections on two different interfaces and ports
#
# Make sure when specifying multiple combinations of
# address-and-port, they are different (non-overlapping)
#
# Overlapping = When more than one directives refer to at least
# one address-and-port combination together
Listen 10.122.0.2:80
Listen 10.47.0.5:8080

# Overlapping address-and-port combination will lead to a fatal
# error preventing the server from starting up or restarting
#
# httpd[4093]: (98)Address already in use: AH00072: make_sock:
# could not bind to address 143.110.176.71:80
Listen 80
Listen 143.110.176.71:80

# Start httpd on the specified IPv6 address, on port 9999
Listen [2400:6180:100:d0::b03:5001]:9999

From the examples above, these are the things or general rules to keep in mind:

  • If only a port number is specified without any IP address, then the server will listen to the given port on all interfaces or IP addresses. Eg: Listen 80.
  • If only an IP is specified without any port, the server will fail to start (or restart). Eg: Listen 143.110.176.71
  • If a port is specified with an IP address, i.e., an address-and-port combination, then the server will bind only to that specified IP address and port. Eg: Listen 143.110.176.71:80.
  • Multiple Listen directives can be specified to listen on different combinations of IP address and ports. Just make sure that they do not conflict with each other, i.e., if two entries target the same address-and-port combination (overlaps) then it’ll lead to a fatal error – could not bind to address – preventing the server from starting or restarting.

Protocol Argument

The keen observers would’ve noticed the protocol argument in the syntax above. By default it is https for port 443 and http for all other ports. Hence in most of the cases you wouldn’t need to specify it. It is just used to:

  • Determine the relevant protocol-related module that should handle an incoming request.
  • Apply some protocol optimizations at the operating system level.

If you ever pass the protocol argument due to reasons like running https on a different port, then you can do it like this:

# Using 9443 instead of 443
Listen 143.110.176.71:9443 https

Wrapping Up

Listen just specifies the address-and-port combination for the Apache web server to bind to or listen for incoming requests. To actually configure Apache to handle requests from clients on different interfaces and ports or for different hostnames, you’ll have to make changes in the main server configuration or define virtual hosts (<VirtualHost>).

Leave a Reply

Your email address will not be published. Required fields are marked *