django, apache, mod_wsgi and python py-scrypt not working together - python

I am running a ec2 instance to host some Django websites. The websites are being served by Apache with the use of mod_wsgi
Since a few days I am trying to deploy a new webplatform we are developing but I am running into a problem that seems impossible for me to solve. For security reasons we use scrypt 0.4 to secure the users personal information like passwords.
On the development server everything works like a charm but when we deploy to our live server we get a 500 Internal Server error. The apache log gives me the following message
Premature end of script headers: socialmarketingplatform.wsgi, referer:
When I uncomment the line where the scrypt module is used everything works fine. Also when I run the server delivered with Django with scrypt enabled everything works on the live server. So it is a combination of mod_wsgi, django and scrypt that generates the error.
I am using the following wsgi file:
import os
import sys
path = '/var/www/vhosts/[sub_domain]'
if path not in sys.path:
sys.path.append(path)
sys.path.append(path + '/socialmarketingplatform')
os.environ['DJANGO_SETTINGS_MODULE'] = 'socialmarketingplatform.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
And the following virtualhost config:
<VirtualHost *:80>
#Basic setup
ServerAdmin [removed email]
ServerName luxdevelopment.net
ServerAlias [sub domain]
DocumentRoot /var/www/vhosts/[sub domain]/socialmarketingplatform/
Alias /media/admin /usr/lib/python2.6/site-packages/Django-1.3-py2.6.egg/django/contrib/admin/media
Alias /media /var/www/vhosts/[sub domain]/socialmarketingplatform/media
<Location media="">
SetHandler None
</Location>
LogLevel warn
ErrorLog /var/log/httpd/smp_error.log
CustomLog /var/log/httpd/smp_access.log combined
WSGIDaemonProcess luxdevelopment.net user=apache group=apache threads=25
WSGIProcessGroup luxdevelopment.net
WSGIScriptAlias / /var/www/cgi-bin/socialmarketingplatform.wsgi
</VirtualHost>
I hope someone can help me with this problem. If there are any further question let me know.

See:
http://code.google.com/p/modwsgi/wiki/FrequentlyAskedQuestions#Apache_Process_Crashes
The message 'premature end of script headers' is usually indicative of your code crashing the daemon process. You can verify this by looking for segmentation fault or similar message in main Apache error log file. If you enable 'LogLevel info' in main Apache config and VirtualHost then mod_wsgi will log more about daemon process restarts.
A quick remedy if running only application in that daemon process group is to add:
WSGIApplicationGroup %{GLOBAL}
This will work around crashes caused by broken third party extension modules for Python which aren't written properly to work in sub interpreters.
Other than that, can be shared library version mismatches as described in the FAQ.

Related

Flask app running on apache resulting in 404 error

I'm using mac os x and trying to get the official flask tutorial to work with Apache, but when I attempt to open the site in localhost on my browser it results in a 404: The requested URL / was not found on this server. I followed these directions for configuring mod_wsgi for flask. I've run the app using the built in server with flask so I know the app itself works.
The following is the resulting error in the Apache error log
[negotiation:error] [pid 7563] [client ::1:50509] AH00687: Negotiation: discovered file(s) matching request: /Library/WebServer/Documents/flaskr/flaskr.wsgi (None could be negotiated).
Here is what I have in my flaskr.wsgi file located in /Library/WebServer/Documents/flaskr/flaskr.wsgi
import sys
sys.path.insert(0, '/Library/WebServer/Documents/flaskr')
from flaskr import app as application
I have the following in my httpd.conf file
Listen 80
<VirtualHost *>
ServerName localhost
WSGIDaemonProcess flaskr user=_www group=_www threads=5
WSGIScriptAlias / /Library/WebServer/Documents/flaskr/flaskr.wsgi
<Directory /Library/WebServer/Documents/flaskr>
WSGIProcessGroup flaskr
WSGIApplicationGroup %{GLOBAL}
Require all granted # httpd 2.4 syntax as noted in the guide
</Directory>
</VirtualHost>
LoadModule wsgi_module /usr/local/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-darwin.so
WSGIPythonHome /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6
From terminal when I run sudo apachectl -M it displays wsgi_module (shared). If I comment out the code in my httpd.conf file that I've listed above, the wsgi_module goes away from the enabled modules list, so unless there's something I'm not aware of mod_wsgi seems to be enabled successfully, but as I've mentioned I get a 404 message when trying to view the site.
EDIT
I don't think there's anything wrong with my Apache configuration. I tried this tutorial for getting flask up and running on Apache and it worked. I'll update once I figure out what I did wrong with the "flaskr" app.
Edit - found the problem
Turns out my flaskr.wsgi file was in the wrong directory. It was supposed to be in /Library/WebServer/Documents/flaskr/flaskr/flaskr.wsgi.
The official Flask tutorial I referenced uses the following directory structure
flaskr <----Initially put my `flaskr.wsgi` file in this directory
init.py
flaskr <----Changed to this directory and it worked
flaskr.py
flaskr.db
flaskr.wsgi
Also changed the system path to sys.path.append('/Library/WebServer/Documents/flaskr/flaskr')

Why apache throws Target WSGI script not found or unable to stat on Flask app?

I want to setup my flask app for apache on my ubuntu server using wsgi. But after my setup, I get the following browser error:
Not Found
The requested URL / was not found on this server.
The apache error log throws:
Target WSGI script not found or unable to stat:
/var/www/html/appname/appname.wsgi
My wsgi file looks like this:
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/html/appname/")
from IdeaHound import app as application
application.secret_key = 'here_the_key'
and my apache config file looks like this:
<VirtualHost *:80>
ServerName server_ip_here
WSGIScriptAlias / /var/www/html/appname/appname.wsgi
<Directory /var/www/html/appname/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel info
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
The appname folder has the following structure:
appname
--application.py
--appname.wsgi
--LICENSE
--README.md
--requirements.txt
--db_manage.py
--appname
----frontend
----__inity__.py
----__init__.pyc
----models
----__pycache__
----socket_interface
--AppName
----__init__.py
----static
----templates
--instance
----config.py
What am I missing here to make the webserver run correctly with Flask?
Please read the following link of Digital Ocean. Do as describe in it. And your app will be up and running in just 5 mins.
https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps
Also do check the apache error logs for more information if it still throws an error.
/var/log/apache2/error.log

Apache set-up with Flask and wsgi

I have a small web application that I have built using Flask and python. With the internal server that I used for developing everything runs fine. However now I want to use apache to start using it. But it doesn`t work. Keep in mind that I have never worked with apache or web based stuff before.
I used this guide as my starting point:
http://flask.pocoo.org/docs/deploying/mod_wsgi/
right now I have my application which is in the file called "/rg/server.py" and looks like this:
app=Flask(__name__)
# all app routes...
if __name__ == '__main__':
app.run(
debug=True,
host="127.0.0.1",
port=80
)
than I have a wsgi file as "/rg/wsgi/minerva.wsgi"
import sys
sys.path.insert(0, /rg)
from server import app as minerva
and finally I have an apache config file in "etc/apach2/sites-available/minerva.com":
<VirtualHost *>
ServerName minerva.test
WSGIDaemonProcess minerva threads=10
WSGIScriptAlias / /rg/wsgi/minerva.wsgi
<Directory /rg>
WSGIProcessGroup minerva
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Then I updated apache with a2ensite minerva.com which succeded. Then I releaded Apache and no errors. However I cannot acces minerva.test in any way...
If I type in apache2ctl -S it does list minerva.test
I have no idea what is going wrong...
system information:
OS: debian 64bit
python 2.7
The WSGI application entry point must be called 'application' for mod_wsgi. You have:
from server import app as minerva
It should be:
from server import app as application
You aren't even getting that far though, else the line:
sys.path.insert(0, /rg)
would give a syntax error.
Going back further, instead of:
<VirtualHost *>
you should have:
<VirtualHost *:80>
and finally, if 'minerva.test' isn't actually a resolvable host, you will not get anywhere.
So fill out your question with the actual URL you are using in the browser and also indicate whether 'minerva.test' is even listed in local hosts file.
The first thing I would check would be to make sure mod_wsgi is installed and loaded by apache. If that's fine, your setup looks pretty similar to mine with a few minor differences:
I had to add WSGISocketPrefix /var/run/wsgi above my VirtualHost definitoin. See here.
I included user and group values on the WSGIDaemonProcess line.

Flask & mod_wsgi app causing ports to stop working

My Flask and mod_wsgi app seems to be breaking ports. Every month or so my page will stop loading and I get a "Google Chrome could not connect to " message, but moving it to a new port fixes it. I've checked the apache log and there doesn't seem to be anything wrong there. If I stop apache from listening to the port and run my dev version of the Flask app on one of the ports that the live version has previously used I get the same "Google Chrome could not connect to " message. While apache is listening Netstat shows that the port is being listened to by apache and lsof -i returns a bunch of apache processes that are using the port. I'm not sure if any of that is normal for mod_wsgi. If I remove the port from apache both netstat and lsof return nothing but the port still doesn't work for mod_wsgi or flask.
Here is the mod_wsgi part of my config file with the ip, domain, and user/group changed
<VirtualHost 0.0.0.0:8880>
ServerName test.example.com
DocumentRoot /var/www/html
WSGIDaemonProcess dash user=user group=group threads=5
WSGIScriptAlias / /var/www/html/dash/dashboard.wsgi
<Directory /var/www/html/dash>
WSGIProcessGroup dash
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
# records regular flask errors
ErrorLog /var/www/html/dash/error.log
LogLevel warn
Here is my wsgi file
import os
import sys
# location of flask app
sys.path.insert(0, '/var/www/flask/dashboard')
from dashboard import app as application
# logs python errors at production.log
if not application.debug:
import logging
this_dir = os.path.dirname(__file__)
log_file = os.path.join(this_dir, 'production.log')
file_handler = logging.FileHandler(log_file)
file_handler.setLevel(logging.WARNING)
application.logger.addHandler(file_handler)
Rewriting the virtual host without an ip address seems to have fixed the issue.
<VirtualHost *:8880>
Thanks to the mod_wsgi user group who found the answer [https://groups.google.com/forum/#!forum/modwsgi][1]

Autoreloading of django files on server

I'm using centos, apache, mod_wsgi on server for django project.
After uploading over sftp of changed project files site opens with changes and without changes randomly.
I think, that changes should not be applied before restarting apache. Isn't it?
Apache settings
<VirtualHost *:88>
ServerName h1.ru
UseCanonicalName Off
ServerAdmin "admin#h1.ru"
DocumentRoot /var/www/h1/h1.ru/
AllowEncodedSlashes On
WSGIDaemonProcess h1 processes=4
#WSGIProcessGroup h1
WSGIScriptAlias /site /var/www/h1/pyh1/h1/wsgi.py
Alias /static /var/www/h1/pyh1/static
<IfModule mod_ssl.c>
SSLEngine off
</IfModule>
<Directory /var/www/h1/h1.ru>
php_admin_flag engine on
php_admin_flag safe_mode off
php_admin_value open_basedir "/var/www/h1/h1.ru:/tmp"
Options -Includes -ExecCGI
</Directory>
</VirtualHost>
wsgi:
#!/usr/local/bin/python2.7
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "h1.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Not true. Normally mod_wsgi will run in multiple separate processes, managing their startup and shutdown automatically. When a new process starts, Python code will be loaded from disk as it is imported, so that process will run with the newest versions of any files. Meanwhile, any processes that were already alive will use the versions they had previously loaded, so will not reflect the changes (unless a little-used code path is activated which leads to importing a so-far-unimported module).
Certainly the only way to be sure you have the latest choose in all processes is to restart Apache.

Categories

Resources