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.
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:
# SSL name based virtual hosts are not yet supported, therefore no
# NameVirtualHost statement here
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:
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.