×


VirtualHost cannot occur within virtualhost section in WAMP

The error, VirtualHost cannot occur within virtualhost section in WAMP trigger while we try to add VirtualHost directive and reconfigure vhost.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to  perform VirtualHost related queries.

In this context, we shall look into a fix for this error.


VirtualHost cannot occur within virtualhost section in WAMP - The causes of this error ?

The causes of the error include:

i. Another file includes the file vhost.conf, but from inside a virtualhost directive.

ii. All VirtualHost directive not closed properly.


How to fix VirtualHost cannot occur within virtualhost section in WAMP ?

1. Keep only directives which we need and ensure that vhost.conf is not within VirtualHost directive.

2. Strip vhost.conf from VirtualHost directive and other duplicated content.

3. Ensure to properly close all VirtualHost directives and enter correct syntax.


Review NameVirtualHost

Generally, each interface and port on which Apache is set to listen to needs a NameVirtualHost directive. 

We can define the directive only once per port:

NameVirtualHost *:80
Listen 80
<IfModule mod_ssl.c>
# SSL name based virtual hosts are not yet supported, therefore no
# NameVirtualHost statement here
Listen 443
</IfModule>

The default NameVirtualHost setting satisfies the requirements at present – Apache will apply named based virtual host logic and settings for HTTP requests made on any available interface (*) at port 80.


How to Define custom virtual hosts ?

Now you are ready to add your own virtual hosts so that you can start to serve your domains.

To Create the vhost file for the domain:

<VirtualHost *:80>
ServerAdmin webmaster@domain1.com
ServerName domain1.com
ServerAlias www.domain1.com
DirectoryIndex index.html
DocumentRoot /wamp/www
LogLevel warn
</VirtualHost>

If someone enters the IP address of the server, they have the contents of that default vhosts file.

Apache searches the enabled vhost files in alphabetical order and if it can’t find one for the requested IP address or domain name, it serves the first one (alphabetically).

If we disable or delete the default vhost file, then the contents of domain1.com will display (being before domain2.com alphabetically).


This is something to consider when planning a website.


[Having issues with this Wamp error? We are available 24*7. ]


Conclusion

This article will guide on steps to fix #VirtualHost cannot occur within virtualhost section in #WAMP which occurs while we try to add VirtualHost #directive and reconfigure #vhost.

Directory directive works only for filesystem objects (e.g. /var/www/mypage, C:\www\mypage), while Location directive works only for URLs (the part after your site #domain name, e.g. www.mypage.com/mylocation).

To Configure name-based virtual #hosts:

1. Install Apache webserver. Make sure you have installed #Apache webserver. 

2. Create web directory for each host. 

3. Create demo web pages for each host. 

4. Create configuration file for each host. 

5. Enable virtual host configuration files. 

6. Test Virtual hosts.