Unix shell scripts to set up vhosts

Discussion in 'General Scripting Chat' started by BlackBeard, May 30, 2009.

  1. BlackBeard

    BlackBeard Newbie

    Joined:
    May 29, 2009
    Messages:
    26
    Likes Received:
    9
    I am guessing that probably most of you use cpanel or webmin, but I'm guessing that some work on the command line. Just wanted to share some shell script I wrote for setting up a vhost fast.

    Make a directory called /var/www/logs and chown it to user www-root or whatever apache runs as like
    # mkdir /var/www/logs
    # chown www-data /var/www/logs

    this script assumes you host in /var/www with a directory for each site

    First run domainsetup.sh which sets up the directory structure and stuff

    domainsetup.sh - after you create this, chmod it to 755 so it becomes executable.
    usage: ./domainsetup.sh domainname.com
    PHP:
    export dom=$1

    mkdir 
    /var/www/$dom
    mkdir 
    /var/www/$dom/ads
    mkdir 
    /var/www/$dom/images

    echo "<html>$dom is up</html>" > /var/www/$dom/index.html

    sed 
    -"s/DOMAIN/$dom/" htaccess-template > /var/www/$dom/.htaccess
    sed 
    -"s/DOMAIN/$dom/" htaccess-images-template > /var/www/$dom/images/.htaccess
    It assumes that you host in /var/www/ and will create a directory /var/www/domainname.com and an images directory, then copy in .htaccess files (below). The /image .htaccess file is set up to prevent image hotlinking by bandwidth thieves. /ads doesn't have it, it's where I would put banner ads or other things I want people to hotlink.

    The two htaccess file templates follow - name them as indicated and put them in the same dir as the script above

    htacceess-template
    PHP:
    # ESSENTIALS
    RewriteEngine on
    ServerSignature Off
    Options All 
    -Indexes
    Options 
    +FollowSymLinks

    # FILTER REQUEST METHODS
    <IfModule mod_rewrite.c>
     
    RewriteCond %{REQUEST_METHOD} ^(TRACE|DELETE|TRACK) [NC]
     
    RewriteRule ^(.*)$ - [F,L]
    </
    IfModule>

    # BLACKLIST CANDIDATES
    <Limit GET POST PUT>
     
    Order Allow,Deny
     Allow from all
     Deny from 24.19.202.10    
    "# attacker"
    </Limit>

    # Disable display of public PHP error messages
    php_flag display_errors "off"

    # Log all PHP errors to a file in private directory
    # here you'd need to create the directory and the file then make sure the file has world write
    # permissions
    php_flag error_log "/var/www/logs/phperrors.DOMAIN.log"

    # Don't keep reporting the same error again and again (keep log file smaller)
    php_flag ignore_repeated_errors On
    htaccess-images-template
    PHP:
    RewriteEngine on
    RewriteCond 
    %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?DOMAIN [NC]
    RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
    And finally apache.sh, which adds the vhost to apache and restarts apache

    if you don't already use vhosts.conf do the following
    # touch /etc/apache2/sites-available/vhosts.conf
    # ln -s /etc/apache2/sites-available/vhosts.conf /etc/apache2/sites-enabled/vhosts.conf

    and again after creating apache.sh do like
    chmod 755 apache.sh

    apache.sh
    usage: ./apache.sh domain.com
    PHP:
    export dom=$1
    export v
    =/etc/apache2/sites-available/vhosts.conf
    echo >> $v
    echo "  <VirtualHost *:80>" >> $v 
    echo "    ServerName $dom>> $v
    echo "    ServerAlias www.$dom>> $v
    echo "    DocumentRoot /var/www/$dom>> $v
    echo  " </VirtualHost>" >> $v

    /etc/init.d/apache2 restart
    if you want to get fancy you could make a text file of all the new domains you just bought like domains.txt
    PHP:
    v14g4r4.com
    warez
    -r-us.com
    dumb
    -seo-words-with-dashes.com
    and then do like

    # for i in `cat list.txt`; do ./domainsetup.sh $i; apache.sh $i; done

    which will do up ur whole list.

    I'm a UNIX guy, not a control panel guy, so I use the shell, these scripts saves me a lot of time when setting up domains! Just thought I'd share them with you all.
     
    • Thanks Thanks x 2
    Last edited: May 30, 2009