≡ Menu

How to add virtual hosts to Apache?

The question is – How to add or change the virtual hosts settings on Apache on CentOS? I need to add two new domain names to the Apache configuration. I don’t see my existing domain name’s configuration in httpd.conf. Where is the config for Apache virtual hosts on my Linux box? Which file should I be editing?

Apache virtual hosts functionality allows you to serve multiple domain names on the same server, and also on the same IP address. This functionality is the most useful functionality of Apache and it allows for shared hosting accounts that can have thousands of domain names on one server.

To edit the Apache configuration and virtual hosts, you need a dedicated server or VPS with root access. If you don’t have root access, you must ask your web hosting provider to setup additional domain names, or setup additional domain names using cPanel’s Add Domain functionality.

The following five steps will guide you through a tutorial on how to create, edit and add domain names to the virtual hosts configuration file.

1) Locate your httpd.conf

To get started on adding new domain names to Apache’s virtual hosts functionality, you must first locate your httpd.conf file.

This can be found out using the following command:

httpd -V

This will output something like the following:

[root@server ~]# httpd -V
Server version: Apache/2.2.15 (Unix)
Server built: Aug 13 2013 17:27:11
Server's Module Magic Number: 20051115:25
Server loaded: APR 1.3.9, APR-Util 1.3.9
Compiled using: APR 1.3.9, APR-Util 1.3.9
Architecture: 32-bit
Server MPM: Prefork
threaded: no
forked: yes (variable process count)
Server compiled with....
-D APACHE_MPM_DIR="server/mpm/prefork"
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=128
-D HTTPD_ROOT="/etc/httpd"
-D SUEXEC_BIN="/usr/sbin/suexec"
-D DEFAULT_PIDLOG="run/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_LOCKFILE="logs/accept.lock"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="conf/mime.types"
-D SERVER_CONFIG_FILE="conf/httpd.conf"

See the lines that are in bold. These lines give you the location of your httpd.conf file.

On my server, httpd.conf is located at /etc/httpd/conf/httpd.conf.

2) Locate or create vhost.conf

In httpd.conf, find all lines that contains the string “Include” in it:

grep -i Include /etc/httpd/conf/httpd.conf

On my server, the above command outputs the following:

Include conf.d/*.conf

You may also see lines like the following:

Include conf.d/vhost.conf

Now, if none of these lines exist and there is no file called vhost.conf  in the conf or the conf.d directories, you must create one and include it in the httpd.conf file. (Don’t forget that both the conf and the conf.d directories are relative to the HTTPD_ROOT folder that the command httpd -V gave us).

3) Create Virtual Hosts

Now that you have your vhost.conf file, open it in your favorite editor and let’s start adding virtual hosts!

The vhost.conf file on my server looks like the following:

NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin info@ewhathow.com
ServerName ewhathow.com
ServerAlias www.ewhathow.com
DocumentRoot /srv/www/ewhathow.com/public_html/
ErrorLog /srv/www/ewhathow.com/logs/error.log
CustomLog /srv/www/ewhathow.com/logs/access.log combined
</VirtualHost>

I will explain all the lines in detail.

NameVirtualHost *:80 – This line says that Apache should run virtual hosts listening on port 80 which is the default HTTP port
Everything between <VirtualHost *:80> and </VirtualHost *:80> is the configuration of one domain name. Everything that goes between the two tags is the information about a domain name that you want to add as a virtual host.
ServerAdmin info@ewhathow.com – This is the email address of the administrator’s email address i.e. your email address
ServerName ewhathow.com – This line contains the domain name which has to be added as the virtual host.
ServerAlias www.ewhathow.com – This is the alias of the domain name. This is generally the domain name with the www sub-domain.
DocumentRoot /srv/www/ewhathow.com/public_html/ – This is the absolute path to the directory which will contain your HTML, PHP or any other types files that are to be served from the Apache web server.
ErrorLog and CustomLog – Apache has two types of log files. The ErrorLog directive gives the location of the error_log which contains all Apache errors. The CustomLog is the location of the access_log file which contains logs of all requests. Don’t forget to enable log rotation on these files; otherwise, they will quickly grow to un-manageable sizes.

Now you have created your first virtual host on Apache on a Linux box!

3) Check your virtual host configuration

After you have added a few virtual hosts to the vhost.conf file, you should check on whether there are any syntax errors in the file.

httpd -S

The above command checks the virtual host configuration and will output the following if there are no errors in the vhost.conf file:

[root@server ~]# httpd -S
VirtualHost configuration:
Syntax OK

If there are errors, the output of the -S flag to httpd will result in something like the following:

Syntax error on line 1 of 
/etc/httpd/conf.d/vhost.conf:
Invalid command 'asdfasdf', perhaps misspelled or 
defined by a module not included in the server 
configuration

So if the syntax is OK, you are all set to start using the virtual host functionality on your server! If there are errors, fix them and use the -S flag till you get the Syntax OK output!

Note that if you changed your httpd.conf file as well, you should check it as well:

httpd -t

4) Restart Apache

Now that you have finished configuring your virtual hosts, you must restart Apache:

service httpd restart

If you have a very high traffic site and you don’t want to waste even a single visitor, you must use the reload directive:

service httpd reload

This will reload the configuration file without restarting Apache! This can be very useful at times.

5) Create DNS Entries

Now that all of your sites are configured on your box, you must create A records for your domain names. The IP addresses of the A records should point to the machine on which you have configured the virtual hosts. This will ensure that the files in the proper directories are served when visitors visit your sites.

Thats it! You have now mastered the creation and editing of virtual hosts and you know enough to administer multiple domain names on a single box!

{ 0 comments… add one }

Leave a Comment