Only a few on here may find this useful however this bash script is for servers that have Plesk control panel installed, and has only been tested on Plesk based servers.
Some information has been edited from the script for privacy, however this was a project for work and figured there are people who could use it, provide suggestions, and learn from some of the stuff in it. So with that said here it is. Enjoy:
Code:
#!/usr/bin/env bash
#**********************************************
# This script Installs Joomla and Wordpress
# Features:
# 1. Download & Extract Distribution
# 2. Setup Database, User, and Password
# 3. Set correct user/group permissions
# 4. Set correct file/directory permissions
# 5. vhost.conf creation
#
# last modified: 10/30/2008
#**********************************************
# CONFIGURATION
PSACONF=/etc/psa/psa.conf
JOOMLA_DL_URL="http://joomlacode.org/gf/download/frsrelease/8376/30991/Joomla_1.5.7-Stable-Full_Package.tar.bz2"
WP_DL_URL="http://wordpress.org/wordpress-2.6.3.tar.gz"
LOG=$0.log
# END CONFIGURATION DO NOT EDIT BELOW THIS LINE
# Exit with error, log error
function die() {
echo
echo ${1}|tee -a ${LOG}
echo
exit 1
}
# Exit with success
function f_quit() {
echo "quit" | tee -a ${LOG}
exit 0
}
# Yes or no Prompter, if no answer is given, defaults to true/yes
function f_yesno() {
read -p "${1} [Y|n] " ANS
case ${ANS} in
y|Y|yes|YES|Yes|t|T|true|TRUE|True)
return 0
;;
n|N|no|NO|No|f|F|false|FALSE|False)
echo "Skipping"
echo "Press Enter to return to main menu"
return 1
;;
*)
return 0
;;
esac
}
# Web App downloader
function f_download() {
echo "Attempting Download ${1}" | tee -a ${LOG}
OS=`uname`
if [ ${OS} == "FreeBSD" ]; then
/usr/bin/env fetch ${1}
else
dl=`/usr/bin/env wget -q ${1}`
if [ $? -eq 0 ]; then
echo "${1} Downloaded OK" | tee -a ${LOG}
else
die "File Couldn't download"
fi
fi
}
# "Domain Chooser" Parses domain names from PSA database
function f_domainchooser() {
echo "Select Domain:"
PSA_PASS=`cat /etc/psa/.psa.shadow`
DOMAINS=`mysql -uadmin -p${PSA_PASS} -N psa -e 'SELECT name FROM domains;'`
select CHOICE in ${DOMAINS} Back Exit
do
break
done
if [ ${CHOICE} == "Back" ]; then
echo "Press Enter to return to main menu"
return 1
fi
if [ ${CHOICE} == "Exit" ]; then
die "Goodbye"
fi
DOMAIN=${CHOICE}
echo "${DOMAIN} was selected" | tee -a ${LOG}
}
# Joomla Installer
function f_joomla() {
echo "Installing Joomla v1.5.7 Stable"
f_domainchooser || return 0
read -p "Set install directory [ex: joomla]: " INSTALL_D
read -p "Set Database Name: " DBNAME
read -p "Set Database User: " DBUSER
read -p "Set User Password: " DBPASS
read -p "Set Joomla Admin Password: " PASSWD
read -p "Set User Email Address: " USRMAIL
JM_INSTALL_D="${HTTPD_VHOSTS_D}/${DOMAIN}/httpdocs/${INSTALL_D}"
if [ ! -d "${JM_INSTALL_D}" ]; then
mkdir -v "${JM_INSTALL_D}" | tee -a ${LOG}
cd "${JM_INSTALL_D}"
echo "Current Dir is: `pwd`" | tee -a ${LOG}
f_download ${JOOMLA_DL_URL}
echo "Extracting file..."
/usr/bin/env tar -xjf `/usr/bin/env basename ${JOOMLA_DL_URL}` | tee -a ${LOG}
${PRODUCT_ROOT_D}/bin/database -c ${DBNAME} -type mysql -server localhost:3306 -domain ${DOMAIN} -add_user ${DBUSER} -passwd ${DBPASS} | tee -a ${LOG}
# set directory permissions
echo "Configuring Joomla directory permissions.."
/usr/bin/env chmod 777 ${JM_INSTALL_D}/{cache,images,templates,components,modules,media,tmp}
/usr/bin/env chmod 777 ${JM_INSTALL_D}/administrator/{backups,cache,components,modules,templates}
/usr/bin/env chmod 777 ${JM_INSTALL_D}/images/{banners,stories}
/usr/bin/env chmod -R 777 ${JM_INSTALL_D}/{language,plugins}
/usr/bin/env chmod -R 777 ${JM_INSTALL_D}/administrator/language
# setup & edit joomla configuration.php
echo "Creating configuration file.."
cp -v ${JM_INSTALL_D}/configuration.php-dist ${JM_INSTALL_D}/configuration.php
sed -i "s/var \$user = ''/var \$user = '${DBUSER}'/" ${JM_INSTALL_D}/configuration.php
sed -i "s/var \$password = ''/var \$password = '${DBPASS}'/" ${JM_INSTALL_D}/configuration.php
sed -i "s/var \$db = ''/var \$db = '${DBNAME}'/" ${JM_INSTALL_D}/configuration.php
sed -i "s/var \$fromname = ''/var \$fromname = '${DOMAIN}'/" ${JM_INSTALL_D}/configuration.php
# setup & joomla database
echo "Creating admin password"
sed -i 's/#__/jos_/g' ${JM_INSTALL_D}/installation/sql/mysql/joomla.sql
MD5PASS=$(echo -n ${PASSWD} | md5sum | awk '{print $1}')
echo "Importing MySQL database.."
mysql -u${DBUSER} -p${DBPASS} ${DBNAME} < ${JM_INSTALL_D}/installation/sql/mysql/joomla.sql
mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e"INSERT INTO jos_users VALUES (62, 'Administrator', 'admin', 'your-email@email.com','$MD5PASS','Super Administrator', 0, 1, 25, '2005-09-28 00:00:00', '2005-09-28 00:00:00', '','');" | tee -a ${LOG}
mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e"INSERT INTO jos_core_acl_aro VALUES (10,'users','62',0,'Administrator',0);" | tee -a ${LOG}
mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e"INSERT INTO jos_core_acl_groups_aro_map VALUES (25,'',10);" | tee -a ${LOG}
# correct permissions (post install)
echo "running post install cleanup.."
f_fixowner ${JM_INSTALL_D} | tee -a ${LOG}
rm -rfv ${JM_INSTALL_D}/installation/ | tee -a ${LOG}
echo "Installation is now complete. Your administrator password is ${PASSWD}"
else
echo "Directory Already Exists, exit to main menu"
fi
}
# Wordpress installer
function f_wordpress() {
echo "Installing WordPress v2.6.3" ${1}
f_domainchooser || return 0
read -p "Set install directory [ex: wordpress]: " INSTALL_D
read -p "Set Database Name: " DBNAME
read -p "Set Database User: " DBUSER
read -p "Set User Password: " DBPASS
WP_INSTALL_D="${HTTPD_VHOSTS_D}/${DOMAIN}/httpdocs/${INSTALL_D}"
if [ ! -d "${WP_INSTALL_D}" ]; then
mkdir -v "${WP_INSTALL_D}" | tee -a ${LOG}
cd "${WP_INSTALL_D}"
echo "Current Dir is: `pwd`" | tee -a ${LOG}
f_download ${WP_DL_URL}
echo "Extracting file..."
/usr/bin/env tar -xvf `/usr/bin/env basename ${WP_DL_URL}` -C /tmp | tee -a ${LOG}
/usr/bin/env cp -r /tmp/wordpress/* "${WP_INSTALL_D}" | tee -a ${LOG}
echo "Creating Database.."
${PRODUCT_ROOT_D}/bin/database -c ${DBNAME} -type mysql -server localhost:3306 -domain ${DOMAIN} -add_user ${DBUSER} -passwd ${DBPASS} | tee -a ${LOG}
/usr/bin/env chmod 777 -R "${WP_INSTALL_D}"/wp-content/*
echo "Writing configuration file.."
cp -v ${WP_INSTALL_D}/wp-config-sample.php ${WP_INSTALL_D}/wp-config.php | tee -a ${LOG}
sed -i 's/putyourdbnamehere/'${DBNAME}'/' ${WP_INSTALL_D}/wp-config.php
sed -i 's/usernamehere/'${DBUSER}'/' ${WP_INSTALL_D}/wp-config.php
sed -i 's/yourpasswordhere/'${DBPASS}'/' ${WP_INSTALL_D}/wp-config.php
echo "Correcting user & group permissions.."
f_fixowner ${WP_INSTALL_D}
echo "Please go to http://${DOMAIN}/${INSTALL_D}/ to complete the installation process"
else
echo "Directory Already Exists, exit to main menu" | tee -a ${LOG}
fi
}
# this function corrects user/group permissions to set according to plesk
function f_fixowner() {
eval `/usr/bin/env cat "${HTTPD_VHOSTS_D}/${DOMAIN}"/conf/httpd.include | grep SuexecUserGroup | head -1 | awk '{print "GROUP="$NF";USER="$(NF-1)}'`
chown -R ${USER}:${GROUP} ${1}
}
# find vhost and set if doesnt exist
function f_vhostconf() {
f_domainchooser || return 0
f_yesno "Configure vhost.conf?" || return 0
if [ -f "${HTTPD_VHOSTS_D}/${DOMAIN}"/conf/vhost.conf ]; then
echo "vhost.conf already exists!" | tee -a ${LOG}
f_yesno "Edit existing vhost.conf?" && /usr/bin/env vi ${HTTPD_VHOSTS_D}/${DOMAIN}/conf/vhost.conf
else
echo "Configuring vhost.conf" | tee -a ${LOG}
/usr/bin/env touch "${HTTPD_VHOSTS_D}/${DOMAIN}"/conf/vhost.conf | tee -a ${LOG}
echo "<Directory ${HTTPD_VHOSTS_D}/${DOMAIN}/httpdocs/>" >> "${HTTPD_VHOSTS_D}/${DOMAIN}"/conf/vhost.conf | tee -a ${LOG}
echo "php_admin_flag safe_mode off" >> "${HTTPD_VHOSTS_D}/${DOMAIN}"/conf/vhost.conf | tee -a ${LOG}
echo "php_admin_value open_basedir none" >> "${HTTPD_VHOSTS_D}/${DOMAIN}"/conf/vhost.conf | tee -a ${LOG}
echo "</Directory>" >> "${HTTPD_VHOSTS_D}/${DOMAIN}"/conf/vhost.conf | tee -a ${LOG}
echo "Press Enter to return to main menu.."
fi
}
# set permissions for pre-existing joomla install
function f_setjoomperms(){
f_domainchooser
read -p "Enter Install Path: " INSTALL_D
JM_INSTALL_D="${HTTPD_VHOSTS_D}/${DOMAIN}/httpdocs/${INSTALL_D}"
cd ${JM_INSTALL_D}
echo "Configuring Joomla File/Folder Permissions..."
/usr/bin/env find ${WP_INSTALL_D} -type f|while read "f";do echo "chmod 644 ${f}";chmod 644 "${f}";done | tee -a ${LOG}
/usr/bin/env find ${WP_INSTALL_D} -type d|while read "d";do echo "chmod 755 ${d}";chmod 755 "${d}";done | tee -a ${LOG}
/usr/bin/env chmod 777 ${JM_INSTALL_D}/{cache,images,templates,components,modules,media,tmp}
/usr/bin/env chmod 777 ${JM_INSTALL_D}/administrator/{backups,cache,components,modules,templates}
/usr/bin/env chmod 777 ${JM_INSTALL_D}/images/{banners,stories}
/usr/bin/env chmod -R 777 ${JM_INSTALL_D}/{language,plugins}
/usr/bin/env chmod -R 777 ${JM_INSTALL_D}/administrator/language
echo "Permissions have been set.. Press Enter to Return to Main Menu"
}
# set permissions for pre-existing WordPress install
function f_setwpperms(){
f_domainchooser
read -p "Enter Install Path: " INSTALL_D
WP_INSTALL_D="${HTTPD_VHOSTS_D}/${DOMAIN}/httpdocs/${INSTALL_D}"
/usr/bin/env find ${WP_INSTALL_D} -type f|while read "f";do echo "chmod 644 ${f}";chmod 644 "${f}";done | tee -a ${LOG}
/usr/bin/env find ${WP_INSTALL_D} -type d|while read "d";do echo "chmod 755 ${d}";chmod 755 "${d}";done | tee -a ${LOG}
echo "Permissions have been set.. Press Enter to Return to Main Menu"
}
# Parse psa.conf file and read in as variables.
function f_psaconfparse() {
if [ -f ${PSACONF} ]; then
while read -a line; do
[[ ${line:0:1} == "#" ]] && continue
[[ -z "$line" ]] && continue
eval ${line[0]}=${line[1]}
done < ${PSACONF}
else
die "${PSACONF} File Doesn't Exist, please verify and correct configurations"
fi
}
# Main Menu
function f_mainmenu() {
clear
echo "##########################################"
echo "# Joomla / WordPress Installer 0.1 Alpha #"
echo "# #"
echo "##########################################"
echo "Choose an option:"
CHOICES="Install_Joomla Install_WordPress Set_vhost.conf Set_Joomla_Permissions Set_WordPress_Permissions Quit"
select CHOICE in ${CHOICES}
do
case ${CHOICE} in
Install_Joomla)
f_joomla ${1}
;;
Install_WordPress)
f_wordpress ${2}
;;
Set_vhost.conf)
f_vhostconf
;;
Set_Joomla_Permissions)
f_setjoomperms
;;
Set_WordPress_Permissions)
f_setwpperms
;;
Quit)
f_quit ${4}
;;
*)
echo "Invalid option.. Press Enter to return to the main menu"
;;
esac
done
}
# BEGIN MAIN LOOP
f_psaconfparse
while true
do
f_mainmenu
done