october 8,2020
VirtualHost is a configuration that we make to have several websites on a single server (apache xampp), so that you can access a “virtual” domain from your browser.
**They are names of local domains, they will only work within your computer.
Functioning
Hard disk space will store Link server domain name.
the website files.
Advantage
Local to remote urls management
Local development files can be accessed faster from any browser and even from operating systems.
http://localhost/proyecto —————–> http://proyecto.web
Process
- Create your host in windows
The hosts file in Windows 7, or Windows 10, is in the folder
There you will see various files and you will have to edit the one called “hosts”.
To edit the hosts file it is important to run it in administrator mode.
To do this, in the programs menu, right-click on the Notepad icon and then select “Run as administrator”.
Then you open the hosts file from the pad and then it will allow you to save the changes.
We modify the file, at the end of the code.
We append the same IP (127.0.0.1), but with a different domain example: marker.com (the name that best suits your project). In this case we name it “myproject-a.localhost”.
We save changes
- Create a folder that manages your projects
For greater management it is recommended to create a folder for your projects, it can be managed according to your needs; taking into account that every folder is created on local disk C.
For this we go to unit C
We create a folder, in this case we name it “www”
Inside the “www” folder, we create another folder with the name of our domain that we write on the windows host (in this case we name it “myproject-a.localhost)
Inside this folder we attach all the files related to the creation of our project.
It is important to highlight that the creation of the folder on the local disk is optional since this procedure can be carried out in the same way in the xampp folder following the following path:
- Configure apache to accept virtualhost
It is about opening the Apache configuration file for the virtual hosts “vhost”. The file is at this path in my Xampp.
We look for the file with the name “httpd-vhosts.conf” and we open it.
In the final line of the code, we copy the following lines.
NameVirtualHost *
<VirtualHost *>
DocumentRoot "C:\xampp\htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *>
DocumentRoot "C:\miproyecto\httpdocs"
ServerName miproyecto.local.com
<Directory "C:\miproyecto\httpdocs">
Require all granted
</Directory>
</VirtualHost>
Resulting in:
The code is modified adhering to the name of your project, below it is explained how:
First virtual host
Tell Apache that the virtual host localhost is still the Xampp folder “htdocs”
C: \ xampp \ htdocs.
Remembering where your folder was created (Explained before in section 2) the code will be modified. If your projects will be managed from xampp \ htdocs the code does not undergo any modification, but if your projects are managed from local disk C, the line of code is modified with the path of the folder that manages your projects, in the example that we follow it would remain thus:
Second virtual host
It is associated with the virtual host that we just created. There you will see a couple of data that you have to edit.
In the two paths that appear in the code, they must be changed to the path of the folder you have on your computer, following our example it would look like this:
Then in the line of code You will have to change the name of the virtual host that you assigned, continuing with the example it would look like this:
In this way we conclude the modification of the code, remaining as follows:
- Restart apache
Don’t forget that after any changes to the Apache configuration files, you must restart the Apache server for them to take effect.
- Final test
To finish in the search engine of your preference, write only the name of your project, obtaining as a result the visualization of your project and a virtual host. Following the example:
Additional information
Fix “ Access prohibited!” Error 403
A typical problem that you may encounter at this point is to receive a 403 “access prohibited” error, with a message “You do not have permission to access the requested directory …” (in English you will find this error as “Access Forbidden”). It happens because Apache is rejecting the connection due to the configuration of your Virtualhost.
In short, it would be to change a few lines in the “Directory” block. With this other configuration, the 403 error has disappeared for me.
<Directory "C:\miproyecto\httpdocs">
Options All
AllowOverride All
Require all granted
</Directory>
The directory listing on your virtualhost is achieved by including a configuration line inside the “directory” element.
<Directory "C:\miproyecto\httpdocs">
Order allow,deny
Allow from all
Options Indexes FollowSymLinks
</Directory>
With this you have configured your virtual host in xampp.