i'm trying to setup mod_wsgi to serve my django media files (i want to use this also in a developement env)
I followed this guide to correctly setup mod_wsgi.
This is my wsgi file ("django.wsgi")
import os, sys
path = '/home/smau/Workspace/Maynard/tothego_frontend/'
if path not in sys.path:
sys.path.append(path)
#Calculate the path based on the location of the WSGI script.
apache_configuration= os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)
workspace = os.path.dirname(project)
sys.path.append(workspace)
os.environ['DJANGO_SETTINGS_MODULE'] = 'tothego_frontend.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
This is my conf file ("django.conf")
Alias /site_media/ "/home/smau/Workspace/Maynard/tothego_frontend/site_media/"
<Directory "/home/smau/Workspace/Maynard/tothego_frontend/site_media">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>
WSGIScriptAlias / "/home/smau/Workspace/Maynard/tothego_frontend/srv/mod_wsgi/django.wsgi"
<Directory "/home/smau/Workspace/Maynard/tothego_frontend/srv/mod_wsgi">
Allow from all
</Directory>
This is my "httpd.conf"
Include /home/smau/Workspace/Maynard/tothego_frontend/srv/mod_wsgi/django.wsgi
Everything seems to be like the guide, however, when i try to start/restart apache i get this error
root#archimedes:/etc/apache2# /etc/init.d/apache2 restart
Syntax error on line 1 of /home/smau/Workspace/Maynard/tothego_frontend/srv/mod_wsgi/django.wsgi:
Invalid command 'import', perhaps misspelled or defined by a module not included in the server configuration
Action 'configtest' failed.
The Apache error log may have more information.
...fail!
This is /var/log/apache2.log
[Thu Jul 14 11:39:31 2011] [notice] Apache/2.2.17 (Ubuntu) PHP/5.3.5-1ubuntu7.2
with Suhosin-Patch mod_wsgi/3.3 Python/2.7.1+ configured -- resuming normal operations
[Thu Jul 14 11:44:28 2011] [notice] caught SIGTERM, shutting down
PHP Warning: PHP Startup: Unable to load dynamic library
'/usr/lib/php5/20090626/gd.so'- /usr/lib/php5/20090626/gd.so: cannot open shared object file:
No such file or directory in Unknown on line 0
The log doesn't seem (to me...) anyhow related to my problem. Why do i keep getting the "import" error? Did i give you enought information or do you need something else? I guess my pythonpath is correct:
You're supposed to include the configuration file (django.conf), not the WSGI script (django.wsgi).
Related
For my django application (called 'iwidget') I have created (and installed using a2ensite) this virtual host configuration file (001-default)
WSGIPythonHome /home/user/.virtualenvs/iwidget
<VirtualHost *:80>
ServerAdmin webmaster#localhost
WSGIDaemonProcess iwidget processes=4 threads=10 display-name=%{GROUP} python-path=/home/user/.virtualenvs/iwidget/lib/python2.7
WSGIProcessGroup iwidget
WSGIScriptAlias /iwidget /home/user/iwidget/iwidget/wsgi.py
Alias /iwidget/static/ /home/user/iwidget/iwidget/staticfiles/
<VirtualHost>
And this is my wsgi.py (in /home/user/iwidget/iwidget/ directory.)
import os, sys, site
site.addsitedir('/home/user/.virtualenvs/iwidget/lib/python2.7/site-packages')
activate_this = os.path.expanduser("/home/user/.virtualenvs/iwidget/bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))
sys.path.append('/home/user/iwidget')
sys.path.append('/home/user/iwidget/iwidget')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "iwidget.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
However, I am getting this error
[warn] mod_wsgi: Compiled for Python/2.7.2+.
[warn] mod_wsgi: Runtime using Python/2.7.3.
[notice] Apache/2.2.22 (Debian) mod_wsgi/3.3 Python/2.7.3 configured -- resuming normal operations
[error] File does not exist: /var/www/iwidget
/var/www/iwidget? Why is Apache looking in this directory? What am I doing wrong?
Note. There is already a default vhost configuration pointing which is working fine. This is my second (that is why I called it 001-default).
You have two configuration files, but no way of distinguishing between them. Apache will simply match everything against the original one, since it's first alphabetically, and never get to the new one - hence why it is looking in /var/www, since that's evidently the DocumentRoot set in 001-default.
Do you actually need this to be in a separate file? There's no reason you couldn't add this configuration inside 001-default. Otherwise, you will need to have them on separate ports, or use a ServerName directive to distinguish them.
I am developing a Flask application on CentOS 6.6 on Apache and Mysql. It is modified from The Flask Megatutorial. I am able to create normally the database, however when I try to access it from my browser I get 500 internal server error and this in the error_log file:
content type: text/html
<h1>Hello world!</h1>
[Sun May 03 18:39:53 2015] [error] [client my.ip.add.res] (104)Connection reset by peer: mod_fcgid: error reading data from FastCGI server
[Sun May 03 18:39:53 2015] [error] [client my.ip.add.res] Premature end of script headers: runp-mysql.fcgi
This is after I've edited down the runp-mysql.fcgi file to this:
#!flask/bin/python
#encoding=UTF-8
#import os
print "content type: text/html\n\n"
print ""
print "<h1>Hello world!</h1>"
Running this from the command line completes correctly.
My httpd.conf file ends with this:
FcgidIPCDir /tmp
AddHandler fcgid-script .fcgi
<VirtualHost *:80>
DocumentRoot /home/apps/my_app/app/static
Alias /static /home/apps/my_app/app/static
ScriptAlias / /home/apps/my_app/runp-mysql.fcgi/
</VirtualHost>
I was doing the same and a major issue was the permissions on the file. make sure it is executable and not read only.
Also I do not think that the runp-mysql.fcgi is where you want to do your printing. It should be in the views file. This is where you should have the connection to the your mysql database then start the wscgi server.
This is a very loose answer based on my actual problem and I am only uploading it in case someone else follows the same tutorial and also has a problem with deployment.
The gist of it is that under the python version I used (2.7) for deployment the flipflop module did not work and I had to use flup instead. For anyone getting the same error - try it, it might work.
I've been doing Flask microblog tutorial by Miguel Grinberg and have got stuck on trying to deploy to my linux VPS.(http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvii-deployment-on-linux-even-on-the-raspberry-pi)
I'm getting a 500 internal server error produced by apache, not flask, and I can't figure it out. It works when running it using the interpreter, but I can't launch it with apache. I've read through so many google searches and SO questions and I'm lost. I'm relatively new to linux/python/flask but I'm willing to learn if someone can point me in the right direction.
Setup:
I'm running a fresh CentOS 6.6 install with Python 2.6.6 and Apache 2.2.15. I'm running it off sqlite. I'm using these flask modules if you're interested: http://pastebin.com/bPnH83bs
Basic App Structure: (left out things for brevity)
Located in: /home/apps/portal
I have the whole directory chown'd to: chown -R apps:apache
User 'apps' is a member of the apache group
flask\ (virtualenv)
app\
static\
templates\
__init__.py
models.py
views.py
forms.py
decorators.py
db_repository\
search.db\
tmp\
app.db
config.py
run.py
runp.py
runp-sqlite.fcgi
Apache conf file setup:
FcgidIPCDir /tmp
AddHandler fcgid-script .fcgi
<VirtualHost *:80>
DocumentRoot /home/apps/portal/app/static
Alias /static /home/apps/portal/app/static
ScriptAlias / /home/apps/portal/runp-sqlite.fcgi/
ErrorLog /var/log/httpd/error_log
CustomLog /var/log/httpd/access_log combined
</VirtualHost>
runp-sqlite.fcgi Contents:
#!flask/bin/python
from flipflop import WSGIServer
from app import app
if __name__ == '__main__':
WSGIServer(app).run()
Error from apache logs when trying to access page and getting 500 error:
[Mon Dec 15 22:21:44 2014] [warn] [client *.*.*.*] mod_fcgid: read data timeout in 40 seconds
[Mon Dec 15 22:21:44 2014] [error] [client *.*.*.*] Premature end of script headers: runp-sqlite.fcgi
Error when I run "runp-sqlite.fcgi" from the console:
[root#**** portal]# sudo -u apache ./runp-sqlite.fcgi
Traceback (most recent call last):
File "./runp-sqlite.fcgi", line 6, in <module>
WSGIServer(app).run()
File "/home/apps/portal/flask/lib/python2.6/site-packages/flipflop.py", line 938, in run
sock.getpeername()
socket.error: [Errno 88] Socket operation on non-socket
Things I've checked:
I've disabled SELinux as it was causing a different problem, didn't fix this issue.
Checked that folders were chown'd to the correct user/group.
Checked that '/etc/httpd/conf/httpd.conf' and '/etc/sysconfig/httpd' PIDFILE locations are correct
Checked that iptables is accepting traffic to ports 80 and 5000(for testing). If I remove the apache configuration I've added I am successfully serving from /var/www/html
Lots and lots of googling and playing around
Sorry for the wall of text, I just don't know what you'll need to see. If anyone can help with this I'll be really greatful. If it's something dumb, I apologise. :)
Update 1:
Changed runp-sqlite.fcgi to call virtualenv:
#!flask/bin/python
activate_this = '/home/apps/portal/flask/bin/activate_this.py')
execfile(activate_this, dict(__file__=activate_this))
from flipflop import WSGIServer
from app import app
if __name__ == '__main__':
WSGIServer(app).run()
Now apache errors_log has a new error message:
[Fri Dec 19 13:43:03 2014] [notice] Apache/2.2.15 (Unix) DAV/2 mod_fcgid/2.3.9 PHP/5.3.3 mod_wsgi/3.2 Python/2.6.6 configured -- resuming normal operations
[Fri Dec 19 13:43:05 2014] [warn] [client 110.143.38.80] (104)Connection reset by peer: mod_fcgid: error reading data from FastCGI server
[Fri Dec 19 13:43:05 2014] [error] [client 110.143.38.80] Premature end of script headers: runp-sqlite.fcgi
Do you have Flask installed in a virtuelenv? If so, the problem may be that your WSGI file isn't activating it. This causes an ImportError or something similar when Apache tries to serve the site, which results in a not-very-helpful 500 Error.
The fix is to activate the virtualenv in your WSGI file before importing the app like this:
#!/bin/python
VENV_DIR = 'your_app/your_venv'
activate_this = os.path.join(VENV_DIR, 'bin', 'activate_this.py')
execfile(activate_this, dict(__file__=activate_this))
from flipflop import WSGIServer
from app import app
if __name__ == '__main__':
WSGIServer(app).run()
See also this previous SO question: Running Python from a virtualenv with Apache/mod_wsgi, on Windows
I have been trying to deploy a Django site using mod_wsgi on a CentOS server recently, but so far when I try to access the django site through my laptop, the web page has only been displaying error: 403 Forbidden You don't have permission to access / on this server.
In addition to reading all the obvious documentation, I have looked at these previous questions:
Django + mod_wsgi + Apache = 403 Forbidden
Error message “Forbidden You don't have permission to access / on this server”
Forbidden You don't have permission to access / on this server
Apache mod_wsgi error: Forbidden You don't have permission to access / on this server
Django on apache wtih mod_wsgi (Linux) - 403 Forbidden
Environment:
Centos 6.5
Python 2.6
Django 1.6
I am running the following version of apache:
# apachectl -V
Server version: Apache/2.2.15 (Unix)
Server built: Apr 3 2014 23:56:16
Server's Module Magic Number: 20051115:25
Server loaded: APR 1.3.9, APR-Util 1.3.9
Compiled using: APR 1.3.9, APR-Util 1.3.9
Architecture: 64-bit
Server MPM: Prefork
threaded: no
forked: yes (variable process count)
I installed mod_wsgi using Yum and have confirmed it is installed on the server:
# httpd -M | grep wsgi
wsgi_module (shared)
Syntax OK
My httpd.conf wsgi config snippet is as follows:
#
# Add WSGI configuration
#
WSGIScriptAlias / /usr/local/django/basic/basic/apache/wsgi.py
WSGIPythonPath /usr/local/django/basic/
WSGIDaemonProcess ###.###.###.###
WSGIProcessGroup ###.###.###.###
<Directory /usr/local/django/basic/basic/apache>
<Files wsgi.py>
Options FollowSymLinks
Order deny,allow
Allow from all
</Files>
</Directory>
Finally my wsgi.py script is:
"""
WSGI config for basic 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
path = "/usr/local/django/basic/basic/apache"
if path not in sys.path:
sys.path.append(path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "basic.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Output from error log:
[Fri Oct 24 14:10:43 2014] [error] [client (redacted)] Symbolic link not allowed or link target not accessible: /usr/local/django/basic
[Fri Oct 24 14:11:25 2014] [error] [client (redacted)] Symbolic link not allowed or link target not accessible: /usr/local/django/basic
[Fri Oct 24 14:14:02 2014] [error] [client (redacted)] Symbolic link not allowed or link target not accessible: /usr/local/django/basic
Notes:
The django project is in my user's home directory but has a symbolic link in `/usr/local/django/ pointing to it
In the past when I have worked on projects Error 403 usually meant that the permissions on a file were wrong, but I had check that and the files should all allow the apache user to access them
My web server works fine when I comment out the wsgi related lines of the Apache config.
Sorry, that I posted this a bit late. This was the final fix to my apache config that ultimately worked.
WSGIScriptAlias /basic /var/www/django/basic/basic/wsgi.py
WSGIPythonPath /var/www/django/basic/
<Directory /var/www/django/basic/basic>
Options FollowSymLinks
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>
Alias /static /var/www/django/basic/basic/static
And this is the final version of my wsgi.py file in python. The key line of code here was the PYTHON_EGG_CACHE. That variable was by default set to a directory that did not exist. I set it to /tmp/.python-eggs Make sure that .python-eggs has correct permissions for the apache user to read/write to it wherever you may place this file.
"""
WSGI config for basic 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
path = "/usr/local/django/basic/basic/apache"
if path not in sys.path:
sys.path.append(path)
os.environ['PYTHON_EGG_CACHE'] = '/tmp/.python-eggs'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "basic.settings")
#print os.getenv("DJANGO_SETTINGS_MODULE")
#print os.getenv("PYTHON_EGG_CACHE")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Side note:
A friendly reminder to make sure that every file in your django application is readable, (and writeable if needed) by the apache user. Git once overwrote a file to an old permission I had set up once and it took me a little time to figure out the permissions had changed without realizing it.
May be you forget set DocumentRoot. You should make the DocumentRoot can be read and wrote by apache users.
just do like this:
sudo chown -R www_default:www_default /path/to/you/DocumentRoot
and you can also do like this :
sudo chmod -R 755 /path/to/your/DocumentRoot
I'm working with my hosting provider to get a Django application up and running, but neither of us are very experienced and we've basically hit a complete dead end.
I don't have direct access to the conf file but here's how its contents have been described to me:
<IfModule mod_wsgi.c>
WSGIScriptAlias /fredapp/ /home/fred/public_html/cgi-bin/fredapp/apache/django.wsgi
WSGIDaemonProcess fred threads=15 display-name=%{GROUP} python-path=/home/fred/public_html/cgi-bin/fredapp/apache/
WSGIProcessGroup fred
WSGIApplicationGroup %{GLOBAL}
</IfModule>
Alias /robots.txt /home/fred/public_html/fred-site/robots.txt
Alias /favicon.ico /home/fred/public_html/fred-site/favicon.ico
Alias /settings/media/ /home/fred/public_html/fred-site/media/
My "django.wsgi" script is nothing fancy:
import os, sys
sys.path.append('/home/fred/public_html/cgi-bin/')
sys.path.append('/home/fred/public_html/cgi-bin/fredapp/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'fredapp.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
So my understanding is that all this means that if a request comes in for domain.com/fredapp/ that it should be turned over to the application via django.wsgi. However, the only response I get is:
[Fri Jan 22 18:46:08 2010] [error] [client xx.xxx.xx.xx] File does not exist: /home/fred/public_html/domain.com/500.shtml
[Fri Jan 22 18:46:08 2010] [error] [client xx.xxx.xx.xx] mod_wsgi (pid=26760): Exception occurred processing WSGI script '/home/fred/public_html/cgi-bin/fredapp/apache/django.wsgi'.
[Fri Jan 22 18:46:03 2010] [error] [client xx.xxx.xx.xx] File does not exist: /home/fred/public_html/domain.com/404.shtml
[Fri Jan 22 18:46:03 2010] [error] [client xx.xxx.xx.xx] File does not exist: /home/fred/public_html/domain
This is running under Apache on Linux. I have tried running each line of the .wsgi script in the Python interpreter on the server, and none of them return any errors. I also tried the sys.stdout = sys.stderr trick and got no further output than what is above. The File does not exist errors have to do with the rest of the site's set-up and occur on any request. I haven't finished setting all that up properly (error pages and index pages and so on) because I'm just trying to get the app itself to run.
I've gotten this app up and running under Apache on my own machine, though NOT in Daemon mode, but it's my first Django app, and I don't think my hosting provider has ever configured one before, so we're flying a little blind. If anyone has any suggestions, I'd be very grateful. Thank you!
If the quoted configuration about is what you are using, the error is rather obvious actually. You have:
WSGIDaemonProcess fred threads=15 display-name=%{GROUP} python-path=/home/fred/public_html/cgi-bin/fredapp/apache/
WSGIProcessGroup scratchf
It should be:
WSGIDaemonProcess fred threads=15 display-name=%{GROUP} python-path=/home/fred/public_html/cgi-bin/fredapp/apache/
WSGIProcessGroup fred
That is, the name of the process group must match.
You should though have seen an error message:
No WSGI daemon process called 'scratchf' has been configured
This would likely be before the logged error:
Exception occurred processing WSGI script
This is why it is important that you supply all the error log messages and don't assume that they aren't relevant.
Alternatively, you have quoted configuration different to what you are using or not all of the configuration.
UPDATE 1
It looks like you may have ErrorDocument directive enabled in Apache to redirect errors to a specific URL. Because however you have mounted Django at root of web server and not excluded those error URLs from being passed through to Django, then when an error is generated Django gets the redirect for the error document but it cannot resolve the URL and subsequently generates a 404. Because Apache saw a 404 for error page redirect, it then returns a 500 default error page. The end result is that true original error and any information is lost.
Thus, go into Apache configuration and comment out the ErrorDocument directives.
UPDATE 2
Change configuration to:
WSGIScriptAlias /fredapp /home/fred/public_html/cgi-bin/fredapp/apache/django.wsgi
You should not have trailing slash on the second value on line. Missed that you were actually trying to mount at sub URL and not at root of web server.
Is it possible that your starting directory is not the one project is in?
Today I was also setting up Apache+mod_wsgi+Django app and after adding to django.wsgi:
os.chdir('/home/user/my_django_project')
everything started to work like a charm.
We had the same error when the user running Apache had not the rights to read the files.