How to route all *.dev to subfolders on vagrant box - apache

I want that every *.dev Host will be routed to my vagrant machine to /var/www/vhosts/*.dev/public, for example my local development environment project1.dev is located in /var/www/vhosts/project1.dev/public
So when I add a new (sub)project into my box, I do not need to change my config.yaml (Vagrant installed via puphpet.com) and reload the machine.
On my computer, I added the following to the hosts file in /private/etc:
192.168.56.101 *.dev
On my VM, I changed my 10-default_vhosts80.conf in /etc/apache2/sites-enabled to:
# ************************************
# Vhost template in module puppetlabs-apache
# Managed by Puppet
# ************************************
<VirtualHost *:80>
ServerName default
## Vhost docroot
DocumentRoot "/var/www/default"
## Directories, there should at least be a declaration for /var/www/default
<Directory "/var/www/default">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Require all granted
</Directory>
## Load additional static includes
## Logging
ErrorLog "/var/log/apache2/default_vhost_80_error.log"
ServerSignature Off
CustomLog "/var/log/apache2/default_vhost_80_access.log" combined
## Custom fragment
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/default/$1
</VirtualHost>
<VirtualHost *.dev:80>
ServerName dev
VirtualDocumentRoot /var/www/vhosts/%0
</VirtualHost>
Unfortunately, this doesn't work. Any ideas? I am a beginner in this subject.

I use a proxy auto configuration file. This works on Windows, MacOS and Linux. Easy, flexible and no additional software required. The following example routes all *.dev traffic to your vagrant box:
function FindProxyForURL(url, host) {
if (dnsDomainIs(host, ".dev")) {
return "PROXY 127.0.0.1:8080";
}
return 'DIRECT';
}
When needed, replace 127.0.0.1:8080 with the IP and webserver port of your vagrant box. Store this file somewhere. You can store it locally or let the webserver on your Vagrant-box host the file.
Windows: See here how to use the PAC file on Windows.
MacOS: See here how to use the PAC file on MacOS. You can link to the file using file:///Users/username/path/to/proxy.pac.
Linux: For linux it depends, but I'm sure linux users will be able to Google for their specific situation.

Unfortunately hosts files do not support using wildcards. You have to manually define each and every host to redirect.
Also, your hosts file is at /etc/hosts

In the end, I use dnsmasq to route all .localdev Domains to 127.0.0.1. Note that I am using .localdev instead of just .dev or .local as this seems to cause problem (OS X 10.10) because .dev is a proposed gTLD and .local is used by Apple's Bonjour.
Then I configured Apache by creating and enabling this site:
<VirtualHost *:80>
ServerAlias localhost *.localdev #wildcard catch all
VirtualDocumentRoot /hosts/%1/public
UseCanonicalName Off
<Directory "hosts">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
(from: http://brunodbo.ca/blog/2013/04/26/setting-up-wildcard-apache-virtual-host-wildcard-dns)

Related

How to access Apache website by public-ip with server name?

I've installed vanilla at Ubuntu server with public-ip by the steps at https://www.vultr.com/docs/how-to-install-vanilla-forum-on-ubuntu-16-04
Then config /etc/apache2/sites-available/forum.example.com.conf as below
<VirtualHost *:80>
ServerName forum.example.com
DocumentRoot /var/www/vanilla
<Directory /var/www/vanilla>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
Then I can access http://public-ip at browser, but the result is Apache2 Ubuntu Default Page.
How can I access the installed vanilla at the browser? Should I make any change to the forum.example.com.conf?
If the request doesn't match a virtual host, Apache will serve files from the top level (not inside a <VirtualHost> block) DocumentRoot config value.
#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot /var/www/html
I don't know Ubuntu well but I'd guess this is in /etc/apache2/apache2.conf. You can change this to /var/www/vanilla to serve your forum instead of the default page.
Alternatively you could rename / delete the existing /var/www/html and make it a symlink to /var/www/vanilla: then Apache would pick up the forum files without any config change.
Or if you just want to set up access for yourself then you can add the DNS name to your hosts file (/etc/hosts on Linux, \Windows\System32\drivers\etc\hosts on Windows)
forum.example.com AAA.BBB.CCC.DDD
and then you can use forum.example.com in your browser as normal, which will send the 'Host' header so Apache will match the virtual host, even though this isn't configured as public DNS.

vhosts dont appear to be working on OS X Mavericks Apache installation

I am trying to set up the Apache server which comes with OS X Mavericks with vhosts so that a domain name resolves to my user level document webroot. I have followed this tutorial which guided me through setting up the apache server with php:
http://coolestguidesontheplanet.com/get-apache-mysql-php-phpmyadmin-working-osx-10-9-mavericks/
as well as this one which guided me through setting up the vhosts:
http://coolestguidesontheplanet.com/set-virtual-hosts-apache-mac-osx-10-9-mavericks-osx-10-8-mountain-lion/
Following these tutorials, if I type localhost in my browser it correctly resolves to the system level root (/Library/WebServer/Documents/ folder). If I use localhost/~myusername it correctly resolves to my user level root (/users/myusername/Sites/).
However, whenever I navigate to my domain, I get redirected to the system level root rather than my user level root.
My vhosts file reads as follows:
<VirtualHost *:80>
ServerName localhost
DocumentRoot /Library/WebServer/Documents/
</VirtualHost>
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias www.mydomain.com
DocumentRoot "/Users/myusername/Sites/mydomain"
<Directory "/Users/myusername/Sites/mydomain">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
Any ideas?
Have you enabled Virtualhosts?
NameVirtualHost *:80
And included your vhosts directory
Include <path>/vhosts/*
On a Macbook, you may need to include /private/ at the beginning of any absolute path, so that Apache can read it correctly off the file system.
For others who have the same problem...
It was frustratingly simple - I had missed the "" around the localhost directory.
How annoying!
Try starting apache manually with the -S option to see what the problem might be:
/usr/sbin/httpd -S

Apache doesn't use DocumentRoot after upgrading to Ubuntu 13.10 (Uses default page that says "It works!")

I have various virtual hosts for my web development work, including
cnm. The
sites-available/cnm
my file says very simply:
<VirtualHost *:80>
ServerName cnm
DocumentRoot /var/www/cnm/public_html
</VirtualHost>
I upgraded to Ubuntu 13.10, and when I point my browser to cnm/, I see the
/var/www/index.html file that seems to be indicated in the default file
sites-available/000-default.conf
which says (among other things):
<VirtualHost *:80>
DocumentRoot /var/www
What do I need to do to get Apache to read my cnm document root when I browse to cnm/ ?
NOTES:
I already tried renaming my sites-available/cnm file to sites-available/cnm.conf and enabling it with a2ensite cnm and service apache2 reload. That is a good thing, but it changes nothing.
I already tried changing <VirtualHost *:80> to <VirtualHost cnm.localhost> or to <VirtualHost cnm>. That did nothing.
Ubuntu 13.10 uses apache 2.4, you should check all your apache configuration. But for this present case you should note that a2ensite and a2dissite commands won't be able to see your files in /etc/apache2/sites-available if it does not end with .conf, so rename it to sites-available/cnm.conf and run a2ensite cnm.
Then your Virtualhost definition is certainly better with *:80, it means this virtualhost is activated for all IP interfaces (*) on port 80. cnm.localhost or cnm are not valid values here, only IP numbers (Ip of your apache server) or * for all, and a port number.
Then check how you configuration is read by apache, running theses commands:
# load apache env
# be careful, there is a dot and a space
. /etc/apache2/envvars
# Check apache Virtualhosts config
apache2 -S
You should get something like:
VirtualHost configuration:
*:80 is a NameVirtualHost
default server something (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost something (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost cnm (/etc/apache2/sites-enabled/cnm.conf:4)
If it is ok, and if you have the right Ip in your hosts file for cnm, and you can test that with a ping, then using http://cnm/ should use the Virtualhost having cnm in the ServerName.
If you have an answer from the default Virtualhost then it means apache is not finding the name used in your Host header in the list of ServerName and ServerAlias available for that IP/port and fallbacks to the default Virtualhost. If you are really stuck (and you did not forgot to restart) you can always remove the default Virtualhost and keep only the one you are working on.
I found the answer to my issue. I needed to delete the files in /etc/apache2/sites-enabled.
Delete files in /etc/apache2/sites-enabled
Rename config files in /etc/apache2/sites-available to have a .conf ending
For each file in sites-available, run sudo a2ensite mysite.
Run sudo service apache2 reload
I couldn't find any step by step tutorial on how to make it work on my side. I gathered bits and pieces here and there, so for those who need all the steps to follow, here there are:
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/local-mydefault.conf
$ sudo gedit /etc/apache2/sites-available/local-mydefault.conf
Paste the following into the local-mydefault.conf file (Change the path '/your/full/path' to where you want to have your files. And change username to your own username):
# ------------------------------------------------------
<VirtualHost *:80>
ServerName localhost
ServerAdmin webmaster#localhost
DocumentRoot /your/full/path
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /your/full/path>
DirectoryIndex index.php
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Require all granted
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
User username
Group username
# ------------------------------------------------------
Then type the following commands
$ cd /etc/apache2/sites-available/
$ sudo a2ensite local-mydefault.conf
$ sudo a2dissite 000-default.conf
$ sudo /etc/init.d/apache2 restart
I found the answer to this issue:
When upgrading to Ubuntu 13.10, the DocumentRoot does not seem to make a difference.
This is because Apache 2.4 moved the Directory configuration somewhere else. Your old .conf files still have these lines:
DocumentRoot "/var/www/myVhost"
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory "/var/www/myVhost">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
Remove (or comment out) the <Directory> directives so you only have:
DocumentRoot "/var/www/myVhost"
Now reload your settings: service apache2 reload and DocumentRoot is back. :)
Rename the file configuration "cnm" with the extension .conf
mv sites-available/cnm sites-available/cnm.conf
a2ensite sites-available/cnm.conf
And ready!
service apache2 reload
I have the same issue
on way is to disable 000-default and reload apache ,but this isn't the solution becuase you must have just one vhost at a same time :(
sudo a2dissite 000-default.conf
sudo service apache2 reload
I ran into a similar issue. My server name and my FQDN were the same to the default was running into the /var/www/html directory. I disabled the default configuration and my site worked like a breeze. Thanks #regilero.
sudo a2dissite 000-default.conf
fixed it for me.

Setting root directory in a development environment

I have a linux server that I do all of my web development on. I cant find an example of a virtual host.
I need a way to set virtual hosts (i think) so that even when i am running http://dev.example.com/blah i need that URL to be treated as a different site.
I know that i cannot use sub domains because to access the server I have to navigate through a sub domain.
If you need any clarifications please ask.
Virtual hosts are set using a <VirtualHost> section in apache configuration file(s) generally being httpd.conf,apache2.conf.It looks like
<VirtualHost *:80>
ServerAdmin webmaster#dev.example.com
ServerName dev.example.com
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
</VirtualHost>
You can change DocumentRoot as per your convenience.
In few versions of distros such different sections of main trivial configuration file httpd.conf are segregated to separate files.
For example apache2 on latest ubuntu has separate files per virtual host located at /etc/apache2/sites-enabled/
You may find 000-default already in this directory which sets the default host(localhost)
You may copy it and start editing to define a new virtual host.With above snippet you will be configuring new host. Key point is setting DocumentRoot to a different directory for dev.example.com to configure as a different site.
I assume your dev machine is able to resolve what is set for ServerName else you may want to configure it too in /etc/hosts on debian based linux.

Only activate web sharing that I can use virtualhost with apache on Mac OS

I have configure httpd.conf and httpd-vhosts.conf, when I active the web sharing the virtualhost is useful, but when I close web sharing and start apache, the virtualhost doesn't work
Here is my virtualhost config
<VirtualHost *:80>
DocumentRoot "/Users/lch/Sites"
ServerName myhome.com
</VirtualHost>
<Directory "/User/lch/Sites">
Options None
Allow from all
</Directory>
And, here is error short-cut:
I am a newer for Mac OS, and I come from china.
Thank you for read my question.
Two things to check:
Is the include for httpd-vhosts.conf uncommented in httpd.conf? I believe it's commented out by default.
Have you added myhome.com to your hosts file? Make sure you've got the following entry in /etc/hosts: 127.0.0.1 myhome.com