Linux.com

Home News Enterprise Computing Systems Management How to set up Apache virtual hosting

How to set up Apache virtual hosting

By James Lees

If Apache is installed correctly, you can set up virtual hosting easily by editing Apache configuration files. The main .conf files in /etc/apache2 include httpd.conf, error.conf, server-tuning.conf, and a bunch more. These files are set for read-only access for normal users; you must gain root access with the sudo or su command to change them.

Edit /etc/apache2/httpd.conf and find this line:

Include /etc/apache2/vhosts.d/*.conf

Replace it with a line that specifies a .conf file we will create and call vhost.conf:

Include /etc/apache2/vhosts.d/vhost.conf

When you've made the change, save the file. Now, again as root, create the vhost.conf by renaming the vhost.template file in /vhosts.d/:

mv vhost.template vhost.conf

Now edit the file to set it to allow multiple name-based Web sites on a single IP address. First add in the virtual host directive with NamevirtualHost *. The asterisk is a wild card character that allows any address to be set. Under that directive you can add your first virtual host:

NamevirtualHost *ServerAdmin 
 This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 
DocumentRoot /srv/www/htdocs/directory_of_your_choice/
ServerAlias yourdomain.com *.yourdomain.com

The first part is the opening tag for the defined virutal host block. ServerAdmin lets you display the administrator email address when an error such as a 404 occurs. DocumentRoot is the root directory for the defined site; for example, sites I have hosted in the past all had accounts set up within /srv/www/htdocs/hosted/sitename/. Next is the ServerAlias or ServerName, which controls what names people can use to see and access a site. You could set this to a directory within the main NameVirtualHost or the domain of the site so the server can display the content associated with the domain name. The reason for the two ServerAlias entries above is that servers are often called from more than one ServerName, so you set up a ServerAlias with more than one address listed. The wild card allows for any domain request with anything in front of .yourdomain.com.

What if you want to have multiple virtual hosts set up under two different NameVirtualHost blocks, with one being the server's main IP address and the other being for the localhost? The code would look like this:

NameVirtualHost *ServerAdmin 
 This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 
DocumentRoot /srv/www/htdocs/
ServerAlias mixfevers.com *.mixfevers.com
ServerAdmin 
 This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 
DocumentRoot /srv/www/htdocs/snapnshare/
ServerAlias snapnshare.com *.snapnshare.com
erverAdmin 
 This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 
DocumentRoot /srv/www/htdocs/~/boomer/
ServerAlias boomerwebzine.com *.boomerwebzine.com
NameVirtualHost 127.0.0.1   
DocumentRoot /srv/www/htdocs/   
ServerName localhost

Once you've saved the configuration file, you need to restart Apache as root in order for your change to take effect:

# /etc/init.d/apache2 restart

The setup above works well for a small company hosting multiple sites on one server, or for a hobbyist who designs sites and wants to host them. It allows you point your domains at a static or dynamic IP address, letting the server figure out what shows up for the domain.

Apache provides more information on setting up and maintaining different setups of virtual host in its documentation.

 

Comments

Subscribe to Comments Feed
  • Prasobh Said:

    I'm using fedora 18 with latest apache. I couldn't set up the virtual host yet. something has wrong, please help me ERROR Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -n' for details. [root@localhost atps]# systemctl status httpd.service httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled) Active: failed (Result: exit-code) since Thu, 2013-02-07 16:21:22 IST; 11s ago Process: 9548 ExecStop=/usr/sbin/httpd $OPTIONS -k graceful-stop (code=exited, status=1/FAILURE) Process: 9546 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=1/FAILURE) Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec" CGroup: name=systemd:/system/httpd.service Feb 07 16:21:22 localhost.localdomain httpd[9546]: httpd: Syntax error on line 61 of /etc/httpd/conf/httpd.conf: Canno...ctory Feb 07 16:21:22 localhost.localdomain systemd[1]: httpd.service: main process exited, code=exited, status=1/FAILURE Feb 07 16:21:22 localhost.localdomain httpd[9548]: httpd: Syntax error on line 61 of /etc/httpd/conf/httpd.conf: Canno...ctory Feb 07 16:21:22 localhost.localdomain systemd[1]: Failed to start The Apache HTTP Server. Feb 07 16:21:22 localhost.localdomain systemd[1]: Unit httpd.service entered failed state

  • Madhab Jha Said:

    Hello friends, Now you can send 5000 FREE emails per month using our email marketing servers with multiple IPs to get your campaign to inbox.Get great inbox deliveries and land your 95% emails to inbox. With our userfriendly and beautiful web-app you would be able to track every movement of your campaign. You would be able to see the users who have opened, clicked, unsubscribed, bounced and marked as spam. You can register here for your FREE account:http://www.client.hostwiser.com/cart.php?a=view HURRY!! * You must have access to your own opt-in list of your subscribers.You must be compliant with the ICANN-SPAM ACT of anti spamming.We do not sell email lists.

  • innowaves123 Said:

    EmailOcean is a brand new platform to cater to your email marketing needs. It has been launched under the patronage of KOONK TECHNOLOGIES PVT. LTD. and is one of a kind platform for email marketing. There is not point in harping on about the advantages of email marketing,as at this point in time,email marketing is the sole way of marketing or promoting your product,service or message in an efficient and quick manner. EmailOcean provides you with the following advantages: Highly Economical: It provides promotional emailing at an amazingly low rate of $0.10 per 1000 emails. The customer only pay for what is utilised. This is the lowest price available for mass mailing. Web App to create and track campaign: The brilliance of EmailOcean lies in it making the process easier and faster for the customer,as compared to Amazon and Sendgrid, EmailOcean has a beautiful Web-app which makes creating and sending the campaign a lot more easier. Using this web-app one can also send and track their campaigns and get detailed analytics.You can see the users who have opened, clicked, unsubscribed and bounced. Inbox Deliveries: Their inbox delivery rate is phenomenal. Excellent Support: They also provide a support staff that is available at your beck and call 24*7. As of now,the registrations are open by invitation but register yourself with the website and keep self updated as to when they are open to all or for your surprise invitation code.

Become an Individual Member
Check out the Friday Funnies

Sign Up For the Linux.com Newsletter


Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board