How to access multiple copies of wsgi site on server - apache

I have a Flask web site providing a service on an apache server, and now would like to run another copy of the code base for testing purposes on the same server (a different svn branch). So, I've installed the codebase in a different place on the server, and added another WSGIScriptAlias entry, referencing the test codebase, to the apache conf file:
# Entry point for the user web interface:
WSGIScriptAlias /mysite /blah/blah/wsgi_entry.py
# Entry point for the test branch of the user web interface:
WSGIScriptAlias /mysiteTEST /blah/blah/testBranch/wsgi_entry.py
I was hoping that sending the browser to "mysiteTEST" instead of the usual "mysite" would have me running the code in the test branch. However it runs the original code, presumably because the wsgi_entry.py simply does something like:
from my_main_module import app as application
and where it's looking for my_main_module is presumably the python path set in the apache config, like so:
WSGIPythonPath /blah/blah/main_code_place
Which is correct for the main site, but I want to mysiteTEST to be running the modules in the test branch location. So perhaps I can override the python path somehow in the testBranch/wsgi_entry.py (and perhaps not?), but is there a simpler way that I can manage this in the apache config? I.e. can I somehow specify one WSGIPythonPath for /mysite, and another for /mysiteTEST?

Set up multiple virtualhosts:
<VirtualHost your.ip:80>
ServerName blahblah
ServerAdmin blahblah#blah.com
WSGIDaemonProcess blahblah user=b group=lah threads=5
WSGIScriptAlias /mysite /blah/blah/wsgi_entry.py
<Directory /blah/blah>
Options Indexes FollowSymLinks Includes ExecCGI
WSGIScriptReloading On
WSGIProcessGroup blahblah
WSGIApplicationGroup %{GLOBAL}
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost your.ip:80>
ServerName blahblahTEST
ServerAdmin blahblah#blah.com
WSGIDaemonProcess blahblahtest user=b group=lah threads=5
WSGIScriptAlias /mysiteTEST /blah/blah/testBranch/wsgi_entry.py
<Directory /blah/blah/testBranch>
Options Indexes FollowSymLinks Includes ExecCGI
WSGIScriptReloading On
WSGIProcessGroup blahblah
WSGIApplicationGroup %{GLOBAL}
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

I ended up solving this by adding a single line to modify the path in the wsgi entry point for the alternate code version (i.e. the file referenced in the apache config line "WSGIScriptAlias /mysiteTEST /blah/blah/testBranch/wsgi_entry.py" in my example). The line overrides the wsgi configured python search path. Not the apache config only solution I wanted, but it's only a single line addition and it does the job:
import sys
sys.path.insert(0, '<path for alternate code modules>')
from my_main_module import app as application

Related

My apachectl commands sends me back "Illegal option ExecGCI"

i just installed apache by source installation, so after i create virtual host my apachectl doesn't restart, start, stop. My webserver is working i can access but these commands sends me back "illegal option ExeCGI"
This is Virtual host code:
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs"
ServerName node.iplaygrid.com
<Directory "/usr/local/apache/htdocs">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Thanks.
Looking at the docs: This option is in a module (mod_cgi). Unless that's active, you won't have the directive available. As you built from source, you'd likely also need to build the modules from source, and definitely load them (or rather: it)

App not responding after flask application Apache deployment(mod_wsgi)

I'm trying to deploy my flask application on Apache web server using mod_wsgi.
After the deployment, when i go to the 'healthCheck' URL i configured to return a simple text message, the app is not responding and it's timing out.
This is my wsgi file:
<VirtualHost *:5000>
ServerName <My ip address here>
ServerAdmin admin#mywebsite.com
WSGIScriptAlias / /var/www/ReleaseServices/ReleaseServices.wsgi
<Directory /var/www/ReleaseServices/ReleaseServices/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/ReleaseServices/ReleaseServices/static
<Directory /var/www/ReleaseServices/ReleaseServices/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I don't see any error in the apache logs as well. What could be the issue?
Please ask me if any extra details are required.
So i found the solution after searching a lot.
I was using scikit-learn in my init.py script and the import statement was causing issues and making the app unresponsive. After removing the import, it was working fine.
When i searched for solutions, i found some interesting facts related to the WSGI configuration, one of which i had missed out:
http://blog.rtwilson.com/how-to-fix-flask-wsgi-webapp-hanging-when-importing-a-module-such-as-numpy-or-matplotlib/
In that link, check the comment by Graham Dumpleton:
The specific solution is forcing use of the main interpreter by setting the application group to %{GLOBAL}. It is documented in
http://code.google.com/p/modwsgi/wiki/ApplicationIssues#Python_Simplified_GIL_State_API
Basically you have to add the following line to your .wsgi file:
WSGIApplicationGroup %{GLOBAL}
This will force a specific WSGI application to be run within the very first Python sub interpreter created when Python is initialised and hence solves the problem!

using flask virtualenv on remote host (not locally)

I have a CentOS 7 WSGI enables Apache Httpd server (on a VirtualBox machine) and it serves my developing Flask site at http://www.myflask.com on port 80.
Since I can't debug there (even if I tried everything I could), like I can on a local version set in a virutalenv at http://127.0.0.1:5000, I was trying to do this:
setting up a virtualenv (with flask installed) on the VB server, called myflaskv (/var/www/myflaskv)
putting myflask dir into myflaskv (/var/www/myflaskv/myflask)
I did end with the following httpd.conf virtual host section:
<VirtualHost *:80>
DocumentRoot "/var/www/myflaskv/myflask"
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory "/var/www/myflaskv/myflask">
Options Indexes FollowSymLinks MultiViews
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / /var/www/myflaskv/myflask/index.wsgi
ServerName www.myflask.com:80
</VirtualHost>
I can load the page http://www.myflask.com (even with virtualenv activated) but I can't load the page http://www.myflask.com:5000 as I would suppose to debug.
So I tried to add a virtualhost on port 5000:
<VirtualHost *:5000>
DocumentRoot "/var/www/myflaskv/myflask"
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory "/var/www/myflaskv/myflask">
Options Indexes FollowSymLinks MultiViews
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / /var/www/myflaskv/myflask/index.wsgi
ServerName www.myflaskv.com:5000
</VirtualHost>
supposing that this way I could load http://www.myfaskv.com, and instead I can't (ERR_CONNECTION_REFUSED).
By the way I temporarly disactivated ip-tables and se-linux.
So is there a way to load from browser the 127.0.0.1:5000 ip address of a remote host?
Or there is a better way to debug flask in an wsgi apache dev environment?
If you want to access the url from outside the box, you will have to run the server at 0.0.0.0:5000.
The url http://127.0.0.1:5000 cannot be accessed from outside the box you are running it in.
When you use wsgi to access your app, the error messages and stack traces are written to the web server error log, by default at /var/log/apache2/error.log. You don't need to use the virtualenv web server. Just write any debug output you need to a local log file (if you have debugging input you want to print) and look for the stack traces in the apache error log.

Run multiple independent Flask apps in Ubuntu

I'm trying to run two or more Flask applications in separate virtual directories with Apache, like http://localhost/site1 for /var/www/myapps/app1 and http://localhost/site2 for /var/www/myapps/app2. Each app has its own virtual environment under an env directory.
I started with a fresh install of Ubuntu 14.04 with Apache2 (v2.4.7), removed the default site configuration with sudo a2dissite 000-default and added configuration for my two apps.
Here's the conf file for app1 at /etc/apache2/sites-available/app1.conf. The cofiguration for app2 is identical, replacing app2 for app1 (and site2 for site1.)
<VirtualHost *:80>
ServerName localhost
WSGIProcessGroup site1
WSGIDaemonProcess site1 user=myserviceuser group=myserviceuser threads=5 python-path=/var/www/myapps/app1:/var/www/myapps/app1/env/lib/python2.7/site-packages
WSGIScriptAlias /site1 /var/www/myapps/app1/application.wsgi
WSGIScriptReloading On
<Directory /var/www/mysites/app1>
WSGIApplicationGroup site1
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Then I enabled each site with sudo a2ensite app1 (and app2) then restarted the server with sudo apache2ctl restart.
Each of these apps has the following application.wsgi file in the root dir:
# put a copy of this file in the root dir of each app instance
import os, sys
# activate the virtual environment
app_dir = os.path.dirname(__file__)
# removed next two lines after a comment left below
# activate_this = os.path.join(app_dir, 'env', 'bin', 'activate_this.py')
# execfile(activate_this, dict(__file__=activate_this))
sys.path.insert(0, app_dir)
from myapp import app as application
What happens is that one of the sites runs fine, responding with the correct page when I hit it. The other gives me an Apache 404 page. There are no typos in the conf files.
It seems the configurations for the apps are clobbering each other and one is "winning." I've spent a lot of time tweaking the configurations but the only way I could make it work was if I added localhost2 to my hosts file and changed the ServerName to localhost2 in one of the app configs, which isn't desirable in my case. Can someone point me to the correct way to configure Apache? Or am I going the wrong way about this?
Ideally I'd want the configuration files to not care which host name was used to reach them, probably becoming multiple copies of this server running behind a load balancer.
I spent more time on this and, if I'm starting to understand Apache terminology and configuration a little better, I cannot use virtual hosts for this purpose. VirtualHost sections are intended for serving different host names (multiple domains or subdomains.)
For configuring parallel applications as subdirs I could have used Directory sections instead. I also didn't realize some of the WSGI* directives in the config file could appear more than once. This new knowledge allowed me to produce the following single config file that does what I wanted. So instead of enabling one Apache site for each app, I could enable a single site with the directories configured in it.
# this goes in /etc/apache2/sites-available/
<VirtualHost *:80>
ServerName localhost
# logs configuration
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
WSGIDaemonProcess site1 user=myserviceuser group=myserviceuser threads=5 python-home=/var/www/myapps/app1:/var/www/myapps/app1/env/lib/python2.7/site-packages
WSGIScriptAlias /site1 /var/www/myapps/app1/application.wsgi
<Directory /var/www/myapps/app1>
WSGIApplicationGroup site1
WSGIProcessGroup site1
Order deny,allow
Allow from all
</Directory>
WSGIDaemonProcess site2 user=myserviceuser group=myserviceuser threads=5 python-home=/var/www/myapps/app2:/var/www/myapps/app2/env/lib/python2.7/site-packages
WSGIScriptAlias /site2 /var/www/myapps/app2/application.wsgi
<Directory /var/www/myapps/app2>
WSGIApplicationGroup site2
WSGIProcessGroup site2
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
EDIT:
I later followed Graham Dumpleton's suggestion and removed the activate_this stuff from application.wsgi and changed the WSGIDaemonProcess directive lines to:
WSGIDaemonProcess site1 user=myserviceuser group=myserviceuser threads=5 python-home=/var/www/myapps/app1/env

