A novice's guide to the wide world of Linux
Adding Virtual Domains — The Easy Way (Ubuntu)
I wrote this script out of my need for a quick method of adding virtual domains. I hope you find this to be of great use.
create a file named addomain
-
sudo vim /usr/bin/adddomain
Place this in your file:
-
#!/bin/bash
-
#Edit the following e-mail address:
-
email="your@email.address"
-
ipaddress=`ifconfig eth0 | grep inet | awk ‘{print $2}’ | sed ‘s/addr://’ | grep .`
-
domain=$1
-
file="/etc/apache2/sites-available/$domain"
-
if [ -z $domain ]; then
-
echo "You did not specify a domain"
-
echo "Usage:"
-
echo " adddomain domain.tdl"
-
echo ""
-
else
-
ping=`ping -c 1 $domain|grep "$ipaddress"`
-
if [ -z $ping ]; then
-
echo "$domain is not hosted on this server, or you have not set up its A record"
-
else
-
touch /etc/apache2/sites-available/$domain;
-
echo "# Place any notes or comments you have here
-
# It will make any customisation easier to understand in the weeks to come
-
# domain: $domain
-
# public: /var/virtualwww/$domain
-
-
<VirtualHost *:80>
-
-
# Admin email, Server Name (domain name) and any aliases
-
ServerAdmin $email
-
ServerName $domain
-
ServerAlias www.$domain
-
-
-
# Index file and Document Root (where the public files are located)
-
DirectoryIndex index.php
-
DocumentRoot /var/virtualwww/$domain
-
-
-
# Custom log file locations
-
LogLevel warn
-
ErrorLog /var/log/apache2/$domain/error.log
-
CustomLog /var/log/apache2/$domain/access.log combined
-
-
</VirtualHost>" > $file;
-
mkdir /var/log/apache2/$domain;
-
mkdir /var/virtualwww/$domain;
-
ln -s /var/www/.htaccess /var/virtualwww/$domain/.htaccess;
-
mkdir /var/virtualwww/$domain/cgi-bin;
-
echo "created $domain";
-
echo "please restart apache2";
-
a2ensite $domain;
-
fi
-
fi
Next, change the permissions of adddomain:
-
sudo chmod 500 /bin/bash/adddomain
Once all that is done, create a new folder to house your virtual domains:
-
sudo mkdir /var/virtualwww
Next, change the adddomain file so it properly points to your .htaccess file (if you have one), and you’re done. To use this script:
-
adddomain domain.tdl
The server will do a lookup on that domain/subdomain, and if it shows that the server you are on does not have an IP on it that matches the (sub)domains IP, it won’t let you add that site.
| Print article | This entry was posted by jonsjava on 06/24/2009 at 8:30 AM, and is filed under How-To(s). Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |














