Using Django w/ mod_wsgi and Apache - python

I've been trying to work out how I'll go about setting up a Django application on production, when it's ready for deployment. I'm using Django v1.11, and my EC2 is running Ubuntu 14.04. I have been attempting to refer to this guide as reference, however it is not specific to Ubuntu, so I've been experiencing a bit of difficulty in this regard. I've referred to several other resources, but much of what is out there seems to be outdated.
I have a host rule set up on my local machine, pointing www.example.com to my EC2 instance's public IP address.
I have a virtualenv set up which lives in /home/django/example.com/ENV. My Django project lives in /home/django/example.com directly. The project name is mysite, and was generated using django-admin startproject mysite, thus it has the default wsgi.py file inside the /home/django/example.com/mysite directory. The contents of wsgi.py look like:
"""
WSGI config for mysite 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.11/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
application = get_wsgi_application()
I've tried adding VirtualHost rules such as the following:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
WSGIScriptAlias / /home/django/example.com/mysite/wsgi.py
<Directory "/home/django/example.com/mysite">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
Similarly, I've tried adding:
Include /etc/apache2/httpd.conf
to /etc/apache/apache2.conf and chucking the following:
WSGIScriptAlias / /home/django/example.com/mysite/wsgi.py
WSGIPythonHome /home/django/example.com/ENV
WSGIPythonPath /home/django/example.com
<Directory /home/django/example.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
into httpd.conf.
In either case, I've restarted the Apache server directly afterwards.
I'm not getting any further than hitting "500 Internal Server Error" or hitting an "ERR_EMPTY_RESPONSE".
Anyone able to shed some light around a) where I'm going wrong, b) where I can refer to for up-to-date instructions, or c) how I can troubleshoot this?

After a lot of troubleshooting, and consulting with the resource mentioned in Graham's comment, here's what I established was required in my VirtualHost:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Alias /static /home/django/example.com/static
<Directory /home/django/example.com/static>
Require all granted
</Directory>
WSGIDaemonProcess mysite python-path=/home/django/example.com:/home/django/ENV/lib/python3.4/site-packages
WSGIProcessGroup mysite
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /home/django/example.com/mysite/wsgi.py
<Directory /home/django/example.com/mysite>
Require all granted
</Directory>
</VirtualHost>
and here are the contents of wsgi.py that I settled on:
"""
WSGI config for mysite 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.11/howto/deployment/wsgi/
"""
import os, sys
from django.core.wsgi import get_wsgi_application
path = '/home/django/example.com'
if path not in sys.path:
sys.path.append(path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
And it is also worth noting (simply because it wasn't immediately obvious to me) that it is necessary to specify:
STATIC_ROOT = '/home/django/example.com/static'
in settings.py, and to run python manage.py collectstatic to collect static files from all applications into said STATIC_ROOT.
I hope this helps somebody else, in future!

Related

408 Request Timeout Using Apache + mod_wsgi + Python Django in Windows

I have a Django app which is deployed in local network using Apache + mod_wsgi under Windows. When I run python manage.py runserver, everything works fine. But when I start the Apache Service, I cannot access the app. The only response I get from the access.log is the error code 408. Below is my httpd.conf:
LoadFile "c:/users/felix/appdata/local/programs/python/python37/python37.dll"
LoadModule wsgi_module "c:/users/felix/appdata/local/programs/python/python37/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome "c:/users/felix/appdata/local/programs/python/python37"
ServerName localhost
WSGIScriptAlias / "D:/dev/test_server/django/django/wsgi_windows.py"
Listen 8000
<VirtualHost *:8000>
WSGIPassAuthorization On
ErrorLog "logs/django.error.log"
CustomLog "logs/django.access.log" combined
Alias /static "D:/dev/test_server/staticfiles"
<Directory "D:/dev/test_server/staticfiles">
Require all granted
</Directory>
<Directory "D:/dev/test_server/django/django">
<Files wsgi_windows.py>
Require all granted
</Files>
</Directory>
<Directory />
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
And below is the wsgi_windows.py file:
# Activate the virtualenv
activate_this = 'D:/dev/test_server/.venv/Scripts/activate_this.py'
exec(open(activate_this).read(), dict(__file__=activate_this))
import os # noqa
import sys # noqa
import site # noqa
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('D:/dev/test_server/.venv/Lib/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('D:/dev/test_server/django')
sys.path.append('D:/dev/test_server/django/django')
os.environ['DJANGO_SETTINGS_MODULE'] = 'django.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django.settings")
from django.core.wsgi import get_wsgi_application # noqa
application = get_wsgi_application()
I'd appreciate any ideas or hints on the issue.
start apache at command line with 'httpd' and look for error messages. If Apache has a problem at startup there is no message in the log files.
check error.log
You can place 'print('xyz') even in the settings.py and elsewhere and this way by checking error.log see how your app is setup and how far a request is processed. If your app get stuck somewhere like this you find the code where it is stuck
I have the same error after install scipy library and use it in some scrip in django. I found that some libraries like "numpy" and "scipy" only work in the Python main interpreter and you have to force the WSGI to run in the global app group to run it. Adding this line in my conf file work for me.
WSGIApplicationGroup %{GLOBAL}

How to keep python server running

I need a bit of help here. I am new to python and django. For my project i am developing an system which deals with api fetching and insertions. I need this system to be online 24 * 7. so i need to the server to be running all the time.
Using the traditional method of:
python manage.py runserver
runs perfectly.
I even tried :
nohup python manage.py &
but when i access my project with the url. Its showing that it project is not online. How do i fix this?
If you are using Apache then mod_wsgi should be used along with Django for deployment. This is covered here in great detail.
If you want step by step guide then you can have a look at Digital Ocean's Guide.
Your Apache configuration will look like following:
<VirtualHost *:80>
Alias /static /home/user/myproject/static
<Directory /home/user/myproject/static>
Require all granted
</Directory>
<Directory /home/user/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-path=/home/user/myproject python-home=/home/user/myproject/myprojectenv
WSGIProcessGroup myproject
WSGIScriptAlias / /home/user/myproject/myproject/wsgi.py
</VirtualHost>
You will need to create a wsgi.py file which looks like following(the default one):
"""
WSGI config for myproject 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.7/howto/deployment/wsgi/
"""
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Hope this helps.

Django website on Apache with wsgi failing

so i'm about to lunch my first django website , i currently have a server that has been configured to host php websites and i've decided to test a simple empty project to get familiar with the process
so the python version in this server is bit old (2.6) so i couldn't install latest version of django , i installed 1.6 and since it's just a test that's not important (im going to upgrade python version when my website is ready to lunch )
so i've installed django and created a new project called testing in this dire
/home/sdfds34fre/public_html/
which you can see using this domain
http://novadmin20.com
and after reading documentation on django (unfortunately they have removed doc for 1.6 and i had to use 1.9) and wsgi i've updated my httpd.conf like this
<VirtualHost 111.111.111.111:80>
ServerName 111.111.111.111
DocumentRoot /usr/local/apache/htdocs
ServerAdmin somemeail#gmail.com
<IfModule mod_suphp.c>
suPHP_UserGroup nobody nobody
</IfModule>
<Directory /home/sdfds34fre/public_html/testing/testing>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess testing python-path=/home/sdfds34fre/public_html/testing:/usr/lib64/python2.6/site-packages/
WSGIProcessGroup testing
WSGIScriptAlias / /home/sdfds34fre/public_html/testing/testing/wsgi.py
</VirtualHost>
but even after restarting httpd service when i go to
http://novadmin20.com/testing/
all i see is directory list , am i missing something ?
here is my wsgi.py file
"""
WSGI config for testing 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
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testing.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
DocumentRoot directive is the main root of your problem. (ref)
try this config:
<VirtualHost 111.111.111.111*:80>
ServerName novadmin20.com
WSGIDaemonProcess testing python-path=/home/sdfds34fre/public_html/testing:/usr/lib64/python2.6/site-packages/
WSGIScriptAlias / /home/sdfds34fre/public_html/testing/testing/wsgi.py
<Directory /home/sdfds34fre/public_html/testing/testing>
<Files wsgi.py>
Order deny,allow
Require all granted
WSGIProcessGroup testing
</Files>
</Directory>
</VirtualHost>

