Setting root directory in a development environment - apache

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.

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.

VirtualBox Ubuntu 16.04 apache2 vhost jumping to https or not showing the page

I am trying to set up some vhosts on a Virtual Box with Ubuntu 16.04 Apache2 PHP 7.0 to start moving the code to PHP7.
I have moved the v-host file from the older VBox which looked like this
<VirtualHost 192.168.2.174:80>
ServerName admin.ubuntu16.de
ServerAlias admin.ubuntu16.de
ServerAdmin email#company.de
DocumentRoot /home/www/public_html/workspace/admin
php_value auto_prepend_file '/home/www/public_html/workspace/admin/administration/conf/register_globals.php'
<Directory /home/www/public_html/workspace/admin>
Options Indexes FollowSymLinks MultiViews ExecCGI
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
But it kept on trying to load the page with https or saying that there is no access to site root folder
These settings work perfectly on Ubuntu 14 Virtuall Box with apache2 and PHP 5.5.something
So I followed the tutorial and changed it to simply
<VirtualHost 192.168.2.174:80>
ServerName admin.ubuntu16.de
ServerAlias admin.ubuntu16.de
ServerAdmin email#company.de
DocumentRoot /home/www/public_html/workspace/admin
</VirtualHost>
But now it just jumps straight to https mode
I obviously ran a2ensite
I also switched off the default site by running "a2dissite 000-default.conf"
I also enable modules to do with proxying and a bunch of others while getting appache to actuallly start :-)
Any ideas on things I might off missed are greatly appreciated.
Just a little edit:
All the files for the sites are situated on the main Windows machine and are imported through /media/sf-workspace
The symbolic link is then created as /home/www/public_html/workspace
I would first eliminate the symbolic links and just make a copy of all the files inside the virtual box to remove that variable.
Apache can be screwy with symlinks.

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

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)

Accessing virtual host from computer on same local network

I am trying to make a setup so that I can access my website on a virtual host in computer A from computer B. Both A and B are on the same network.
I am using xampp on Win 7.
So here is as the problem goes computer A(server) has a virtual host configuration as follows in the httpd-vhosts.conf file.
NameVirtualHost project:81
<VirtualHost project:81>
DocumentRoot "D:/work/website"
ServerName project:81
<Directory "D:/work/website">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
(using port 81 as port 80 has IIS running, dont know much about these things however)
this configuration works fine on the local machine(server). i.e project:81 in the address bar of the browser opens up the website as it should.
Now on computer B(client) I changed the hosts file to contain the IP of the server along with the name of the virtual host like:-
192.168.1.7 project
now when I enter project:81 on the client browser .. it takes me to the server but its not taking me to the virtual host directory, instead it takes to the default directory .. i.e in my case is
C:\xampp\htdocs
Now I am stuck and unable to make the client to point to the current destination.
So can anybody suggest what I am doing wrong here or something else I need to do in order to have access to the correct virtual host site from the client machine.
Thanks in advance for any help
Ok So Seto El Kahfi's reply to my very old question led me to do some more research and reading on Apache's website.
So what I got is this, my NameVirtualHost directive was improper.
So Instead of this,
NameVirtualHost project:81
<VirtualHost project:81>
DocumentRoot "D:/work/website"
ServerName project:81
<Directory "D:/work/website">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
What I had to do was this.
NameVirtualHost *:81
<VirtualHost *:81>
DocumentRoot "D:/work/website"
ServerName project
<Directory "D:/work/website">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
Notice the ' * ' , I could have used an IP Address there too.(In this case my server's(machine A) local IP) both work. Now all I had to do is enter "project:81" on the client machine, and I get what my eyes wished to see.. :)
Few things I got from this.
1) How to use NameVirtualHost(or what it's purpose basically is.). Read More here
http://httpd.apache.org/docs/2.2/mod/core.html#namevirtualhost
This one is also good http://www.thegeekstuff.com/2011/07/apache-virtual-host/
2)You can use this via command line:
httpd -D DUMP_VHOSTS
to know how your virtual hosts are setup(will also give you some warnings regarding precedence if something's wrong with your setup)
3)Other's gesture to help you makes you help yourself.. :) So keeping helping and rocking.
Have you try to include the port at your client host's file?
192.168.1.7:81 project

Apache Stopped Following Symlinks

Yesterday, I had a fistful of sites running locally with no problem. Today, nothing opens and I have a log full of this:
Symbolic link not allowed or link target not accessible: /var/www
I have no idea what I did (I didn't open/change my httpd.conf file in any way), but clearly it was something bad. I run virtual hosts and the root directories are located in ~/Developer/www. In order to share the config files across multiple Macs with different home directories, I've created a symlink, /var/www which points to ~/Developer/www.
All of the virtualhost config files point their DocumentRoot to /var/www/project_directory and its own root directory has the FollowSymLinks option:
<VirtualHost *:80>
ServerName localhost
ServerAlias localhost.local localhost.dev
DocumentRoot /var/www/_localhost
<Directory /var/www/_localhost>
Options FollowSymLinks Indexes
AllowOverride None
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
My main httpd.conf file, similarly, has the FollowSymLinks option enabled for /:
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>
Any idea what I could have done to stop Apache from understanding symlinks or, better yet, what I can do to get it back on track?
Thanks.
UPDATE:
I should add that all of the directories in the "stack" are executable by all users and that this is the native Apache install on OS X Lion.
I guess I made an assumption that I shouldn't have. I had verified every relevant permission except the one that evidently mattered. Apache didn't have execute permissions on my top level home directory. I checked, re-checked and triple checked everything under that, but having never changed anything in that directory itself, I just didn't anticipate it being the issue.