Multiple domains all incorrectly point to the same VirtualHost with Django WSGI - django

I'm running Django using wsgi. I have two domains and one sub domain pointing to three seperate apache2 virtual hosts. For some (probably very obvious) reason each domain is landing to the same site (the first one that was put online using a2ensite). My configuration is as follows:
<VirtualHost *:80>
ServerName www.one.com/
ServerAlias one.com
ServerAdmin andy#one.com
DocumentRoot /srv/www/one.com/public_html
<Directory /srv/www/one.com/application>
Order allow,deny
Allow from all
</Directory>
WSGIDaemonProcess one.com processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup one.com
WSGIScriptAlias / /srv/www/one/application/apache/one.wsgi
Alias /robots.txt /srv/www/one.com/public_html/robots.txt
Alias /favicon.ico /srv/www/one.com/public_html/favicon.ico
Alias /media /srv/www/one.com/public_html/media
Alias /static /srv/www/one.com/public_html/static
ErrorLog /srv/www/one.com/logs/error.log
CustomLog /srv/www/one.com/logs/access.log combined
</VirtualHost>
////// /// one.wsgi //////////
import os
import sys
sys.path.append('/srv/www/one.com/application')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
//////////////////
<VirtualHost *:80>
ServerName dev.one.co.uk/
ServerAlias www.dev.one.co.uk/
ServerAdmin andy#one.com
DocumentRoot /srv/www/dev.one.com/public_html
<Directory /srv/www/dev.one.com/application>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /srv/www/dev.one.com/application/apache/devone.wsgi
Alias /robots.txt /srv/www/dev.one.com/public_html/robots.txt
Alias /favicon.ico /srv/www/dev.one.com/public_html/favicon.ico
Alias /media /srv/www/dev.one.com/public_html/media
Alias /static /srv/www/dev.one.com/public_html/static
ErrorLog /srv/www/dev.one.com/logs/error.log
CustomLog /srv/www/dev.one.com/logs/access.log combined
</VirtualHost>
//////// devone.wsgi ///////////
import os
import sys
sys.path.append('/srv/www/dev.one.com/application')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
/////////////
<VirtualHost *:80>
ServerName dev.two.com/
ServerAlias www.dev.two.com/
ServerAdmin andy#two.com
DocumentRoot /srv/www/dev.two.com/public_html
<Directory /srv/www/dev.two.com/application>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /srv/www/dev.two.com/application/apache/devtwo.wsgi
Alias /robots.txt /srv/www/dev.two.com/public_html/robots.txt
Alias /favicon.ico /srv/www/dev.two.com/public_html/favicon.ico
Alias /media /srv/www/dev.two.com/public_html/media
Alias /static /srv/www/dev.two.com/public_html/static
ErrorLog /srv/www/dev.two.com/logs/error.log
CustomLog /srv/www/dev.two.com/logs/access.log combined
</VirtualHost>
//////// devtwo.wsgi /////
import os
import sys
sys.path.append('/srv/www/dev.two.com/application')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
The domains are all hitting my servers IP fine, but they all land on the same page. What am I missing? Thanks in advance!
Update:
NameVirtualHost *:80
Is declared in ports.conf

Make sure the vhosts definitions are being loaded in order, from most specific to least specific (ex, possibly by naming them 10_dev.two.com.conf, 20_dev.one.com.conf, 30_one.com.conf)
The ServerName and ServerAlias directives shouldn't have the trailing /

Related

After setting up ssl I cannot access my site django&apache

