Django deploy to Production in Ubuntu 16.04 - django

I'm fairly new to django , i have to deploy the project to production mode in ubuntu 16.04. I have installed wsgi with apache2 and included the system path to the django project and python site-packages.
My sites-enabled/000-default.config is
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName myproject.com
ServerAlias www.myproject.com
Alias /static /var/www/html/TEST/myproject/static
<Directory /var/www/html/TEST/myproject/static>
Require all granted
</Directory>
<Directory /var/www/html/TEST/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-home=/var/www/html/TEST/newenv python-path=/var/www/html/TEST/myproject:/var/www/html/TEST/newenv/lib/python3.5/site-packages
WSGIProcessGroup myproject
WSGIScriptAlias / /var/www/html/TEST/myproject/myproject/wsgi.py
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
and my wsgi file
import os , sys
sys.path.append('/var/www/html/TEST/myproject/')
sys.path.append('/var/www/html/TEST/newenv/lib/python3.5/dist-packages/')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
when i run the link i am getting Internal Server Error(505)
and i have checked the error in the error log ,it's showing me from
django.core.wsgi import get_wsgi_application
[wsgi:error] ImportError: No module named django.core.wsgi
Can someone please help me with this ? i have googled and checked out all other options and solutions . Thanks in advance

Instead of:
WSGIDaemonProcess myproject python-home=/var/www/html/TEST/newenv \
python-path=/var/www/html/TEST/myproject:/var/www/html/TEST/newenv/lib/python3.5/site-packages
WSGIProcessGroup myproject
WSGIScriptAlias / /var/www/html/TEST/myproject/myproject/wsgi.py
all you should need is:
WSGIDaemonProcess myproject python-home=/var/www/html/TEST/newenv \
python-path=/var/www/html/TEST/myproject
WSGIProcessGroup myproject
WSGIScriptAlias / /var/www/html/TEST/myproject/myproject/wsgi.py
You don't need to add the site-packages directory.
In the wsgi.py file you don't need:
sys.path.append('/var/www/html/TEST/myproject/')
sys.path.append('/var/www/html/TEST/newenv/lib/python3.5/dist-packages/')
as python-home and python-path on WSGIDaemonProcess achieve same result.
Another issue is that you have put your Django project code under the directory /var/www/html. You should not put it there. That is the default DocumentRoot directory. If you were to uncomment WSGIScriptAlias, someone could download your source code. So is better to not put your code under that directory.
Now neither of those things should make a difference as far as solving your problem, as the first just gets of redundant settings. The second is just changing to better practices.
At this point, the only reason can see why Django module could not be found is if the permissions on the directories/files are such that the user that Apache runs your code as cannot read them. So check permissions on /var/www/html/TEST/newenv.

Related

Timeout when reading response headers from daemon process even after setting WSGIApplication group to Global

I am hosting a Django based webpage locally using Apache. However, I am getting the following error :
Timeout when reading response headers from daemon process 'office':var/www/office/office/wsgi.py.
I tried adding the line WSGIApplicationGroup %{GLOBAL} to the conf file, but still getting the same error.
This is my .conf file.
WSGIPythonPath /var/www/office
ServerName office.org
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
WSGIApplicationGroup %{GLOBAL}
<VirtualHost 0.0.0.0:80>
ServerAlias www.office.org
DocumentRoot /var/www/example.com/public_html
<Directory /var/www/example.com>
Require all granted
</Directory>
<Directory /var/www/office/office>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess office python-home=/var/www/venv python-path=/var/www/office
WSGIProcessGroup office
WSGIScriptAlias /verify /var/www/office/office/wsgi.py process-group=office
ErrorLog /var/www/logs/error.log
CustomLog /var/www/logs/custom.log combined
</VirtualHost>
This is wsgi.py file:
"""
WSGI config for office 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/2.0/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "office.settings")
application = get_wsgi_application()
I've got the same problem and after a lot of struggling and trying different things, this tutorial helped me to solve the problems.
I created and successfully run the demo project. The problem for me were the paths in the config file and also using the server python version instead of creating a virtual environment for the project.
I hope it will help you
I have got the same problem "Timeout when reading response headers" in Flask framework
I resolved it by adding TimeOut 600 in httpd.conf/app.conf
Reference : https://ubiq.co/tech-blog/increase-request-timeout-apache/
if this happened on a working server, You can try to force renew Your ssl certificate.
the reason could be found in apache error logs:
Name-based SSL virtual hosts only work for clients with TLS server name indication support (RFC 4366)
helped for me.

Deploying Django on Apache with mod_wsgi

I'm currently trying to set up a Django application on Apache on a Centos server, with the use of mod_wsgi. This is set up to run on https. It is on virtualenv.
However, currently it gives a 504 Gateway Timeout error.
Error log:
(2)No such file or directory: mod_wsgi (pid=15007): Unable to stat Python home /bin/virtualenv:/var/www/django/mysite/mysite/lib/python3.6/site-packages. Python interpreter may not be able to be initialized correctly. Verify the supplied path and access permissions for whole of the path.
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
httpd.conf
<VirtualHost *:443>
ServerName mysite.example.com
ServerAlias www.mysite.example.com
# DocumentRoot /var/www/django/mysite
LogLevel info
ErrorLog /etc/httpd/logs/mysite_error.log
Alias /static /var/www/django/mysite/static
<Directory /var/www/django/mysite/static>
Require all granted
</Directory>
<Directory /var/www/django/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess mysite python-home=/bin/virtualenv:/var/www/django/mysite/mysite/lib/python3.6/site-packages
WSGIProcessGroup mysite
WSGIScriptAlias / /var/www/django/mysite/mysite/wsgi.py
WSGIApplicationGroup %{GLOBAL}
SSLEngine on
SSLProtocol all -SSLv2
SSLCompression off
SSLCertificateFile /etc/pki/tls/certs/mysite.cert.pem
SSLCertificateKeyFile /etc/pki/tls/private/mysite.key.pem
SSLCertificateChainFile /etc/pki/tls/certs/mysite.chain.pem
</VirtualHost>
This is incorrect:
WSGIDaemonProcess mysite python-home=/bin/virtualenv:/var/www/django/mysite/mysite/lib/python3.6/site-packages
Review:
http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html
The python-home value should be a single directory, which is the same as sys.prefix for Python when using virtual environment. Do not added site-packages directory.
Most important, mod_wsgi must be compiled for same Python version as was used to create the virtual environment. You can't use mod_wsgi compiled for Python 2.7 with virtual environment created with Python 3.6.
Check what version of Python mod_wsgi was compiled for using:
http://modwsgi.readthedocs.io/en/develop/user-guides/checking-your-installation.html#python-installation-in-use

WSGIDaemonProcess causes 'You don't have permission error'

Hello I am trying to deploy my django application on digital ocean server. I did run the application inside a virtual environment and it all worked fine. But I want my domain to point to my application and followed this tutorial. But the problem is when the apache conf file is edited with the following code i get the error.
Forbidden You don't have permission to access / on this server.
This is how my directly looks
+root
+myproject
+myproject
settings.py
urls.py
wsgi.py
+static
manage.py
+myprojectenv
Here is my WSGI.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
application = get_wsgi_application()
Here is my Apache Default conf file 000-default.conf
<VirtualHost *:80>
#ServerAdmin webmaster#localhost
#DocumentRoot /var/www/html
Alias /static /home/user/alpha/static
<Directory /home/user/alpha/static>
Require all granted
</Directory>
Alias /static /home/root/myproject/static
<Directory /home/root/myproject/static>
Require all granted
</Directory>
<Directory /home/root/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-home=/home/root/myproject/myprojectenv python-path=/home/root/myproject
WSGIProcessGroup myproject
WSGIScriptAlias / /home/root/myproject/myproject/wsgi.py
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
I am not experience enough to find out what is causing the error. If i remove below lines in the apache conf file. I get Ubuntu home page when i try to access my server
WSGIDaemonProcess myproject python-home=/home/root/myproject/myprojectenv python-path=/home/root/myproject
WSGIProcessGroup myproject
WSGIScriptAlias / /home/root/myproject/myproject/wsgi.py
Your Python code will run as the Apache user. As a result it likely will not have access to files under home directories as they don't have permissions on them that allows access by others.
You should move stuff from out of the home directories and put it under somewhere like /var/www/project.
See:
http://modwsgi.readthedocs.io/en/develop/user-guides/application-issues.html#access-rights-of-apache-user

how to resolve You don't have permission to access / on this server

Hi I need help in integrating django with apache and mod_wsgi on centos6.
I am getting following error every time---"Forbidden You don't have permission to access / on this server."
My django project path= /home/mchauras/esapp/eswebsite
my apache version is 2.2.15
my .conf file looks like this----
<VirtualHost *:80>
DocumentRoot /home/mchauras/esapp/eswebsite/
Alias /static /home/mchauras/esapp/eswebsite/esapp/static
<Directory /home/mchauras/esapp/eswebsite/esapp/static>
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride None
Order deny,allow
Allow from all
</Directory>
<Directory /home/mchauras/esapp/eswebsite/eswebsite>
<Files wsgi.py>
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride None
Order deny,allow
Allow from all
</Files>
</Directory>
WSGIDaemonProcess esapp python-path=/home/user/myproject:/home/mchauras/esapp/eswebsite/myvenv/lib/python3.5/site-packages/
WSGIProcessGroup esapp
WSGIScriptAlias / /home/mchauras/esapp/eswebsite/eswebsite/wsgi.py
ErrorLog /home/mchauras/esapp/eswebsite/error.log
CustomLog /home/mchauras/esapp/eswebsite/access.log combined
</VirtualHost>
my wsgi.py file is like this---
import os
import sys
from django.core.wsgi import get_wsgi_application
sys.path.append('/home/mchauras/esapp/eswebsite')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "eswebsite.settings")
application = get_wsgi_application()
its looks like you did not login as a administrator user or maybe you need to changes and enable all file permission in the path
This specific error is usually always caused by the user that Apache runs as not being able to access the WSGI script file. It is not enough to just make the file itself readable to others. All the directories from '/' down to that directory must also be accessible to the user Apache runs as. As home directories for users are not usually readable by others, the Apache user cannot see into it. You are better off moving the application directory outside of your home directory.
Another possible cause, although one which usually results in a slightly different error, is having SELinux enabled where the profile for Apache httpd server on SELinux doesn't allow access to the directories where your application is.

How to deploy Django with mod_wsgi on Debian machine?

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.
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.
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
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