1

I have installed apache2, mod_wsgi on my Debian machine and added this on my apache2.conf file:

WSGIScriptAlias /home/zurelsoft/Documents/workspace/genalytics/genalytics/wsgi.py
WSGIPythonPath /home/zurelsoft/Documents/workspace/genalytics

<Directory /home/zurelsoft/Documents/workspace/genalytics/genalytics>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

My project name is genalytics. I am using Django 1.5. There's already wsgi.py available. What should I do run the django with mod_wsgi and where should I give the path of my static files. Thanks

Edit

I have this on my apache.conf file:

Listen 8000

Alias /static/ /home/zurelsoft/Documents/workspace/genalytics/fileupload/static

<Directory /home/zurelsoft/Documents/workspace/genalytics/fileupload/static>
Order deny,allow
Allow from all
</Directory>


WSGIScriptAlias / /home/zurelsoft/Documents/workspace/genalytics/fileupload/static


<Directory /home/zurelsoft/Documents/workspace/genalytics/fileupload/static>
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>

But when I run try to start apache I get this error:

(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80 no listening sockets available, shutting down Unable to open logs Action 'start' failed.

pynovice
  • 7,424
  • 25
  • 69
  • 109

3 Answers3

1

Presuming that you have set up everything correctly, you don't have much left to do.

In your application root, create a file named django.wsgi and write the following code.

import os
import sys

sys.path.append('/path/to/your/app')

os.environ['PYTHON_EGG_CACHE'] = '/path/to/your/app/.python-egg'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Now, add a virtual host in your apache configuration for serving static and other files and add the following lines:

   WSGIScriptAlias / /path/to/your/app/django.wsgi

   <Directory /path/to/your/app>
      Order allow,deny
      Allow from all
   </Directory>

   Alias /robots.txt /path/to/your/app/robots.txt
   Alias /favicon.ico /path/to/your/app/favicon.ico
   Alias /images /path/to/your/app/images
   Alias /static /path/to/your/app/static

   ErrorLog /path/to/your/app/logs/error.log
   CustomLog /path/to/your/app/access.log combined

Remember to restart apache. You can check this and this links for complete information. Also, if you need to know how to add virtual host, check this out.

Hope that helps.

QuestionEverything
  • 4,809
  • 7
  • 42
  • 61
  • I already have a file called wsgi.py provided by Django. Do I need to create another file called django.wsgi – pynovice Jun 12 '13 at 08:43
  • I get this error: [....] Starting web server: apache2(98)Address already in use: make_sock: could not bind to address [::]:80 (98)Address already in use: make_sock: could not bind to address 0.0.0.0:80 – pynovice Jun 12 '13 at 09:26
  • In the first case, i'm not sure, try both. 2nd case, some app has already taken that port 80. Change that number in your VirtualHost declaration. – QuestionEverything Jun 12 '13 at 09:53
  • Did that, didn't work. I have specified line listen 8000 but doesn't work. – pynovice Jun 12 '13 at 09:57
  • this might help http://stackoverflow.com/questions/1553165/multiple-django-sites-with-apache-mod-wsgi?rq=1 – QuestionEverything Jun 12 '13 at 10:06
0

There are a number of howtos on the web, most of which work with current Django versions, but I was unhappy with their lack of conformance with Django's current docs and found the easiest path to follow these instructions:

https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/modwsgi/

There it says: 'As of Django version 1.4, startproject will have created wsgi.py for you' - which looks like this:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

So now it's wsgi.py that connects to mod_wsgi, which you installed with aptitude, and django.wsgi is deprecated.

Now we want to honor the debian method of configuring apache sites, so instead of putting the following code into httpd.conf, as django-docs propose, we create a dj-myapp file in /etc/apache2/sites-available, activate it with a2ensite dj-myapp and disable default with a2dissite default. The WSGI-directives are written before the virtualhost section:

WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com
<VirtualHost *:80>
    <Directory /path/to/mysite.com/mysite>
      <Files wsgi.py>
            Order deny,allow
            Allow from all
      </Files>
    </Directory>
</VirtualHost>

This is for apache 2.2x, for 2.4+ use Require all granted instead of Allow from all.

Finally configure the static file serving, as described in the django docs. The directives are also placed in dj-myapp. For the admin static files this line worked for me:

Alias /static/admin /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin
Ed Doerr
  • 61
  • 5
  • there is no mention of the website URL here, how does apache knows what website it is? – David 天宇 Wong Feb 18 '14 at 08:45
  • If you want apache to serve multiple sites on separate domains, you just add these kind of directives to the (or each) dj-myapp in the Virtualhost section: ' ServerName www.supersite.de ServerAlias supersite.de *.supersite.de DocumentRoot /var/www/super/' There is nothing specific with django, wsgi or python here. – Ed Doerr Feb 22 '14 at 18:45
  • - - - - - - - - - ( its stupid that I can not enter line breaks in comments :( – Ed Doerr Feb 22 '14 at 18:53
  • (it's also stupid of stackoverflow that I can edit comments only for 5 minutes :( – Ed Doerr Feb 22 '14 at 19:00
0

create file called app.conf in /etc/apache2/sites-available.The app.conf:

WSGIPassAuthorization On
WSGIPythonPath /home/brms/manage/web/brms
WSGIDaemonProcess pyramid user=brms group=brms threads=4 \
   python-path=/usr/local/lib/python3.4/dist-packages/
<VirtualHost *:80>
    <Directory /home/brms/manage/>
        <Files wsgi.py>
                WSGIProcessGroup pyramid
                Require all granted
        </Files>
    </Directory>
    Alias /meetingApp /var/www/meetingApp
</VirtualHost>
WSGIScriptAlias / /home/brms/manage/wsgi.py

Enable this site:sudo a2ensite app.conf Restart Apache: sudo service apache2 restart

xiaoyy
  • 11
  • 3