igen med få ændringer:
1. koden
2. ouputet
1. #!/bin/bash
set -x
#
ROOTPASSWD='kode'
MNDOMN='juac.dk'
MNDOMNUNDERSCORE='Juac_dk'
IP=90.184.106.85
#
#for USRNME in /home/*
USRARRAY=$(ls -l --format=single-column /home/)
for USRNME in ${USRARRAY[@]}
do
DIR="/home/${USRNME}/PUBLIC_HTML"
if [ ! -d ${DIR} ];
then
mkdir ${DIR}
fi
if [ ! -e /home/${USRNME}/PUBLIC_HTML/DB_USER_PASSWD.txt ];
then
DBPASSWD=`mkpasswd qwerty`
echo "${DBPASSWD}"
mysql --user=root --password=${ROOTPASSWD} << EOT
create user \'$USRNME\' identified by \'${DBPASSWD}\';
grant all privileges on \'$UNDERSCORENAME\'.* to \'$USRNME\'@\'localhost\';
exit;
EOT
echo "The username of the databases is ${USRNME}. And the password for the databases is ${DBPASSWD}" > "/home/${USRNME}/PUBLIC_HTML/your_databases.txt"
echo ${DBPASSWD} > "/home/${USRNME}/PUBLIC_HTML/DB_USER_PASSWD.txt"
fi
#for USRDOMN in /home/${USRNME}/PUBLIC_HTML/*
USRDOMNARRAY=$(ls -l --format=single-column /home/${USRNME}/PUBLIC_HTML/)
for USRDOMN in ${USRDOMNARRAY[@]}
do
LNK=${USRDOMN}.${USRNME}.${MNDOMN}
UNDERSCORENAME=${USRDOMN}_${USRNME}_${MNDOMNUNDERSCORE}
echo "USRDOMN:"${USRDOMN}
echo "USRNME:"${USRNME}
if [ ! -e /home/${USRNME}/PUBLIC_HTML/${USRDOMN}.reserved -a -d /home/${USRNME}/PUBLIC_HTML/${USRDOMN} ];
then
mkdir "/usr/lib/cgi-bin/${UNDERSCORENAME}"
echo "<VirtualHost *:80>
ServerAdmin ${USRNME}@${MNDOMN}
ServerName www.${LNK}
ServerAlias ${LNK}
DocumentRoot ${DIR}/${USRDOMN}
# Set Document Root directory options
<Directory />
Options FollowSymlinks -Indexes Includes
# Allow use of .htaccess file
AllowOverride Limit FileInfo
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/${UNDERSCORENAME}
# Set CGI-BIN directory options
<Directory /cgi-bin>
AllowOverride None
Options +ExecCGI -Multiviews +SymlinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
CustomLog /var/log/apache2/access.log common
ErrorLog /var/log/apache2/error.log
</VirtualHost>" > "/etc/apache2/sites-available/${UNDERSCORENAME}"
/usr/sbin/a2ensite ${UNDERSCORENAME}
echo "${IP} WWW.${LNK} ${LNK}" >> /etc/hosts
#/etc/init.d/apache2 stop
#/etc/init.d/apache2 start
#/usr/sbin/apache2ctl configtest &&
#/usr/sbin/apache2ctl graceful
/etc/init.d/apache2 reload
#echo "${DBPASSWD}"
DBPASSWD_TMP=`cat "/home/${USRNME}/PUBLIC_HTML/DB_USER_PASSWD.txt"`
echo "${DBPASSWD_TMP}"
mysqladmin create ${UNDERSCORENAME} --user=${USRNME} --password=${DBPASSWD_TMP}
touch /home/${USRNME}/PUBLIC_HTML/${USRDOMN}.reserved
chown -R www-data:${USRNME} /home/${USRNME}/PUBLIC_HTML/
find /home/${USRNME}/PUBLIC_HTML/ -type f | xargs chmod 0644
find /home/${USRNME}/PUBLIC_HTML/ -type d | xargs chmod 0755
fi
done
done
2.
root@debian:/media/d644e576-b072-47d8-8f77-7b11448079b1# ./addsubdomain42
+ ROOTPASSWD=kode
+ MNDOMN=juac.dk
+ MNDOMNUNDERSCORE=Juac_dk
+ IP=90.184.106.85
++ ls -l --format=single-column /home/
+ USRARRAY=tj
+ for USRNME in '${USRARRAY[@]}'
+ DIR=/home/tj/PUBLIC_HTML
+ '[' '!' -d /home/tj/PUBLIC_HTML ']'
+ '[' '!' -e /home/tj/PUBLIC_HTML/DB_USER_PASSWD.txt ']'
++ mkpasswd qwerty
+ DBPASSWD=pgeOj8R/HSfE.
+ echo pgeOj8R/HSfE.
pgeOj8R/HSfE.
+ mysql --user=root --password=kode
ERROR at line 1: Unknown command '\''.
+ echo 'The username of the databases is tj. And the password for the databases is pgeOj8R/HSfE.'
+ echo pgeOj8R/HSfE.
++ ls -l --format=single-column /home/tj/PUBLIC_HTML/
+ USRDOMNARRAY='DB_USER_PASSWD.txt
test
your_databases.txt'
+ for USRDOMN in '${USRDOMNARRAY[@]}'
+ LNK=DB_USER_PASSWD.txt.tj.juac.dk
+ UNDERSCORENAME=DB_USER_PASSWD.txt_tj_Juac_dk
+ echo USRDOMN:DB_USER_PASSWD.txt
USRDOMN:DB_USER_PASSWD.txt
+ echo USRNME:tj
USRNME:tj
+ '[' '!' -e /home/tj/PUBLIC_HTML/DB_USER_PASSWD.txt.reserved -a -d /home/tj/PUBLIC_HTML/DB_USER_PASSWD.txt ']'
+ for USRDOMN in '${USRDOMNARRAY[@]}'
+ LNK=test.tj.juac.dk
+ UNDERSCORENAME=test_tj_Juac_dk
+ echo USRDOMN:test
USRDOMN:test
+ echo USRNME:tj
USRNME:tj
+ '[' '!' -e /home/tj/PUBLIC_HTML/test.reserved -a -d /home/tj/PUBLIC_HTML/test ']'
+ mkdir /usr/lib/cgi-bin/test_tj_Juac_dk
+ echo '<VirtualHost *:80>
ServerAdmin tj@juac.dk
ServerName
www.test.tj.juac.dkServerAlias test.tj.juac.dk
DocumentRoot /home/tj/PUBLIC_HTML/test
# Set Document Root directory options
<Directory />
Options FollowSymlinks -Indexes Includes
# Allow use of .htaccess file
AllowOverride Limit FileInfo
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/test_tj_Juac_dk
# Set CGI-BIN directory options
<Directory /cgi-bin>
AllowOverride None
Options +ExecCGI -Multiviews +SymlinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
CustomLog /var/log/apache2/access.log common
ErrorLog /var/log/apache2/error.log
</VirtualHost>'
+ /usr/sbin/a2ensite test_tj_Juac_dk
Enabling site test_tj_Juac_dk.
Run '/etc/init.d/apache2 reload' to activate new configuration!
+ echo '90.184.106.85
www.test.tj.juac.dk test.tj.juac.dk'
+ /etc/init.d/apache2 reload
Reloading web server config: apache2apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
.
++ cat /home/tj/PUBLIC_HTML/DB_USER_PASSWD.txt
+ DBPASSWD_TMP=pgeOj8R/HSfE.
+ echo pgeOj8R/HSfE.
pgeOj8R/HSfE.
+ mysqladmin create test_tj_Juac_dk --user=tj --password=pgeOj8R/HSfE.
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'tj'@'localhost' (using password: YES)'
+ touch /home/tj/PUBLIC_HTML/test.reserved
+ chown -R www-data:tj /home/tj/PUBLIC_HTML/
+ find /home/tj/PUBLIC_HTML/ -type f
+ xargs chmod 0644
+ find /home/tj/PUBLIC_HTML/ -type d
+ xargs chmod 0755
+ for USRDOMN in '${USRDOMNARRAY[@]}'
+ LNK=your_databases.txt.tj.juac.dk
+ UNDERSCORENAME=your_databases.txt_tj_Juac_dk
+ echo USRDOMN:your_databases.txt
USRDOMN:your_databases.txt
+ echo USRNME:tj
USRNME:tj
+ '[' '!' -e /home/tj/PUBLIC_HTML/your_databases.txt.reserved -a -d /home/tj/PUBLIC_HTML/your_databases.txt ']'
root@debian:/media/d644e576-b072-47d8-8f77-7b11448079b1# hostname localhost
root@debian:/media/d644e576-b072-47d8-8f77-7b11448079b1#
root@debian:/media/d644e576-b072-47d8-8f77-7b11448079b1#