Deploy cherrypy on apache with mod-wsgi - python

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!

Related

Problems with deploying Flask application on Apache 2.4

I'm trying to deplay my Flask application on a RHEL7 Apache 2.4 server.
File structure is the following inside /var/www/html
/app
app.wsgi
/app
app.py
/templates
/static
In my /etc/httpd/conf/httpd.conf I have the following code to set up my project:
<VirtualHost *>
ServerName 10.65.112.75:443
WSGIDaemonProcess app user=apache group=apache threads=5 home=/var/www/html/app/app
WSGIScriptAlias / /var/html/app/app.wsgi
<Directory /var/www/html/app/app/>
WSGIProcessGroup app
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
Order deny,allow
Require all granted
</Directory>
Alias /static /var/www/html/app/app/static/
<Directory /var/www/html/app/app/static/>
Order deny,allow
Require all granted
</Directory>
And my app.wsgi contains the following:
#!/usr/bin/python
import sys
sys.path.insert(0, "/var/www/html/app/app/")
from app import app as application
The code for the project itself can be found in my github repository here.
I do not get any errors when trying to browse the server. It just doesnt do anything. Running my script from the terminal works, though.
Thanks for the help.
There are various things which aren't right.
You have:
WSGIScriptAlias / /var/html/app/app.wsgi
whereas it appears it should be:
WSGIScriptAlias / /var/www/html/app/app.wsgi
And:
<Directory /var/www/html/app/app/>
appears it should be:
<Directory /var/www/html/app>
Your VirtualHost definition also looks wrong. You have:
<VirtualHost *>
ServerName 10.65.112.75:443
If you really want this to be for HTTPS connections, you are missing all the SSL directives to add a SSL certificate.
ServerName would also usually be a fully qualified domain name and not an IP:PORT. The port number would usually be in the VirtualHost directive. For example, for HTTP, use:
<VirtualHost *:80>
ServerName my.host.name
where my.host.name is the full public host name for your machine which you use in the URL, not an IP address.

Django website on Apache with wsgi failing

so i'm about to lunch my first django website , i currently have a server that has been configured to host php websites and i've decided to test a simple empty project to get familiar with the process
so the python version in this server is bit old (2.6) so i couldn't install latest version of django , i installed 1.6 and since it's just a test that's not important (im going to upgrade python version when my website is ready to lunch )
so i've installed django and created a new project called testing in this dire
/home/sdfds34fre/public_html/
which you can see using this domain
http://novadmin20.com
and after reading documentation on django (unfortunately they have removed doc for 1.6 and i had to use 1.9) and wsgi i've updated my httpd.conf like this
<VirtualHost 111.111.111.111:80>
ServerName 111.111.111.111
DocumentRoot /usr/local/apache/htdocs
ServerAdmin somemeail#gmail.com
<IfModule mod_suphp.c>
suPHP_UserGroup nobody nobody
</IfModule>
<Directory /home/sdfds34fre/public_html/testing/testing>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess testing python-path=/home/sdfds34fre/public_html/testing:/usr/lib64/python2.6/site-packages/
WSGIProcessGroup testing
WSGIScriptAlias / /home/sdfds34fre/public_html/testing/testing/wsgi.py
</VirtualHost>
but even after restarting httpd service when i go to
http://novadmin20.com/testing/
all i see is directory list , am i missing something ?
here is my wsgi.py file
"""
WSGI config for testing project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/
"""
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testing.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
DocumentRoot directive is the main root of your problem. (ref)
try this config:
<VirtualHost 111.111.111.111*:80>
ServerName novadmin20.com
WSGIDaemonProcess testing python-path=/home/sdfds34fre/public_html/testing:/usr/lib64/python2.6/site-packages/
WSGIScriptAlias / /home/sdfds34fre/public_html/testing/testing/wsgi.py
<Directory /home/sdfds34fre/public_html/testing/testing>
<Files wsgi.py>
Order deny,allow
Require all granted
WSGIProcessGroup testing
</Files>
</Directory>
</VirtualHost>

Flask + apache + mod_wsgi + scipy/numpy

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>

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