Apache VirtualHost script for FreeBSD

Adding new virtual hosts over and over again can be a pain in the a**, so I wrote a simple bash script witch allows you to easily generate new virtual hosts and makes your life a little less miserable. Script is user friendly and prompts user for needed data with dialog boxes. It add virtual host settings to your Apache httpd.conf file, create virtual FTP user (mysql or file) and if you wish, MySQL user with database. Script was developed on FreeBSD 8.0. To be able to use this script you will also need Apache HTTP server, MySQL (optional) and Pure-FTPd. It is also recommended that you have phpMyAdmin on your system, so users can access their databases via their browsers. If you’re interested, read this fully!

At beginning you must define global variables that script needs in order to operate properly. You must also add follwing line to your Apache configuration file – httpd.conf: ‘Include /path/to/your/hosts-conf-files/*.conf’. This is the path where your template files will be stored in (/usr/local/etc/apache22/Includes/). Of corse, you must replace /path/to/your/hosts-conf-files/ with real path that you want to defined as your template path. You must also define this path in global variables (TEMPLATEPATH) so that script can create two template files (virtual-hosts.conf and virtual-hosts-directory.conf). Those two template files will contain your virtualhosts settings. If you will use mysql database to store your virtual FTP users, you must define insert sql statement according to your pure-ftpd database (check line 69). My database may be different than one on your system. So download this script and add it to the sbin directory (/usr/local/sbin – on freebsd). Then chmod it with: chmod 700 /path/to/script/addvirtualhost. Now you can simple call this script with command addvirtualhost. Run it and follow the steps.

That’s it!  Enjoy!  Write a comment here if you wish ;)

Download script here

Here is the code preview:

#!/usr/local/bin/bash

#### Script was developed on FreeBSD 8.0 by Igor Mazej – www.mazej.net
# This script was designed so that it will make your life a little less miserable.
# Don’t forget to change variables compatible to your system and needs.
####
# Virtualhosts are added to virtual-host.conf and virtual-hosts-directory.conf files. Those files are template files and are included in httpd.conf.
# If you don’t have this files, than you must create one.
# Add line ‘Include /path/to/your/hosts-conf-files/*.conf’ to your httpd.conf file (change path to location where your template files are).
####
# This script is designed for Pure-FTPd server. If you will use mysql to store your
# virtual users, than you must change insert statement to your needs. My mysql database for pureftpd can be diferent than yours.
# If you will use pureftpd.passwd file to store your virtual users, define path to file in global variables.
####
# Add this script to your sbin folder and chmod it to 700. Type addvirtualhost to run script.
# Big tnx to Dali for a help with a few things -> http://www.dc.si
# You can leave comment on my blog @ http://www.mazej.net/blog
####

#Global variables (change them according to your system)
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin
ROOTDIR=”/usr/local/www/” # Dir where users directory’s will be created
TEMPLATEPATH=”/usr/local/etc/apache22/Includes/” # template files path (virtual-hosts.conf, virtual-hosts-directory.conf)
PHPADMINLINK=”http://phpmyadmin.yoururl.com” # Link to your phpmyadmin site
TMPFILE=”/tmp/dialog.tmp” # Template file where dialogs store temporary data
APACHE=”/usr/local/sbin/apachectl” # Path to apache manage scripts
PUREPASSW=”/usr/local/etc/pureftpd.passwd” # Pure-FTPd virtual users password file
VIRTUALFTP=0 # Virtual ftp users option. 1-virtual user is created in mysql database; 2-virtual user is created in pureftpd.passwd

### Function delete dialog temp file
function delete_tmp_file()
{
if [ -s $1 ]; then
rm -rf $1
fi
}

### Define template files
HPATH=$TEMPLATEPATH”virtual-hosts.conf”
DPATH=$TEMPLATEPATH”virtual-hosts-directory.conf”

### Check if virtual-hosts.conf exists. If not, then we create one
if [ ! -e $HPATH ]; then
echo -e “### This is your virtualhosts template file\n” > $HPATH
fi

### Check if virtual-hosts-directory.conf exists. If not, then we create one
if [ ! -e $DPATH ]; then
echo -e “### This is your virtualhosts directory template file\n” > $DPATH
fi

### Generate password for new user
PASS=$(/usr/bin/perl -le’print map+(A..Z,a..z,0..9)[rand 62],1..’8);

### Function inserts virtual ftp user in mysql ftp table. You must define insert statement in ftp_mysql function
# Arguments of function: $1-username; $2-password; $3-user directory; $4-dialog tmp file
# Don’t forget to change Uid and Gid fields to your system ftp user values!
# Set other fields as you wish
function ftp_mysql()
{
dialog \
–title “ADD VIRTUALHOST – FTP user (MySQL)” \
–inputbox “Enter MySQL root password:” 8 55 2> $4
ROOTPASS=$(cat $4)

### Insert into table
mysql -u’root’ -h’localhost’ -p”$ROOTPASS” -e “INSERT INTO ftpusers.users (`User`, `Password`, `Uid`, `Gid`, `Dir`, `QuotaFiles`, `QuotaSize`, `ULBandwidth`, `DLBandwidth`,`Ipaddress`,`Comment`, `Status`, `ULRatio`, `DLRatio`) VALUES ($1, $2, ‘2001’, ‘2001’, $3, ‘500’, ’30’,’80’, ’80’,’*’,NULL,’1′, ‘1’, ‘1’)”;

}

### Restart apache function. Argument $1 is apachectl dir
function apacheRestart()
{
$($1 -t)
if [ $? -eq 0 ]; then
$($1 graceful)
dialog –title ‘ADD VIRTUALHOST – SUCSESS!’ –infobox “\nVirtualhost was sucsessfuly added to your system.\n
Please wait while apache is being restarted …” 5 65 ; sleep 5
else

dialog –title ‘ERROR’ –msgbox ‘Apache was not restarted!’ 7 40
fi
}

### Show dialog for virtualhost name
dialog \
–title “ADD VIRTUALHOST – Step 1/3” \
–inputbox “Enter virtualhost title:” 8 45 2> $TMPFILE
NAME=$(cat $TMPFILE)

### Show dialog for virtualhost domain
dialog \
–title “ADD VIRTUALHOST – Step 2/3” \
–inputbox “Enter virtualhost domain:” 8 45 2> $TMPFILE
DOMAIN=$(cat $TMPFILE)

### Show dialog for database and ftp option
dialog \
–title “ADD VIRTUALHOST – Step 3/3” \
–checklist “Do you want MySQL support?” 8 45 2 \
mysql “Include MySQL” off 2> $TMPFILE
DATABASE=$(cat $TMPFILE)
DATABASE=${DATABASE//\”/}

### When we get all data from user, empty tmp file
delete_tmp_file $TMPFILE

### Check if user directory with that name exists. If exists, generate unique name
if [ -d “$ROOTDIR$NAME” ]; then
NAME=”$NAME$((RANDOM%999+1))”
fi

### Make user directory
USERDIR=”$ROOTDIR$NAME/public_html”
mkdir -p “$ROOTDIR$NAME”
mkdir -p “$USERDIR”
cp $ROOTDIR”index.php” $USERDIR/

### Write phpinfo() function to index.php
echo -e “> $USERDIR/index.php
echo -e “\tphpinfo();” >> $USERDIR/index.php
echo -e “?>” >> $USERDIR/index.php

### Add virtualhost to httpd.conf
echo -e “\n” >> $HPATH
echo -e “” >> $HPATH
echo -e “\t DocumentRoot $USERDIR/” >> $HPATH
echo -e “\t ServerName $DOMAIN” >> $HPATH
echo -e “” >> $HPATH

### Add directory to httpd.conf
echo -e “\n” >> $DPATH
echo -e “” >> $DPATH
echo -e “\t Options none” >> $DPATH
echo -e “\t AllowOverride Limit” >> $DPATH
echo -e “\t Order Deny,Allow” >> $DPATH
echo -e “\t Allow from all” >> $DPATH
echo -e “” >> $DPATH

### Create ftp user
if [ $VIRTUALFTP -eq 1 ]; then
ftp_mysql $NAME $PASS $USERDIR $TMPFILE
else
for i in {1..2}; do echo $PASS >> /tmp/tmppasswd; done
$(cat /tmp/tmppasswd | pure-pw useradd $NAME -u ftpuser -d $USERDIR -f $PUREPASSW)
fi

### If database support was selected then we create mysql user and database
if [ “$DATABASE” = ‘mysql’ ]; then

### Show dialog box for mysql root password
dialog \
–title “ADD VIRTUALHOST – MySQL” \
–inputbox “Enter MySQL root password:” 8 55 2> $TMPFILE
ROOTPASS=$(cat $TMPFILE)

### Create new user and create users database
mysql -u’root’ -h’localhost’ -p”$ROOTPASS” -e “CREATE DATABASE $NAME; USE mysql; INSERT INTO user (Host, User, Password)
VALUES (‘localhost’, ‘$NAME’, password(‘$PASS’)); FLUSH PRIVILEGES; GRANT ALL PRIVILEGES ON $NAME.* TO $NAME@localhost;”

### Restart apache
apacheRestart $APACHE

### Print account details
dialog –title “$NAME ACCOUNT DETAILS” –msgbox “\nYour account details are listed below:\n
Document root: $USERDIR
Domain: $DOMAIN
Database name: $NAME
MySQL username: $NAME
MySQL password: $PASS
phpMyAdmin: $PHPADMINLINK” 14 75

else

### Restart apache
apacheRestart $APACHE

### Print account details
dialog –title “$NAME ACCOUNT DETAILS” –msgbox “\nYour account details are listed below:\n
Document root: $USERDIR
Domain: $DOMAIN” 14 75

fi

### Empty tmp file
delete_tmp_file $TMPFILE

Leave a Reply

Your email address will not be published. Required fields are marked *