How to configure sub-domain in Linux?

If you want to set up multiple sub-domain on your web server then you need to change the configuration file of Apache. Below are an example for multiple sub-domain settings. Its tested on ubuntu 12.04/12.10/13.04/13.10 and 14.04 web server. You need to follow the below step by step guide to set-up the multiple sub-domain. I am setting up two sub-domain like delhi.prafulkr.in and another one is mumbai.prafulkr.in in below code.

  • First, You need to edit the ports.conf file by executing below command
    sudo vim /etc/apache2/ports.conf
  • Second, then change the below code as per you domain and sub-domain and add it into your configuration file.

    <-VirtualHost *:80->
    ServerName delhi.prafulkr.in
    ServerAlias delhi.prafulkr.in
    DocumentRoot /var/www/baseCodeFolder/

    <-Directory /var/www/baseCodeFolder/->
    Options Indexes FollowSymLinks
    AllowOverride all
    Order allow,deny
    Allow from all
    FileETag MTime Size
    SetOutputFilter DEFLATE
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4.0[678] no-gzip
    BrowserMatch bMSIE !no-gzip !gzip-only-text/html

    ErrorLog /var/www/logs/errorbaseCodedelhi.log
    <-/VirtualHost>

    <-VirtualHost *:80->
    ServerName mumbai.prafulkr.in
    ServerAlias mumbai.prafulkr.in
    DocumentRoot /var/www/baseCodeFolder/

    <-Directory /var/www/baseCodeFolder/->
    Options Indexes FollowSymLinks
    AllowOverride all
    Order allow,deny
    Allow from all
    FileETag MTime Size
    SetOutputFilter DEFLATE
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4.0[678] no-gzip
    BrowserMatch bMSIE !no-gzip !gzip-only-text/html

    ErrorLog /var/www/logs/errorbaseCodemumbai.log
    <-/VirtualHost>

  • Save the conf file and exit.
    if you are using vim or vi press “esc” and then “:wq”
  • Restart your web server using below command

    sudo /etc/init.d/apache2 restart

  • Now test you new sub domain, its ready for your use.

Note: please remove “-” from “VirtualHost” as well as “Directory”.

Posted in Linux | Tagged , , , , | Comments Off

Comments are closed.