It’s not difficult to run a website without using expensive applications like plesk. It’s really not that difficult to setup a subdomain or site yourself. Here I will show an easy method of setting up subdomains. Though this might not be a perfect way of doing it. At least it easy, and you have the site up and running in 5 minutes.If we do this guide by setup the site for example.tld and one subdomain, admin. The DNS records did I show how to setup in this guide about how to setup DNS in BIND.
We start by setting up the paths to our new domin. It might be a good idea to put new domains in the home directory. Though you are free to put them where you want.
I personally use the home directory, and will base my short guide on doing so.
Start by creating the domain as a folder in the home directory.
# mkdir /home/example.tld
Next give apache owner rights over the folder
# chown apache:apache /home/example.tld
Change the directory to the example.tld.
# cd /home/example.tld
We want to make two directories, one to store our web page to example.tld, and one to store subdomains.
# mkdir htdocs
# mkdir subdomains
# chown apache:apache htdocs
# chown apache:apache subdomains
Since we are going to add one subdomain, we will add our subdomain “admin” to our subdomain folder.
# mkdir subdomains/admin
# chown apache:apache subdomains/admin
And at last create a htdocs folder in the admin subdomain folder.
# mkdir subdomins/admin/htdocs
# chown apache:apache subdomains/admin/htdocs
We are almoust ready now. Put your web page files into the htdocs folders, or just create a dummy index.html with “Hello World” or something. If you get many folders and files and need th change the owner on all you can run this command to change all files and dirs to the apache user and group.
# chown apache:apache -R /home/example.tld
Now we are ready to edit the apache configuration.
Open the configuration file (you need to be root or have sudo privileges)
#vi /etc/httpd/conf/httpd.conf
Go to the end of the configuration file and go into insert mode. Add two virtual hosts in this file at the far end.
First Add the virtual host if it’s not present
NameVirtualHost *:80
Then add the virtual host for the main domain, example.tld
<VirtualHost *:80>
DocumentRoot /home/example.tld/htdocs
ServerName example.tld
<Directory “/home/example.tld/htdocs”>
allow from all
Options +Indexes
</Directory>
</VirtualHost>
Next add the virtual host for the subdomain. Repeat this one for every subdomain you want
<VirtualHost *:80>
DocumentRoot /home/example.tld/subdomains/admin/htdocs
ServerName admin.example.tld
<Directory “/home/example.tld/subdomains/admin/htdocs”>
allow from all
Options +Indexes
</Directory>
Save the configuration and restart apache
# service httpd restart
Finished!











Saturday, 15. November 2008
Hello. I have problem with this and I found your article very helpfull but for me it’s not working…I have website http://www.pets-memories.com and I have migrated to another webserver but they can not configure my server like I want… Please can you help me with configurating my server… I only to configure subdomains folder…That means I have two folders one httpdocs where is the main index.php == http://www.pets-memories.com and the second folder _sub and now I need that if I create by php script there folder _sub/example/ and I upload there index.php it will work like example.pets-memories.com … I don’t know what I am doing wrong but it don’t work for me…Please can you help to configure my apache….Thank You… enti
Saturday, 15. November 2008
Hey,
You remembered to setup the DNS records for your subdomain? Take a look at http://www.varslashlog.com/2008/10/30/how-to-setup-dns-in-bind/
Saturday, 27. March 2010
Just one thing, on my host (Linode), I have to replace the * with the actual IP address as in:
NameVirtualHost 12.34.56.78:80
And
This is a good post. It’s accurate and easy to follow. Thanks.
Saturday, 27. March 2010
It removes one of the lines as it contains the brackets. It should say:
…
NameVirtualHost 12.34.56.78:80
And
<VirtualHost 12.34.56.78:80>
…