0

Currently I'm hosting my own (ubuntu) server with the following services: samba, ftp and a webserver. I've bought a domain and linked the DNS A-record to my ISP's IP. This is working correctly. Now I'd like to use the DNS wildcard-record to create subdomains. I want to avoid waiting 24hrs before the DNS change completes.

Thusfar I'm only able to redirect all incoming wildcards to the same directory:

test1.domain.com redirects to /var/www

test2.domain.com redirects to /var/www

Although I'd like to get:

test1.domain.com redirects to /var/www/test1

test2.domain.com redirects to /var/www/test2

My guess would be to change the file /etc/apache2/sites-available/domain.

Any help or tips would be welcome!

Thanks,

Mark

EDIT:

This is what my /etc/apache2/sites-available/domain file looks like:

<VirtualHost *:80>
        ServerAdmin webmaster@domain

        DocumentRoot /var/www/webroot
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/webroot>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

2 Answers 2

2

You should be able to get the desired behavior with VirtualDocumentRoot.

Within your <VirtualHost>, add a ServerAlias to catch all the domains you're interested in:

    ServerAlias *.example.com

...then map them to the desired directories. Remove your DocumentRoot, and in its place, add:

    VirtualDocumentRoot /var/www/%1

You'll want to have a <Directory /var/www/> block allowing access, and keep in mind that this vhost should only be handling service for your dynamically configured vhosts - if you want example.com and www.example.com to be handled separately, then you'll want them to have their own <VirtualHost>.

Edit:

You'll want to use a different vhost to handle the "base" domains. Building off of the current config in the comment:

NameVirtualHost *:80
<VirtualHost *:80>
    ServerName catchall.example.com
    ServerAlias *.example.com
    # NOTE: this config will map test1.example.com to /var/www/test1
    VirtualDocumentRoot /var/www/%1
    # If you want that to map instead to /var/www/test1.example.com, then use %0:
    # VirtualDocumentRoot /var/www/%0
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Order Allow,Deny
        Allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
# This next vhost should be in a different file in sites-available to
# fit with debian-style vhosts - but make sure it's alphabetically
# after the file that contains the first vhost - we want it to load first
# so that it's default.  It can also just go in the same file.
<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot /var/www/www.example.com
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Order Allow,Deny
        Allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
6
  • Thanks for your respond! I've been able to configure apache2 VirtualHost with the given lines. Although it ain't working like it should. Whenever browsing to www.example.com or example.com leads to seperately directories, which i don't want - they need to link to the same directory. Basically, I don't get your following line: "You'll want to have a <Directory /var/www/> block allowing access, and keep in mind that this vhost should only be handling service for your dynamically configured vhosts". Could you give me a last hand with this? Thanks!
    – Velth
    Aug 30, 2012 at 7:02
  • This is how my /etc/apache2/sites-available/domain file looks like: snipt.org/vDjj2
    – Velth
    Aug 30, 2012 at 7:18
  • Can you clarify how you're looking to configure the www.example.com and example.com domains? They're not the same domain, so having them serve the same content doesn't fit with having the dynamic virtual docroot determine the directory automatically. Do you want them to be a special case where they serve the same content, or do you want one to redirect to the other? Aug 30, 2012 at 7:23
  • I'd like to achieve subdomains so I can avoid creating DNS A-records for every subdomain, therefor I'd like to use the wildcard DNS record. Both www.example.com and example.com should point to the same directory: /var/www/www.example.com. For subdomains it would be: sub.example.com whereas the directory: /var/www/sub.example.com.
    – Velth
    Aug 30, 2012 at 7:33
  • @MarkvanVelthoven Edited my answer with info on how to make that happen. Aug 30, 2012 at 7:44
-1

Thanks for helping in finding the answer. Here I got solution for my problem, I think this will help another beginers like me in finding solution for the above mentioned problem.

step1: configure your website as given below

vi /etc/apache2/sites-available/yoursite

<VirtualHost *:80> ServerAlias localhost *.yoursite #wildcard catch all VirtualDocumentRoot /path/to/your/workspace/%1/public UseCanonicalName Off <Directory "path/to/your/workspace"> Options FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost>

2)sudo a2ensite /etc/apache2/sites-available/yoursite

3) sudo service apache2 reload

4)Install Dnsmasq: sudo apt-get install dnsmasq

5) open up /etc/NetworkManager/NetworkManager.conf and comment out the line that reads dns=dnsmasq. Restart NetworkManager afterwards: sudo restart network-manager.

6)vi /etc/dnsmasq.conf and the line listen-address=127.0.0.1.

7)Create a new file in /etc/dnsmasq.d and open the file and edit it as follow

address=/yourdomain/127.0.0.1

8)Restart Dnsmasq: sudo /etc/init.d/dnsmasq restart.

This can also done in nginx also. and please note this solution has worked for me. I guess this will work for all others too.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .