Virtual Sites on Apache 2

Mauriat Miranda (https://www.mjmwired.net/contact/)

Other Guides/Resources

Published: 15 September 2005 (updated: 15 September 2005)

NOTE: The content of this page may be outdated. Links may not be valid and the steps detailed may no longer work. This page is saved for archival purposes only.

This mini guide is how I set up a simple virtual site on my Fedora Core linux. I enable user web directories and then configure a virtual site using that location. You can create OTHER locations and create OTHER virtual sites if you prefer.


Previous Steps

This guide assumes you have Apache installed and working (on Fedora Core have the httpd RPM installed). You also need root access to your machine.

You can select ANY name you want for your virtual site, even sites existing on the internet (microsoft.com). By modifying the local list of hosts and IP address, Linux will re-route all requests for your virtual site to the local Apache server.


Setting Up Virtual Site Structure

I am using the local user directory site in Apache for the virtual site location (this requires NO root access). I am using the ~/public_html convention commonly used in Unix. The minimum I would recommend is the public_html directory and the cgi-bin directory. (You can change this as you see fit). Keep in mind that each directory needs to have some global permissions for the server to access them.

[mirandam@charon ~]$ cd /home/
[mirandam@charon home]$ chmod 711 ~
[mirandam@charon home]$ cd ~
[mirandam@charon ~]$ mkdir public_html
[mirandam@charon ~]$ chmod 755 public_html
[mirandam@charon ~]$ cd public_html
[mirandam@charon public_html]$ mkdir cgi-bin
[mirandam@charon public_html]$ chmod 701 cgi-bin

Each and every virtual site should have their own unique configured directory structure.

You can use other locations if you choose. Common places maybe /usr/local/, /opt/, and /var/www/. Examples:

/usr/local/vsites/site1
/usr/local/vsites/site2

Modify Apache Configuration

As root, edit httpd.conf. In Fedora Core this is located at: /etc/httpd/conf/httpd.conf. It is a good idea to make a backup copy first. After all changes to the configuration file please make sure to restart Apache.

Setup user directories.

To enable user directory edit httpd.conf near line ~333 and change the following from:

<IfModule mod_userdir.c>
	UserDir disable
	# UserDir public_html
</IfModule>

To:

<IfModule mod_userdir.c>
	# UserDir disable
	UserDir public_html
</IfModule>

If you want to restrict who has a user website, then use the UserDir parameter with the list of usernames:

UserDir enable username

Setup Virtual Site(s)

Enable virtual sites on the local machine by uncommenting line ~953:

# Use name-based virtual hosting.
#
NameVirtualHost *:80

Add each virtual site at the end of the file. The following example is how I mirror my personal website on Fedora Core. The final address (for me) will be http://mjmwired.charon.

<VirtualHost *:80>
	ServerAdmin mirandam@googles_email_service
	DocumentRoot /home/mirandam/public_html/
	ServerName mjmwired.charon
	ServerAlias mjwmired.charon *.mjmwired.charon
	ErrorLog logs/mjmwired-error_log
	CustomLog logs/mjmwired-access_log common

	ErrorDocument 403 /403.html
	ErrorDocument 404 /404.html

	ScriptAlias /cgi-bin/ "/home/mirandam/public_html/cgi-bin/"
	<Directory "/home/mirandam/public_html/cgi-bin/">
		AllowOverride None
		Options None
		Order allow,deny
		Allow from all
	</Directory>
</VirtualHost>

For setting unique parameters for each site (things like SSI, redirect, etc.) you can place between its <VirtualHost> tags. For example, I put my 403 and 404 documents to files I created.

Note: Your directory locations must match the directory structure you created above.

After making these changes, save the configuration file and restart Apache. On Fedora it will be done like:

[root@charon ~]# /etc/init.d/httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]

Setting Up Hostname Addresses

Since every virtual site will be hosted on the local machine, you must let your machine know how to resolve the ip address for each virtual site.

Edit /etc/hosts (as root) to add an entry for every virtual site you add. Every site will point to the localhost address which will always be 127.0.0.1. Example from my machine:

127.0.0.1       mjmwired.charon

Make sure to match what you have in the ServerName parameter.


Testing

Now there should be at least 3 sites you can access on your machine: the main site (on localhost), your user site (~username), and your virtual site. Example:

  1. http://localhost/
  2. http://localhost/~mirandam/
  3. http://mjmwired.charon/

Note: By using this guide and the above configuration your user directory site and virtual site will be the same.


More Information


Comments, suggestions, questions or any feedback welcome for this page or any of my Resources. Please use the contact link.

Help Out: If you found this guide or any Resource helpful, please consider supporting this site by recommending this page to others or linking to this page. I appreciate all the support I receive. Thank you in advance.

Disclaimer: The author makes no claim to the accuracy of the information provided. This information is provided in the hope that it will be useful, but WITHOUT ANY WARRANTY. There is no implied support from referencing this guide. Any help that is provided is at will. Use this information at your own risk. Always make proper backups and use caution when modifying critical system files.

PLEASE DO NOT mirror, translate or duplicate this page without contacting me.

Copyright © 2003-2013 by Mauriat Miranda (mjmwired.net).