I created a demo for my domain www.example.com which only returns the index as you can see below.
def index(request):
return HttpResponse("This is a demo")
urlpatterns = [
path('admin/', admin.site.urls),
path("",index),
]
I was able to access my site with domain name (I made the "A" dns record in godaddy site) and this was the <VirtualHost *:80>
<VirtualHost *:80>
ServerName loopingaround.com
ServerAlias www.loopingaround.com
ErrorLog /django-pro/site/logs/error.log
CustomLog /django-pro/site/logs/access.log combine
alias /static /django-pro/site/public/static
<Directory /django-pro/site/public/static>
Require all granted
</Directory>
alias /media /django-pro/site/public/media
<Directory /django-pro/site/public/media>
Require all granted
</Directory>
<Directory /django-pro/src/tutorial>
Require all granted
</Directory>
WSGIDaemonProcess tutorial python-home=/django-pro/venv python-path=/django-pro/src/
WSGIProcessGroup tutorial
WSGIScriptAlias / /django-pro/src/tutorial/wsgi.py
</VirtualHost>
then I used "ssl for free" for creating a ssl certificate for my site and set the files they provided.
<VirtualHost *:80>
ServerName loopingaround.com
ServerAlias www.loopingaround.com
Redirect / https://loopingaround.com
</VirtualHost>
<VirtualHost *:443>
ServerName loopingaround.com
ServerAlias www.loopingaround.com
ErrorLog /django-pro/site/logs/error.log
CustomLog /django-pro/site/logs/access.log combine
SSLEngine on
SSLCertificateFile /etc/ssl/certificate.crt
SSLCertificateKeyFile /etc/ssl/private/private.key
SSLCertificateChainFile /etc/ssl/ca_bundle.crt
alias /static /django-pro/site/public/static
<Directory /django-pro/site/public/static>
Require all granted
</Directory>
alias /media /django-pro/site/public/media
<Directory /django-pro/site/public/media>
Require all granted
</Directory>
<Directory /django-pro/src/tutorial>
Require all granted
</Directory>
WSGIDaemonProcess tutorial python-home=/django-pro/venv python-path=/django-pro/src/
WSGIProcessGroup tutorial
WSGIScriptAlias / /django-pro/src/tutorial/wsgi.py
</VirtualHost>
now even if I revert the changes I made, I cannot access my site as before (I am restarting apache2 service everytime) nor I can access it using https
if possible please tell me my mistakes and how I can also implement ssl for my django project to return 403.

Serving multiple domains with Django and Apache maps to one location only

I am attempting to setup Django to serve multiple sites (in the below configuration I am using examples www.domain1.com and www.domain2.com). I have installed Apache2 successfully, configured wsgi.py successfully (or so it seems).
I have built my appache configuration file to attempt the following mapping:
www.example1.com to serve from /var/www/mysite1
www.example2.com to serve from /var/www/mysite2
DNS A records for both example1.com and example2.com point to the same IP address.
The trouble with my setup as it exists is that both domain1.com and domain2.com map to the Django residing at /var/www/mysite1, despite the fact that I have (in theory) set them to map to their respective locations.
My apache config files are as follows, and both of the files (mysite1.conf and mysite2.conf) have had symlinks made for them using a2ensite:
#/etc/apache2/sites-available/mysite1.conf
WSGIDaemonProcess mysite processes=2 threads=25 python-home=/var/www/mysite1/myenv1 python-path=/var/www/mysite1/myenv1/mys
ite1
WSGIProcessGroup mysite
WSGIScriptAlias / /var/www/mysite1/myenv1/mysite1/mysite1/wsgi.py
<VirtualHost *:80>
ServerName domain1.com
ServerAlias xx.xx.xx.xx
DocumentRoot /var/www/mysite1
ErrorLog ${APACHE_LOG_DIR}/mysite1-error.log
CustomLog ${APACHE_LOG_DIR}/mysite1-access.log combined
Alias /robots.txt /var/www/mysite1/myenv1/mysite1/static/robots.txt
Alias /favicon.ico /var/www/mysite1/myenv1/mysite1/static/favicon.ico
Alias /static/ /var/www/mysite1/myenv1/mysite1/static/
<Directory /var/www/mysite1/myenv1/mysite1/mysite1>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /var/www/mysite1/myenv1/mysite1/static>
Require all granted
</Directory>
</VirtualHost>
#/etc/apache2/sites-available/mysite2.conf
WSGIDaemonProcess mysite2 processes=2 threads=25 python-home=/var/www/mysite2/myenv2 python-path=/var/www/mysite2/myenv
2/mysite2
WSGIProcessGroup mysite2
WSGIScriptAlias / /var/www/mysite2/myenv2/mysite2/mysite2/wsgi.py process-group=mysite2
WSGISocketPrefix /var/run/wsgi
<VirtualHost *:80>
ServerName domain2.com
ErrorLog ${APACHE_LOG_DIR}/mysite2-error.log
CustomLog ${APACHE_LOG_DIR}/mysite2-access.log combined
DocumentRoot /var/www/mysite2
Alias /robots.txt /var/www/mysite2/myenv2/mysite2/static/robots.txt
Alias /favicon.ico /var/www/mysite2/myenv2/mysite2/static/favicon.ico
Alias /static/ /var/www/mysite2/myenv2/mysite2/static/
<Directory /var/www/mysite2/myenv2/mysite2/mysite2>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /var/www/mysite2/myenv2/mysite2/static>
Require all granted
</Directory>
</VirtualHost>
An example wsgi.py file looks as below, and is working correctly (ie loading from the correct location) when it does load:
import os
import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite1.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite1.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
There are no errors being reported in either logs (/var/syslog/apache2/mysiteX.conf where x=1 or 2)
What am I doing wrong? How can I correct this so that domain2 starts to map to /var/www/mysite2?
Many thanks in advance!
The WSGI tags need to be inside the VirtualHost block for their respective domain names.
E.g.
<VirtualHost *:80>
ServerName domain1.com
ServerAlias xx.xx.xx.xx
DocumentRoot /var/www/mysite1
WSGIDaemonProcess mysite processes=2 threads=25 python-home=/var/www/mysite1/myenv1 python-path=/var/www/mysite1/myenv1/mys
ite1
WSGIProcessGroup mysite
WSGIScriptAlias / /var/www/mysite1/myenv1/mysite1/mysite1/wsgi.py
...
</VirtualHost>

VirtualHost configuration doesn't allow second website to run

I have this in my /etc/apache2/sites-available/staginnx.com.conf
WSGIScriptAlias / /home/ubuntu/v1/staginnx-info/app/website/website/wsgi.py
WSGIPythonPath /home/ubuntu/v1/staginnx-info/app/website
<VirtualHost *:80>
# Admin email, Server Name (domain name) and any aliases
ServerAdmin info#staginnx.com
ServerName staginnx.com
ServerAlias www.staginnx.com
<Directory /home/ubuntu/v1/staginnx-info/app/website/website>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static/ /home/ubuntu/v1/staginnx-info/app/website/ui/static/
<Directory /home/ubuntu/v1/staginnx-info/app/website/ui/static>
Require all granted
</Directory>
Alias /favicon.ico /home/ubuntu/v1/staginnx-info/app/website/ui/static/images/favicon.ico
ErrorLog /var/log/apache2/error.log
</VirtualHost>
<VirtualHost *:80>
# Admin email, Server Name (domain name) and any aliases
ServerAdmin blog.staginnx#staginnx.com
ServerName blog.staginnx.com
ServerAlias blog.staginnx.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.php
DocumentRoot /var/www/html/blog/
<Directory /var/www/html/blog>
Require all granted
</Directory>
# Custom log file locations
LogLevel warn
ErrorLog /var/www/html/blog/log/error.log
CustomLog /var/www/html/blog/log/access.log combined
</VirtualHost>
Now the first one, the main website is working fine. The second one, BLOG is not working.
I have done sudo a2ensite staginnx.com.conf and a symlink is generated at sites-enabled. I have done reload apache and restart too.
Surfed around but no luck.
Want to install wordpress blog in my blog folder.
In order to use your django application inside a virtual host you need to set the WSGIScriptAlias inside the virtualhost settings and also you can't use the WSGIPythonPath, you need to use WSGIDaemonProcess like in the following settings.
<VirtualHost *:80>
WSGIScriptAlias / /home/ubuntu/v1/staginnx-info/app/website/website/wsgi.py
WSGIDaemonProcess example.com python-path=/home/ubuntu/v1/staginnx-info/app/website
# Admin email, Server Name (domain name) and any aliases
ServerAdmin info#staginnx.com
ServerName staginnx.com
ServerAlias www.staginnx.com
<Directory /home/ubuntu/v1/staginnx-info/app/website/website>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static/ /home/ubuntu/v1/staginnx-info/app/website/ui/static/
<Directory /home/ubuntu/v1/staginnx-info/app/website/ui/static>
Require all granted
</Directory>
Alias /favicon.ico /home/ubuntu/v1/staginnx-info/app/website/ui/static/images/favicon.ico
ErrorLog /var/log/apache2/error.log
</VirtualHost>
Also in case you want to host multiple application on the same host then you need to change a small setting in the wsgi.py file as well.
instead of using
os.environ.setdefault(DJANGO_SETTINGS_MODULE, "app.settings")
you should use
os.environ["DJANGO_SETTINGS_MODULE"] = "app.settings"
I hope this helps you.
For WSGIDaemonProcess setting

