3

I'm having troubles with setting a vhost with vagrant. I've configured my host file on my host (192.168.33.10 local.dev). Yet I don't know how to set up my vhost within my VM and how to access it. And I don't want to use puppet or chef or other as I want to understand what I'm doing... :) At the moment here are my settings of my vhost:

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/support/mysite
<Directory /var/www/html/support/mysite>
     Options -Indexes +FollowSymLinks
     AllowOverride All
     Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/mysite-error.log
CustomLog ${APACHE_LOG_DIR}/mysite-access.log combined

The problem is I don't really know what I should put as server name or alias and how to access it then. Thanks for any help !!!

Matheus
  • 225
  • 1
  • 4
  • 8

3 Answers3

4

If you already added all the sites you want to run as virtual ones on your host file (all with same IP), then all you need to do is add multiple "VirtualHost" configurations to your apache conf file and use the same "site.dev" name you defined. Apache will read that and direct to the correct "DocumentRoot".

Here is a snippet with two sites defined. Just add more definitions for more sites:

<VirtualHost *:80>
  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/html/site1/
  ErrorLog logs/site1_error_log
  CustomLog logs/site1_access_log combined
  ServerName site1.dev
</VirtualHost>

<VirtualHost *:80>
  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/html/site2/
  ErrorLog logs/site2_error_log
  CustomLog logs/site2_access_log combined
  ServerName site2.dev
</VirtualHost>
noeldiaz
  • 906
  • 5
  • 5
  • Thanks ! I actually find out by myself after a while. Unfortunately I did not see your answer sooner :) – Matheus Jul 22 '14 at 13:46
0

You don't need a vhost as long as you are serving only one site per machine - local.dev in this case. You can leave the default apache2 configuration.

You need to understand what virtual hosting is actually for to see this:

The main purpose of virtual hosting is the possibility to run multiple web sites with different DNS domain names on one single machine having only one public IP address assigned to it. While there are many websites which would never exhaust the physical power of a server because of their low traffic, the most obvious advantage is that the number of available domains is now independent from the number of available IP (v4) addresses, which is very limited.

This is being achieved by a change in HTTP which was launched in version 1.1. The change adds the Host header to the HTTP request which contains the servers domain name. A HTTP server normally would not have this information: "What was the hostname used by the client to access me", as the DNS resolution will happen before the client issues the HTTP request and the server will then being accessed by it's IP address.

Having the information from the Host header, the server, while being accessed by it's IP address, can decide which site should being served. Multiple sites will then have different document root folders for that.


However, you are serving just a single page from your vagrant box, therefore setting up a virtual host isn't required. Just use apache's default one.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Thanks for your answer. However I'd like to set 1 VM for different projects as they use the same configuration. That's why I'd like to set up vhosts :) – Matheus Jul 16 '14 at 12:30
  • your setting up two vms? one for every project? – hek2mgl Jul 16 '14 at 12:45
  • I'd like to have 1 VM for multiple projects... is that possible ? or do I have to keep one VM per project? – Matheus Jul 16 '14 at 12:59
  • what i'd like is when I type my project url I get to one project and I type another I get the other... but all of this within 1 VM. That's why i need vhosting – Matheus Jul 16 '14 at 13:01
  • Then it remains just the question: "How to setup virtual hosting with apache2". While this is really intuitive, there are billions on answers out there. – hek2mgl Jul 16 '14 at 14:57
  • Just in case you wanted to setup a muliple vhost using vagrant, here is a post that had done that. --> http://stackoverflow.com/questions/24777829/set-up-a-vhost-with-vagrant?rq=1 – GaryP Feb 11 '15 at 03:27
0

I think there is a more elegant solution to the issue of VHosts on your Vagrant Box.

Set your Virtual hosts directly in your Vagrant file so your configuration can commit to git.

Check out the instructions Setting VHosts in the Vagrantfile here:

https://github.com/onema/vagrant-lamp-development#creating-custom-vhosts

Docs on Chef Solo Here

https://docs.chef.io/chef_solo.html

David Lundquist
  • 690
  • 7
  • 19