Apache | Basic Apache Configuration | Domain Name and Port Number (ServerName, Listen)
This article explains how to configure the domain name and port number, ServerName and Listen, in Apache.
ServerName directive: specifying a domain
The ServerName directive specifies the name Apache uses to identify its own domain name. For example, the domain name configured here is used when Apache displays an error page and includes the domain name that identifies itself.
It is normally the domain name assigned to the IP address, but it can also be used when you want to assign a separate name and use it as the official domain name. If ServerName is not specified, Apache performs a reverse lookup on its assigned IP address to obtain the domain name.
The format is as follows.
ServerName [scheme://]domain name|IP address[:port number]
ServerName is usually specified in the form domain name:port number.
Search for ServerName in the httpd.conf file, and you should find content like the following.
#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
#ServerName www.example.com:80
By default, the line is commented out with # at the beginning.
This example is for running Apache in a local environment, but for a web server published externally, it would typically be specified like www.example.com.
ServerName www.example.com:80
The port number is optional and can be omitted, but when omitted, the HTTP port number is used. Specifying the port number is recommended.
Listen directive: specifying the port number that accepts requests
The Listen directive specifies the port number on which Apache accepts requests from outside. The format is as follows.
Listen [IP address:] port number [protocol]
Search for Listen in the httpd.conf file, and you should find content like the following.
#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 80
You can confirm that port 80 is specified as the port number that accepts requests.
Multiple port numbers can be specified by writing multiple Listen directives. The following configuration accepts requests on ports 80 and 8080.
Listen 80
Listen 8080
If only the port number is specified, Apache accepts requests for that port on all interfaces. If the server running Apache has multiple interfaces and you want to accept requests only on a specific interface, specify that interface’s IP address.
Listen 192.170.2.1:80
Listen 192.170.2.5:8000
For the protocol, https is used by default for port 443, and http is used by default for other ports. If you want to use HTTPS on a port other than 443, specify it explicitly. For example, the following uses HTTPS on port 8443.
Listen 192.170.2.1:8443 https
References
- https://httpd.apache.org/docs/2.4/mod/core.html#servername
- https://httpd.apache.org/docs/2.4/mod/mpm_common.html#listen