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]
Related
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
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.
I am trying to set up Flask to run on SSL, along with redirecting all HTTP traffic to HTTPS.
I am currently using Apache as a web server, and it is serving traffic on port 80 HTTP properly. However, when I move the configuration under the port 80 VirtualHost into the port 443 and set up redirection for port 80, redirection works but Apache keeps showing the Apache Test Page, and is not serving the Flask application. The error logs are not showing anything useful. The only error I see is Directory index forbidden by Options directive: /var/www/html. I don't even use /var/www/html, and I know this is mostly a warning for older browsers.
Here is my Apache virtual host set up:
LoadModule wsgi_module modules/mod_wsgi.so
WSGISocketPrefix run/wsgi
NameVirtualHost *:80
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>
NameVirtualHost *:443
<VirtualHost *:443>
SSLEngine on
SSLEngine on
SSLCertificateFile <<FILE PATH>>
SSLCertificateKeyFile <<FILE PATH>>
SSLCertificateChainFile <<FILE PATH>>
SSLProtocol all -SSLv2
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW
WSGIPassAuthorization On
WSGIDaemonProcess api processes=4 threads=1
WSGIProcessGroup api
WSGIScriptAlias / /usr/local/app/api/current/conf/application.wsgi
AddType text/html .py
<Directory /usr/local/app/api/current/>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
What is wrong with it? When I copy all the WSGI and Directory lines into port 80, and copy out 443, it works properly.
This question belongs to Super User.
Btw it's not Flask related.
You're missing DocumentRoot directive in your VirtualHost definition.
So Apache uses the default /var/www/html.
Checkout SSlify. Using this small flask extension all traffic gets redirected from HTTP to HTTPS.
All you have to do is to run the following when initializing your flask app:
from flask import Flask
from flask_sslify import SSLify
app = Flask(__name__)
sslify = SSLify(app)
Ι have developed a Django project and uploaded it to a cloud VΜ. Currently i have access to it through 8080 port.
python manage.py runserver 0.0.0.0:8080
If i enter the url without the 8080 port, it shows the "it works" page. How can i set my Django project to run by default on 80 port?
I am using Ubuntu 12.04 server
As docs say, runserver isn't meant as a deployment server. It also mentions that you probably can't start it on port 80 unless you run it as root.
Here is the kind of file you will need to put in /etc/apache2/sites-enabled, you need to adjust the paths in it. You also need to load mod_wsgi which you can do via apt-get.
<VirtualHost 192.168.1.14:80>
ServerAdmin youremail#whatever.com
ServerName www.whatever.com
ServerAlias whatever.com
Alias /robots.txt /home/dimitris/Python/mysite/site_media/robots.txt
Alias /favicon.ico /home/dimitris/Python/mysite/site_media/favicon.png
Alias /static/ /home/dimitris/Python/mysite/site_media/static
WSGIDaemonProcess mysite user=dimitris processes=1 threads=5
WSGIProcessGroup mysite
WSGIScriptAlias / /home/dimitris/Python/mysite/deploy/mysqite_wsgi.py
# Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
LogLevel debug
ErrorLog ${APACHE_LOG_DIR}/mysite.error.log
CustomLog ${APACHE_LOG_DIR}/mysite.access.log combined
ServerSignature Off
</VirtualHost>
Most of this assumes you are running in a virtualenv, that means you will need a wsgi file to get things running. The above file sets up apache to run your "wsgi" file which looks something like this:
import os
from os.path import abspath, dirname, join
import sys
with open("/tmp/mysite.sys.path", "w") as f:
for i in sys.path:
f.write(i+"\n")
#redirect sys.stdout to sys.stderr for libraries that use
#print statements for optional import exceptions.
sys.stdout = sys.stderr
sys.path.insert(0, abspath(join(dirname(__file__), "../../")))
sys.path.insert(0, abspath(join(dirname(__file__), "../../lib/python2.7/site-packages/")))
from django.conf import settings
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
mod_wsgi, then opens this one file and executes it. application is the part that waits for requests from the webserver, and the rest behaves just like runserver except it can be multi-process and multi-threaded.
In the terminal while in a virtual environment, run:
sudo ./manage.py runserver 80
You will be asked for your password and it will run the server on port 80.
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.