Skip to content

Instantly share code, notes, and snippets.

@dragonmantank
Created February 25, 2015 14:17
Show Gist options
  • Save dragonmantank/d48234eb8190b8e9c484 to your computer and use it in GitHub Desktop.
Save dragonmantank/d48234eb8190b8e9c484 to your computer and use it in GitHub Desktop.
add_domain.sh
#!/bin/bash
VERSION=0.5
usage() {
cat <<EOF
Domain Administration Script v${VERSION}
=================================
Allows various functions for administering domains on this machine.
Mandatory Arguments:
--------------------
-d Domain to run against (for example, domain.com)
-t Task to perform
Create a Site:
--------------
Creates a new site and jailed user
usage: ./domain-admin.sh -t create_site -d [domain] -u [user] [-w]
Arguments:
-u User to create
-w Enable automatic wordpress creation
Destroy a site:
---------------
Completely destroys a website, including the database.
usage: ./domain-admin.sh -t destroy_site -d [domain -u [user]
Arguments:
-u User to destroy
EOF
}
DA_TASK=
DA_DOMAIN=
VALID=1
DA_USER=
DA_INSTALL_WP=0
while getopts "ht:d:u:w" OPTION
do
case ${OPTION} in
h)
usage
exit 1
;;
t)
DA_TASK=$OPTARG
;;
d)
DA_DOMAIN=$OPTARG
;;
u)
DA_USER=$OPTARG
;;
w)
DA_INSTALL_WP=1
;;
esac
done
if [[ -z ${DA_TASK} ]]
then
echo "Please enter a task to perform"
VALID=0
fi
if [[ -z ${DA_DOMAIN} ]]
then
echo "Please enter a domain"
VALID=0
fi
if [[ ${VALID} -eq 0 ]]
then
exit 1
fi
JAIL_PATH=/var/www/clients/${DA_DOMAIN}
if [[ ${DA_TASK} = "create_site" ]]
then
DA_MYSQL_USER=
DA_MYSQL_PASS=
adduser --force-badname ${DA_USER}
mkdir -p ${JAIL_PATH}/{www/public,tmp,logs}
chown -R ${DA_USER}:${DA_USER} ${JAIL_PATH}/*
jk_init -c /etc/jailkit/jk_init.ini ${JAIL_PATH} extshellplusnet
jk_init -c /etc/jailkit/jk_init.ini ${JAIL_PATH} limitedshell
jk_cp ${JAIL_PATH}/ -k /bin/bash
jk_cp ${JAIL_PATH}/ -k /lib/x86_64-linux-gnu/libnss*
jk_cp ${JAIL_PATH}/ -k /usr/bin/groups
jk_cp ${JAIL_PATH}/ -k /usr/bin/curl
jk_cp ${JAIL_PATH}/ /lib/terminfo
jk_cp ${JAIL_PATH}/ /usr/share/zoneinfo
jk_cp ${JAIL_PATH}/ `which php`
jk_cp ${JAIL_PATH}/ /etc/php5/
jk_cp ${JAIL_PATH}/ /usr/lib/php5/
jk_cp ${JAIL_PATH}/ /usr/share/doc/php5
jk_cp ${JAIL_PATH}/ /usr/share/php5
jk_cp ${JAIL_PATH}/ `which git`
jk_cp ${JAIL_PATH}/ `which basename`
jk_cp ${JAIL_PATH}/ `which uname`
jk_cp ${JAIL_PATH}/ /usr/lib/git-core/
jk_jailuser -s /bin/bash -m -j ${JAIL_PATH}/ ${DA_USER}
cp sample.vhost /etc/apache2/sites-available/${DA_DOMAIN}
sed -i "s/\[domain\]/${DA_DOMAIN}/g" /etc/apache2/sites-available/${DA_DOMAIN}
sed -i "s/\[user\]/${DA_USER}/g" /etc/apache2/sites-available/${DA_DOMAIN}
a2ensite ${DA_DOMAIN}
service apache2 reload
echo 'Please enter the password for new MySQL account:'
read sqlPassword
echo 'Now, we will set up the account. The next two password prompts are for the root MySQL account, not the one you just set up'
mysql -u root -h 127.0.0.1 -p -e "CREATE DATABASE ${DA_USER}"
mysql -u root -h 127.0.0.1 -p -e "GRANT ALL ON ${DA_USER}.* to ${DA_USER}@localhost IDENTIFIED BY '$sqlPassword'"
fi
if [[ ${DA_TASK} = 'destroy_site' ]]
then
deluser ${DA_USER}
a2dissite ${DA_DOMAIN}
rm /etc/apache2/sites-available/${DA_DOMAIN}
service apache2 reload
rm -rf ${JAIL_PATH}
mysql -u root -h 127.0.0.1 -p -e "DROP DATABASE ${DA_USER}"
mysql -u root -h 127.0.0.1 -p -e "DROP USER ${DA_USER}@localhost"
fi
if [ ${DA_INSTALL_WP} -eq 1 ]
then
cp install-plugins.sh ${JAIL_PATH}/www/public
chmod 755 install-plugins.sh
cd ${JAIL_PATH}/www/public
echo ''
echo '------------------------------------------------------------------------'
echo '---> DOWNLOADING WORDPRESS'
echo '------------------------------------------------------------------------'
echo ''
curl -H "Accept-Encoding: gzip,deflate" http://wordpress.org/latest.tar.gz > latest.tar.gz
tar --strip-components=1 -zxf latest.tar.gz
rm latest.tar.gz
rm license.txt
rm readme.html
echo ''
echo '------------------------------------------------------------------------'
echo '---> WORDPRESS DOWNLOADED'
echo '------------------------------------------------------------------------'
echo ''
cp wp-config-sample.php wp-config.php
WP_SECURITY_KEYS=("AUTH_KEY" "SECURE_AUTH_KEY" "LOGGED_IN_KEY" "NONCE_KEY" "AUTH_SALT" "SECURE_AUTH_SALT" "LOGGED_IN_SALT" "NONCE_SALT")
for i in "${WP_SECURITY_KEYS[@]}"
do
sed -i "/${i}/d" wp-config.php
done
sed -i "s/database_name_here/${DA_USER}/g" wp-config.php
sed -i "s/username_here/${DA_USER}/g" wp-config.php
sed -i "s/password_here/${sqlPassword}/g" wp-config.php
curl -o /tmp/wp.keys -s https://api.wordpress.org/secret-key/1.1/salt/
sed -i '/#@-/r /tmp/wp.keys' wp-config.php
sed -i "/#@+/,/#@-/d" wp-config.php
WP_PASSWORD=`pwgen -N 1 18`
echo -n "Enter the name of the site: "
read WP_SITENAME
echo -n "Enter the admin user: "
read WP_ADMIN
echo -n "Enter the admin email: "
read WP_ADMIN_EMAIL
echo ${WP_SITENAME} ${WP_ADMIN} ${WP_ADMIN_EMAIL} ${WP_PASSWORD}
`which php` -r "
include './wp-admin/install.php';
wp_install('${WP_SITENAME}', '${WP_ADMIN}', '${WP_ADMIN_EMAIL}', 1, '', '${WP_PASSWORD}');
" > /dev/null 2>&1
###########################
# Install default plugins #
###########################
# WP Super Cache
sh install-plugins.sh wp-super-cache
# WordPress SEO by Yoast
sh install-plugins.sh wordpress-seo
# Better WP Security
sh install-plugins.sh better-wp-security
# Google Analytics for WordPress
sh install-plugins.sh google-analytics-for-wordpress
# Really Simple CAPTCHA
sh install-plugins.sh really-simple-captcha
# Ultimate Coming Soon Page
sh install-plugins.sh ultimate-coming-soon-page
# Soliloquy Lite
sh install-plugins.sh soliloquy-lite
# Contact Form 7
sh install-plugins.sh contact-form-7
rm install-plugins.sh
chown ${DA_USER}:${DA_USER} -R wp-content/plugins/
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment