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.
Related
I am no expert on Django. I am able to run the app on my local machine well. I want to deploy this app on our internal server.
Versions I am using: Python: 2.7, Django 1.11, Apache: 2.4
Ports Open: 9991
When I access the port from any machine, it gives me no error. For e.g. xxx.xx.xxx.xxx:9991 shows Apache page.
But when I add the URL to the app
xxx.xx.xxx.xxx:9991/WebApp1Url
it gives error.
Below is my configuration of httpd.conf
Listen 9991
<VirtualHost *:9991>
Alias /static "C:/xxx/DjangoSite/WebApps/WebApp1/static"
<Directory "C:/xxx/DjangoSite/WebApps/WebApp1/static">
Require all granted
</Directory>
<Directory "C:/xxx/DjangoSite/WebApps/WebApps">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
Below is my wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "WebApps.settings")
application = get_wsgi_application()
from WebApp1.wsgi import WebApp1
application = WebApp1(application)
settings.py contains:
WSGI_APPLICATION = 'WebApps.wsgi.application'
I can assure that the paths to static and wsgi.py are correct. I can't figure out what is not right. Can someone please provide pointers as to which step is missing? Any help is appreciated. Pardon any typos above. Thank you in advance.
The first stop for any problems, new user or experienced, should be Django's very detailed, extensive, up-to-date, versioned documentation. There's even a section for deploying a Django project with Apache HTTPD.
Some stuff quoted below, but broadly:
Install Apache HTTPD with mod_wsgi (installing the module on Windows is probably the hard part, the mod_wsgi docs link to this guide)
Add WSGI directives to a conf file
The official mod_wsgi documentation is your source for all the details about how to use mod_wsgi. You’ll probably want to start with the installation and configuration documentation.
Basic configuration
Once you’ve got mod_wsgi installed and activated, edit your Apache server’s httpd.conf file and add the following. If you are using a version of Apache older than 2.4, replace Require all granted with Allow from all and also add the line Order deny,allow above it.
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonHome /path/to/venv
WSGIPythonPath /path/to/mysite.com
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
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!
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>
I have a micro instance on EC2 with Django installed there. I've also installed mod-WSGI, PostgreSQL, etc following several tutorials.
Finally, I pulled my project from bitbucket and started Apache on my EC2. Unfortunately, the only thing I have is the default Apache page and I've already spent a day and night reading and trying to figure out what am I doing wrong.
my_project is in /home/ubuntu dir. In it's folder I have wsgi.py file:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
This is my .conf file stored in /etc/apache2/httpd.conf:
Listen 80
NameVirtualHost *:80
WSGIPythonPath /home/ubuntu/my_project/
<VirtualHost *:80>
DocumentRoot /home/ubuntu/my_project
ServerName www.ec2-54-***-104-**.us-west-2.compute.amazonaws.com
ServerAlias ec2-54-***-104-**.us-west-2.compute.amazonaws.com
ErrorLog /home/ubuntu/my_project/apache/error_log
CustomLog /home/ubuntu/my_project/access_log_common
WSGIScriptAlias / /home/ubuntu/my_project/wsgi.py
Alias /static/ /home/ubuntu/my_project/static/
<Directory /home/ubuntu/my_project/static>
Require all granted
</Directory>
<Directory /home/ubuntu/my_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
When I'm trying to
/etc/init.d/apache2 stop
and instead:
python manage.py runserver 0.0.0.0:80
It works fine and I'm able to connect to my app from a browser with IP as URL.
It gives me understanding that the setting.py file is fine (am I right?) and the problem is or in wsgi.py or httpd.conf.
When I stop my development server and start Apache again, I get the default Apache page. Can you help me please to find what's wrong with my files?
Permissions for apache:
ubuntu#ip-172-**-**-69:~$ ls -ld my_project/
drwxr-xr-x 10 www-data www-data 4096 May 26 19:51 project/
You normally shouldn't use an IP address for ServerName, it has to be the hostname (FQDN) that the site is accessed as.
You are missing a WSGIDaemonProcess directive to correspond to your use of WSGIProcessGroup.
It is bad security practice to set DocumentRoot to be where your source code for your application is.
Finally, stuff under a users home directory is not usually accessible to the user that Apache runs as.
Suggest go back and review the mod_wsgi deployment documentation on the Django site again for a start.
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.