Django project doesn't show up with Apache and mod_wsgi - django

I've installed Apache and mod_wsgi on windows xp service pack 3 and added these line to my httpd.conf :
WSGIScriptAlias / "C:/Documents and Settings/X/My Documents/Downloads/Foo/Foo/wsgi.py"
WSGIPythonPath "C:/Documents and Settings/X/My Documents/Downloads/Foo"
<Directory "C:/Documents and Settings/X/My Documents/Downloads/Foo/Foo">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
but when I open localhost on my firefox, it shows Apache's It Works! message, what should I do to run my project on localhost ?
EDIT :
I checked and recognized that my project's path is not included in PYTHONPATH. Isn't the line WSGIPythonPath ... expected to add the address to PYTHONPATH ?

Alright, so my setup is in linux so this is not tested on windows, but:
I did not see your LoadModule statement
File: httpd.conf
LoadModule wsgi_module modules/mod_wsgi.so
modwsgi wont work without that.
Also: the your grant statement seems a bit suspicious.
In the wsgi configuration guide suggests using a Directory directive for allowing this access to your mod_wsgi application.
<Directory "C:/Documents and Settings/X/My Documents/Downloads/Foo/Foo/">
Order allow,deny
Allow from all
</Directory>
Finally:
Make your life easy down the road.
configure apache in worker mode
configure mod_wsgi in daemon mode.
profit
Might I suggest watching this PyCon talk Making Apache suck less for hosting Python web applications from 'the-man' Graham. I wish I knew all of that stuff years ago.
Note: To figure out if you have apache in mpm worker mode.
httpd.exe -V
look for the "Server MPM" value of worker.

Django runs on port 8000 so you'll want to do two things. First, you need to run the server by entering into your console python manage.py runserver. Second, you need to direct your browser to localhost:8000.
As an aside, you don't need Apache to run a simple, local development environment. Django has its own server built in that you can leverage.

Related

Server isnt recognizing python virtualenv anymore

I have a deployed Django app on a Centos 7 server. I ran into problems when git pulling from my repository. It doesn't recognize python-home at the location where my virtualenv is anymore.
When i print(sys.prefix), it shows '/usr', but when i activate my virualenv (source /path/to/virtualenv/bin/activate) it shows the correct path that is assigned to python-home in my httpd .conf file
.conf file
Alias /static /var/www/ek/static
<Directory /var/www/ek/static>
Require all granted
</Directory>
<Directory /var/www/ek/new>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIApplicationGroup %{GLOBAL}
WSGIDaemonProcess ek python-path=/var/www/ek python-home= /var/www/kpi_env
WSGIProcessGroup ek
WSGIScriptAlias / /var/www/ek/new/wsgi.py
I'm not exactly sure what the problem is, when im not in my virtualenv, should the prefix be the path to the virtualenv? Should print(sys.prefix) only show var/www/kpi_env when im in the virtualenv?
Whatever it is, it doesn't recognize python-home=/var/www/kpi_env as anything anymore.
When i change it to python-home = /usr it works but the application is giving me errors that i've never run into before on my server or on my workstation. This error being Permission denied is Server running on host 12.0.0.1 and accepting tcp con on port 5432 This is referring to postgresql, and i'm sure that it is running and accepting tcp connection on port 5432. And that my settings.py reflect the right configurations. I havent changed any configurations with changes with git pull and havent touched configs for postgresql at all, so i can only think that its because of the python library it is using, because thats the only thing that has changed.
How do i make my httpd server recognize python-home= /var/www/kpi_envagain ?
This doesn't exactly answer what i asked but this was the workaround i did:
I ran this command to allow my httpd server to accept the connection to my postgresql database,
setsebool -P httpd_can_network_connect_db 1
-P will do this permanently and 1 will set it to on.
And in this, i used python-home = /usr since i couldnt get my virtualenv to work anymore

How to configure Apache to work with Django

I've followed the instructions on Django website for configuring Apache with my Django app on a CentOS 7 server. This included building mod_wsgi from sources to work with the installed python3.4.
Apache restarts without errors but when I hit my app with the URL
http://example.com/myapp/
I get a 503 error like:
Service Temporarily Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
Apache/2.2.15 (CentOS) Server at example.com Port 80
I'm not sure how I troubleshoot what's wrong here. Can anyone help?
Details of the config:
My django app lives at /mnt/net/django/myapp
I've added the file wsgi.conf to my apache conf.d directory and it looks like this:
#LoadModule wsgi_module modules/mod_wsgi.so
# use python34 pip installes mod_wsgi
LoadModule wsgi_module "/usr/lib64/python3.4/site-packages/mod_wsgi/server/mod_wsgi-py34.cpython-34m.so"
#WSGIPythonHome "/usr"
Alias /robots.txt /mnt/net/django/myapp/static/robots.txt
Alias /favicon.ico /mnt/net/django/myapp/static/favicon.ico
Alias /media /mnt/net/django/myapp/media/
Alias /static/ /mnt/net/django/myapp/static/
<Directory /mnt/net/django/myapp/static>
Order deny,allow
Allow from all
</Directory>
<Directory /mnt/net/django/myapp/media>
Order deny,allow
Allow from all
</Directory>
# Allows URLs like example.com/myapp to forward to django
WSGIScriptAlias /myapp /mnt/net/django/myapp/myappsite/wsgi.py process-group=example.com
# Use the virtual env for the myapp site
#WSGIPythonHome /mnt/net/django/myapp/env-myapp-py3-4
# Need to use WSGIDaemon
WSGIDaemonProcess example.com python-home=/mnt/net/django/myapp/env-myapp-py3-4 python-path=/mnt/net/django/myapp
#WSGIPythonPath /mnt/net/django/myapp
<Directory /mnt/net/django/myapp/myappsite>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
How to use Django with Apache and mod_wsgi -- follow this: I have done it myself many times, it is very straight forward.
Tip: Create a document and record everything you do whilst setting this up, this way if it doesn't work then you can retrace your steps, but if it does work... Great, you have your very own guide to setting up an Apache server for Django.
Solved my problem (mostly)
The problem is that mod_wsgi with a daemon process tries to write a socket file into the apache logs directory and permissions are denied.
Solution is to tell apache another place to write the socket like this:
WSGISocketPrefix /var/run/wsgi

Django (1.6) + Apache2

First time I setup Django and Apache, and I'm having a tough time. It's seems easy by following tutorials, but it doesn't seem to work.
Basically, the steps I followed (on Debian):
Install Django (and some libraries)
Install Apache2
Install mod_wsgi
Then I've put my Django app in /root/, so the path is something like :
root/
.Projet/
.myprojet/
.site/
. .#here the models.py, views.py, etc of my site
.myprojet/
. .#here the settings.py, wsgi.py
. .static/
. .#static files in folders
.templates
. .#my templates
.database.sql
.manage.py
Then in /etc/apache2 I created the file httpd.conf, with this inside :
WSGIScriptAlias / /root/Projet/myprojet/myprojet/wsgi.py
WSGIPythonPath /root/Projet/myprojet
<Directory /root/Projet/myprojet/myprojet>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
I do a2enmod wsgi (it tells me "Module wsgi already enabled").
Then service apache2 restart (successful, but tells me I have no VirtualHosts).
When I go to my website (using the IP adress), just the Apache "It works" display.
Thank you for your help ^^
You shouldn't put your Apache configuration file in /etc/apache2/. The fact that you mention a2enmod implies that you are on a Debian-based distro like Ubuntu. For those systems, you need to give that file a name related to the app - call it django if you like - and put it in /etc/apache2/sites-available. Then you can run a2ensite django to link it to sites-enabled, restart Apache, and it should work.

Configuring Apache 2 with mod_wsgi for Django - no httpd.conf file

I am using Apache 2 on raspbian os. I read somewhere that in the newest versions of Apache there is no httpd.conf file, is this true? [Edit] (This is true)
My apache server is running (I can see the default web page), and mod_wsgi is successfully installed. So I began the instructions here for using django 1.5 with apache. However it tells me I need to add the following lines to the httpd.conf file:
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
However, there is no httpd.conf file present in my version of apache. I have checked /etc/httpd/confand /etc/apache2 for the http.conf file. /etc/apache2 is where apache is though.
In short, where should I add the required settings so that apache recognizes mod_wsgi.
Possibly, many distros use
/etc/apache2/apache2.conf
Instead. However you shouldn't really edit that and it affects apache2 globally, i.e. all you sites (virtual-hosts). If you have only the one site better edit the contents of:
/etc/apache2/sites-available/default
The Ubuntu Apache2 configuration documentation is a good getting started guide: https://help.ubuntu.com/14.04/serverguide/httpd.html#http-configuration
Here is tutorial configuring Apache2 with mod_wsgi for Django on Ubuntu 11.04 wihtout httpd.conf: http://blog.madspace.me/configure-python-django-with-apache/, and one on 12.04: http://www.lennu.net/2012/05/14/django-deployement-installation-to-ubuntu-12-dot-04-server/

I need help on configuring mod_wsgi and Django

Apache & mod_wsgi are configured correctly (I've created a hello
world .html apache file and a hello world mod_wsgi application with
no problems). I now need my Django app to work with my django.wsgi
file. What makes me think that it's not recognizing my wsgi file is that I
went into my django.wsgi file I created and completely deleted all of
the code in the file and restarted Apache and it still gives me the
same page (a listing of the files from Django app, not my actual
Django application. Configuring Apache and mod_wsgi went really well
but I'm at a loss of how to fix this. Here are some details:
Here is my current django.wsgi file:
import os
import sys
sys.path.append('/srv/www/duckling.org/store/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'store.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I've tried a few different versions of the django.wsgi file
(including a version like the one over at http://www.djangoproject.com/).
This version of my wsgi is from here:
http://library.linode.com/frameworks/django-apache-mod-wsgi/ubuntu-10...
Also, here is my vhost apache configuration file below. I think these
are the main files that are suppose to do the job for me. Let me know if
you see any errors in what I'm doing and what else I might do to fix
this. The django app runs fine on the django's built-in development
server so I'm thinking it might have something with my paths.
No errors in my apache error.log file as well. It's acting as there's
no problem at all, which is not the case...the project isn't loading,
like I said just a listing of my files and directories of my Django
project. Here is my apache config file:
<VirtualHost 1.2.3.4:80>
ServerAdmin hi#duckling.org
ServerName duckling.org
ServerAlias www.duckling.org
DocumentRoot /srv/www/duckling.org/store/
<Directory /srv/www/duckling.org/store/>
Order Allow,Deny
Allow from all
</Directory>
Alias /static/ /srv/www/duckling.org/store/static/
<Directory /srv/www/duckling.org/store/static>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias store/ /srv/www/duckling.org/store/wsgi-scripts/django.wsgi
<Directory /srv/www/wsgi-scripts>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
And here are versions of the stack that I'm using, I saw over at the
mod_wsgi site that you all would like the versions of what I'm using
on the server:
Apache/2.2.14 (Ubuntu) PHP/5.3.2-1ubuntu4.5 with Suhosin-Patch
mod_python/3.3.1 Python/2.6.5 mod_wsgi/2.8
thanks,
j.
For a start, you should definitely not keep your Django files under your DocumentRoot. There's no need for them to be there, and it's a potential security risk - as you've seen, your current misconfiguration allows Apache to serve up your files directly: an attacker could guess that and download your settings.py, complete with your database password.
So, get rid of that DocumentRoot directive completely, as well as the first Directory section which allows direct access to /srv/www/duckling.org/store/. (You probably don't need the one serving up /srv/www/wsgi-scripts either.) That should make things a bit better.
By the way, this configuration will serve your website under duckling.org/store - is that what you want? If you want it under the root, you should just use:
WSGIScriptAlias / /srv/www/duckling.org/store/wsgi-scripts/django.wsgi