Django on Debian

so I use Debian 6.0.9 (squeeze).
I have my project that I coded locally, that I uploaded on my server in /home/project/
now I already have mysql, apache2.2.16 and a lot of different PHP websites hosted.
Here's a typical sites-enabled/* I use :
<VirtualHost *:80>
ServerName project.com
ServerAlias www.project.com
DocumentRoot /var/www/project.com/htdocs/
</VirtualHost>
I installed wsgi mod and tried following what it says here https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/modwsgi/
and nothing works. I don't get how it could work anyway since I don't precise the URL I want to use for my project. The Documentation is really not clear... Anyone have a clue what I am supposed to do?
Edit : I forgot to write how my file looks like:
<VirtualHost *:80>
ServerName project.com
ServerAlias project.com
DocumentRoot /var/www/project.com/htdocs
WSGIScriptAlias / /home/project/project/wsgi.py
<Directory /home/project/project>
Order allow,deny
Allow from all
</Directory>
Alias /robots.txt /var/www/project.com/htdocs/robots.txt
Alias /favicon.ico /var/www/project.com/htdocs/favicon.ico
Alias /images /var/www/project.com/htdocs/images
Alias /static /var/www/project.com/htdocs/static
ErrorLog /var/www/project.com/logs/error.log
CustomLog /var/www/project.com/logs/access.log combined
</VirtualHost>
In your VirtualHost def:
Make sure you configure:
DocumentRoot, WSGIScriptAlias - configured properly is a MUST
ErrorLog, CustomLog - it helps in debugging
WSGIDaemonProcess and WSGIProcessGroup - is a good security practice
For example:
WSGIPythonHome PATH_TO_YOUR_PYTHON
<VirtualHost *:80>
ServerName project.com
ServerAlias www.project.com, *.project.*
ErrorLog "/var/log/httpd/vhost_test_error.log"
CustomLog "/var/log/httpd/vhost_test_access.log" combined
WSGIDaemonProcess project processes=2 threads=15 display-name=%{GROUP} python-path=PATH_TO_YOUR_VIRTUAL_ENV
WSGIProcessGroup project
#DocumentRoot /var/www/project.com/htdocs/
DocumentRoot "/var/www/project.com/PATH_TO_DJANGO_PROJECT"
WSGIScriptAlias / /var/www/project.com/PATH_TO_DJANGO_PROJECT/wsgi.py
</VirtualHost>

Deploy a Django site and a PHP site on the same server with Apache and mod_wsgi

