Flask + apache + mod_wsgi + scipy/numpy - python

I have two Flask webservices that make use of Scipy/Numpy libraries. Due to C extensions in both libraries, I followed directions mentioned in a few posts and websites and had both under %{GLOBAL} application group.
The problem I'm having is that the first app that I send a http request to returns valid results, while the second app will always return a 404.
If I deploy only a single app then each work fine on their own, and hence I believe that the issue has to do with the numpy/scipy C modules.
Below is the virtual hosts file.
Anyone has any idea how to solve the issue?
WSGIPythonHome /home/myuser/miniconda3
WSGIChunkedRequest On
<VirtualHost *:80>
WSGIDaemonProcess app1 user=myuser group=mygroup
WSGIScriptAlias /app1 /var/www/app1/app1.wsgi
<Location /var/www/app1/>
WSGIProcessGroup app1
</Location>
WSGIDaemonProcess app2 user=myuser group=mygroup
WSGIScriptAlias /app2 /var/www/app2/app2.wsgi
<Location /var/www/app2/>
WSGIProcessGroup app2
</Location>
WSGIApplicationGroup %{GLOBAL}
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel debug
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Related

Deploying Flask app on a vps (Apache/Debian)

I'm trying to deploy a Flask app on a vps which OS is Debian and which is using Apache. I'm completely new to Apache so my problem may look simple but yet I was not able to find any solution on the web (I read a couple of tutorials and searched in this forum).
The server is running fine (it is already used for other purposes). When I try to access my app with www.myservername/web_app I get a 404 Not Found Error. Following http://flask.pocoo.org/docs/0.12/deploying/mod_wsgi/, here is what I've done:
I placed my application in /var/www/web_app/. In /var/www/web_app/app/init.py I created an instance of my app (named app) and configured it.
I placed the following .wsgi file in /var/www/web_app/:
activate_this = '/var/www/web_app/.pyenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this)
import sys
from app import app as application
Here is the /etc/apache2/sites-available/000-default.conf file (the site is enabled) :
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www
<Directory /var/www/html/redmine>
RailsBaseURI /redmine
PassengerResolveSymlinksInDocumentRoot on
</Directory>
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
WSGIDaemonProcess app user=www-data group=www-data threads=5
WSGIScriptAlias /web_app /var/www/web_app/app.wsgi
<Directory /var/www/web_app>
WSGIProcessGroup app
WSGIApplicationGroup %{GLOBAL}
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I also tried to replace /web_app with /var/www/web_app in the WSGIScriptAlias and also to access myservername/var/www/web_app. Moreover I tried to comment out the Rails/Passenger directives in the first Directory tag.
Of course I installed the wsgi module. I restarted the server before trying to access the page. Here is the access log:
... "GET /web_app HTTP/1.1" 404 508
And the error log :
[error] ... File does not exist : /var/www/web_app
Finally, my flask app - when using flask development server - works fine.
Thanks for your help!

how to configure django on apache and make https in the apache so that django url should open with https://<ip>:80 port

