mod_wsgi: Multiple instances of the same app, with different configurations - mod-wsgi

I want to host multiple instances of an application with different configurations. I have it currently set up like this:
WSGIScriptAlias /foo /path/to/app.wsgi
<Location /foo>
SetEnv CONFIG foo.conf
</Location>
WSGIScriptAlias /bar /path/to/app.wsgi
<Location /bar>
SetEnv CONFIG bar.conf
</Location>
app.wsgi then looks like this:
def application(environ, start_response):
if not config.is_loaded:
config.load(environ.get("CONFIG"))
[...]
This seems to work, but I wonder if it is correct - ie will requests for /foo always arrive at an instance of the app that loaded foo.conf?
Also, is there a better way to set this up? (Note: I need this to run on Windows)

I think i figured it out through RTFM :-)
The default WSGIApplicationGroup %{RESOURCE} should apply, with %{RESOURCE} being distinct for the /foo and /bar aliases.
Since each application group is executed in it's own sub-interpreter, there should in fact always be distinct instances of the application for the two locations.

Related

Apache: <Directory> directive with no path specified

I have a local server environment running on my OS X machine using MAMP, and I am currently setting up a few virtual hosts. I found that, in order for one of my virtual hosts to recognize my site's .htaccess file, I needed to add a <Directory> directive within the <VirtualHost> directive.
Here is an example of what I am using to configure one of my virtual hosts:
<VirtualHost *>
DocumentRoot "/path/to/mysite"
ServerName mysite.local
<Directory "/path/to/mysite">
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
Now, I would like to avoid redundancy by removing the path from that <Directory> directive. I tried doing so and it seems to work, although I am not familiar with Apache enough to know the potential consequences of doing this. Furthermore, I could not find an explanation of this case in the Apache Documentation.
This is what I would like to do:
<VirtualHost *>
DocumentRoot "/path/to/mysite"
ServerName mysite.local
<Directory>
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
Therefore, my question is this: what happens when I omit the path from the <Directory> directive (in this case)?
Directory is a directive targeting a file system directory, and should always have a path (waildcards allowed).
Using some instructions targetd to no directory may be discarded silently, or maybe with an error (you could test it), but is would certainly be useless.
Why would you write some configuration instructions targeted to no directory at all?
But there's maybe more things in your question.
Ususally a Virtualhost should contains at least one Directory instruction, the DocumentRoot directory, and usually we also add one targeting the root of the filesystem, this way:
<Directory />
(instructions to deny access and avoid .htaccess reads)
</Directory>
<Directory /my/document/root/>
(instructions to allow access and still avoid .htaccess reads or to allow them if you are lazy and feel over confident that .htaccess are useful in any way and you do not need speed)
</Directory>
And you could add more, especially to avoid usage of .htaccess files as such file stored in /path/to/somewhere is the same thing as a <Directory /path/to/somewhere> section, but slower.
Now you do not like to repeat yourself in your VH configuration, so you coud maybe use .htaccess files or global level instructions for things that are usually set there. And you could also use another magical keyword, which is Location.
You can use a lot of Apache instruction in a <Location /foo> directive. Locations works on url, not on filesystem paths.
So <Location /> is your documentRoot, and <Location /foo> is maybe the subdirectory 'foo' under you DocumentRoot. This could help you write things in your VH without reusing the documentroot prefix everywhere. But be careful, urls are not file-system paths. Any protection applied on the url level may be weaker or stronger compared to a protection applied on a directory level. It depends on your alias, the way you use urls, etc.
Update:
If you use the new Apache 2.4 version you can now use mod_macro or even easier, built-in variables support:
Define docroot "/path/to/mysite"
DocumentRoot ${docroot}
<Directory ${docroot}>
(...)

Apache mod_proxy with Redmine on sub-URI

