• 0
virtual_host

Creating Virtual Host on Apache

Tags :

Category : Blog

This is a tutorial for the beginners who find it difficult to host a website on Apache. Now, this is just for the use in a local testing environment. We usually encounter a common problem while setting up a virtual host in a local testing environment, which is getting a Forbidden message as follows:

Forbidden

You don’t have permission to access/index.php on this server.

To rectify or to avoid this permission issue, I strongly recommend creating a folder to hold all the virtual hosts. Here, C:\vhosts signifies that all the virtual hosts are located in this folder.

Note: There are security restrictions on Windows Vista and Windows 8 which prevents you from saving edits to the files even though you are logged in as administrator. To avoid this, open Notepad or your script editor. Then open the relevant files by using File>Open inside the program you have just launched. By default, Notepad shows only files with .txt extension, so you need to select the option for All Files (*.*). This rectifies your problem and you can edit and save the files normally.

Now follow the steps below-

  1. Add multiple virtual hosts to the Apache server by creating individual subfolder in C:\vhost.
  2. Open C:\WINDOWS\SYSTEM32\drivers\etc\hosts in Notepad and look for the following line:

127.0.0.1 localhost

  1. Now, enter 127.0.0.1 on a separate line, followed by a space and the name of virtual host you want to register. For instance, to set up a virtual host called phpbezoar, enter the following:

127.0.0.1  phpbezoar

  1. Similarly add other virtual hosts you want to register each on a separate line pointing to the same IP address. Save the host file and close it.
  2. Open the main Apache configuration file (httpd.conf) in a text editor; you can get in the Apache conf For the ones who are using XAMPP, you can access your file from C:\xampp\apache\conf\httpd.conf. Scroll down to the Supplemental configuration section at the end and locate the following section:
  3. Remove # from the second line, save conf and close it.
  4. Open extra\httpd-vhosts.conf in Notepad. For the one using XAMPP, the location is C:\xampp\apache\conf\extra\httpd-vhosts.conf. You will get the following:

    <VirtualHost  *: 80>

    DocumentRoot C:\xampp\www

    ServerName localhost

    </VirtualHost>

    <VirtualHost  *: 80>

    DocumentRoot C:\xampp\www\phpbezoar

    ServerName phpbezoar

    </VirtualHost >

  5. In XAMPP, all the lines are commented out. You must remove the # from the beginning of the line that contains the following directive:

NameVirtualHost *.80   

  1. Insert the following code

<Directory C:/vhosts>

Require all granted

</Directory>

  1. This will set the correct permissions for the folder that contains the sites you want to treat as virtual hosts.
  2. Considering the above screenshot, the code shows examples of how to define virtual hosts. It shows all the commands that can be used; just the Document Root and ServerName are required.
  3. Now as soon as you enable virtual hosting, Apache disables the main server root, so you need to reproduce the original server root. Then you can add each virtual host within <VirtualHost> tags, using the location of the site’s web files as the value for DocumentRoot, and the name of the virtual host for Again, use forward slashes and if the path contains any spaces, enclose the path within quotes.
  4. If your server root is located like mine, at C:\docs, and you are adding phpbezoar as a virtual host in C:\vhosts, change the code it may appear as below:

<VirtualHost  *: 80>

DocumentRoot C:\docs

ServerName localhost

</VirtualHost>

<VirtualHost  *: 80>

DocumentRoot C:\vhosts\phpbezoar

ServerName phpbezoar

</VirtualHost >

  1. Save httpd-vhosts.conf, and restart Apache.
  2. You can continue accessing all the sites in the server root through the path http://localhost/sitename/ and you can directly access anything on the virtual host through http://phpbezoar/.
  3. I hope it would be useful and now you can access your virtual hosts. Again still if you have any difficulty check for index.php in the DirectoryIndex directive in httpd.conf