Setting Up Apache Virtual Hosts on Fedora

Apache HTTP server is capable of hosting multiple websites on the same server. The feature is popularly known as virtual host.

The term Virtual Host refers to the practice of running more than one web site (such as www.company1.com and www.company2.com) on a single machine. When you configure virtual hosts your web server runs many websites at any given instance. You don't need to edit Apache configuration file and restart Apache every time you switch to a different website.

Follow the instructions below to configure virtual hosts in Apache.

Step 1: Open the Apache configuration file to edit it

#vi /etc/httpd/conf/httpd.conf

Step 2: Add the below directive to the Apache configuration file since we are using name-based virtual hosts. This directive tells the server to use any and all IP addresses on the machine.

NameVirtualHost *:80

Step 3: Add the VirtualHost block to each different website in the Apache configuration file

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

The above block tells the server to run website1.example.com using the document root /var/www/html/website1.

Step 4: Add as many virtual hosts as you want using VirtualHost blocks like below:

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

Save the file and exit.

Step 5: Restart Apache

#/etc/init.d/httpd restart

The virtual host configuration is done. If the hostnames are fictitious like this example,you have to add the hostnames in your network configuration.

Step 6: Open /etc/hosts

#vi /etc/hosts

Step 7: Add the host names

127.0.0.1    website1.example.com
127.0.0.1    website2.example.com

save the file and exit.

You will now be able to access http://website1.example.com and http://website2.example.com from your browser. At any given instance your local web server runs both website1.example.com and website2.example.com How convenient it is to work on multiple websites with virtual hosts!

I use subdomains of example.com while testing websites on my desktop because they don't exist in the Internet.

Allowing Override

Apache allows overriding certain directives using a .htaccess file. You might want to allow .htaccess files to override all possible directives.
In your httpd.conf file, find the section with the following

<Directory />
    Options FollowSymLinks
    AllowOverride none
</Directory>

Change AllowOverride from 'none' to 'all'. After changing the settings your file should look like

<Directory />
    Options FollowSymLinks
    AllowOverride All
</Directory>
Taxonomy upgrade extras: 

Comments

my acknowledgment to this article, it has a expert interest.

So... what if I want my DocumentRoot to be in a different place, not in the /var/www/html path? I have managed on Debian distributions to create virtual hosts with DocumentRoot being on the path /home/my-name/workspace/my-project/web, but on Fedora I haven't achieved this yet. When I try to access the web page, Apache response with this error:

Forbidden

You don't have permission to access / on this server.
Apache/2.2.13 (Fedora) Server at jobeet.localhost Port 80

Any hint why is this happening? I supposed it has to do with permissions and groups but what should be the correct ones to make this work? Thanks in advance and I hope someone can help me with this.

On Fedora, you have to take care of two things - SELinux and file system permissions. If SELinux is on, you have to write a policy to allow httpd to read and execute scripts in the /home/* directory. Also the user apache has to have read and execute access to your scripts.

I have already disabled SELinux a while ago. So it must be the permissions for the apache user. My files on /home/my/workspace/my-project are owned by my user and my own group. I have used chmod 777 to give permissions to the folders in this path but still not getting any improvement. Why? I'm I missing something? Or perhaps you were referring to something different when talking about giving read and write permissions to the apache user...

Check the permission upwards the my-project directior.
Does apache have read and execute access to /home/my/workspace/?
Does apache have read and execute access to /home/my/?

Bonjour a tous
j'ai défini mes hotes virtuels et tous ca marche trés bien en local
dans le fichier httpd.conf j'ai ajoutée

(((NameVirtualHost *:80

ServerName localhost
DocumentRoot /var/www/html

ServerName site1.site.com
DocumentRoot /var/www/html/bibliotheque

)))
et dans le fichier hosts
j'ai ajoutée
(((127.0.0.1 localhost localhost.localdomain localhost
::1 localhost localhost6.localdomain6 localhost6
::1 site1.site.com localhost))))

mais à distance je me choppe une erreur
Quelqu'un a une idee ?

Can you post your comment in English please?

Good little post. I assume the virtual directories can be anywhere (like one's own home) for quick and easy development. I wish a2.. scripts that are in debian were also on fedora but oh well...

Thanks again for the post.

really this is very useful for me .
now i can easily create virtual host.

thanks bro.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.