Running django webapp on apache using mod_wsgi

I am new to working with apache and mod_wsgi. But I do have little experience in django, so from some tutorials I tried to run django app through apache webserver using mod_wsgi.
I created mysite in /var/www/
then in mysite/application I created application.wsgi ...
import os
import sys
sys.path.append('/var/www/mysite/application')
os.environ['PYTHON_EGG_CACHE'] = '/var/www/mysite/.python-egg'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
and in /etc/httpd/sites-available I created file named mysite.conf ...
<VirtualHost *:80>
ServerName mysite.com
ServerAdmin id#somewhere.com
ServerAlias mysite.com
DocumentRoot /var/www/mysite/
<Directory /var/www/mysite>
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
</Directory>
WSGIDaemonProcess mysite processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup mysite
</Virtualhost>
Then I ran a2ensite mysite.conf, didn't showed any error.
Then in /etc/httpd/hosts/ I added one line my-ipddress mysite.com
I gave permission chmod 777 to all the above files and to folder /var/www/mysite. Now when I open mysite.com on browser I see apahce's default page nothing from django.
I am using fedora 21.
You haven't put in anything in that configuration to serve your Django site via WSGI: you're missing the WSGIScriptAlias line (see the documentation:
WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py
Note that you shouldn't really be putting things in /var/www; and also you shouldn't need to create your own WSGI file, Django creates one for you when you create a project.

Setting up django and apache using the tutorial isn't working

I am trying to set up apache with an existing django project using the tutorial in django site here. My os is Ubuntu, and everything is installed (django apache2 libapache2-mod-wsgi)
My conf file
WSGIPythonPath /home/avlahop/development/django/rhombus2/rhombus/
<VirtualHost *:80>
ServerName myrhobmus.com
ServerAlias www.myrhombus.com
WSGIScriptAlias / /home/avlahop/development/django/rhombus2/rhombus/rhombus/wsgi.py
</VirtualHost>
After I created the conf file I ran the a2ensite Ubuntu command for enabling the site.
Putting WSGIPythonPath inside VirtualHost gives me an apache configtest failure
Files inside directive inside directory (as described in the example) gives me the same failure
If I go to www.myrhombus.com I get a Google chrome could not find the specified address message.
What am I doing wrong? Every tutorial on the Internet is using the old file.wsgi while now Django creates this for you but it is a python file with .py extension. How can I serve my django with apache? And If I wanted to go production at my own server where would you put django code and where would you put template files?
EDIT: I am only getting the Index of / page. Is there something I have to do with mysites location in terms of permissions? Could you give me a working example of an django site apache conf file?
I use Django 1.8 and I successfully deployed my project Apache server. I followed basic concept like you and enable Apache this module early.
enable module
You can my project structure this question.Refer this question to understand project structure
--------------this is my virtual host file----------------------------------
WSGIPythonPath /home/umayanga/Desktop/view_site/serialKey_gen_site:/home/umayanga/Desktop/view_site/serialKey_gen_site/myvenv/lib/python3.4/site$
<VirtualHost *:80>
ServerAdmin admin#key.com
ServerName key.com
ServerAlias www.key.com
Alias /templates/ /home/umayanga/Desktop/view_site/serialKey_gen_site/templates/
Alias /static/ /home/umayanga/Desktop/view_site/serialKey_gen_site/static/
<Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/static">
Require all granted
</Directory>
<Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/templates">
Require all granted
</Directory>
WSGIScriptAlias / /home/umayanga/Desktop/view_site/serialKey_gen_site/mysite/wsgi.py
<Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/mysite">
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
-----------------wsgi.py---------------------------------------
"""
WSGI config for mysite 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.8/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
application = get_wsgi_application()
I think this will be help to you.

Categories

Resources