I currently have a Django site working at cinepass.com.ec , I would like to deploy an additional PHP site to the same server at mobile.cinepass.com.ec
My current httpd.conf (from DjangoFoo) :
<Directory "/home/ec2-user/cinepass/media">
Order deny,allow
Allow from all
</Directory>
<Directory "/home/ec2-user/cinepass/cinepass">
AllowOverride All
Order deny,allow
Allow from all
</Directory>
Alias /media/ /home/ec2-user/cinepass/media/
ServerAdmin smansfield#palapa.com.ec
ErrorLog "logs/cinepass.com-error_log"
CustomLog "logs/cinepass.com-access_log" common
# mod_wsgi configuration is here
# we are running as user/group 'deamon', if you don't have those you need to change or create.
WSGIDaemonProcess cinepass python-path=/home/ec2-user/cinepass:/home/ec2-user/cinepass/venv/lib/python2.6/site-packages user=daemon group=daemon processes=2 threads=25
WSGIProcessGroup cinepass
# this is our WSGI file.
WSGIScriptAlias / /home/ec2-user/cinepass/cinepass/wsgi.py
My current wsgi.py :
import os, sys
sys.path.append('/home/')
sys.path.append('/home/ec2-user/cinepass/')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cinepass.settings_production.py")
os.environ['PYTHON_EGG_CACHE'] = '/tmp'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
How would I edit my Apache configuration so that I can also run a php site at mobile.cinepass.com.ec?
Using apache´s virtualhosts, here I put an example of something similar in a server of mine, in which I have a djangp app in the main domain and a joomla in a subdomain. Both files are located in /etc/apache2/sites-enabled
Joomla´s apache conf file (named /etc/apache2/sites-enabled/manual.domain.com):
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin dsanabria#domain.com
ServerName manual.domain.com
DocumentRoot "/home/ubuntu/manual/"
<Directory /home/ubuntu/manual/>
Order deny,allow
Allow from all
</Directory>
ErrorLog /var/log/apache2/manual.domain-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel debug
CustomLog /var/log/apache2/manual.domain-access.log combined
</VirtualHost>
And the django app (named /etc/apache2/sites-enabled/www.domain.co):
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin diego#diegue.us
ServerName domain.co
ServerAlias machete.anotherdomain.com
Alias /admin/media/ /home/ubuntu/webapps/machete/lib/python2.7/site-packages/grappelli/media/
Alias /media/ /home/ubuntu/webapps/machete/machete/media/
Alias /static/ /home/ubuntu/webapps/machete/machete/collected/
<Directory /home/ubuntu/webapps/machete/lib/python2.7/site-packages/grappelli/media/>
Order deny,allow
Allow from all
</Directory>
<Directory /home/ubuntu/webapps/machete/lib/python2.7/site-packages/django/contrib/admin/media/ >
Order deny,allow
Allow from all
</Directory>
<Directory /home/ubuntu/webapps/machete/machete/media/>
Order deny,allow
Allow from all
</Directory>
<Directory /home/ubuntu/webapps/machete/machete/collected/>
Order deny,allow
Allow from all
</Directory>
WSGIScriptReloading On
WSGIDaemonProcess machete python-path=/home/ubuntu/webapps/machete/lib/python2.7/site-packages
WSGIProcessGroup machete
WSGIApplicationGroup machete
WSGIPassAuthorization On
WSGIScriptAlias / /home/ubuntu/webapps/machete/machete/machete/wsgi.py
ErrorLog /var/log/apache2/machete-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel debug
CustomLog /var/log/apache2/machete-access.log combined
</VirtualHost>
The first, tells to apache, that if the user gets to manual.domain.com, just response with a php application (joomla). The second file says to apache, that if the user calls the server with www.domain.com response with a python wsgy, (django).
This is in a ubuntu server, redhat/centos/fedora locates the folder sites-enabled in another location that I can´t remember, but anyway you can use virtualhosts.
Generraly, I avoid to mess with the httpd.conf file and prefer use virtualhosts.