I want to get django to run on https server securely without using thirdparty packages like runsslserver, or sslserver for that i was this approach
in the settings.py file i have configured these line.
SECURE_SSL_REDIRECT = True
so when giving the url in the browser url able to redirect to https://192.168.31.2/cp_vm/details but i'm getting secure ssl error.
please suggest me any ways to get the output, or running the django on apache with https also fine but i'm able to find the links
https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04
but after that giving the ip in the browser 192.168.41.5/cp_vm/details it shows unable to connect.
i'm attaching the apache 000.default.conf file also please help me in this, Pleas forgive me if any mistakes in the above kindly suggest me some pointers so that
I can run django on apache server first .
Then getting the https// on the same apache server. please provide me some help would be great if any helping hands.
My config file as follows:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerName 10.206.51.6
ServerAdmin 10.206.51.6
#ServerAdmin webmaster#localhost
#DocumentRoot /var/www/html
DocumentRoot /var/www/nfvs_portal
## imc urls
ProxyPass /imc http://10.206.50.12:8080/imc
ProxyPassReverse /imc http://10.206.50.12:8080/imc
<Directory /var/www/html/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
Alias /static /opt/hpe_nfvs/nfvs_portal/static
<Directory /opt/hpe_nfvs/nfvs_portal/static>
Require all granted
</Directory>
<Directory /opt/hpe_nfvs/nfvs_portal/nfvs_portal>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess nfvs_portal python-path=//opt/hpe_nfvs/nfvs_portal:/opt/hpe_nfvs/nfvs_portal/nfvs_portal/lib/python2.7/site-packages
WSGIScriptAlias / /opt/hpe_nfvs/nfvs_portal/nfvs_portal/wsgi.py
</VirtualHost>
I faced a lot of troubles and did a lot of homework to complete django site + apache with https configuration on my ubuntu server. And finally, I could able to get it configured successfully and my site is working as expected with https now.
The below are the .py and apache configuration files. Please correct your files and let me know if you still face any issues.
wigs.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE","go_portal_site.settings")
os.environ['HTTPS'] = "on"
application = get_wsgi_application()
/etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
redirect permanent / https://site.site.com
Alias /static /home/ubuntu/django_portal/ci/site_portal/portal_site/static
<Directory /home/ubuntu/django_portal/ci/site_portal/portal_site/static>
Require all granted
</Directory>
<Directory /home/ubuntu/django_portal/ci/site_portal/portal_site/go_portal_site>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
#WSGIDaemonProcess go_portal_site python-path=/home/ubuntu/django_portal/ci/site_portal/portal_site python-home=/home/ubuntu/django_portal/ci/site_portal/portal_site/phase2_env
WSGIProcessGroup go_portal_site
WSGIScriptAlias / /home/ubuntu/django_portal/ci/site_portal/portal_site/go_portal_site/wsgi.py
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerName site.site.com
Alias /static /home/ubuntu/django_portal/ci/site_portal/portal_site/static
<Directory /home/ubuntu/django_portal/ci/site_portal/portal_site/static>
Require all granted
</Directory>
<Directory /home/ubuntu/django_portal/ci/site_portal/portal_site/go_portal_site>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess go_portal_site python-path=/home/ubuntu/django_portal/ci/site_portal/portal_site python-home=/home/ubuntu/django_portal/ci/site_portal/portal_site/phase2_env
WSGIProcessGroup go_portal_site
WSGIScriptAlias / /home/ubuntu/django_portal/ci/site_portal/portal_site/go_portal_site/wsgi.py
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/site.site.com-cer.pem
SSLCertificateKeyFile /etc/apache2/ssl/site.site.com-key.pem
</VirtualHost>
(phase2_env) ubuntu#ip-10-0-10-125:~/django_portal/ci/site_portal/portal_site$ sudo service apache2 restart
* Restarting web server apache2 [ OK ]
(phase2_env) ubuntu#ip-10-0-10-125:~/django_portal/ci/site_portal/portal_site$
On top of the HTTP server, you need to run an SSL server with Apache2, i.e. configure a <VirtualHost *:443>. Also, I would let Apache2 handle the redirection instead of Django.
<VirtualHost *:80>
# Virtual host for the redirection
ServerName 10.206.51.6
ServerAdmin 10.206.51.6 # <- This should be an email!
DocumentRoot /dev/null/
Redirect permanent / https://10.206.51.6/ # Redirects also what is after the "/"
</VirtualHost>
<VirtualHost *:443>
# SSL site
ServerName 10.206.51.6
ServerAdmin 10.206.51.6 # <- This should be an email!
DocumentRoot /var/www/nfvs_portal/
SSLCertificateFile /etc/apache2/certs/your-server-certificate.pem
SSLCertificateKeyFile /etc/apache2/certs/your-server-certificate.key.pem
# ... [The rest of your site configuration]
</VirtualHost>
If you get into trouble even with this configuration, I would split the problem in (1) making Apache2 manage to redirect and serve a static SSL page in your document root (without using WSGI), and (2) modify that configuration to use your Django site.

Deploy cherrypy on apache with mod-wsgi

I installed mod-wsgi and have a cherrypy helloworld code I want to deploy it to apache because I want to use multithreading and measure performance of cherrypy.
What apache configs I have to do including virtualhost and what to do with cherrypy code.
try this...
<VirtualHost *>
ServerName example.com
WSGIDaemonProcess yourapplication user=user1 group=group1 threads=5
WSGIScriptAlias / /var/www/yourapplication/yourapplication.wsgi
<Directory /var/www/yourapplication>
WSGIProcessGroup yourapplication
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
http://flask.pocoo.org/docs/deploying/mod_wsgi/#configuring-apache
your yourapplication.wsgi file will refer to your webapp.py file.
Hope this helps!

Howto configure Apache WSGI for multiple separate Django instances?

I have an apache instance where I have the following
WSGIPythonPath /production/somelocation/django12/lib/python2.4/site-packages/
<VirtualHost 192.168.1.1:443>
WSGIScriptAlias / /opt/project.wsgi
.....
My Django 1.5 app apache config looks like,
WSGIPythonPath /production/somelocation/django15/lib/python2.7/site-packages/
<VirtualHost 192.168.1.2:443>
....
WSGIScriptAlias / /opt/project2.wsgi
My /opt/project.wsgi looks like
import os
import sys
# django1.2 virtualenv
import site
site.addsitedir("/production/somelocation/django12/lib/python2.4/site-packages")
.....
However when I go to the site I still get my default django (1.5) instance. What am I missing ?
The other answers mention setting the python path, however using WSGIPythonPath or WSGIPythonHome are not correct. The WSGIPythonPath / WSGIPythonHome can only be set server-wide, so no different paths per virtualhost.
You would want to use the WSGIDaemonProcess python-path and home arguments to set the python path and your apps home directory per virtualhost.
Also, within your code there is no need to adjust python paths; just make sure your virtualhost config is correct.
You may need to set WSGIPythonHome since you have different Django installations.
WSGIPythonPath is used to define additional directories, but this option do not set default python installation. So probably, your default python directory also includes django (1.5) and recognize this version as the default django version. I do not know your python and django installation and configuration but this might be the reason.
Additional info for WSGIPythonHome
This is how I do with Pyramid:
<VirtualHost *:80>
Servername hackintosh
DocumentRoot "/Library/WebServer/Documents"
</VirtualHost>
<VirtualHost *:80>
ServerName modwebsocket.local
ErrorLog "/PythonProjects/MOD_WEBSOCKET/logs/error_log"
CustomLog "/PythonProjects/MOD_WEBSOCKET/logs/access_log" common
WSGIDaemonProcess pyramid-modwebsocket user=apero group=staff threads=4 python-path=/PythonProjects/MOD_WEBSOCKET/lib/python2.7/site-packages
WSGIProcessGroup pyramid-modwebsocket
WSGIScriptAlias / /PythonProjects/MOD_WEBSOCKET/wsgi/pyramid.wsgi
<Directory "/PythonProjects/MOD_WEBSOCKET/wsgi">
WSGIProcessGroup pyramid-modwebsocket
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName ai.local
ErrorLog "/PythonProjects/AI/logs/error_log"
CustomLog "/PythonProjects/AI/logs/access_log" common
WSGIApplicationGroup %{GLOBAL}
WSGIPassAuthorization On
WSGIDaemonProcess pyramid-ai user=apero group=staff threads=4 python-path=/PythonProjects/AI/lib/python2.7/site-packages
WSGIProcessGroup pyramid-wizard
WSGIScriptAlias / /PythonProjects/AI/wsgi/pyramid.wsgi
<Directory "/PythonProjects/AI/wsgi">
WSGIProcessGroup pyramid-ai
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
This topic and the typical causes are detailed in:
http://blog.dscpl.com.au/2012/10/requests-running-in-wrong-django.html
There is not enough information in your question to properly evaluate which of the problems you are encountering.

Flask WSGI application hangs when import nltk

I followed the instructions here to create a onefile flask-app deployed to apache2 with mod-wsgi on ubuntu. That all works fine when using the original flask app. However, when adding import nltk to the flask app apache hangs (no 500).
I use python 2.7 and nltk 2.0.4
Others seem to have had similar problems with other packages. Setting
WSGIApplicationGroup %{GLOBAL}
in the VirtualHost configuration seemed to have helped. However, I still get the same behavior. Did anybody run into the same issue? Thanks for the help!
Here is the VirtualHost Configuration file:
<VirtualHost *:8080>
# ---- Configure VirtualHost Defaults ----
ServerAdmin jsmith#whoi.edu
DocumentRoot /home/bitnami/public_html/http
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/bitnami/public_html/http/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
# ---- Configure WSGI Listener(s) ----
WSGIDaemonProcess flaskapp user=www-data group=www-data processes=1 threads=5
WSGIScriptAlias /flasktest1 /home/bitnami/public_html/wsgi/flasktest1.wsgi
<Directory /home/bitnami/public_html/http/flasktest1>
WSGIProcessGroup flaskapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
# ---- Configure Logging ----
ErrorLog /home/bitnami/public_html/logs/error.log
LogLevel warn
CustomLog /home/bitnami/public_html/logs/access.log combined
Here is the modified flask code
#!/usr/bin/python
from flask import Flask
import nltk
app = Flask(__name__)
#app.route('/')
def home():
return """<html>
<h2>Hello from Test Application 1</h2>
</html>"""
#app.route('/<foo>')
def foo(foo):
return """<html>
<h2>Test Application 1</2>
<h3>/%s</h3>
</html>""" % foo
if __name__ == '__main__':
"Are we in the __main__ scope? Start test server."
app.run(host='0.0.0.0',port=5000,debug=True)
Where you have:
<Directory /home/bitnami/public_html/http/flasktest1>
WSGIProcessGroup flaskapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
it should be:
<Directory /home/bitnami/public_html/http>
WSGIProcessGroup flaskapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
as you are neither running your app in daemon mode or in the main interpreter because of the directives being in the wrong context.
That Directory directive then conflicts with one for same directory above so merge them.
If using mod_wsgi 3.0 or later count instead perhaps drop that second Directory block and use:
WSGIDaemonProcess flaskapp threads=5
WSGIScriptAlias /flasktest1 /home/bitnami/public_html/wsgi/flasktest1.wsgi process-group=flaskapp application-group=%{GLOBAL}
Note that processes=1 has been dropped as that is the default and setting it implies other things you likely don't want. You also don't need to set user/group as it will automatically run as the Apache user anyway.

Categories

Resources