[英]Apache2 Setup Virtual Hosts with Subdomains using Django Projects
I'm using Django 1.4 with Python 2.7 on Ubuntu Server 12.04. 我在Ubuntu Server 12.04上使用Django 1.4和Python 2.7。 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.
我是Apache的新手,在设置虚拟主机时似乎写错了一些东西。
I own 2 domains that I'm trying to host on a single server. 我拥有2个要在单个服务器上托管的域。 Call them
www.my_first_domain.com
and www.my_second_domain.com
. 称它们为
www.my_first_domain.com
和www.my_second_domain.com
。
Now, I have more than 3 Django projects I'm going to be hosting. 现在,我将主持3个以上的Django项目。 One project will be pointed to
www.my_first_domain.com
. 一个项目将指向
www.my_first_domain.com
。 One project will be pointed to www.my_second_domain.com
. 一个项目将指向
www.my_second_domain.com
。 All other projects will be pointed to subdomains of www.my_second_domain.com
. 所有其他项目都将指向
www.my_second_domain.com
子域。
ie project3.my_second_domain.com
, project4.my_second_domain.com
, etc. 即
project3.my_second_domain.com
, project4.my_second_domain.com
等。
I've managed the DNS to have all these point to the correct IP. 我已经管理DNS,使所有这些都指向正确的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. 我已经通过
`host www.my_first_domain.com
, host www.my_second_domain.com
, host project3.my_second_domain.com
等进行了host project3.my_second_domain.com
。它们都指向正确的IP。
Below are 3 examples of the files I've setup to try and get this to work. 以下是我设置的3个文件示例,以尝试使其正常工作。
/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. 如果我访问www.my_first_domain.com,一切看起来都正确。 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).
当我访问www.my_second_domain.com时,我会看到www.my_first_domain.com上的内容(我的第一个项目,而不是第二个项目)。 If I go to project3.my_second_domain.com I get an
Internal Server Error
. 如果我转到project3.my_second_domain.com,则会收到
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. 如果我查看project3的错误日志,它似乎正在尝试加载project1的Django设置。
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. 我有意清空了
/etc/apache2/httpd.conf
因为我的印象是,以我的方式使用虚拟主机时,将使用sites-enabled
文件代替httpd.conf
。
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: 这是我的conf文件的样子:
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: 这是我的.wsgi文件:
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. 确保您已经在
sites-available
获得了confs,并且在每个站点上都运行了一个a2ensite,并且应该很好。
Let me know if you need anything else. 需要帮助请叫我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.