Created
May 22, 2017 15:03
-
-
Save yradunchev/61be0abc03b4f571d895dff8bde38cae to your computer and use it in GitHub Desktop.
script to create local user + vhost + cgi + suexec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash - | |
| #=============================================================================== | |
| # | |
| # FILE: vhostadd | |
| # | |
| # USAGE: ./vhostadd -n UserName -p Password -d FQDN | |
| # | |
| # DESCRIPTION: | |
| # | |
| # OPTIONS: --- | |
| # REQUIREMENTS: --- | |
| # BUGS: --- | |
| # NOTES: --- | |
| # AUTHOR: Yordan Radunchev (), [email protected] | |
| # ORGANIZATION: | |
| # CREATED: 2017-05-17 16:08 | |
| # REVISION: 003 | |
| #=============================================================================== | |
| #set -o nounset # Treat unset variables as an error | |
| # settings | |
| readonly ASAD=/etc/apache2/sites-available | |
| readonly ASED=/etc/apache2/sites-enabled | |
| readonly HOMD=/home | |
| #============================================================================== | |
| # DO NOT CHANGE ANYTHING PASS THIS LINE! | |
| #============================================================================== | |
| function usage() { | |
| echo "Usage $(basename $0) -n USERNAME -p PASSWORD -d FQDN" >&2 | |
| exit 2 | |
| } | |
| if [ "$(id -u)" != "0" ]; then | |
| echo "This script must be run as root" 1>&2 | |
| exit 1 | |
| fi | |
| [[ $# -ne 6 ]] && usage | |
| while getopts ":n:p:d:" opt; do | |
| case $opt in | |
| n) NAME="$OPTARG" ;; | |
| p) PASS="$OPTARG" ;; | |
| d) FQDN="$OPTARG" ;; | |
| \?) | |
| echo "Invalid option -$OPTARG" >&2 | |
| exit 2 | |
| ;; | |
| esac | |
| done | |
| [[ -z ${NAME} || -z ${PASS} || -z ${FQDN} ]] && usage | |
| readonly NAME PASS FQDN | |
| vhst=${ASAD}/${NAME}.${FQDN}.conf | |
| wrot=/home/${NAME}/www/${FQDN} | |
| wbin=/home/${NAME}/www-bin | |
| pcgi=${wbin}/php5.cgi | |
| # check if the user exists | |
| id ${NAME} >/dev/null 2>&1 && echo "User with that username already exists!" \ | |
| && exit 2 | |
| # check if user name valid | |
| [[ ${NAME} =~ ^[a-z][a-z0-9]?{6,14}$ ]] || { echo "Invalid user name (6-14 long\ | |
| alphanumeric, lower case)!"; exit 2; } | |
| # create user and homedir add www roots | |
| useradd -m ${NAME} --shell "/bin/false" | |
| chpasswd <<<"${NAME}:${PASS}" | |
| mkdir -p ${wrot} | |
| chown ${NAME}:${NAME} ${wrot} | |
| # create php.cgi wrapper for user ${NAME} in ${HOMD}/${wbin} | |
| cat <<EOF > ${pcgi} | |
| #!/bin/sh | |
| ### Set PATH ### | |
| PHP_CGI=/usr/bin/php-cgi | |
| ### no editing below ### | |
| exec \$PHP_CGI | |
| EOF | |
| chown ${NAME}:${NAME} ${pcgi} | |
| chmod +x ${pcgi} | |
| # create vhost from template and enable | |
| echo "Creating the new ${NAME} virtual host file that has a webroot of: ${wrot}" | |
| cat <<EOF > ${vhst} | |
| <VirtualHost *:80> | |
| SuexecUserGroup ${NAME} ${NAME} | |
| ServerName ${FQDN} | |
| DocumentRoot ${wrot} | |
| <Directory /> | |
| Options FollowSymLinks | |
| AllowOverride All | |
| </Directory> | |
| <Directory ${wrot}/> | |
| Options Indexes FollowSymLinks MultiViews | |
| AllowOverride All | |
| Order allow,deny | |
| allow from All | |
| </Directory> | |
| ScriptAlias /www-bin/ ${wbin}/ | |
| <Directory "${wbin}"> | |
| AllowOverride All | |
| Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch | |
| Order allow,deny | |
| Allow from all | |
| </Directory> | |
| ErrorLog \${APACHE_LOG_DIR}/error.log | |
| LogLevel warn | |
| CustomLog \${APACHE_LOG_DIR}/access.log vhost_combined | |
| </VirtualHost> | |
| EOF | |
| ln -s ${vhst} ${ASED}/ | |
| systemctl reload apache2 | |
| echo "Virtual host ${NAME} created with a webroot at ${wrot} reachable from http://${FQDN}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment