How do I host multiple MVC3 sites on a single virtual host running Apache2? - apache

I'm trying to configure mod_mono with Apache2 on OSX. I would like to run multiple MVC3 projects on the same virtual host, but for some reason only the first one listed is working. Any help on this would be much appreciated as there is not much documentation on this. I've tried a lot of different config options, none of which seem to work.
Listen *:9005
<VirtualHost *:9005>
DocumentRoot "/Library/WebServer/vhosts/api"
ServerName api
MonoAutoApplication disabled
Alias /gamecenter "/Library/WebServer/vhosts/api/gamecenter"
AddMonoApplications gamecenter "/gamecenter:/Library/WebServer/vhosts/api/gamecenter"
MonoServerPath gamecenter "/usr/bin/mod-mono-server4"
MonoDebug gamecenter true
MonoSetEnv gamecenter MONO_IOMAP=all
MonoUnixSocket gamecenter-stage /tmp/mod_mono_server_gc
<Location /gamecenter>
Allow from all
Order allow,deny
MonoSetServerAlias gamecenter
SetHandler mono
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip dont-vary
</Location>
Alias /gamecenter-stage "/Library/WebServer/vhosts/api/gamecenter-stage"
MonoServerPath gamecenter-stage "/usr/bin/mod-mono-server4"
MonoDebug gamecenter-stage true
MonoSetEnv gamecenter-stage MONO_IOMAP=all
AddMonoApplications gamecenter-stage "/gamecenter-stage:/Library/WebServer/vhosts/api/gamecenter-stage"
MonoUnixSocket gamecenter-stage /tmp/mod_mono_server_gcs
<Location /gamecenter-stage>
Allow from all
Order allow,deny
MonoSetServerAlias gamecenter-stage
SetHandler mono
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip dont-vary
</Location>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript
</IfModule>
</VirtualHost>

your problem is that your Alias name and physical path are one and the same, so apache doesn't know which one to serve up.
NOTE: I'm giving the answer based on general Apache2 configuration, and not on mod_mono, maybe mod_mono does something to prevent this, I've not set MVC apps up under a *nix box before :-)
Anyway...
if you look at your path configurations you have...
/Library/WebServer/vhosts/api
/Library/WebServer/vhosts/api/gamecenter
/Library/WebServer/vhosts/api/gamecenter-stage
without your aliases in place, these already resolve to the paths your trying to map.
/Library/WebServer/vhosts/api = /
/Library/WebServer/vhosts/api/gamecenter = /gamecenter
/Library/WebServer/vhosts/api/gamecenter-stage = /gamecenter-stage
Your then telling Apache that
/ = /
/gamecenter = /gamecenter
/gamecenter-stage = /gamecenter-stage
When Apache tries to deliver the content if there is no file subfix or existing slash (as in the last 2) it will automatically, subfix the folder with a / then issue a redirect (306 I believe) essentially telling the browser to redirect from EG:
/gamecenter to /gamecenter/
With the alias in place to tell it that Alias ... is at location x it then has to try and make a desicion to serve
/gamecenter/
or
/gamecenter/gamecenter/../ (Because in terms of folder structure the alias name is 1 folder level down in the web than it is physically)
and ends up getting confused, and so does what any virtual host set up does when it's unable to resolve the path, and that's return the website root.
AS I SAY however, this is general NON-MONO Apache behaviour, it is possible that mod_mono may alter the processing pipeline in some way to that may change this behaviour.
What I would recommend is to split this into 3 virtual hosts which you can do very very easily even on just one IP.
First thing you'll want to do is somwhere in your master Apache config file, have a
Listen 9005
statement. This will make ALL virtual instances listen on that port as well as any other configured port EG: 80
Next make sure you have a default catch all virtual host, this will catch any server name not mapped elsewhere:
<VirtualHost *>
DocumentRoot "/some/folder/where/the/default/is/"
#Followed by other server directives. NOTE: there is NO servername line
</VirtualHost>
Once you have that set up, then move onto your "api" sub domain
<VirtualHost *>
ServerName api
DocumentRoot "/Library/WebServer/vhosts/api/"
#Other required directives here
</VirtualHost>
At this point, I'm going to pause to discuss your domain name. If this is an internal test system (Which I suspect it is) then you'll find life with virtual domains way easier if you install a DNS server on you box, then set that up as a master domain using a private internal network address.
EG:
Create a root zone, and call it "mydevnetwork.local"
then add machine names to it:
EG: if your pc is called devpc1, create an IP address for "devpc1.mydevnetwork.local" and give your pc a static IP address of EG: 192.168.50.1
Then set an alias for that so
api.mydevnetwork.local = devpc1.mydevnetwork.local
Iv'e not got the room to do a full DNS setup post here, but hopefully you get the idea.
Once you have DNS (or at a minimum host file entries) set up, then your virtual hosts under Apache become really easy to manage:
<VirtualHost *>
ServerName api.mydevnetwork.local
DocumentRoot "/Library/WebServer/vhosts/api/"
#Other required directives here
</VirtualHost>
and easy to relocate to another machine should you need too.
You can set the rest of your virtual hosts up in much the same way
<VirtualHost *>
ServerName gamecenter.mydevnetwork.local
DocumentRoot "/Library/WebServer/vhosts/api/gamecenter/"
#Other required directives here
</VirtualHost>
<VirtualHost *>
ServerName gamecenter-stage.mydevnetwork.local
DocumentRoot "/Library/WebServer/vhosts/api/gamecenter-stage/"
#Other required directives here
</VirtualHost>
Note iv'e set the paths to be the same as you had above, and even though this will work, I'd strongly advise that you give each one it's own unique folder, I generally do something like:
wwwroot
api.mydevnetwork.local
htdocs <-- Web files go here
cgi-bin <-- cgi scripts go here and it's mapped to /cgi-bin/
logs <-- logs here
access <-- htpasswd files here
Hopefully if the above is not a complete solution, you might at least get some further ideas of investigation from it.

Related

Multiple apache2 Virtual host sitses - All work except one

I have a problem with apache2 and Virtual Hosts. I run a server with multiple sites. I have subdomains setup that point to my server IP. My customers, those that choose to have their own domain names point their domain/sub-domain to one of my subdomains. I've done this with multiple sites and it's always worked, except for now, and I cant figure out why.
This is the virtual host that doesnt seem to work, it always points to my default site when i try to access it.
The domain redirecting goes like this. customer.domain.com -> customer.mydomain.com -> server IP
<VirtualHost customer.domain.com:443>
ServerName customer.domain.com
ServerAlias *customer.domain.com *customer.mydomain.com
DocumentRoot /var/www/page
<Directory />
Order Deny,Allow
Deny from all
</Directory>
<Directory /var/www/page>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/customer/logs/error.log
LogLevel warn
CustomLog /var/log/apache2/customer/logs/access.log combined
SSLEngine on
SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
BrowserMatch ".*MSIE.*" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
</VirtualHost>
This format is used for all other sites, and they are directed to the correct folder and site. But with this particular site it always gets directed to the default site.
I've noticed that when i run apache2ctl -S I get two IPs. One is my servers IP with all of the virtual hosts, and the second IP seems connected to the new virtual host, the one that doesnt work.
52.52.52.52:443 customer.domain.com (/etc/apache2/sites-enabled/006-customer.domain.com.conf:1)
12.12.12.12:443 is a NameVirtualHost
default server default.mydomain.com (/etc/apache2/sites-enabled/001-default.mydomain.com.conf:1)
port 443 namevhost default.mydomain.com (/etc/apache2/sites-enabled/001-default.mydomain.com.conf:1)
alias www.default.mydomain.com
port 443 namevhost customer2.domain.com (/etc/apache2/sites-enabled/002-customer2.domain.com.conf:1)
alias www.customer2.domain.com
wild alias *subdomain.mydomain.com
*:80 default.mydomain.com (/etc/apache2/sites-enabled/000-default.conf:1) ..... more
I'm not really sure what to look for, the logs don't seem to tell me anything useful.
Any ideas on what I might be doing wrong? Thank you.
You should avoid putting hostnames inside of <virtualhost> at all costs. On a typical system, *:port in each vhost is sufficient.
If you really care about what local interface a connection is related to BEFORE checking the hostname used, put a local interface address name and not a hostname. But most people simply do not care what interface was used.
The particular risk of using a hostname is that it might not resolve to any local interface, in which case Apache will never map a connection to it. The per-request hostname matching only kicks in from the best matching connection-level info.

How to serve sites from http://ip/folder?

Please help.
I'm trying to setup 3 test sites, so each could be reached from my server's ip like:
my-server-ip/site1
my-server-ip/site2
my-server-ip/site3
When I use ServerName as ip it works fine:
<VirtualHost 127.0.0.1:8080>
ServerName x.x.x.x
DocumentRoot "/home/myfolder/public_html"
<Directory "/home/myfolder/public_html">
AllowOverride All
</Directory>
</VirtualHost>
but using ServerName x.x.x.x/site1 is not working ofcourse.
I tryed to use Alias /site1 /path and it works for first virtual host but when i add 2 more virtual hosts to vhosts.conf only first works.
Another trick that i tryed is to add:
Alias /site1 /home/folder1/public_html
Alias /site2 /home/folder2/public_html
Alias /site3 /home/folder3/public_html
into /usr/local/apache/conf.d/domain-redirects.conf
and add options into /usr/local/apache/conf/httpd.conf
And this does work but not correctly.
I know I could use some domain name and setup a buch of subdomains for other sites..but using ip is better.
Is there a way to set things up this way?
You need to deposit your sites into the directory that the server accesses. The default that apache gives is /var/www. So if your file is at /var/www/test.html you can access it with 127.0.0.1:8080/test.html .
Thanks! Your idea led to solution. Just in case someone needs the same setup, here is what i've done (running on Centos 6 + Apache+Varnish).
Create and upload folders/files just like you do for normal domains.
By default http://ip -> my apache looks into a folder:
/usr/local/apache/htdocs
I could create a folder here and upload a site here but rather I've created a symlink to my testsite:
ln -s /home/user/public_html/test_site_folder testsite1
now my testsite is available at http://ip/testsite1
almost...to make it work for user I added in httpd.conf this statement (AllowOverride) in order to be able to use mod_rewrite and others in .htaccess located in root folder of testsite1
*** added
<Directory "/usr/local/apache/htdocs">
suPHP_UserGroup user user
AllowOverride All ***
Order allow,deny ***
Allow from all ***

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.

Setup Dynamic Virtual Host (Apache2 on Ubuntu)

I want to set up a single virtual host that can dynamically handle all requests based on the hostname used to access it. If %{HTTP_HOST} could be used in a DocumentRoot, this is probably exactly what I want:
<VirtualHost *:80>
ServerAdmin me#example.com
DocumentRoot /var/www/live/%{HTTP_HOST}/public
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/live/%{HTTP_HOST}/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
# Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
LogLevel warn
ErrorLog /var/www/live/%{HTTP_HOST}/logs/error.log
CustomLog /var/www/live/%{HTTP_HOST}/logs/access.log combined
</VirtualHost>
...unfortunately, %{HTTP_HOST} is not allowed in the DocumentRoot (Warning: DocumentRoot [/var/www/live/%{HTTP_HOST}/public] does not exist). How else can I achieve my goal?
Update: I thought of pointing a catch-all vhost to a single directory and having a .htaccess use mod_rewrite to dynamically select the path but (honestly) I'm exhausted. I'll try at it again in the morning, but in the meantime, if anyone has good ideas, I'd love to hear them! Thank you!
Maybe you can try the following solution from this article: Apache: Dynamic Virtual Hosts
A few months back I looked for a solution to overcome the problem of
creating individual Virtual Hosts in Apache every time I wanted to
configure a new site on a development machine (something that is a big
issue in work where we have a lot of websites). Apache is able to
support this functionality relatively easy using a module and a few
lines in the configuration file. I set this up on Fedora 14, so
results may be slightly different for other OS's (different paths,
configuration file setup, etc)
Open up the main Apache conf (/etc/httpd/conf/httpd.conf), and ensure
the module mod_vhost_alias is enabled. There should be a line in the
configuration like
LoadModule vhost_alias_module modules/mod_vhost_alias.so
Next, add the
following lines to the bottom of this file. You'll need to edit the
file with sudo privileges.
NameVirtualHost *:80
UseCanonicalName Off
<VirtualHost *:80>
VirtualDocumentRoot /var/www/html/domains/%0
</VirtualHost>
This sets up a catch all for any domain coming in over port 80 (the
default port for http traffic, if your using https you will need to
use 443 - alternatively you could remove the port restriction). The
important line here is the VirtualDocumentRoot. The tells Apache where
your files will reside on disk. The %0 part takes the whole domain
name and inserts it into the path. To illustrate this if we went to a
domain testing.com.dev the VirtualDocumentRoot would be:
/var/www/html/domains/testing.com.dev
This type of configuration might
be suitable for most situations, however I didn't want to have the
.dev part of the domain in my folders on disk. I was able to achieve
this by setting the VirtualDocumentRoot to:
VirtualDocumentRoot /var/www/html/domains/%-2+
The above example of testing.com.dev would now point to:
/var/www/html/domains/testing.com
Remember to add the domain to your
hosts file (/etc/hosts)
For a full list of options see the mod_vhost_alias documentation.
Additional documentation can be found here.
The official methods for achieving dynamic virtual hosts are explained in the Apache documentation:
http://httpd.apache.org/docs/2.0/vhosts/mass.html

