How to Change the Host Name on an Apache Server
The Apache HTTP daemon supports name-based hosting so that multiple domain names can share one IP address. Changing a host name effectively changes your website's domain name, since your visitors request the site by sending its host name, in the form of a URL, to your specified port in the request's HTTP headers. Whether you run Apache in Linux, Windows or OS X, your host names are stored in the main Apache configuration file, which may include virtual host names from other configuration files.
Open the main Apache configuration file, httpd.conf, in your default text editor. Depending on your operating system, it's located in one of the following directories:Linux: /etc/httpd/conf/Windows: C:\Program Files\Apache Software Foundation\conf\OS X: /etc/apache2/
Search for the word “ServerName” (without quotes, here and throughout) in the file. You can access the search function in most text editors by pressing “Ctrl-F” or by clicking “Search” or “Edit” in the menu bar. The ServerName line contains your current host name and port number separated by a colon.
Enter a new host name after the ServerName keyword and ensure that you don't erase the trailing port number. The ServerName line should resemble the following example:ServerName www.mynewhostname.com:80
Search for the term “Virtual Hosts” in the configuration file. The Virtual Hosts section occurs around line 500 of the default file, but it may be lower if you've added many customizations. If your configuration file includes virtual hosts from other files, those files and their absolute paths are listed in this section. Alternatively, the virtual host information may be included directly in the httpd.conf file.
Open the files included in the Virtual Hosts section in your text editor. In most cases, the included file is called httpd-vhosts.conf, and it's located in the same directory as httpd.conf. However, servers with too many virtual hosts to fit in one file often store them in separate files and include each one on a separate Include line. If the virtual host information is printed directly in the httpd.conf file, you can edit this section where it is.
Change your virtual host's ServerName line to your new host name, but don't append the port number to the virtual host name. If Apache has been serving Web pages from your virtual host using the old host name, your IP address and port number are already included in the opening VirtualHost brackets and resemble the following example:
Restart Apache by restarting your computer or by opening a console and executing the appropriate command for your operating system. In Linux and OS X, enter “sudo apachectl -k restart,” and in Windows, enter “httpd.exe -k restart.”
Tips & Warnings
- Ensure that your new domain name is registered and that your site visitors know the new name before changing it.
- Information in this article applies to Apache 2.2. It may vary slightly or significantly with other versions or products.