Apache vhosting ubuntu one named entry serving all urls - django

I have 2 URL's and two django webapps to serve. Using the following example configuration, a get request for www.server2.com results in the page for www.server1.com.
Example Incoming URLS: ( verified in apache logs)
www.server1.com
www.server2.com
vhost confs:
* server1
<VirtualHost *:80>
ServerName www.server1.com
ErrorLog /.../apache/error.log
AliasMatch ^/([^/]*\.css) /opt/ef/static/css/$1
<Directory "/.../apache">
Allow from all
</Directory>
Alias /media/ /...f/media/
Alias /static/ /...f/static/
<Directory /.../static>
Order deny,allow
Allow from all
</Directory>
<Directory /../media>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
WSGIScriptAlias / /...apache/server.wsgi
**server2
<VirtualHost *:80>
ServerName www.server2.com
ErrorLog /.../apache/error.log
AliasMatch ^/([^/]*\.css) /opt/ef/static/css/$1
<Directory "/.../apache">
Allow from all
</Directory>
Alias /media/ /...f/media/
Alias /static/ /...f/static/
<Directory /.../static>
Order deny,allow
Allow from all
</Directory>
<Directory /../media>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
WSGIScriptAlias / /...apache/server.wsgi
Am I matching incorrectly?

Related

Need help configuring apache .conf file

I want to deploy my django app on a Apache 2.4 server. The same server will host static files. The thing is that this server hosts other php based web sites.
In order for all this to work I just need to install mod_wsgi and configure apache's .conf file related to this web site, is that right?
After reading few articles I came up with this config, assuming that the web site will be in the var/www/ folder :
<VirtualHost *:80>
ServerName example.com
# Alias /events /var/www/events/html
ServerAdmin webmaster#localhost
DocumentRoot /var/www/example
Alias /media/ /var/www/example/media/
Alias /static/ /var/www/example/static/
<Directory /var/www/example/static>
Order deny,allow
Require all granted
</Directory>
<Directory /path/to/example/media>
Order deny,allow
Require all granted
</Directory>
WSGIScriptAlias / /var/www/example/events_promgruz/wsgi.py
WSGIDaemonProcess example.com python-path=/var/www/example:/opt/envs/lib/python3.6/site-packages
WSGIProcessGroup example.com
<Directory /path/to/example/example>
<Files wsgi.py>
Order allow,deny
Require all granted
</Files>
</Directory>
LogLevel debug
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
What would you suggested to change or add to config?
Is there some other steps to ensure that django app will work and that it doesn't interfere other non wsgi apps?
This is what I ended up using:
<VirtualHost *:80>
ServerName expample-domen.com
ServerAdmin webmaster#localhost
Alias /static /var/www/example/static
Alias /media /var/www/example/media
<Directory /var/www/example/static>
Require all granted
</Directory>
<Directory /var/www/example/media>
Order deny,allow
Require all granted
</Directory>
<Directory /var/www/example/example>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess example python-home=/path/to/virtualEnv python-path=/var/www/example
WSGIProcessGroup example
WSGIScriptAlias / /var/www/example/example/wsgi.py
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Multiple django projects with different domains on one server

I am trying to run two different django projects one the same server, with two different domains. Django version is 1.8, and the webserver is Apache 2.4.7.
These are my conf files in sites-available:
#example.conf
<VirtualHost *:80>
ServerName example.com
ServerAlias example.com
ServerAdmin admin#example.com
WSGIDaemonProcess example.com python-path=/path/example/WebPortal:/path/example/venv/lib/python3.4/site-packages
WSGIProcessGroup example.com
WSGIPassAuthorization On
WSGIScriptAlias / /path/example/WebPortal/WebPortal/wsgi.py
WSGIApplicationGroup %{GLOBAL}
<Directory /path/example/WebPortal/WebPortal>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /robots.txt /path/example/WebPortal/static/robots.txt
Alias /favicon.ico /path/example/WebPortal/static/favicon.ico
Alias /media/ /path/example/WebPortal/media/
Alias /static/ /path/example/WebPortal/static/
<Directory /path/example/WebPortal/static>
Require all granted
Options -Indexes
</Directory>
<Directory /path/example/WebPortal/media>
Require all granted
Options -Indexes
</Directory>
ErrorLog "/path/example/WebPortal/logs/webportal-error.log"
CustomLog "/path/example/WebPortal/logs/webportal-access.log" combined
CustomLog "/path/example/WebPortal/logs/webportal-bandwidth.log" common
and:
#sub.example2.conf
<VirtualHost *:80>
ServerName sub.example2.com
ServerAlias sub.example2.com
ServerAdmin admin#example2.com
WSGIDaemonProcess sub.example2.com python-path=/path/sub_example2/WebPortal:/path/sub_example2/venv/lib/python3.4/site-packages
WSGIProcessGroup sub.example2.com
WSGIPassAuthorization On
WSGIScriptAlias / /path/sub_example2/WebPortal/WebPortal/wsgi.py
WSGIApplicationGroup %{GLOBAL}
<Directory /path/sub_example2/WebPortal/WebPortal>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /robots.txt /path/sub_example2/WebPortal/static/robots.txt
Alias /favicon.ico /path/sub_example2/WebPortal/static/favicon.ico
Alias /media/ /path/sub_example2/WebPortal/media/
Alias /static/ /path/sub_example2/WebPortal/static/
<Directory /path/sub_example2/WebPortal/static>
Require all granted
Options -Indexes
</Directory>
<Directory /path/sub_example2/WebPortal/media>
Require all granted
Options -Indexes
</Directory>
ErrorLog "/path/sub_example2/WebPortal/logs/webportal-error.log"
CustomLog "/path/sub_example2/WebPortal/logs/webportal-access.log" combined
CustomLog "/path/sub_example2/WebPortal/logs/webportal-bandwidth.log" common
With this configuration when I enable both, only one domain work properly...on the other one I get a Bad Request (400).
I have tried many options but no one work.
Any ideas?

run multiple django project in different virtualenv on apache

I want to run two different django_projects each in different virtualenv.
This is the code :
ServerName ubuntu_server_apache
<VirtualHost *:80>
ServerName dev.hexxie.com
ErrorLog "/home/ashish/deployments/mysite_dev/conf/mysite_dev_error.log"
WSGIScriptAlias / /home/ashish/deployments/mysite_dev/mysite/mysite/wsgi.py
Alias /static /home/ashish/deployments/mysite_dev/static_root
<Directory /home/ashish/deployments/mysite_dev/static_root>
Require all granted
</Directory>
Alias /media /home/ashish/deployments/mysite_prod/data/media
<Directory /home/ashish/deployments/mysite_prod/data/media>
Require all granted
</Directory>
<Directory /home/ashish/deployments/mysite_dev/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
WSGIPythonPath /home/ashish/deployments/mysite_dev/mysite:/home/ashish/.virtualenvs/mysite_dev/lib/python2.7/site-packages
<VirtualHost *:80>
ServerName hexxie.com
ServerAlias *.hexxie.com
ErrorLog "/home/ashish/deployments/mysite_prod/conf/mysite_error.log"
WSGIScriptAlias / /home/ashish/deployments/mysite_prod/mysite/mysite/wsgi.py
Alias /static /home/ashish/deployments/mysite_prod/static_root
<Directory /home/ashish/deployments/mysite_prod/static_root>
Require all granted
</Directory>
Alias /media /home/ashish/deployments/mysite_prod/data/media
<Directory /home/ashish/deployments/mysite_prod/data/media>
Require all granted
</Directory>
<Directory /home/ashish/deployments/mysite_prod/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
WSGIPythonPath /home/ashish/deployments/mysite_prod/mysite:/home/ashish/.virtualenvs/mysite_prod/lib/python2.7/site-packages
But I am getting internal server error using this apache conf. I feel that this is due to WSGIPythonPath used twice in conf. WSGIPythonPath can't be included inside virtualhost. So how to run two diff django project each on diff virtualenv on apache ?
For a start, use a daemon process group so each runs in a separate process and then use the python-home option on the respective WSGIDaemonProcess group directives. See:
http://blog.dscpl.com.au/2012/10/why-are-you-using-embedded-mode-of.html
http://blog.dscpl.com.au/2014/09/using-python-virtual-environments-with.html
Here's an example of how I do it. You'll have to change paths / project names based on your setup, naturally (example is SSL on port 443, but works on port 80 as well):
LoadModule wsgi_module modules/mod_wsgi.so
LoadModule ssl_module modules/mod_ssl.so
WSGISocketPrefix /var/run/wsgi
NameVirtualHost *:443
Listen 443
<VirtualHost *:443>
ServerName your.server.com
ErrorLog /home/user/apache_errors.log
WSGIDaemonProcess project1-https python-home=/home/user/.virtualenvs/project1
WSGIScriptAlias /project1 /var/www/html/project1/project1/wsgi.py process-group=project1-https application-group=project1-https
WSGIProcessGroup project1-https
Alias /project1/static/ /var/www/html/project1/static/
WSGIDaemonProcess project2-https python-home=/home/user/.virtualenvs/project2
WSGIScriptAlias /project2 /var/www/html/project2/project2/wsgi.py process-group=project2-https application-group=project2-https
WSGIProcessGroup project2-https
Alias /project2/static/ /var/www/html/project2/static/
</VirtualHost>
The virtualenv takes care of the Python executable and path when configured in this manner.

Django to pass through some urls to base apache

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>

problem using WSGIApplicationGroup %{GLOBAL} in apache configuration

im using django with apache and mod_wsgi
i am facing a problem when i use WSGIApplicationGroup %{GLOBAL} in apache configuration file (.conf) . i dont know if i am using this directive correctly or i need to use it in another way , the problem is that i needed to add this directive to fix a problem for xapian as described in this ticket (http://trac.xapian.org/ticket/185) after that the search started to work but all my sites contents got mixed up, meaning site1 content appears on site2.when i removed WSGIApplicationGroup %{GLOBAL} , sites are rendering properly again but search stopped working.
here is my .conf file contents:
NameVirtualHost my_ip_address:80
WSGIApplicationGroup %{GLOBAL}
<VirtualHost my_ip_address:80>
ServerName www.site1.com
ServerAlias site1
WSGIScriptAlias / "/home/sa/www/site1/apache/django.wsgi"
<Directory "/home/sa/www/site1/apache">
Allow from all
</Directory>
Alias /site_media/ "/home/sa/www/site1/media/"
<Directory "/home/sa/www/site1/media/">
Order allow,deny
Options Indexes FollowSymLinks
Allow from all
IndexOptions FancyIndexing
</Directory>
</VirtualHost>
WSGIApplicationGroup %{GLOBAL}
<VirtualHost my_ip_address:80>
ServerName www.site2.com
ServerAlias site2
WSGIScriptAlias / "/home/sa/www/site2/apache/django.wsgi"
<Directory "/home/sa/www/site2/apache">
Allow from all
</Directory>
Alias /site_media/ "/home/sa/www/site2/media/"
<Directory "/home/sa/www/site2/media/">
Order allow,deny
Options Indexes FollowSymLinks
Allow from all
IndexOptions FancyIndexing
</Directory>
Alias /media/ "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/"
<Directory "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/">
Order allow,deny
Options Indexes FollowSymLinks
Allow from all
IndexOptions FancyIndexing
</Directory>
</VirtualHost>
WSGIApplicationGroup %{GLOBAL}
< VirtualHost my_ip_address:80 >
ServerName www.site3.com
ServerAlias site3
WSGIScriptAlias / "/home/sa/www/site3/apache/django.wsgi"
<Directory "/home/sa/www/site3/apache">
Allow from all
</Directory>
Alias /site_media/ "/home/sa/www/site3/media/"
<Directory "/home/sa/www/site3/media/">
Order allow,deny
Options Indexes FollowSymLinks
Allow from all
IndexOptions FancyIndexing
</Directory>
Alias /media/ "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/"
<Directory "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/">
Order allow,deny
Options Indexes FollowSymLinks
Allow from all
IndexOptions FancyIndexing
</Directory>
</VirtualHost>
WSGIApplicationGroup %{GLOBAL}
<VirtualHost my_ip_address:80>
ServerName www.site4.com
ServerAlias site4
WSGIScriptAlias / "/home/sa/www/site4/apache/django.wsgi"
<Directory "/home/sa/www/site4/apache">
Allow from all
</Directory>
Alias /site_media/ "/home/sa/www/site4/media/"
<Directory "/home/sa/www/site4/media/">
Order allow,deny
Options Indexes FollowSymLinks
Allow from all
IndexOptions FancyIndexing
</Directory>
Alias /media/ "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/"
<Directory "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/">
Order allow,deny
Options Indexes FollowSymLinks
Allow from all
IndexOptions FancyIndexing
</Directory>
</VirtualHost>
WSGIApplicationGroup %{GLOBAL}
<VirtualHost my_ip_address:80>
ServerName www.site5.com
ServerAlias site5
WSGIScriptAlias / "/home/sa/www/site5/apache/django.wsgi"
<Directory "/home/sa/www/site5/apache">
Allow from all
</Directory>
Alias /site_media/ "/home/sa/www/site5/media/"
<Directory "/home/sa/www/site5/media/">
Order allow,deny
Options Indexes FollowSymLinks
Allow from all
IndexOptions FancyIndexing
</Directory>
Alias /media/ "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/"
<Directory "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/">
Order allow,deny
Options Indexes FollowSymLinks
Allow from all
IndexOptions FancyIndexing
</Directory>
</VirtualHost>
has anybody faced this issue
any suggestions
thanks
Django's implementation prevents multiple Django instances running in same interpreter (application group). Thus if running multiple Django sites on same Apache server and must set WSGIApplicationGroup to %{GLOBAL}, then you MUST use daemon mode and delegate each Django site to a separate daemon process group. Daemon mode is preferred anyway.
Ensure you read:
http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide
http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode
The latter explaining part why daemon mode is good as far as making code reloading easier.