Django to pass through some urls to base apache - django

Is there a way for Django to pass through some urls to whatever apache would resolve them too?
For instance, if I type in: http://< my-ip >. Is there a way to have django just serve up whatever is in /var/www/html/index.html?
Similarly, if I type in http://< my-ip >/devel, is there a way to make django simply serve what's in /var/www/html/devel/?
Basically, I want some urls to "not be touched".
Thanks!
Edit
Following Anentropic's comment, I'd like Apache to call Django only if it can't match the url. How would I need to change httpd.conf to do that? Let's say I want Django to respond to only /polls/, /admin/ and /accounts/.
Here's the relevant portion from my httpd conf file:
<VirtualHost *:80>
#DocumentRoot /home/ec2-user/srv/mysite
DocumentRoot /var/www/html/
ServerName <My IP ADDRESS>
WSGIScriptAlias / /home/ec2-user/srv/mysite/apache/wsgi.py
# Alias /phpmyadmin /var/www/html/phpmyadmin
# <Location /phpmyadmin>
# SetHandler None
# </Location>
<Directory /home/ec2-user/srv/mysite/media>
Order deny,allow
Allow from all
</Directory>
<Directory /home/ec2-user/srv/mysite/apache>
Order deny,allow
Allow from all
</Directory>
LogLevel warn
Alias /media/ /home/ec2-user/srv/mysite/media/
Alias /static/ /home/ec2-user/srv/mysite/static/
<Directory /home/ec2-user/srv/mysite/static>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>

If you'd like to serve static html pages you could use the flatpages app.
Again if you'd like to use different languages (php etc) I believe that you'd have to set up different virtual host in you apache config file to catch the url before it gets served to Django and redirect it to the correct folder. i.e:
<VirtualHost *:80>
ServerName staging.mydomain.com
DocumentRoot /var/www/html/mydomain.com/staging/
</VirtualHost>
<VirtualHost *:80>
ServerName dev.mydomain.com
DocumentRoot /var/www/html/mydomain.com/dev/
</VirtualHost>
EDIT
<VirtualHost *:80>
#DocumentRoot /home/ec2-user/srv/mysite
DocumentRoot /var/www/html/
ServerName <My IP ADDRESS>
Alias /phpmyadmin /var/www/html/phpmyadmin
<Location /phpmyadmin>
SetHandler None
</Location>
<Directory /home/ec2-user/srv/mysite/media>
Order deny,allow
Allow from all
</Directory>
<Directory /home/ec2-user/srv/mysite/apache>
Order deny,allow
Allow from all
</Directory>
LogLevel warn
WSGIScriptAlias /polls /home/ec2-user/srv/mysite/apache/wsgi.py
WSGIScriptAlias /admin /home/ec2-user/srv/mysite/apache/wsgi.py
WSGIScriptAlias /accounts /home/ec2-user/srv/mysite/apache/wsgi.py
Alias /media/ /home/ec2-user/srv/mysite/media/
Alias /static/ /home/ec2-user/srv/mysite/static/
<Directory /home/ec2-user/srv/mysite/static>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>

Related

Django project set up with Apache2 - i want to access it through I.P/application_name/

Hi I want to set up my django project to be access through apache2 by I.P/application_name/
So "192.0.0.0/app/" (Ip being an example)
Here is my 000-default.conf file in etc/apache2/sites-available
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
WSGIDaemonProcess HTSv2 python-home=/home/django/config/env python-path=/usr/local/django/app
WSGIProcessGroup HTSv2
WSGIScriptAlias / /usr/local/django/app/application/wsgi.py
<Directory /usr/local/django/app/application>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static/ /usr/local/django/app/static/
<Directory /usr/local/django/app/static>
Require all granted
</Directory>
<Directory /usr/local/django/app>
Deny from all
Allow from ...
</Directory>
</VirtualHost>
When I change the line:
WSGIScriptAlias / /usr/local/django/app/application/wsgi.py
to
WSGIScriptAlias /app/ /usr/local/django/app/application/wsgi.py
I can access the home page of my application by 192.0.0.0/app/
However when I click on any buttons, obviously does not keep to this, so for example going to page1, does not go to 192.0.0.0/app/page1, it goes to 192.0.0.0/page1
How can I configure apache2 to do this? The reason I want to do this is so I can host multiple projects on the same server, so the root of 1 project is 192.0.0.0/app1/ and then the root of the other is 192.0.0.0/app2/ etc...

Serving Multiple WSGI Applications As Different Virtual Hosts on Apache

I have an EC2 AWS server on which I would like to host a couple of Django applications. Each of these apps has its own URL. For instance,
example1.com
example2.com
By itself, example1.com works. The problem is getting example2.com to work with it at the same time.
When I visit example2.com, I get an error:
DisallowedHost at /
Invalid HTTP_HOST header: 'example2.com'. You may need to add 'example2.com' to ALLOWED_HOSTS.
Request Method: GET
Request URL: http://example2.com
Django Version: 1.9.13
Exception Type: DisallowedHost
Exception Value:
Invalid HTTP_HOST header: 'example2.com'. You may need to add 'example2.com' to ALLOWED_HOSTS.
Exception Location: /var/www/vhosts/example1/example1-env/lib/python3.5/site-packages/django/http/request.py in get_host, line 109
Python Executable: /usr/bin/python3
Python Version: 3.5.1
Python Path:
['/usr/lib64/python3.5',
'/usr/lib64/python3.5/plat-linux',
'/usr/lib64/python3.5/lib-dynload',
'/usr/local/lib64/python3.5/site-packages',
'/usr/local/lib/python3.5/site-packages',
'/usr/lib64/python3.5/dist-packages',
'/usr/lib/python3.5/dist-packages',
'/var/www/vhosts/example1/',
'/var/www/vhosts/example1/example1-env/lib/python3.5/site-packages']
Server time: Wed, 14 Jun 2017 20:31:27 +0000
As you can see, somehow Apache is trying to use the virtual environment of example1.com when it serves example2.com. How could I correct that? Each one should be served with its own virtualenv.
Here is the Apache configuration file:
<VirtualHost *:80>
# This is name based virtual hosting. So place an appropriate server name
# here. Example: django.devsrv.local
ServerName example1.com
WSGIDaemonProcess example1 python-home=/var/www/vhosts/example1/example1-env
WSGIProcessGroup %{GLOBAL}
# Insert the full path to the wsgi.py-file here
WSGIScriptAlias / /var/www/vhosts/example1/example1/wsgi.py
<Directory /var/www/vhosts/example1/>
Require all granted
</Directory>
Alias /static/ /var/www/vhosts/example1/static/
<Directory /var/www/vhosts/example1/static/>
Order deny,allow
Allow from all
</Directory>
Alias /media/ /var/www/vhosts/example1/media/
<Directory /var/www/vhosts/example1/media/>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
# This is name based virtual hosting. So place an appropriate server name
# here. Example: django.devsrv.local
ServerName example2.com
WSGIDaemonProcess example2 python-home=/var/www/vhosts/example2/example2-env
WSGIProcessGroup %{GLOBAL}
# Insert the full path to the wsgi.py-file here
WSGIScriptAlias / /var/www/vhosts/example2/example2/wsgi.py
<Directory /var/www/vhosts/example2/>
Require all granted
</Directory>
Alias /static/ /var/www/vhosts/example2/static/
<Directory /var/www/vhosts/example2/static/>
Order deny,allow
Allow from all
</Directory>
Alias /media/ /var/www/vhosts/example2/media/
<Directory /var/www/vhosts/example2/media/>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Edit:
Having read some suggestions in the comments, I have come to this. This still does not work.
ServerName example1.com
WSGIDaemonProcess example1 display-name=%{GROUP} python-path=/var/www/vhosts/example1/ python-home=/var/www/vhosts/example1/example1-env/
WSGIApplicationGroup %{GLOBAL}
WSGIProcessGroup example1
# Insert the full path to the wsgi.py-file here
WSGIScriptAlias / /var/www/vhosts/example1/example1/wsgi.py process-group=example1
...
ServerName example2.com
WSGIDaemonProcess example2 display-name=%{GROUP} python-home=/var/www/vhosts/example2/example2-env/ python-path=/var/www/vhosts/example2/
WSGIApplicationGroup %{GLOBAL}
WSGIProcessGroup example2
# Insert the full path to the wsgi.py-file here
WSGIScriptAlias / /var/www/vhosts/example2/example2/wsgi.py process-group=example2
The following configuration worked for me. In short, it serves two different Django applications at example1.com and example2.com using their respective virtual environments.
As you can see, inserting the ServerAlias AND ServerName made all the difference, alongside a couple of other corrections by suggested by the community.
Apache configuration:
<IfModule !wsgi_module>
LoadModule wsgi_module modules/mod_wsgi.so
</IfModule>
<VirtualHost *:80>
ServerName www.example1.com
ServerAlias example1.com
WSGIDaemonProcess example1 display-name=%{GROUP} python-path=/var/www/vhosts/example1/ python-home=/var/www/vhosts/example1/example1-env/
WSGIApplicationGroup %{GLOBAL}
WSGIProcessGroup example1
# Insert the full path to the wsgi.py-file here
WSGIScriptAlias / /var/www/vhosts/example1/example1/wsgi.py process-group=example1
<Directory /var/www/vhosts/example1/>
Require all granted
</Directory>
Alias /static/ /var/www/vhosts/example1/static/
<Directory /var/www/vhosts/example1/static/>
Order deny,allow
Allow from all
</Directory>
Alias /media/ /var/www/vhosts/example1/media/
<Directory /var/www/vhosts/example1/media/>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName www.example2.com
ServerAlias example2.com
WSGIDaemonProcess example2 display-name=%{GROUP} python-home=/var/www/vhosts/example2/example2-env/ python-path=/var/www/vhosts/example2/
WSGIApplicationGroup %{GLOBAL}
WSGIProcessGroup example2
# Insert the full path to the wsgi.py-file here
WSGIScriptAlias / /var/www/vhosts/example2/example2/wsgi.py process-group=example2
<Directory /var/www/vhosts/example2/>
Require all granted
</Directory>
Alias /static/ /var/www/vhosts/example2/static/
<Directory /var/www/vhosts/example2/static/>
Order deny,allow
Allow from all
</Directory>
Alias /media/ /var/www/vhosts/example2/media/
<Directory /var/www/vhosts/example2/media/>
</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