Magento not accessible since tried to move to multi website setup. Apache issue?

I wish I had never seen this article:
http://www.magentocommerce.com/knowledge-base/entry/tutorial-multi-site-multi-domain-setup
I have Apache 2.2 installed on my XP machine and until a while ago I had a Magento site that I could test the development of a custom module on. I decided that I wanted to have multiple websites and multiple stores so that I could test that my modules configuration variables set at the different scopes (global, website, and store) were working as expected.
So I followed the instructions in the above Magento article. I created a website and gave it a name of “paulsplace.com”. I created a couple of Stores under that website. I then went to System/Configuration/General/Web and, with the scope set to paulsplace.com, I set the unsecured and secured URLs to http://paulsplace.com/ and https://paulsplace.com/ and hit Save Config - what a mistake!!
I got a 404 error. And now I can’t get to my magento front end or back end.
I tried a couple of things:
I added these lines to my hosts lookup file:
127.0.0.1 paulsplace.com
127.0.0.1 www.paulsplace.com
I then uncommented this line in my httpd,conf file:
Include conf/extra/httpd-hosts.conf
and added the following to the conf/extra/httpd-hosts.conf file:
<VirtualHost *:80>
ServerAdmin me#myemail.com
DocumentRoot "C:/Applications/Apache Software Foundation/Apache2.2/htdocs"
ServerName paulsplace.com
ErrorLog "logs/paulsplace.com-error.log"
CustomLog "logs/paulsplace.com-access.log" common
</VirtualHost>
and restarted Apache.
If I browse to “http://www.paulsplace.com” I now get a page that just says “It works!”. Same for “http://paulsplace.com” and “http://www.paulsplace.com/magento/index.php”.
I tried a few more things - I added this line to httpd.conf:
AccessFileName htaccess
(I did this because Windows Explorer didn’t let me create a file starting with a dot; I could do it from the command prompt, but I believe what I have done should be ok).
I changed AllowOverride to All from None:
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
</Directory>
<Directory "C:/Applications/Apache Software Foundation/Apache2.2/htdocs">
AllowOverride All
</Directory>
and in C:\Applications\Apache Software Foundation\Apache2.2\htdocs\htaccess (a file that I created), I entered:
SetEnvIf Host www\.paulsplace\.com MAGE_RUN_CODE=pws1
SetEnvIf Host www\.paulsplace\.com MAGE_RUN_TYPE=website
SetEnvIf Host ^paulsplace\.com MAGE_RUN_CODE=pws1
SetEnvIf Host ^paulsplace\.com MAGE_RUN_TYPE=website
(pws was the value I used for the “Code” when creating my store).
Please tell me how I can put this right. I feel like I’m taking one step forward and three backward at the moment.
Any help really would be greatly appreciated.
<VirtualHost *:80>
ServerAdmin me#myemail.com
DocumentRoot "Change this to point at your magento install"
ServerName paulsplace.com
ErrorLog "logs/paulsplace.com-error.log"
CustomLog "logs/paulsplace.com-access.log" common
SetEnv MAGE_RUN_TYPE website
SetEnv MAGE_RUN_CODE pws1
</VirtualHost>
If changing anything in System Configuration borks your system, you can always clear out the bad values in the database directly, and clear your Magento cache. Do a
select * from core_config_data where value LIKE '%paulsplace.com%'
This will give you the two rows that were added when you clicked save. Remove the rows. Next, clear out all the files in
var/cache/*
to clear your cache. Then restore your Apache config to what it was before you started monkeying around. This should restore your site back to its previous state, and you can continue to experiment with things.