I must have read every single article on the internet regarding Redmine and Apache and I still cant get it to work!
I have Redmine running on http://server:3000 perfectly using Thin. We have another Apache service on the same server which hosts our internal web tools on a different port (8096) so I would like Redmine to follow the same convention.
In that respect on Apache in httpd.conf I've added:
ProxyPass /redmine http://127.0.0.1:3000/
ProxyPassReverse /redmine http://127.0.0.1:3000/
ProxyPreserveHost on
Which seems to work fine. If you type in http://server:8096/redmine it will bring up the Redmine start page, but clicking on any other link results is a 404.
It looks like its not appending the /redmine on links from the site as clicking on the 'admin' link takes me too http://server:8096/admin instead of http://server:8096/redmine/admin
I've also looked into adding any of the three below to config/environment.rb but there seems to be conflicting info on which to use, and none of them seem to:
ActionController::AbstractRequest.relative_url_root = "/redmine"
ActionController::Base.relative_url_root = "/redmine"
Redmine::Utils::relative_url_root = "/redmine"
As always any help is greatly appreciated!
Edit::
I am also trialling out
ProxyPass /redmine http://server:3000
ProxyHTMLURLMap http://server:3000 /redmine
<Location /redmine>
ProxyPassReverse http://server:3000
SetOutputFilter proxy-html
ProxyHTMLURLMap / /redmine/
ProxyHTMLURLMap /redmine/ /redmine
</Location>
but seems to have the same result. Interestingly, if I access http://localhost:8096/redmine from the local server all the links seems to work and direct to the correct page, except the CSS and JS is not working.
If remotely I manually type in any of the links http://server:8096/redmine/admin then I get the correct page with CSS etc, it's just the linking between pages that isn't working!
Man, I've never seen an working setup of apache proxy changing context like this:
ProxyPass /redmine http://127.0.0.1:3000/
This should work:
ProxyPass /redmine http://127.0.0.1:3000/redmine
To do that, you need to setup your backend server (mongrel or whatever) to work on a sub-uri.
I would suggest you to use mod_passenger instead. This is the easiest way to setup redmine and it doesn't involve proxy. Here follows my setup:
brunojcm#brunojcm-htpc:~$ cat /etc/apache2/sites-available/redmine
<Directory /var/www/redmine>
RailsBaseURI /redmine
PassengerResolveSymlinksInDocumentRoot on
</Directory>
Where /var/www/redmine is a link to your public redmine folder.
brunojcm#brunojcm-htpc:~$ ll /var/www/redmine
lrwxrwxrwx 1 root root 27 Sep 4 2011 /var/www/redmine -> /opt/redmine/current/public/
You will also need to install mod_passenger. I don't remember exactly how to do it, but I believe you need to gem install passenger and after run passenger-install-apache2-module and follow the instructions.
Hope it helps!

Change path chiliproject

I want to use a subdirectory for a chiliproject instance. Using apache passenger, I was thinking of using rewrites + alias, but then it gives me a 404. Adding a RailsBaseURI i get connection reset.
Is it routes.rb I should adapt or am I looking at the wrong place? It is working right now on https://mydomain.com but I'd like to have it on https://mydomain.com/tracker
You can use passenger directly without having to use an alias or redirection. However, Passenger requires some special configuration for that. Please see one of our guides for a complete installation example.
Generally you need to configure similar to this (cited from the linked guide):
At first, we assume you have installed ChiliProject to /srv/www/chiliproject. This is not your DocumentRoot.
You need to hint Passenger a bit here so that it correctly finds your ChiliProject. So we create a symlink from the existing DocumentRoot directory to out ChiliProject installation.
ln -s /srv/www/chiliproject/public DOCUMENTROOT/chiliproject
Now add the following directives into your existing virtual host:
# TODO: Remember to replace DOCUMENTROOT with your actual path
<Directory DOCUMENTROOT>
Options +SymLinksIfOwnerMatch
</Directory>
RailsBaseURI /chiliproject
# TODO: Remember to replace DOCUMENTROOT with your actual path
<Directory DOCUMENTROOT/chiliproject>
Options -MultiViews
Order deny,allow
Allow from all
</Directory>

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

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.

using apache location directive to list folders from trac

I have the following directory structure:
--var
----trac
------company1
--------project1
--------project2
------company2
--------project3
--------project4
and i was wondering if theres a way to specify in httpd.conf to list the directories when i go to domain.com/trac. Currently i wrote:
<Location /trac>
Options Indexes
</Location>
But i dont know how to specify the document root to /var/trac.
I tried to do
PythonOption TracEnvParentDir "/var/trac"
PythonOption TracUriRoot "/trac
but i get error 500, and i believe that is because the folders in /var/trac are not trac environments.
thanks.
I think you're right. You need to find a way to let Apache handle requests to "/" without the help of Python and trac.
It's a bit hard to give you advice because I don't know what your httpd.conf looks right now, but my trac-setup used a <LocationMatch> directive to catch everything that should not be handled by trac so Apache can take care of it.
So you could do something like this:
<LocationMatch "^/trac/.+">
# Your trac directives here
PythonHandler trac.web.modpython_frontend
....
</Location>
Alias /trac "/var/trac"
<Directory "/var/trac">
Options Indexes
Order allow,deny
Allow from all
</Directory>