Skip to content

Instantly share code, notes, and snippets.

@Asenar Asenar/nouveau_site
Last active Dec 19, 2015

Embed
What would you like to do?
quick script to create new vhost I made a couple years ago when I started to use bash. This is a really basic script with lot of case who can break it. Feel free to comment
#!/bin/bash
# file : nouveau_site
# description : script to quickly create new vhost. script has to be in /usr/local/sbin/nouveau_site
# usage :
# nouveau_site mondomaine.com
EMAILS="user1@domaine.com"
main_ndd="monsite"
source /usr/local/include/custom_lib
sitename="$1"
alias1="$2"
apache_sites_dir=/etc/apache2/sites-available
base_dir=/home/web
if (test "$sitename" = "" ) then
echo "erreur"
exit 1
fi
# test if this name is valid and available
valid_name=`echo "$sitename"|grep '^[0-9a-z.-]*\.\(com\|net\|fr\|org\|info\)$'`
if ( test "$valid_name" != "$sitename") then
echo "nom de domaine non valide : le format n'est pas respecte ^[a-z.-]*\.(com|net|fr|org|info)$"
exit 2
fi
user=$(echo $sitename|sed 's#\(.*\)\.[^.]*#\1#' )
if ( test -e "$base_dir/$user" ) then
echo "oups, dossier existe deja "
exit 3
fi
if ( test -e "$apache_sites_dir/$sitename" ) then
echo "oups, le vhost existes deja"
exit 4
else
echo "Site absent :) "
fi
# creating user for that directory only :)
# username is, by default, the domain name without ".fr/.com" at the end
user=$(echo $sitename|sed 's#\(.*\)\.[^.]*#\1#' )
pass=`apg -q -a 0 -n 1 -M NCL`
pass_crypted=$(perl -e "print crypt($pass, 'password')");
# creating directory with correct rights :)
mkdir -p $base_dir/$user/www $base_dir/$user/prive
useradd -s /bin/false -M -d $base_dir/$user -g sftp -p $pass_crypted $user
if (test $? -eq 0 ) then
echo "user $user created."
else
echo "User $user NOT CREATED...... MAYBE ALREADY EXIST ?"
fi
chown root:sftp $base_dir/$user -R && chmod 755 $base_dir/$user/www -R
chmod g+w $base_dir/$user/www $base_dir/$usr/prive
#@TODO utiliser un fichier de template
echo "ici" > $base_dir/$user/www/index.html
chmod +x $base_dir/$user -R
tmpfile=`mktemp`
vhost_def="<VirtualHost *:80>
ServerAdmin webmaster@$main_ndd
ServerName $sitename
ServerAlias www.$sitename
php_admin_value engine off
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
DocumentRoot /home/web/$user/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/web/$user/www/>
Options -Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
# Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
LogLevel warn
ErrorLog \${APACHE_LOG_DIR}/error-$sitename.log
CustomLog \${APACHE_LOG_DIR}/access-$sitename.log combined
</VirtualHost>
"
echo "$vhost_def" > $tmpfile
echo "$vhost_def"
echo "Valider (y/n) ?"
read pouet
if (test "$pouet" != "y" -a "$pouet" != "Y") then
echo "Edition vhost ..."
# vim +":r! cat $tmpfile" $apache_sites_dir/$sitename
vim $tmpfile
fi
cp $tmpfile $apache_sites_dir/$sitename
# always cleaning after works is done
rm $tmpfile
echo "Alors là, je suis assez fier de ce script qui :
- créé un vhost
- créé un user sftp pour qu'il ne puisse écrire que sur ce vhost :)
- m'envoie tout ça !
host $sitename
user $user
pass $pass
"|mail -s "$main_ndd:sftp - nouvel utilisateur $user" -t "$EMAILS"
# how to handle password ?
# passwd
# how to send all this informations :p
# enabling vhost
a2ensite $sitename
# reload apache :)
service apache2 reload
@Asenar

This comment has been minimized.

Copy link
Owner Author

commented Jul 4, 2013

TODO :

WGET=$(which wget)
WGET_OPTS='--truc -bidule'
2° vérifier que "service existe", ou faire /etc/init.d/apache2 reload
3° check $? (specially for a2ensite)
4° use ${VAR} instead of $VAR
5° EOF for vhost
6° more CONSTANTS
7° use functions insteads of echoes + --verbose + --quiet
8° check_rcode if cp or rm has error
9° rename to a2addvhost ?
10° getopt
11° source path/to/lib , define $lib_version , getopt, verbose, etc...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.