VirtualHost for django site on dedicated server with multiple web sites

So, i'm trying to run Django web site on my dedicated server which has multiple sites on itself and its folder structure looks like this:
home/user/www/
site1/
site2/
site3/
mydjangosite.com/
site1, site2 etc. are php/html web sites, and of course i need to keep them runing, so i'm wondering how should i configure my httpd.conf because these lines below are not working:
<VirtualHost *:80>
DocumentRoot home/user/www/mydjangosite.com
ServerName mydjangosite
WSGIScriptAlias / home/user/www/mydjangosite.com/testing/wsgi.py
<Directory "home/user/www/mydjangosite.com/testing">
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
EDIT
I've modified my httpd.conf and instead of lines above (with VirtualHost *:80) i've added next:
WSGIScriptAlias / /home/user/www/mydjangosite.com/testing/wsgi.py
WSGIPythonPath /home/user/www/mydjangosite.com
<Directory /home/user/www/mydjangosite.com/testing>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
and finally i've got Django succes page:
It worked!
Congratulations on your first Django-powered page.
But now i get that Django page for every domain that is on my server.
Try this:
<VirtualHost *:80>
DocumentRoot /home/user/www/mydjangosite.com
ServerName mydjangosite
WSGIScriptAlias / /home/user/www/mydjangosite.com/testing/wsgi.py
<Directory "/home/user/www/mydjangosite.com/testing">
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Instead of:
<VirtualHost *:80>
DocumentRoot home/user/www/mydjangosite.com
ServerName mydjangosite
WSGIScriptAlias / home/user/www/mydjangosite.com/testing/wsgi.py
<Directory "home/user/www/mydjangosite.com/testing">
Order allow,deny
Allow from all
</Directory>
</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.