Access Apache virtual hosts stored in /users directory - apache

I just upgraded my Mac to Yosemite and managed to get my development environment almost back to normal. In my old set up (which I didn't create), I stored all of my virtual hosts in /etc/apache2/users as separate .conf files.
I'm now no longer able to access those sites and I need to set up my virtual hosts in the /etc/apache2/extra in the httpd-vhosts.conf file. While it does work, I'd rather not have to do this as it's easier for me to organize all of these extra sites when they're in separate files.
Is there anything I can do to get it back to my original set up? Using my old httpd.conf file doesn't work and I've left the new file that originally came with the Yosemite installation because at the very least it left my development environment functional.
The contents of the .conf files I set up in /etc/apache2/users look like this
<directory "/Users/dev/Sites/*/">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
#Require all granted
</directory>
NameVirtualHost *:80
<virtualhost *:80>
DocumentRoot "/Users/dev/Sites/mysitename"
ServerName mysitename.dev
</virtualhost>
I was able to get my virtual hosts working by putting them all inside of the httpd-vhosts.conf file. I only had to add the virtualhost bock and left the directory block and the namevirtualhost *:80 line out as that was already inside of the httpd-vhosts.conf file.

Apache's Configuration file /etc/apache2/httpd.conf can include separate configuration files for processing through the Include and IncludeOptional directive (the difference being, if Include doesn't get a matching file it will fail with error but IncludeOptional will just ignore it)
What you can do now is, add this line at the very bottom of your httpd.conf
IncludeOptional users/*.conf
and place your separate configuration .conf files in /etc/apache2/users directory for including them in the main httpd.conf file

Related

Xampp apache virtualhost

I have two "htdocs" folder for two websites. I named the folders "website1" and "website2".
As you can see, both folders are inside C:/xampp.
When I type "website1.com" on my browser, I want xampp to serve the files on folder "website1" and its subfolders/subfiles as needed.
When I type "website2.com" on my browser, I want xampp to serve the files on folder "website2" and its subfolders/subfiles as needed.
For simplicity, let's say the landing page is index.php (think of sign-in page). This is the page called when I type "website1.com" or "website2.com".
Then after I hit submit button on index.php, it will call main.php.
I edited two files.
For hosts file, I add the host name that I wanted to be resolved into my localhost. Since I want to serve both website1 and website2 on my local computer via xampp, I added the following lines on hosts file located on C:/Windows/System32/drivers/etc
127.0.0.1 website1.com
127.0.0.1 website2.com
I also edited the apache config file which is httpd.conf
I added the following after the line: Listen localhost:80
<VirtualHost website1.com:80>
DocumentRoot "C:\xampp\website1"
ServerName website1.com
<Directory "C:\xampp\website1">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost website2.com:80>
DocumentRoot "C:\xampp\website2"
ServerName website2.com
<Directory "C:\xampp\website2">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
The current behavior with these configs is that:
Typing "website1.com" or "website2.com" directs me/loads the page properly. For simplicity, the index.php of website1 has just different text compared to website2.
The problem is, once I hit submit button and it tries to load main.php (DIFFERENT folder locations depending if website1 or website2 but SAME file name), I get the following error:
Any help would be appreciated. I am new to web development.
I am also new to this topic (virtual hosts). I happen to search for this because I am getting tired of renaming my htdocs folder. RIght now, when I want to test website1, I rename website1 folder to htdocs. I am thinking that if what I am trying is possible, it may save me time in the long run.

XAMPP Virtual Host wildcard subdomains on Mac

I want to set up my virtual hosts in a way so that I don't have my projects in the htdocs folder and I would like to use subdomains to get to them. So for example instead of going to "http://localhost/myproject" which would be located inside "/xampp/htdocs/myproject", I'd rather like to go to "http://myproject.sites.local" which would be located inside "/Users/myname/Projects/myproject".
I know I have to go to httpd.conf inside the "etc" folder in xampp and uncomment the line that includes virtual hosts, done that. Then I went to "/etc/hosts" file to add "127.0.0.1 sites.local" and after that I have this chunk of code inside my httpd-vhosts.conf file:
<Directory "/Users/marioplantosar/Projects">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<Virtualhost *:80>
VirtualDocumentRoot "/Users/marioplantosar/Projects"
UseCanonicalName Off
</Virtualhost>
<Virtualhost *:80>
VirtualDocumentRoot "/Users/marioplantosar/Projects/%1"
ServerName sites.local
ServerAlias *.sites.local
UseCanonicalName Off
</Virtualhost>
The first part works I guess because if I visit "http://sites.local" it opens the Projects folder root (it just lists the subfolders because there is no index file inside of the "Projects" folder. But the other part is not working and it just throws the "ERR_NAME_NOT_RESOLVED" error. So I have the wildcard "*" in the ServerAlias so that it catches every subdomain of sites.local, and I use the "%1" to open the corresponding folder inside "Projects" folder, however it's just not working. The weirdest part is that it worked for months up until yesterday and I have no idea what happened that it just suddenly stopped working.
I figured out the problem. Everything I wrote in the question was configured correctly, but since I was doing all of that little over a year ago I totally forgot about the dnsmasq part which somehow got messed up. "hosts" file doesn't support wildcards so you have to use a tool like dnsmasq that does the dns masking automatically for every folder inside my "Projects" folder.
For anyone that gets into a similar situation like me, you configured everything in httpd-vhosts.conf, httpd.conf and hosts files correctly but it's not working you should follow this tutorial to set up the dnsmasq: https://passingcuriosity.com/2013/dnsmasq-dev-osx/

Prevent access to files through ip address - apache 2.4

I have asked a similar question before
Restrict access to directories through ip address
at that time the problem was solved for apache 2.2. Recently I re-installed the OS (to Debian 8) and it comes with apache 2.4.
I want to restrict access to files - when the request comes "by" IP. Mainly if in the browser I try to open http://192.168.252.178/test/image.jpg it should show error - 403 forbidden. Directory test is in www directory of apache. However I should be able to access that image if I type http://www.example.com/image.jpg - considering that example.com points to that test directory.
With apache version 2.2 I would simply put this lines in my default site config file - and the problem was solved
<Files ~ ".+">
Order allow,deny
Deny from all
</Files>
Now, trying the same thing does not work: I am getting 403 forbidden even if I try to open any site by the domain name.
Considering the changes in 2.4 I also tried this, but again getting the the same 403 forbidden when trying to open some site.
<Files ~ ".+">
Require all denied
</Files>
My goal is to prevent any kind of access to directories and files - if they are being accessed through ip address. I have also this lines in my default site's config to prevent the directory access and this works fine.
<Directory /home/username/www>
Options -Indexes
AllowOverride All
Require all granted
</Directory>
So, the question is - how to prevent file access through IP address. Also I need to achieve this by apache config, by htaccess is not a solution for me. And I need to achieve this for all the directories/files inside www recursively, so specifying the exact file names and/or directories is not a solution either.
Thanks
When you use name based virtual hosts, the main server goes away. Apache will choose which virtual host to use according to IP address (you may have more than one) and port first, and only after this first selection it will search for a corresponding ServerName or ServerAlias in this subset of candidates, in the order in which the virtual hosts appear in the configuration.
If no virtual host is found, then the first VHost in this subset (also in order of configuration) will be choosen. More.
I mention this because it will be important you have only one type of VirtualHost directive:
<VirutalHost *:80>
or
<VirtualHost 123.45.67.89:80>
I'll use the wildcard in the example.
You need a directory like /var/www/catchall with a file index.html or similar, as you prefer.
<VirtualHost *:80>
# This first-listed virtual host is also the default for *:80
# It will be used as the catchall.
ServerName 123.45.67.89
# Giving this DocRoot will avoid any request based on IP or any other
# wrong request to get to the other users directories.
DocumentRoot "/var/www/catchall"
<Directory /var/www/catchall>
...
</Directory>
</VirtualHost>
# Now you can add as usuall the configuration for any other VHost you need.
<VirtualHost *:80>
ServerName site1.com
ServerAlias www.site2.com
DocumentRoot "/home/username1/www"
<Directory /home/username1/www>
...
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName site2.com
ServerAlias www.site2.com
DocumentRoot "/home/username2/www"
<Directory /home/username2/www>
...
</Directory>
</VirtualHost>
Debian specific :
For Debian, you ideally put one VHost configuration per file, and put the file in the /etc/apache2/sites-available directory.
Name the files as you like, only the file containing the catchall vhost should be named something like 000-catchall, because they will be read in alphabetic order from the /etc/apache2/sites-enabled directory.
Then you disable Debian's usual default site :
a2dissite 000-default
and you enable the new catchall site and the other VHosts if needed :
a2ensite 000-catchall
An ls /etc/apache2/sites-enabled command should show the catchall as the first of list, if not change its file name so that it will always be the first. Restart Apache: service apache2 restart
Of course you could do all this changes in the original default VHost config file, but I usually prefer keep an original model.

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.

Apache ignore missing DocumentRoot

I would like to configure my apache different. Now, if one of DocumentRoots is missing, all server fail to start. That's annoying..
Is there any option, how to ignore this error?
Based on the assumption that your DocumentRoot directive is used synonomously with a VirtualHost, you could separate the VirtualHosts configuration into separate conf files, place these individually into their document roots and then include them with a directory wide include directive in a generic, serverwide conf. See below for an example:
Create a conf in the directory below:
/etc/apache/sites-enabled/sites.conf
add the following
Include /data/www/sites/
This will include any conf file in the above directory and any subdirectory
For a host with the following DocumentRoot
/data/www/sites/website-one
Create a VirtualHost conf file and place it in the above directory e.g:
/data/www/sites/website-one/website-one.conf
And your conf file will contain all the information about the VirtualHost including the DocumentRoot e.g
<VirtualHost *:80>
DocumentRoot /data/www/sites/website-one
ServerName www.website-one.com
# Other directives here
</VirtualHost>
If the DocumentRoot doesn't exist, then the conf file doesn't get loaded and the DocumentRoot directive never executed. For security reasons be careful that your conf files don't become dereferenceable. This should probably be disabled at a server level, but it is something to be aware of. Finally, the above pattern means that any bad conf files in the /data/www/sites directory and subdirectories will cause the server not to start.