django deployment with apache and mod_wsgi - django

In order to deploy my django project, i've downloaded httpd and mod_wsgi. To configure this module, I've also downloaded apxs and gcc and I've followed this tutorial :
https://modwsgi.readthedocs.io/en/develop/user-guides/quick-configuration-guide.html
And then I've found in the django documentation :
https://docs.djangoproject.com/fr/2.0/howto/deployment/wsgi/modwsgi/
a way to deploy my project.
So I actually have this wigs.py :
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ProjetIML.settings")
application = get_wsgi_application()
And this httpd.conf :
WSGIScriptAlias / /home/ninon/ProjetIML/ProjetIML/wsgi.py
WSGIPythonPath /home/ninon/ProjetIML
<Directory /home/ninon/ProjetIML/ProjetIML>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Then I've restart the apache server with "apachectl restart" and when I try to access my site with localhost:80 nothing append. Did I miss something ?
I've seen many posts explaining that they have coded files like "django.wsgi" but I haven't found anything about it in the django documentation.
I've also tried to add a Virtual host like this :
<VirtualHost *:80>
WSGIScriptAlias / /home/ninon/ProjetIML/ProjetIML/wsgi.py
WSGIPythonPath /home/ninon/ProjetIML
<Directory /home/ninon/ProjetIML/ProjetIML>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
But the apache server didn't want to restart.

Related

Hosting multiple sub domains on the same windows 10 server running django application

I need to host multiple sites with subdomains all pointing to the same windows10 server. virtual hosts for different sub-domains points to different WSGI files. Venv activation is added in wsgi file. The issue happening is first Django settings file which is invoked is applied to all sub-domains.
eg.
company1.example.com -> /company1_wsgi.py -> company1_settings.py
company2.example.com -> /company2_wsgi.py -> company2_settings.py
Now, company1_settings is applied for both company1.example.com and company2.example.com. I see right WSGI files are called based on the sub-domain but for some reason os.environ['DJANGO_SETTINGS_MODULE'] is not being updated dynamically based on the subdomains and always uses the first value
wsgi file
python_home= 'E:/app/demo/env'
activate_this = 'E:/app/demo/env/Scripts/activate_this.py'
exec(open(activate_this).read(),dict(__file__=activate_this))
import os
import sys
from django.core.wsgi import get_wsgi_application
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('E:/app/demo/env/Lib/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('E:/app/demo/crm')
sys.path.append('E:/app/demo/crm/appointment_scheduler')
os.environ['DJANGO_SETTINGS_MODULE'] = 'appointment_scheduler.settings.preprod'
application = get_wsgi_application()
<VirtualHost *:443>
ServerName demo.crmbaylyn.com
WSGIScriptAlias / "E:/app/demo/crm/appointment_scheduler/wsgi_win.py"
WSGIPassAuthorization On
WSGIScriptReloading On
<Directory "E:/app/demo/crm/appointment_scheduler">
<Files wsgi.py>
Order deny,allow
Allow from all
Require all granted
</Files>
Order allow,deny
Allow from all
Require all granted
</Directory>
Alias /static "E:/app/demo/crm/static"
<Directory "E:/app/demo/crm/static">
Require all granted
</Directory>
SSLEngine on
SSLCertificateFile "C:/Apache24/conf/crmbaylyn.cert"
SSLCertificateKeyFile "C:/Apache24/conf/crmbaylyn.key"
</VirtualHost>
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule !/robots.txt https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
</VirtualHost>
<VirtualHost *:443>
ServerName dev.crmbaylyn.com
WSGIScriptAlias / "E:/app/dev/crm/appointment_scheduler/wsgi_collection/dev_wsgi.py"
WSGIPassAuthorization On
WSGIScriptReloading On
<Directory "E:/app/dev/crm/appointment_scheduler">
<Files wsgi.py>
Order deny,allow
Allow from all
Require all granted
</Files>
Order allow,deny
Allow from all
Require all granted
</Directory>
Alias /static "E:/app/dev/crm/static"
<Directory "E:/app/dev/crm/static">
Require all granted
</Directory>
SSLEngine on
SSLCertificateFile "C:/Apache24/conf/crmbaylyn.cert"
SSLCertificateKeyFile "C:/Apache24/conf/crmbaylyn.key"
</VirtualHost>
This would be solved by mod_wsgi's WSGIDaemonProcess or WSGIProcessGroup (as then you'd have separate os.environs and so on), but those features are not available on Windows.
If you really need to run on Windows, I would suggest switching away from mod_wsgi to using a regular reverse proxy configuration to proxy requests to e.g. Waitress or another HTTP/WSGI server running your app.
Deploy your projects to IIS, then configure the binding to each WebSite as you need.
You can use the django-windowsauth package to deploy your project to IIS using few simple commands. https://github.com/danyi1212/django-windowsauth.
After adding the websites to IIS, specify your different host names in the bindings of each project.

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.

can't reload apache with wsgi

So I'm following the tutorial on Django on how to deploy : https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/modwsgi/
I copypasted this in httpd.conf
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>
and now all my websites are redirected to my django app
so I tried puting a VirtualHost around that script, and if I do that I cannot reload apache2. Any idea why or how I should do it?
I don't know how long I've been up working on that problem. But I found something:
WSGIPythonPath /path/to/mysite.com
If I write this line I can't reload apache.
So I removed it, and I wrote:
import sys
sys.path.append('/home/myproject/')
in wsgi.py
and it works :)

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