The error, VirtualHost cannot occur within virtualhost section in WAMP trigger while we try to add VirtualHost directive and reconfigure vhost.
As part of our Server Management Services, we assist our customers with several VirtualHost queries.
Today, let us find an effective solution for this error.
VirtualHost cannot occur within virtualhost section in WAMP
The causes of the error include:
- Another file includes the file vhost.conf, but from inside a virtualhost directive
- All VirtualHost directive not closed properly
- Keep only directives which we need and ensure that vhost.conf is not within VirtualHost directive.
- Strip vhost.conf from VirtualHost directive and other duplicated content.
- Ensure to properly close all VirtualHost directives and enter correct syntax.
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.
Define custom virtual hosts
Now you are ready to add your own virtual hosts so that you can start to serve your domains.
Create the vhost file for the domain:
<VirtualHost *:80> ServerAdmin email@example.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.
[Stuck with the error? We are available 24*7]
To conclude, VirtualHost cannot occur within virtualhost section in WAMP trigger while we try to add VirtualHost directive and reconfigure vhost. Today, we saw an effective method our Support Engineers employ.