Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Beagon/b0c72b0e5299dd1dfbe1 to your computer and use it in GitHub Desktop.

Select an option

Save Beagon/b0c72b0e5299dd1dfbe1 to your computer and use it in GitHub Desktop.
#!/bin/bash
WEBSITE_DIRECTORY="/var/www/"
APACHE2_DIRECTORY="/etc/apache2/"
MYSQL_USER="root"
MYSQL_PASSWORD=""
#DON'T EDIT ANYTHING UNDERNEATH HERE UNLESS YOU KNOW WHAT YOU ARE DOING!
checkWebsiteDirectoryConfig()
{
if [ -z "${WEBSITE_DIRECTORY}" ]
then
echo "[To disable this message please fill in WEBSITE_DIRECTORY in the script]"
read -p "WEBSITE_DIRECTORY is empty! Please fill in your website directory e.g. /var/www/: " WEBSITE_DIRECTORY
checkWebsiteDirectoryConfig
else
checkApache2DirectoryConfig
fi
}
checkApache2DirectoryConfig()
{
if [ -z "${APACHE2_DIRECTORY}" ]
then
echo "[To disable this message please fill in APACHE2_DIRECTORY in the script]"
read -p "APACHE2_DIRECTORY is empty! Please fill in your apache2 directory e.g. /etc/apache2/: " APACHE2_DIRECTORY
checkApache2DirectoryConfig
else
checkMysqlUserConfig
fi
}
checkMysqlUserConfig()
{
if [ -z "${MYSQL_USER}" ]
then
echo "[To disable this message please fill in MYSQL_USER in the script]"
read -p "MYSQL_USER is empty! Please fill in your mysql username: " MYSQL_USER
checkMysqlUserConfig
#else
# checkMysqlPasswordConfig
fi
}
checkMysqlPasswordConfig()
{
if [ -z "${MYSQL_PASSWORD}" ]
then
echo "[To disable this message please fill in MYSQL_PASSWORD in the script]"
read -p "MYSQL_PASSWORD is empty! Please fill in your mysql password: " MYSQL_PASSWORD
checkMysqlUserConfig
fi
}
getVhostName()
{
read -p "Please enter a domain name: " vhostName
if [ -z "${vhostName}" ]
then
echo "Please fill in a domain name!"
getVhostName
else
if [ -z "${USER_USERNAME}" ]
then
WEB_DIR=$WEBSITE_DIRECTORY/$vhostName
else
WEB_DIR=/home/$USER_USERNAME/$vhostName
fi
fi
}
getCmsSystem()
{
read -p "Please enter what kind of CMS you're setting up (none, symfony2): " cmsSystem
if [ -z "${cmsSystem,,}" ]
then
getCmsSystem
else
case $cmsSystem in
symfony2)
buildSymfony2Vhost
;;
none)
buildNormalVhost
;;
*)
echo "This is not a valid CMS system!"
getCmsSystem
;;
esac
fi
}
setUserDetails()
{
if [ -z "${USER_USERNAME}" ]
then
read -p "Username for the user: " USER_USERNAME
fi
USER_PASSWORD=$(date +%s|sha256sum|base64|head -c 16)
}
createUser()
{
read -p "Do you want to create an user for this domain? (Y)es/(N)o:" -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
setUserDetails
useradd -g users -d /home/$USER_USERNAME -s /bin/bash -p $(echo "${USER_PASSWORD}" | openssl passwd -1 -stdin ) -m $USER_USERNAME
elif [[ $REPLY =~ ^[Nn]$ ]]
then
return
else
echo "${REPLY} is not a valid option!"
createUser
fi
}
setMysqlDetails()
{
if [ -z "${NMYSQL_USERNAME}" ]
then
read -p "MySQL username for the user: " NMYSQL_USERNAME
fi
NMYSQL_PASSWORD=$(date +%s|sha256sum|base64|head -c 16)
}
createMysqlUser()
{
read -p "Do you want to create an mysql user for this domain? (Y)es/(N)o:" -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
setMysqlDetails
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "CREATE USER '${NMYSQL_USERNAME}'@'localhost' IDENTIFIED BY '${NMYSQL_PASSWORD}';"
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "GRANT ALL PRIVILEGES ON \`${NMYSQL_USERNAME}\_%\` . * TO '${NMYSQL_USERNAME}'@'localhost';"
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "FLUSH PRIVILEGES;"
elif [[ $REPLY =~ ^[Nn]$ ]]
then
return
else
echo "${REPLY} is not a valid option!"
createMysqlUser
fi
}
createDirAndVhostFiles()
{
echo "Creating directory "
mkdir -p "${WEB_DIR}/public_html"
mkdir -p "${WEB_DIR}/logs"
if [ -z "${USER_USERNAME}" ]
then
chown -R www-data:www-data "${WEB_DIR}"
else
chown -R $USER_USERNAME:www-data "${WEB_DIR}"
fi
chmod 755 "${WEB_DIR}/public_html"
chmod 777 "${WEB_DIR}/logs"
echo "Directories created."
echo "==================="
echo "Creating vHost file"
touch "${APACHE2_DIRECTORY}sites-available/${vhostName}.conf"
echo "vHost created"
ln -s "${APACHE2_DIRECTORY}sites-available/${vhostName}.conf" "${APACHE2_DIRECTORY}sites-enabled/${vhostName}.conf"
echo "Symlink created"
echo "==================="
}
buildNormalVhost()
{
echo "None selected"
echo "==================="
createDirAndVhostFiles
echo "Filling in vHost"
echo "
<VirtualHost *:80>
ServerAdmin admin@${vhostName}
ServerName ${vhostName}
ServerAlias www.${vhostName}
DocumentRoot ${WEB_DIR}/public_html/
DirectoryIndex index.php index.html index.htm
LogLevel Emerg
ErrorLog ${WEB_DIR}/logs/error.log
CustomLog ${WEB_DIR}/logs/access.log Combined
<Directory ${WEB_DIR}/public_html/>
Options +ExecCGI
Require all granted
AllowOverride all
</Directory>
</VirtualHost>
}" > "${APACHE2_DIRECTORY}sites-available/${vhostName}.conf"
echo "vHost filled in."
}
buildSymfony2Vhost()
{
echo "Symfony2 selected"
echo "==================="
createDirAndVhostFiles
echo "Filling in vHost"
echo "
<VirtualHost *:80>
ServerName domain.tld
ServerAlias www.domain.tld
DocumentRoot ${WEB_DIR}/public_html/web/
<Directory ${WEB_DIR}/public_html/web/>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
# uncomment the following lines if you install assets as symlinks
# or run into problems when compiling LESS/Sass/CoffeScript assets
# <Directory /var/www/project>
# Options FollowSymlinks
# </Directory>
ErrorLog ${WEB_DIR}/logs/error.log
CustomLog ${WEB_DIR}/logs/project_access.log combined
</VirtualHost>" > "${APACHE2_DIRECTORY}sites-available/${vhostName}.conf"
echo "vHost filled in."
}
restartApache2()
{
echo "==================="
echo "Reloading apache2..."
service apache2 reload
echo "apache2 reloaded!"
echo "If you didn't encounter any errors the vhost has been setup!"
echo "==================="
}
showInfo()
{
echo "====================================================="
echo "! PLEASE COPY THIS INFO! IT WILL NOT BE SHOWN AGAIN !"
echo "====================================================="
echo "The server details:"
echo "Domain name: ${vhostName}"
echo "Website root: ${WEB_DIR}/public_html"
echo "Website logs: ${WEB_DIR}/logs"
if [ -z "${USER_USERNAME}" ]
then
return
else
echo "Username: ${USER_USERNAME}"
echo "Password: ${USER_PASSWORD}"
fi
if [ -z "${NMYSQL_USERNAME}" ]
then
return
else
echo "The MySQL details:"
echo "Username: ${NMYSQL_USERNAME}"
echo "Password: ${NMYSQL_PASSWORD}"
fi
echo "====================================================="
echo "! PLEASE COPY THIS INFO! IT WILL NOT BE SHOWN AGAIN !"
echo "====================================================="
read something
clear
}
init() {
typeset -l cmsSystem
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root!" 1>&2
exit 1
fi
echo "Apache2 Add Vhosts - Copyright Robin Rijkeboer 2016"
checkWebsiteDirectoryConfig
createUser
getVhostName
createMysqlUser
getCmsSystem
restartApache2
showInfo
}
init
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment