mardi 8 avril 2014

Apache2 configuration des hôtes virtuels avec sous-domaines à l'aide de projets Django - Stack Overflow


I'm using Django 1.4 with Python 2.7 on Ubuntu Server 12.04. I'm trying to host multiple websites on a single server. I'm new to Apache and seem to have written something incorrectly when setting up virtual hosts.


I own 2 domains that I'm trying to host on a single server. Call them www.my_first_domain.com and www.my_second_domain.com.


Now, I have more than 3 Django projects I'm going to be hosting. One project will be pointed to www.my_first_domain.com. One project will be pointed to www.my_second_domain.com. All other projects will be pointed to subdomains of www.my_second_domain.com.


i.e. project3.my_second_domain.com, project4.my_second_domain.com, etc.


I've managed the DNS to have all these point to the correct IP. I've verified this with `host www.my_first_domain.com, host www.my_second_domain.com, host project3.my_second_domain.com, etc. They all point to the correct IP.


Below are 3 examples of the files I've setup to try and get this to work.


/etc/apache2/sites-enabled/project1


<VirtualHost *:80>
ServerName www.my_first_domain.com
ServerAlias *.my_first_domain.com my_first_domain.com
DocumentRoot /var/www/project1

CustomLog /var/log/apache2/www.my_first_domain.com-access.log combined
ErrorLog /var/log/apache2/www.my_first_domain.com-error.log

WSGIScriptAlias / /home/user1/website/project1/project1/wsgi.py

<Directory /home/user1/website/project1/project1>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>

/etc/apache2/sites-enabled/project2


<VirtualHost *:80>
ServerName www.my_second_domain.com
ServerAlias my_second_domain.com
DocumentRoot /var/www/project2

CustomLog /var/log/apache2/www.my_second_domain.com-access.log combined
ErrorLog /var/log/apache2/www.my_second_domain.com-error.log

WSGIScriptAlias / /home/user2/website/project2/project2/wsgi.py

<Directory /home/user2/website/project2/project2>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>

/etc/apache2/sites-enabled/project3


<VirtualHost *:80>
ServerName project3.my_second_domain.com
DocumentRoot /var/www/project3

CustomLog /var/log/apache2/project3.my_second_domain.com-access.log combined
ErrorLog /var/log/apache2/project3.my_second_domain.com-error.log

WSGIScriptAlias / /home/user3/website/project3/project3/wsgi.py

<Directory /home/user3/website/project3/project3>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>

If I go to www.my_first_domain.com everything looks correct. When I go to www.my_second_domain.com I see what is at www.my_first_domain.com (my first project, not my second project). If I go to project3.my_second_domain.com I get an Internal Server Error.


If I look at the error log for project3 it looks like it's trying to load the Django settings for project1.


I purposefully emptied /etc/apache2/httpd.conf as I was under the impression that the sites-enabled files will be used in place of httpd.conf when using virtual hosts the way I am.


I don't believe I've modified any other configuration files. Any ideas as to what I've done wrong?




Here's what my conf file looks like:


VirtualHost *:80>
ServerName www.domain.com
ServerAlias domain.com *.domain.com
DocumentRoot /var/www/domain

Alias /static/ /var/www/domain/static/

WSGIScriptAlias / /var/www/domain/django.wsgi

RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domain.com$
RewriteRule ^/(.*)$ http://www.domain/invite/%1/$1 [QSA,P]

ErrorLog ${APACHE_LOG_DIR}/domain/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/domain/access.log combined

<Directory /var/www/domain/static>
Order deny,allow
Allow from all
</Directory>
<Directory /var/www/domain/>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

And here's my .wsgi file:


import os
import sys
sys.path.append('/home/ubuntu/django')
sys.path.append('/home/ubuntu/django/domain')

os.environ['DJANGO_SETTINGS_MODULE'] = 'domain.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

I have a number of these files and they are all named by the domain name I'm using for the site. Make sure you've got the confs in sites-available and you run a2ensite on each one and you should be good to go.


Let me know if you need anything else.



I'm using Django 1.4 with Python 2.7 on Ubuntu Server 12.04. I'm trying to host multiple websites on a single server. I'm new to Apache and seem to have written something incorrectly when setting up virtual hosts.


I own 2 domains that I'm trying to host on a single server. Call them www.my_first_domain.com and www.my_second_domain.com.


Now, I have more than 3 Django projects I'm going to be hosting. One project will be pointed to www.my_first_domain.com. One project will be pointed to www.my_second_domain.com. All other projects will be pointed to subdomains of www.my_second_domain.com.


i.e. project3.my_second_domain.com, project4.my_second_domain.com, etc.


I've managed the DNS to have all these point to the correct IP. I've verified this with `host www.my_first_domain.com, host www.my_second_domain.com, host project3.my_second_domain.com, etc. They all point to the correct IP.


Below are 3 examples of the files I've setup to try and get this to work.


/etc/apache2/sites-enabled/project1


<VirtualHost *:80>
ServerName www.my_first_domain.com
ServerAlias *.my_first_domain.com my_first_domain.com
DocumentRoot /var/www/project1

CustomLog /var/log/apache2/www.my_first_domain.com-access.log combined
ErrorLog /var/log/apache2/www.my_first_domain.com-error.log

WSGIScriptAlias / /home/user1/website/project1/project1/wsgi.py

<Directory /home/user1/website/project1/project1>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>

/etc/apache2/sites-enabled/project2


<VirtualHost *:80>
ServerName www.my_second_domain.com
ServerAlias my_second_domain.com
DocumentRoot /var/www/project2

CustomLog /var/log/apache2/www.my_second_domain.com-access.log combined
ErrorLog /var/log/apache2/www.my_second_domain.com-error.log

WSGIScriptAlias / /home/user2/website/project2/project2/wsgi.py

<Directory /home/user2/website/project2/project2>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>

/etc/apache2/sites-enabled/project3


<VirtualHost *:80>
ServerName project3.my_second_domain.com
DocumentRoot /var/www/project3

CustomLog /var/log/apache2/project3.my_second_domain.com-access.log combined
ErrorLog /var/log/apache2/project3.my_second_domain.com-error.log

WSGIScriptAlias / /home/user3/website/project3/project3/wsgi.py

<Directory /home/user3/website/project3/project3>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>

If I go to www.my_first_domain.com everything looks correct. When I go to www.my_second_domain.com I see what is at www.my_first_domain.com (my first project, not my second project). If I go to project3.my_second_domain.com I get an Internal Server Error.


If I look at the error log for project3 it looks like it's trying to load the Django settings for project1.


I purposefully emptied /etc/apache2/httpd.conf as I was under the impression that the sites-enabled files will be used in place of httpd.conf when using virtual hosts the way I am.


I don't believe I've modified any other configuration files. Any ideas as to what I've done wrong?



Here's what my conf file looks like:


VirtualHost *:80>
ServerName www.domain.com
ServerAlias domain.com *.domain.com
DocumentRoot /var/www/domain

Alias /static/ /var/www/domain/static/

WSGIScriptAlias / /var/www/domain/django.wsgi

RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domain.com$
RewriteRule ^/(.*)$ http://www.domain/invite/%1/$1 [QSA,P]

ErrorLog ${APACHE_LOG_DIR}/domain/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/domain/access.log combined

<Directory /var/www/domain/static>
Order deny,allow
Allow from all
</Directory>
<Directory /var/www/domain/>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

And here's my .wsgi file:


import os
import sys
sys.path.append('/home/ubuntu/django')
sys.path.append('/home/ubuntu/django/domain')

os.environ['DJANGO_SETTINGS_MODULE'] = 'domain.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

I have a number of these files and they are all named by the domain name I'm using for the site. Make sure you've got the confs in sites-available and you run a2ensite on each one and you should be good to go.


Let me know if you need anything else.


0 commentaires:

Enregistrer un commentaire