fork download
  1. #!/bin/bash
  2. [[ $# -ge 2 ]] || { echo '!!! newvhost <vhost> <user>'; exit 1; }
  3. vhost=$1
  4. user=$2
  5.  
  6. apache_dir="/etc/httpd/rincewind"
  7. home_dir="/home/www"
  8. vhost_conf="${apache_dir}/vhosts/${vhost}.conf"
  9. vhost_link="${apache_dir}/registry/50_${vhost}.conf"
  10. vhost_dir="${home_dir}/${vhost}"
  11. user_vhost_dir="/home/${user}/www"
  12. user_vhost_link="${user_vhost_dir}/${vhost}"
  13. apache_user="http"
  14.  
  15. cat <<EOF
  16. Will create:
  17.  - ${vhost_conf} owned by ${apache_user}:${apache_user}; u=rw,g=r,o=
  18.  - ${vhost_link} -> ${vhost_conf}
  19.  - ${vhost_dir}/{logs,htdocs} owned by ${user}:${user}; u=rwx,g=rx,o=
  20.  - ${vhost_dir}/logs/{access,error}.log owned by ${apache_user}:${user}, u=rw,g=r,o=
  21.  - ${user_vhost_dir} owned by ${user}:${user}; u=rwx,g=rx,o=
  22.  - ${user_vhost_link} -> ${vhost_dir}
  23.  
  24. Will also add "${apache_user}" user to "${user}" group and do graceful restart.
  25.  
  26. CTRL+C to abort. ENTER to proceed.
  27. EOF
  28. read
  29.  
  30. # sanity checks
  31. id ${user} >/dev/null 2>&1 || { echo "!!! User ${user} does not exist."; exit 1; }
  32. #[[ `id -u ${user}` -ge 1000 ]] || { echo " ! UID too low: `id -u ${user}` (should be >=1000)."; exit 1; }
  33. [[ ! -d ${vhost_dir} ]] || { echo "!!! ${vhost_dir} already exists."; exit 1; }
  34. [[ ! -f ${vhost_conf} ]] || { echo "!!! ${vhost_conf} already exists."; exit 1; }
  35.  
  36. mkdir -v -p ${vhost_dir}/{logs,htdocs}
  37. chown -vR ${user}:${user} ${vhost_dir}
  38. touch ${vhost_dir}/logs/{error,access}.log
  39. chown -v ${apache_user}:${user} ${vhost_dir}/logs/*.log
  40. chmod -vR u=rwX,g=rX,o= ${vhost_dir}
  41.  
  42. cat > ${vhost_conf} <<EOF
  43. <VirtualHost *:80>
  44.   SuexecUserGroup ${user} ${user}
  45.   ServerName ${vhost}
  46.   DocumentRoot ${vhost_dir}/htdocs
  47.   ErrorLog ${vhost_dir}/logs/error.log
  48.   CustomLog ${vhost_dir}/logs/access.log combined
  49.  
  50.   <Directory "${vhost_dir}/htdocs">
  51.   Order allow,deny
  52.   Allow from all
  53.   AllowOverride All
  54.   </Directory>
  55. </VirtualHost>
  56. EOF
  57. chown -v ${apache_user}:${apache_user} ${vhost_conf}
  58. chmod -v u=rw,g=r,o= ${vhost_conf}
  59. ln -v -s ${vhost_conf} ${vhost_link}
  60.  
  61. mkdir -v -p ${user_vhost_dir}
  62. ln -s -v ${vhost_dir} ${user_vhost_link}
  63. chown -vR ${user}:${user} ${user_vhost_dir}
  64. chmod -vR u=rwX,g=rX,o= ${user_vhost_dir}
  65.  
  66. usermod -a -G ${user} http
  67.  
  68. rc.d reload apache
  69.  
  70. echo 'Done.'
  71.  
Runtime error #stdin #stdout 0.02s 5312KB
stdin
Standard input is empty
stdout
!!! newvhost <vhost> <user>