[Apache] Setting up a simple virtual host

Discussion in 'Apache' started by shareurlinks, Sep 17, 2010.

  1. #1
    IMPORTANT: Take backups of all the files you edit during this tutorial, follow the steps in this tutorial at your own risk!
    This tutorial will go through a very simple way of setting up virtual hosts in apache that is very easy and quick to do..

    Step 1 – Edit the default virtual host
    First of all you need to edit the default virtual host to tell apache to look look for the domains…
    Make a backup of the default virtual host file first!
    The default virtual host can be found on Ubuntu in the folder:

    /etc/apache2/sites-available/
    Code (markup):
    Replace the code in the default file for this:
    (Replace the variables in {THESE} to reflect your server configuration)

        <Directory />
        Options FollowSymLinks
        AllowOverride None
        </Directory>
        <Directory /domains/*/*/*/web/public_html>
        AllowOverride All
        Options IncludesNoExec ExecCGI
        </Directory>
        <Directory /home/*/*/*/web/public_html>
        AllowOverride All
        Options IncludesNoExec ExecCGI
        </Directory>
        <Directory /domains/*/*/*/web/public_html/_vti_bin>
        AddHandler application/x-suphp-cgi .exe
        RLimitCPU 40 40
        </Directory>
        <VirtualHost {SERVER IP}:80>
        ServerName {SERVER NAME}
        ErrorLog /var/log/apache2/error_log
        CustomLog /var/log/apache2/sites_access_log ald
        RewriteEngine On
        RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
        RewriteRule ^(.*)$ http://%1$1 [R=301,L]
        VirtualDocumentRoot /domains/%1.1/%1.2/%0/web/public_html
        # VirtualDocumentRoot only sets the effective document root for each site;
        # it doesn’t update the DOCUMENT_ROOT environment variable to reflect that
        # (and Apache developers are adamant this is the correct behaviour).  Also
        # it isn’t possible to set DOCUMENT_ROOT manually, so instead set
        # VIRTUAL_DOCUMENT_ROOT to what we want DOCUMENT_ROOT to be, then use
        # suphp-docroot to intercept calls to suPHP and move the value of
        # VIRTUAL_DOCUMENT_ROOT into DOCUMENT_ROOT:
        RewriteEngine on
        RewriteCond %{LA-U:SCRIPT_FILENAME} ^/domains/(.*)/(.*)/(.*)/public_html/[^/]+
        RewriteRule ^ – [E=VIRTUAL_DOCUMENT_ROOT:/domains/%1/%2/%3/public_html/]
        </VirtualHost>
        RewriteLock “/tmp/rewrite.lock”
        <VirtualHost *:81>
        ErrorLog /var/log/apache2/error_log
        CustomLog /var/log/apache2/sites_access_log ald
        RewriteEngine on
        RewriteLogLevel 0
        RewriteRule ^/((.)(.)[^/]*)(.*) /home/$2/$3/$1/web/public_html/$4
        </VirtualHost>
    
    Code (markup):
    Step 2: Create the folders for the domains.
    Run these commands in shell to create the directories required for the domains:

        cd /
        mkdir domains
        cd /domains
        mkdir -p a b c d e f g h i j k l m n o p q r s t u v w x y z
        mkdir /domains/tmp
        cd /domains/tmp
        mkdir -p a b c d e f g h i j k l m n o p q r s t u v w x y z
        cp -R /domains/tmp/* /domains/a
        cp -R /domains/tmp/* /domains/b
        cp -R /domains/tmp/* /domains/c
        cp -R /domains/tmp/* /domains/d
        cp -R /domains/tmp/* /domains/e
        cp -R /domains/tmp/* /domains/f
        cp -R /domains/tmp/* /domains/g
        cp -R /domains/tmp/* /domains/h
        cp -R /domains/tmp/* /domains/i
        cp -R /domains/tmp/* /domains/j
        cp -R /domains/tmp/* /domains/k
        cp -R /domains/tmp/* /domains/l
        cp -R /domains/tmp/* /domains/m
        cp -R /domains/tmp/* /domains/n
        cp -R /domains/tmp/* /domains/o
        cp -R /domains/tmp/* /domains/p
        cp -R /domains/tmp/* /domains/q
        cp -R /domains/tmp/* /domains/r
        cp -R /domains/tmp/* /domains/s
        cp -R /domains/tmp/* /domains/t
        cp -R /domains/tmp/* /domains/u
        cp -R /domains/tmp/* /domains/v
        cp -R /domains/tmp/* /domains/w
        cp -R /domains/tmp/* /domains/x
        cp -R /domains/tmp/* /domains/y
        cp -R /domains/tmp/* /domains/z
        cp -R /domains/tmp/* /home/
        cp -R /domains/tmp/* /home/a
        cp -R /domains/tmp/* /home/b
        cp -R /domains/tmp/* /home/c
        cp -R /domains/tmp/* /home/d
        cp -R /domains/tmp/* /home/e
        cp -R /domains/tmp/* /home/f
        cp -R /domains/tmp/* /home/g
        cp -R /domains/tmp/* /home/h
        cp -R /domains/tmp/* /home/i
        cp -R /domains/tmp/* /home/j
        cp -R /domains/tmp/* /home/k
        cp -R /domains/tmp/* /home/l
        cp -R /domains/tmp/* /home/m
        cp -R /domains/tmp/* /home/n
        cp -R /domains/tmp/* /home/o
        cp -R /domains/tmp/* /home/p
        cp -R /domains/tmp/* /home/q
        cp -R /domains/tmp/* /home/r
        cp -R /domains/tmp/* /home/s
        cp -R /domains/tmp/* /home/t
        cp -R /domains/tmp/* /home/u
        cp -R /domains/tmp/* /home/v
        cp -R /domains/tmp/* /home/w
        cp -R /domains/tmp/* /home/x
        cp -R /domains/tmp/* /home/y
        cp -R /domains/tmp/* /home/z
        rmdir /domains/tmp/*
        rmdir /domains/tmp/
    
    Code (markup):
    Step 3 – Restart Apache
    Restart apache using this command in ubuntu:

    /etc/init.d/apache2 restart
    Code (markup):
    Step 4 – Set up a domain
    To set up a domain in this configuration, you need to make a directory in the home folder and a symbolic link in the domains folder to it as I will show you below:
    If you wanted to use the domain example.com:

        mkdir /home/e/x/example
        mkdir /home/e/x/example/web
        mkdir /home/e/x/example/web/public_html
        ln -s /home/e/x/example /domains/e/x/example.com
    
    Code (markup):
    You can now place the website files in the public_html folder where the domain is pointed to.
    If you don’t want to do this every time you want to add a domain, you could use the php script that is attached to this post and upload it to a passworded folder on the server to create more domains quickly and easily.
    © http://www.mywebforums.net
    04blatca
    haynes11
    shareurlinks


    You may not copy this tutorial without my permission.
    Please let me know by replying below if you find anything wrong with this tutorial, Thanks

    For more tutorials visit: http://www.mywebforums.net
     
    shareurlinks, Sep 17, 2010 IP