1

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?

5
  • Two comments: 1) you ran a2ensite on each of your configurations, correct? 2) Make sure your Python paths are correct in your wsgi.py scripts. Hope that helps. EDIT: Make sure your confs are in sites-available and then run a2ensite don't just create the files in sites-enabled. Commented Nov 28, 2012 at 20:28
  • @themanatuf I did use a2ensite then edited them to have the virtual host info. I've also double checked the Python paths in the wsgi.py scripts - sys.path.append('/home/user3/website/project3'), etc. Also, the conf files are also in sites-available as they are just symbolic links. Commented Nov 28, 2012 at 20:34
  • I don't have access to my server right this second, but I thought the symbolic links were under sites-enabled and the raw files were under sites-available. I've done exactly what you're asking without any issues, so I'll double check my server when I get home. In the meantime, double check my thoughts on the raw files supposed to be in sites-available. Commented Nov 28, 2012 at 20:35
  • Yes, you are right that the sites-available are where the raw files are...but if I edit either "file" it will change in both places since sites-enabled are symbolic links. So, it shouldn't matter. Unless I'm missing something. Commented Nov 28, 2012 at 22:57
  • It does matter, I'd remove the symbolic links and move the raw files under sites-available. Then run a2ensite. See my solution below for copies of my conf files as well as my wsgi files. Commented Nov 29, 2012 at 1:07

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help, but this just isn't working for me. I see no change. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.