1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Easy webhosting with Ubuntu and bash scripts

Discussion in 'BlackHat Lounge' started by fatboy, May 19, 2011.

  1. fatboy

    fatboy Elite Member

    Joined:
    Aug 13, 2008
    Messages:
    1,618
    Likes Received:
    3,227
    Occupation:
    Retired
    Location:
    Old Peoples Home
    Okay, just spent 10 minutes trying to figure out if this went into the webhosting forum or the scripting forum.....couldn't decide so put it here, mods feel free to dump it where you like.

    If you grab yourself a small Ubuntu VPS and want to host a few little sites, here is a script that will let you set up virtualhosts within seconds :)

    Put the create.sh file and the template file in the same directory.

    The template file is a blank virtualhost host entry (very basic but enough to a site online) that the create.sh file will edit. Create a file and put the following in it, then save it as 'template'

    Code:
    <VirtualHost *:80>
    ServerAdmin EMAIL
    ServerName www.DOMAIN
    ServerAlias DOMAIN
    
    # Indexes + Directory Root.
    DirectoryIndex index.html index.htm index.php
    DocumentRoot /home/DOMAIN/htdocs/
    # CGI Directory
    ScriptAlias /cgi-bin/ /home/DOMAIN/cgi-bin/
    <Location /cgi-bin>
    Options +ExecCGI
    </Location>
    
    # Logfiles
    ErrorLog /home/logs/DOMAIN-error.log
    CustomLog /home/logs/DOMAIN-access.log combined
    </VirtualHost>
    
    This will put all your domains in the /home directory and anything you want online you stick in the htdocs directory. All log files are in the /home/domain.com/logs directory.

    If you want to put them elsewhere, edit the template file.

    Now, to get the details to make the virtualhost, put the following into a file called create.sh (and then make it 'executable' with chmod +x create.sh):

    Code:
    #!/bin/bash
    
    clear
    
    echo "Domain name to set up: "
    read domainname
    echo "Administrator email: "
    read email
    
    cd /etc/apache2/sites-available
    
    clear
    
    sed s/DOMAIN/$domainname/g template > $domainname
    sed -i s/EMAIL/$email/g $domainname
    
    if [ ! -d "/home/logs" ]; then
    	mkdir /home/logs
    fi
    
    mkdir /home/$domainname
    mkdir /home/$domainname/htdocs
    mkdir /home/$domainname/cgi-bin
    touch /home/$domainname/htdocs/index.html
    
    chown -R www-data.www-data /home/$domainname
    
    a2ensite $domainname
    
    Now, every time you want to set a new domain up, just run create.sh and type in the details :)

    Saves a good few minutes messing around - the script also puts a blank html file in the root so no one can see any other files stored in there :)

    Hope it helps someone!