Rails: Vhost config for Apache and Passenger

I'm trying to get a simple Rails 4 app deployed on my server which already has Apache2 and is hosting several other sites and services (ie there are several vhost configs under sites-enabled). I've had some problems doing this on my local machine as well as my test server so I'm trying first to get it working on an AWS t1.micro instance with only one vhost config. I've written a script to do most of the heavy lifting for me which is on my github at rails-apache-passenger.
I have two vhost config files in the repo and have tried to get one or the other working. The script just copies over and enables one at a time.
Using the my-ruby-app-basic or the my-ruby-app vhost config I navigate to http://54.xxx.xxx.xxx/my-ruby-app/ but all I see is "The page you were looking for doesn't exist. You may have mistyped the address or the page may have moved." When I go to ttp://54.xxx.xxx.xxx/ I just get the default apache2 page ("It works!").
My /var/www/my-ruby-app/log/production.log shows
I, [2014-01-24T10:47:36.900542 #9612] INFO -- : Started GET "/my-ruby-app" for 80.81.17.94 at 2014-01-24 10:47:36 +0000
F, [2014-01-24T10:47:36.902169 #9612] FATAL -- :
ActionController::RoutingError (No route matches [GET] "/my-ruby-app"):
So clearly I need to modify my routes.rb file, but what am I supposed to change? As you can see from the script in the git repo, it's just the default routes.rb from rails new. I just want to see the default rails app landing page at this point so I'm not sure what to do to the routes.rb file.
Here are the vhost configs
my-ruby-app-basic
#This is the config suggested by the passenger module after it finishes compiling, modified for 'my-ruby-app'
<VirtualHost *:80>
ServerName www.my-ruby-app-host.com
# !!! Be sure to point DocumentRoot to 'public'!
DocumentRoot /var/www/my-ruby-app/public
<Directory /var/www/my-ruby-app/public>
# This relaxes Apache security settings.
AllowOverride all
# MultiViews must be turned off.
Options -MultiViews
</Directory>
</VirtualHost>
my-ruby-app
#Based on Apache section of Passenger documents
<VirtualHost *:80>
ServerName www.my-ruby-app-host.com
# !!! Be sure to point DocumentRoot to 'public'!
DocumentRoot /var/www/
<Directory /var/www/>
Allow from all
</Directory>
Alias /my-ruby-app /var/www/my-ruby-app/public
<Location /my-ruby-app>
PassengerBaseURI /my-ruby-app
PassengerAppRoot /var/www/my-ruby-app
</Location>
<Directory /var/www/my-ruby-app/public>
# This relaxes Apache security settings.
AllowOverride all
# MultiViews must be turned off.
Options -MultiViews
</Directory>
</VirtualHost>
Using Apache and Passenger is a short term solution but I want to know how to do it in any case (The long term view is that I want to maintain compatibility with Jruby and just run our app through Tomcat or Glassfish, which will no doubt be another Apache config debacle ;-) )