Wildcard Subdomain Hosting on Linux

I recently had to set up wildcard hosting on one of my Ubuntu servers. Previously I had only done this on FreeBSD so I had to do a bit of research to find out what the best method would be. It turns out the solution is quite simple and elegant.

Now why would you need a setup like this? Well there are many reasons, perhaps you’ve set up your own free webhosting company where each account gets their own subdomain, or you need to set up a large number of subdomains for your new website but you just can’t be bothered to enter them all by hand.

I will assume you already have your standard LAMP setup, with Bind9 installed. Your first job will be to set up an A record for your wildcard domain. Open up your zone file with your favourite editor, and add the following to the bottom:

*.yourdomain.com.    IN      A       192.168.0.1

Naturally you’ll be replacing both the domain name and IP with ones that are appropriate to your configuration. Also, make sure you specify any other subdomains that you don’t want to handled by the wildcard. Your final zone record should look something like the following:

yourdomain.com. 86400
    IN      SOA     yourdomain.com. hostmaster.yourdomain.com. (
        2009103004
        10800
        3600
        3600000
        86400 )
    IN      NS      ns1.yourdomain.com.
    IN      NS      ns2.yourdomain.com.
    IN      MX      10      mail.yourdomain.com.
    IN      A       192.168.0.1
mail                    IN      A       192.168.0.1
ns1                     IN      A       192.168.0.1
ns2                     IN      A       192.168.1.1
*.example.com.		IN      A       192.168.0.1

Great, now that we have our zone configured we need to enable it by reloading bind9:

sudo /etc/init.d/bind9 reload

Next on the list is configuring your virtual host to handle all of these subdomains. There are many ways you can do this, depending on the result you want. You may want one virtual host for your primary subdomain (www) and one for all your subdomains, or you may want to combine them all into one and handle the subdomains with mod_rewrite. For the purpose of this demonstration I will assume the later.

Open up your existing virtual host definition file or create a new one if needed. These are stored in /etc/apache2/sites-available/ and are usually named the same as the domain. You’re going to be adding a ServerAlias definition, and your file should end up looking something like this:

<VirtualHost *>
    ServerName  yourdomain.com
    ServerAlias *.yourdomain.com
    DocumentRoot /home/yourdomain.com/www
    ....
</VirtualHost>

I’ve slimmed down this example, your virtual host file may have other declarations in it. Once you’ve finished editing this file, enable it if you haven’t done so already. This involves creating a symlink of the configuration file in /etc/apache2/sites-enabled, which can be done very easily by the use of the a2ensite command which is available only in Debian based distributions. After adding the new site, reload Apache.

sudo a2ensite yourdomain.com
sudo /etc/init.d/apache reload

So now you’ve set up your DNS to accept connections on any subdomain, and Apache is set up to direct these connections to a specific folder on your system. Where do we go from here? Once again, that depends on what your trying to accomplish, but I’ll give you a few suggestions.

The simplest way is to setup your index file to check what the subdomain is and generate the output based on that. An example would be switching the database depending on which subdomain is used:

# Determine Subdomain
$domain = $_SERVER['HTTP_HOST'];
$domain_parts = explode(".", $domain);
$subdomain = $domain_parts[0];

# Select Required Database
mysql_connect("localhost", "root", "password");
mysql_select_db($subdomain);

Or if you have mod_rewrite enabled you can modify your .htaccess file and redirect any subdomain’s traffic to a matching folder name in the DocumentRoot:

RewriteEngine on
RewriteCond %{http_host} .
RewriteCond %{http_host} !^www.yourdomain.com [NC]
RewriteCond %{http_host} ^([^.]+)\.yourdomain.com [NC]
RewriteRule ^(.*) http://www.yourdomain.com/%1/ [R=301,L,QSA] 

So if a visitor came to the URL http://example.yourdomain.com they would then be redirected to http://www.yourdomain.com/example/. Beyond that, I’ll let you use your own imagination.

Tags: , , , , ,

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS
read comments

How To Remove the Fortune Message from the Linux Mint Terminal

LinuxMint is a refreshing new take on Ubuntu, for those of you who are getting tired of the “same old thing ” but aren’t quite ready to venture away from a Debian based distro. It has a crisp UI, with a slightly more familiar menu for those of you coming straight from Windows. However, it has some quirks and requires a bit of fine tuning.

The first thing that had to go was the annoying “fortune” message that would display every time you launched a terminal. I have no idea why such a thing would be included in Mint as I can’t see very many people wanting it. Thankfully, removing it is quite simple.

If you just want to disable it, run the following command:

sudo pico /etc/bash.bashrc

And scroll all the way to the bottom, you’ll want to disable or completely remove the line that says:

/usr/bin/mint-fortune

If, like me, you’re running Mint on a Netbook and need to free up as much resource as possible, you can completely remove it from your system by running the following command:

sudo apt-get remove fortune

Just be sure to complete the above step as well or you will get the error bash: /usr/bin/mint-fortune: No such file or directory every time you open your terminal.

Tags: , ,

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS
read comments

How To Make Your Own LCD Screen Cleaner

A while back I bought a screen cleaning kit from Staples. I forget what I paid but I would guess it was about $20. It came with a small spray bottle of a screen cleaning solution and an interesting device that had a brush on one end and on the other end you could attach one of the many microfibre clothes it came with. The kit worked great, but eventually I ran out of cleaner and forgot about it. Recently, after discovering it buried in my laptop bag, I went into Staples to get a replacement bottle. For a small spray bottle containing only a few ounces of screen cleaner they were asking $12.99. For a slightly larger bottle they wanted $19.99. I scoffed at this, knowing that the liquid inside was worth only pennies and had not expected to pay more than $5 for a bottle. So I left empty handed.

I knew there had to be some way to make my own screen cleaner. I also knew, like many others, that you should never use Windex or lens cleaner on an LCD screen as it would eat at the finish. It turns out that the recipe for making your own screen cleaner is quite simple and the ingredients are quite cheap. All you need is:

  • 1 Part Distilled Water
  • 1 Part Isopropyl Alcohol (70%)

Make sure you mix the solution in a well ventilated area, as isopropyl alcohol evaporates very quickly and its fumes are not only toxic but flammable. Also be sure to use distilled water and not mineral water or tap water as they contain dissolved minerals which will result in streaks on your LCD. After mixing your solution, pour it into an empty spray bottle. An empty lens cleaner bottle works well and is small enough to fit in a laptop bag.

When applying the solution to your monitor make sure you power it off first. Also never spray it directly on to the screen, rather spray it onto your cloth and then rub it on to the screen. Make sure to use a soft, clean cloth, and not a paper towel or tissue as they can scratch the screen or leave fluff on it. After cleaning the monitor, wait a few moments for it to dry before turning it back on.

Tags: , , ,

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS
read comments
 Page 29 of 51  « First  ... « 27  28  29  30  31 » ...  Last »