Apache mod_wsgi flask crashes after about a minute - apache

I have an Amazon EC2 server running Apache 2.4. I am running one website on there using Python and regular CGI, and then another virtual host using mod_wsgi and an index.wsgi script. When I use a default WSGI callable class object script in my wsgi file, it works fine. However, if I use a WSGI-compatible framework like Flask or Bottle, it loads and works perfectly for about a minute, and then suddenly gives an error 503 ON BOTH OF MY SITES. Even if I change my script back to the default, this error persists for about 5 minutes and then it starts working again. I am using mod_wsgi with the usual daemon mode. Please help. I am using RedHat Linux, Apache 2.4, Python 2.7, and the latest flask and mod_wsgi.
EDIT: Here's my site-specific apache .conf file
<VirtualHost *:80>
ServerName ihave.nolife.lol
WSGIScriptAlias / /var/www/ihave/index.wsgi
WSGIDaemonProcess ihave user=apache group=apache processes=1 threads=5
<Directory /var/www/ihave>
Require all granted
WSGIProcessGroup ihave
WSGIApplicationGroup %{GLOBAL}
</Directory>
ErrorLog /var/www/html/ihave/errorlog
LogLevel debug
CustomLog /var/www/html/ihave/requests combined

Not enough information. But at a guess it is because you are using some third party Python packages which use a C extension module which will not work in sub interpreters. Read the following and set that directive. Also recommended that you make sure you are using daemon mode and not embedded mode.
http://modwsgi.readthedocs.io/en/develop/user-guides/application-issues.html#python-simplified-gil-state-api

Related

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!

Deploying Multiple Flask Apps On Server

I am attempting to deploy a 2nd flask application to an apache webserver. I have added a 2nd listener to the httpd.conf file for 127.0.0.1:8868 because I am accessing the api through a gateway located on my machine. The first app I deployed was set up in a very similar fashion and works perfectly. The flask app works fine when launched from the .py file. I have attached excerpts from the httdpd.conf file, httpd-vhosts.conf file, and the web.wsgi file. It doesn't record any errors in the apache errors log and or the access logs. It will record it in the access and error logs if I have an error in the .wsgi file. Having both listeners enabled also seems to interfere with the first application listening on port 5000.
httpd listeners:
Listen 127.0.0.1:5000
Listen 127.0.0.1:8868
httpd-vhosts.conf:
<VirtualHost *:5000>
WSGIScriptAlias / C:/Users/me/web_apps/task_manager/app/index/web.wsgi
<Directory C:/Users/me/web_apps/task_manager>
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:8868>
WSGIScriptAlias / C:/Users/me/web_apps/template_autoupdate/app/index/web.wsgi
<Directory C:/Users/me/web_apps/template_autoupdate>
Require all granted
</Directory>
</VirtualHost>
web.wsgi
import sys
sys.path.insert(0, "C:/Users/me/web_apps/template_autoupdate/app")
from autoupdater import app as application

Can't get apache2.4 to pass requests to web2py using mod_wsgi

I'm having a hell of a time setting up a web2py server in a way that allows me to access the admin and appadmin interfaces from anywhere other than localhost, which is a requirement for me because the web2py server is hosted in a cloud location without a browser or X server. I understand that to access web2py's admin or appadmin interfaces from outside localhost that I must use SSL/HTTPS.
The web2py documentation seems to declare two different ways to do this. First, from the general web2py startup tutorial:
The administrative interface, admin, is only accessible from localhost
unless you run web2py behind Apache with mod_proxy. If admin detects a
proxy, the session cookie is set to secure and admin login does not
work unless the communication between the client and the proxy goes
over HTTPS
This lead me to search the web for how to setup web2py behind and apache which lead me to the web2py deployment recipes page, where it describes setting up web2py behind apache using mod_wsgi instead of mod_proxy. These are the instructions I'm currently following exactly, with the exception of updating the /etc/apache2/sites-available/web2py file to apache2.4 syntax and modifying the ServerName directive's value to "foo.bar.com" (omitted: the domain name of cloud box containing the server, this is replaced with foo.bar.com throughout this question).
However, when I get to the
When you restart Apache, it should pass all the requests to web2py without going through the Rocket wsgiserver.
part of the that web2py+apache+mod_wsgi tutorial, apache does not appear to be passing anything to web2py for me. I went ahead and moved the wsgihandler.py file as the next step in the tutorial describes, and here's my current state:
If I browse to foo.bar.com:80 from another machine, I get the Apache2 Ubuntu Default Page (the "It works!" page).
If I browse to foo.bar.com:8000 (web2py port), I the web2py server's default interface, but with no access to admin or appadmin because of the unsecure channel.
If I try to browse to foo.bar.com:443, I get the generic "Index of /" apache server
page. On this page "/" contains one directory link "html", which
is a link to the Apache2 Ubuntu Default Page (the "It works!" page).
Other information:
I have an untouched instance of web2py installed at /home/www-data/web2py except that I've moved /home/www-data/web2py/handlers/wsgihandler.py to /home/www-data/web2py/wsgihandler.py
The contents of my /etc/apache2/site-available/ directory are:
000-default.conf (not touched by me)
default-ssl.conf (not touched by me)
web2py (created by me)
I've manually started up web2py with python ~/web2py/web2py.py --ip 10.7.166.27 (that's the IP for foo.bar.com (real domain name omitted))
apache2 is running as user www-data
The server OS is Ubuntu Server 14.04 x64
This is my /etc/apache2/sites-available/web2py file:
<VirtualHost *:80>
ServerName foo.bar.com
WSGIDaemonProcess web2py user=www-data group=www-data display-name=%{GROUP}
WSGIProcessGroup web2py
WSGIScriptAlias / /home/www-data/web2py/wsgihandler.py
<Directory /home/www-data/web2py>
AllowOverride None
Require all denied
<Files wsgihandler.py>
Require all granted
</Files>
</Directory>
AliasMatch ^/([^/]+)/static/(.*) /users/www-data/web2py/applications/$1/static/$2
<Directory /users/www-data/web2py/applications/*/static/>
Order Allow,Deny
Allow from all
</Directory>
<Location /admin>
Require all denied
</Location>
<LocationMatch ^/([^/]+)/appadmin>
Require all denied
</LocationMatch>
CustomLog /private/var/log/apache2/access.log common
ErrorLog /private/var/log/apache2/error.log
</VirtualHost>
<VirtualHost *:443>
ServerName foo.bar.com
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/server.crt
SSLCertificateKeyFile /etc/apache2/ssl/server.key
WSGIProcessGroup web2py
WSGIScriptAlias / /users/www-data/web2py/wsgihandler.py
<Directory /users/www-data/web2py>
AllowOverride None
Require all denied
<Files wsgihandler.py>
Require all granted
</Files>
</Directory>
AliasMatch ^/([^/]+)/static/(.*) /users/www-data/web2py/applications/$1/static/$2
<Directory /users/www-data/web2py/applications/*/static/>
Require all granted
</Directory>
CustomLog /private/var/log/apache2/access.log common
ErrorLog /private/var/log/apache2/error.log
</VirtualHost>
Alright! I found my stupid mistake while writing this up. I had never bothered to properly configure apache itself:
/etc/apache2/sites-available/000-default.conf and /etc/apache2/sites-available/default-ssl.conf were why I was seeing what I was seeing when browsing to foo.bar.com:80 and foo.bar.com:443 respectively.
apache2.4 seems to require the .conf on configuration files, I didn't have that so I renamed /etc/apache2/sites-available/web2py to /etc/apache2/sites-available/web2py.conf
I didn't have a symlink to /etc/apache2/sites-available/web2py at /etc/apache2/sites-enabled/web2py, so I made one.
So, as of now:
When I browse to foo.bar.com:80 I get the same Apache2 Ubuntu Default Page, BUT
When I browse to foo.bar.com:80/web2py I get an internal server error which the apache logs tell me is a python import error from wsgihandler.py, which means the apache<->mod_wsgi<->web2py link is up and running
When I browse to foo.bar.com:443/web2py I get Chrome's yellow screen complaining about my self-signed certificate and after clicking through I get a permissions-denied page, presumably because of something I haven't set up yet in web2py/SSL
This means my original question is answered, but I might come back here and comment/edit if I get stumped again getting SSL to work.

Setup Rails 4 with passenger for local development in localhost/rails_app

I can't believe that it turned out to be so difficult, but I need to be able to run my rails app at localhost/rails_app for development reasons, without breaking other applications I have. (localhost/php_app, for example)
I've followed a lot of tutorials, but I still can't get it working.
I have passenger installed and I think the missing step is to properly configure a VirtualHost.
(Mac OS X, Rails 4, Apache)
Edit: The big problem is that I can't run in localhost:3000 or any other port
Edit2:
With this in the apache configuration file:
<VirtualHost *:80>
ServerName localhost
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location /my_rails_app/ >
ProxyPass http://localhost:3000/
ProxyPassReverse http://localhost:3000/
</Location>
</VirtualHost>
The application runs on localhost/my_rails_app/ but it still uses localhost/ as the base url, missing every asset and making all the links wrong.
First off, on a local system you can just use the built-in test server that comes with Rails. Here's a virtual host that I use on a ubuntu server to run Rails:
<VirtualHost *:80>
ServerName errandlist.com
ServerAlias www.errandlist.com
# !!! Be sure to point DocumentRoot to 'public'!
DocumentRoot /var/www/errandlist/public
RailsEnv production
<Directory /var/www/errandlist/public>
# This relaxes Apache security settings.
AllowOverride all
# MultiViews must be turned off.
Options -MultiViews
</Directory>
</VirtualHost>
And you are going to need to enable Passenger in your httpd.conf:
# Ruby Passenger support
LoadModule passenger_module /usr/local/rvm/gems/ruby-1.9.3-p448/gems/passenger-4.0.19/buildout/apache2/mod_passenger.so
PassengerRoot /usr/local/rvm/gems/ruby-1.9.3-p448/gems/passenger-4.0.19
PassengerDefaultRuby /usr/local/rvm/wrappers/ruby-1.9.3-p448/ruby
It will be very similar on OSX, your versions may vary.
Edit: here's a question about running the test server on port 80: How to run rails s -p80 on 80 port?
I just use this script to run on localhost from the command line for debugging purposes for a project known as "priority_tree":
c:\ruby\bin\ruby C:\web\priority_tree\script\server -e test -p 3000
Then just open up IE or Edge with http:\localhost:3000 to test the program's operation.
You have to have dns setup to access a sql server or other database instance if you use it, but otherwise this should work.

redmine and mod_vhost_dbd

I've problem to run redmine with vhost_dbd_module from apache. Redmine was installed under /var/www/redmine/ directory. My apache config look:
<VirtualHost *:80>
ServerName HOSTNAME
DocumentRoot "/var/www/" # THIS IS NOT WORKING
# DocumentRoot "/var/www/redmine/public" # THIS WORKS WITH REDMINE
<Directory />
Options FollowSymLinks -MultiViews -Indexes
AllowOverride All
</Directory>
DBDriver mysql
DBDParams host=localhost,user=test,pass=test,dbname=test
DBDocRoot "SELECT document_root FROM vhosts WHERE server_name = %s" HOSTNAME
</VirtualHost>
Field document_root from database return '/var/www/redmain/public' - so it should works. It works for php projects where index file is under '/var/www/project/public'. Besides the page return 404 because in /var/www/redmain/public there is no index file. I want universal config
Any suggestions?
Best regards, Peter
Not havng a index.html is ok :) Check your config/routes.rb.
If rails service is running then your redmine rails check your routes.rb and deliver the default page there (view).
Passenger as an apache module is not compatible with about all modules used to define "dynamic" document roots (including mod_vhost_dbd). For passenger, you have to statically configure your apps in the Apache configuration.
Generally, you will have a hard time emulating your PHP setup with any Ruby app server, as they expect to be started once and run continuously, unlike PHP "apps" which act as scripts resolved and started new for each request. You should rethink your setup for persistent application server processes.
I created .htaccess with content below and it working :)
RailsBaseURI /
PassengerAppRoot /var/www/redmine