henrywright
henrywright

Reputation: 10240

What values are valid for use with ServerName when setting up Apache virtual hosts locally?

I'm currently setting up Apache virtual hosts for a number of websites I need to develop on localhost. Using the following configuration (and after updating my /etc/hosts file), I can access the content stored at /var/www/example.com/public_html by going to example.com in my web browser:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example.com/public_html
    ...
</VirtualHost>

My question:

Are there rules for choosing values for ServerName? For example, must the value I use be a domain name such as ServerName example.com or can I omit the .com extension and use simply ServerName example? Must I use all lowercase characters, etc.?

Upvotes: 0

Views: 1858

Answers (3)

Andr&#225;s Korn
Andr&#225;s Korn

Reputation: 170

The ServerName setting has two purposes:

  1. along with the port in the <VirtualHost> declaration, help Apache match the request to a virtualhost, based on the incoming Host: request header. A virtualhost will be used to serve a request if the contents of the Host: header matches the ServerName or one of the ServerAliases of the virtualhost, AND the request arrived on the port specified in the <VirtualHost> declaration. (If the HTTP verb is follwed by a full URL instead of just a path, e.g. GET http://example.com/index.html HTTP/1.1, then the hostname portion of the URL can also be used for virtualhost matching, not just the Host header.) If there is no match with any virtualhost, the default virtualhost gets served. The default is the one that occurs first in the configuration. As long as you can get clients to a) talk to your server at all and b) put the same string in a Hosts: header when they do, any ServerName is "valid" for this use of the ServerName, but not the second:

  2. Depending on the value of UseCanonicalName, the ServerName will be used in outgoing Location: response headers when generating redirects. (If UseCanonicalName is off, the Location header will reference the same hostname that the client used when making the request.) The Location: header must contain a valid absolute URI, which rules out completely arbitrary strings for ServerNames (but just "example", without ".com", would be technically fine, as long as your clients can be made to understand it's a fully qualified domain name and that no further qualification, e.g. by appending suffixes from the domain search list, should be attempted).

While you can really use almost any string as a ServerName, it's probably only useful to use ServerNames that the clients you care about will actually use when talking to your server; that is, when the clients look up your ServerName using whatever mechanism they use to translate names to IP addresses (in almost all cases, this will be /etc/hosts followed by DNS if the first lookup fails), they should see an IP they can reach your server on. (The ServerName can also be an IP address itself.)

Since ServerNames are considered to be domain names (in the DNS sense), they should be case insensitive.

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 60037

You can use whatever is a valid FQDN - https://en.wikipedia.org/wiki/Fully_qualified_domain_name.

Put that name in /etc/hosts as well and get it to use 127.0.0.1.

PS: Make one up that either does not exist in the outside world or even the future intended web site

EDIT

Line in /etc/hosts

  127.0.0.1 example.com 

Then

 ServerName example.com

This will make example.com be on your local machine. Ideal for development. When done all you need to do (assuming example.com has a CNAME) is remove the line from /etc/hosts. You can even do the development off line

Upvotes: 0

jyvet
jyvet

Reputation: 2201

You can use whatever name you want for local domain. Look at the documentation.

ServerName Apache

ServerName [scheme://]fully-qualified-domain-name[:port]

The ServerName directive sets the request scheme, hostname and port 
thatthe server uses to identify itself. This is used when creating
redirection URLs.

Upvotes: 1

Related Questions