You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

535 lines
14 KiB

#!/bin/bash
# Checking Permissions
Permission=$(id -u)
if [ $Permission -ne 0 ]
then
echo -e "\033[31m Root Privilege Required... \e[0m"
echo -e "\033[31m Uses: sudo $0 \e[0m"
exit 100
fi
# Capture Errors
OwnError()
{
echo -e "[ `date` ] \033[31m $@ \e[0m" | tee -ai $ERRORLOG
exit 101
}
# Make Variables Available For Later Use
LOGDIR=/var/log/easyengine
MAINLOG=/var/log/easyengine/main.log
ERRORLOG=/var/log/easyengine/error.log
# Help Function
EngineHelp()
{
echo
echo "Usage: `basename $0` [OPTION] [ARGUMENT]..."
echo "Admin tools for nginx based wordpress sites management."
echo
echo "OPTION:"
echo " `basename $0` [system] [install|remove|purge] [nginx|php|mysql|postfix|--all]"
echo
echo " `basename $0` [site] [read] [all|active|sitename]"
echo " `basename $0` [site] [create] [sitename] [--with-wordpress]"
echo " `basename $0` [site] [update] [sitename] []"
echo " `basename $0` [site] [delete] [sitename] [--with-data]"
echo
echo " `basename $0` [config] [set|get] [memory|timeout]"
echo
echo "Exit status:"
echo " 0 if OK,"
echo " 100 sudo privilege required,"
echo " 101 command failed to execute,"
}
# GET Information Function
GETINFO()
{
# Get The MySQL Username/Password
read -p "MySQL Host [localhost]: " TEMP
read -p "Enter The MySQL Username: " MYSQLUSER
# Turn Off Echo For Passwords
stty -echo
read -p "Enter The MySQL Password: " MYSQLPASS
stty echo
echo
read -p "Enter The MySQL Database Name For $DOMAIN: " WPDBNAME
# If Enter Is Pressed, Then Use localhost as MySQL Host
if [[ $TEMP = "" ]]
then
MYSQLHOST=localhost
else
MYSQLHOST=$TEMP
fi
# Create Database
mysql -u $MYSQLUSER -p$MYSQLPASS -e "create database \`$WPDBNAME\`" \
|| OwnError "Unable to create $WPDBNAME"
# Nginx User
NGINXUSER=$(grep user /etc/nginx/nginx.conf | cut -d' ' -f2 | cut -d';' -f1) \
|| OwnError "Unable to findout nginx user"
}
NGINXRELOAD()
{
# Test & Reload Nginx
echo -e "\033[34m Reloading nginx, please wait... \e[0m"
nginx -t && service nginx reload || OwnError "Unable to reload nginx"
}
# Pre Checks To Avoid Later Screw Ups
# Checking Logs Directory
if [ ! -d $LOGDIR ]
then
echo -e "\033[34m Creating easyengine log directory... \e[0m"
mkdir -p $LOGDIR || OwnError "Unable to create log directory $LOGDIR"
fi
# Checking Tee
if [ ! -x /usr/bin/tee ]
then
echo -e "\033[31m Tee command not found !! \e[0m"
echo -e "\033[34m Installing tee \e[0m"
sudo apt-get -y install coreutils || OwnError "Unable to install tee"
fi
# Checking Wget
if [ ! -x /usr/bin/wget ]
then
echo -e "\033[31m Wget command not found !! \e[0m"
echo -e "\033[34m Installing wget \e[0m"
sudo apt-get -y install wget || OwnError "Unable to install wget"
fi
# Checking Tar
if [ ! -x /bin/tar ]
then
echo -e "\033[31m Tar command not found !! \e[0m"
echo -e "\033[34m Installing tar \e[0m"
sudo apt-get -y install tar || OwnError "Unable to install tar"
fi
# Checking Name Servers
if [[ -z $(cat /etc/resolv.conf | grep -v ^#) ]]
then
echo -e "\033[31m No nameservers detected !! \e[0m" | tee -ai $ERRORLOG
echo -e "\033[31m Please configure /etc/resolv.conf \e[0m" | tee -ai $ERRORLOG
exit 100
fi
# Pre Checks End
# Easy Engine System Settings
if [ "$1" = "system" ]
then
# Easy Engine Install
if [ "$2" = "install" ]
then
if [ "$3" = "nginx" ]
then
# Install Python Software Properties
echo -e "\033[34m Installing python software properties... \e[0m"
sudo apt-get -y install python-software-properties \
|| OwnError "Unable to install python software properties"
# Add Nginx Launchpad Repository
echo -e "\033[34m Adding nginx launchpad repository... \e[0m"
sudo add-apt-repository ppa:nginx/stable \
|| OwnError "Unable to add nginx launchpad repository"
# Update The APT Cache
echo -e "\033[34m Updating apt cache... \e[0m"
sudo apt-get update || OwnError "Unable to update apt cache"
# Install Nginx
echo -e "\033[34m Installing nginx... \e[0m"
sudo apt-get -y install nginx || OwnError "Unable to install nginx"
elif [ "$3" = "php" ]
then
# Install Python Software Properties
echo -e "\033[34m Installing python software properties... \e[0m"
sudo apt-get -y install python-software-properties \
|| OwnError "Unable to install python software properties"
# Add PHP Launchpad Repository
echo -e "\033[34m Adding php5 launchpad repository... \e[0m"
sudo add-apt-repository ppa:ondrej/php5 \
|| OwnError "Unable to add php5 launchpad repository"
# Update The APT Cache
echo -e "\033[34m Updating apt cache... \e[0m"
sudo apt-get update || OwnError "Unable to update apt cache"
# Install PHP5
echo -e "\033[34m Installing php5... \e[0m"
sudo apt-get -y install php5-common php5-mysql php5-xmlrpc \
php5-cgi php5-curl php5-gd php5-cli php5-fpm php-apc php-pear \
php5-dev php5-imap php5-mcrypt || OwnError "Unable to install php5"
elif [ "$3" = "mysql" ]
then
# Update The APT Cache
echo -e "\033[34m Updating apt cache... \e[0m"
sudo apt-get update || OwnError "Unable to update apt cache"
# Install MySQL
echo -e "\033[34m Installing mysql... \e[0m"
sudo apt-get -y install mysql-server mysqltuner \
|| OwnError "Unable to install mysql"
elif [ "$3" = "postfix" ]
then
# Update The APT Cache
echo -e "\033[34m Updating apt cache... \e[0m"
sudo apt-get update || OwnError "Unable to update apt cache"
# Install Postfix
echo -e "\033[34m Installing postfix... \e[0m"
sudo apt-get -y install postfix || OwnError "Unable to install postfix"
elif [ "$3" = "--all" ]
then
# Install Python Software Properties
echo -e "\033[34m Installing python software properties... \e[0m"
sudo apt-get -y install python-software-properties \
|| OwnError "Unable to install python software properties"
# Add Nginx Launchpad Repository
echo -e "\033[34m Adding nginx launchpad repository... \e[0m"
sudo add-apt-repository ppa:nginx/stable \
|| OwnError "Unable to add nginx launchpad repository"
# Add PHP Launchpad Repository
echo -e "\033[34m Adding php5 launchpad repository... \e[0m"
sudo add-apt-repository ppa:ondrej/php5 \
|| OwnError "Unable to add php5 launchpad repository"
# Update The APT Cache
echo -e "\033[34m Updating apt cache... \e[0m"
sudo apt-get update || OwnError "Unable to update apt cache"
# Install Nginx PHP5 MySQL Postfix
echo -e "\033[34m Installing nginx php5 mysql postfix... \e[0m"
sudo apt-get -y install nginx php5-common php5-mysql php5-xmlrpc \
php5-cgi php5-curl php5-gd php5-cli php5-fpm php-apc php-pear \
php5-dev php5-imap php5-mcrypt mysql-server mysqltuner postfix \
|| OwnError "Unable to install nginx php5 mysql postfix"
else
EngineHelp
fi
# Easy Engine Remove
elif [ "$2" = "remove" ]
then
if [ "$3" = "nginx" ]
then
# Remove Nginx
echo -e "\033[34m Removing nginx... \e[0m"
sudo apt-get -y remove nginx || OwnError "Unable to remove nginx"
elif [ "$3" = "php" ]
then
# Remove PHP5
echo -e "\033[34m Removing php5... \e[0m"
sudo apt-get -y remove php5-common php5-mysql php5-xmlrpc \
php5-cgi php5-curl php5-gd php5-cli php5-fpm php-apc php-pear \
php5-dev php5-imap php5-mcrypt || OwnError "Unable to remove php5"
elif [ "$3" = "mysql" ]
then
# Remove MySQL
echo -e "\033[34m Removing mysql... \e[0m"
sudo apt-get -y remove mysql-server mysqltuner \
|| OwnError "Unable to remove mysql"
elif [ "$3" = "postfix" ]
then
# Remove Postfix
echo -e "\033[34m Removing postfix... \e[0m"
sudo apt-get -y remove postfix || OwnError "Unable to remove postfix"
elif [ "$3" = "--all" ]
then
# Remove All
echo -e "\033[34m Removing nginx php5 mysql postfix... \e[0m"
sudo apt-get -y remove nginx php5-common php5-mysql php5-xmlrpc \
php5-cgi php5-curl php5-gd php5-cli php5-fpm php-apc php-pear \
php5-dev php5-imap php5-mcrypt mysql-server mysqltuner postfix \
|| OwnError "Unable to remove nginx php5 mysql postfix"
else
EngineHelp
fi
# Easy Engine Purge
elif [ "$2" = "purge" ]
then
if [ "$3" = "nginx" ]
then
# Purge Nginx
echo -e "\033[34m Purge nginx... \e[0m"
sudo apt-get -y remove --purge nginx || OwnError "Unable to purge nginx"
elif [ "$3" = "php" ]
then
# Purge PHP5
echo -e "\033[34m Purge php5... \e[0m"
sudo apt-get -y remove --purge php5-common php5-mysql php5-xmlrpc \
php5-cgi php5-curl php5-gd php5-cli php5-fpm php-apc php-pear \
php5-dev php5-imap php5-mcrypt || OwnError "Unable to purge php5"
elif [ "$3" = "mysql" ]
then
# Purge MySQL
echo -e "\033[34m Purge mysql... \e[0m"
sudo apt-get -y remove --purge mysql-server mysqltuner \
|| OwnError "Unable to purge mysql"
elif [ "$3" = "postfix" ]
then
# Purge Postfix
echo -e "\033[34m Purge postfix... \e[0m"
sudo apt-get -y remove --purge postfix || OwnError "Unable to purge postfix"
elif [ "$3" = "--all" ]
then
# Purge All
echo -e "\033[34m Purge nginx php5 mysql postfix... \e[0m"
sudo apt-get -y remove --purge nginx php5-common php5-mysql php5-xmlrpc \
php5-cgi php5-curl php5-gd php5-cli php5-fpm php-apc php-pear \
php5-dev php5-imap php5-mcrypt mysql-server mysqltuner postfix \
|| OwnError "Unable to purge nginx php5 mysql postfix"
else
EngineHelp
fi
# Easy Engine Help
else
EngineHelp
fi
# Easy Engine Site Settings
elif [ "$1" = "site" ]
then
# Easy Engine Read
if [ "$2" = "read" ]
then
if [ "$3" = "all" ]
then
# Display The List Of All Sites
ls /etc/nginx/sites-available/ \
|| OwnError "Unable to display the list of websites"
elif [ "$3" = "active" ]
then
# Display The List Of Active Sites
ls /etc/nginx/sites-enabled/ \
|| OwnError "Unable to display the list of active websites"
elif [ "$3" != "all" ] && [ "$3" != "active" ] && [ -n "$3" ]
then
# Check The Website Is Exist
ls /etc/nginx/sites-available/$3 &> /dev/null \
|| OwnError "The $3 is not found in available websites list"
if [ $? -eq 0 ]
then
# Display The Specific Site Configuration
cat /etc/nginx/sites-available/$3 \
|| OwnError "Unable to display the $3 configuration settings"
fi
else
EngineHelp
fi
# Easy Engine Create
elif [ "$2" = "create" ]
then
if [ -n "$3" ]
then
# Remove http:// & www.
DOMAIN=$(echo $3 | sed "s'http://''" | sed "s'www.''")
# Creating Site
echo -e "\033[34m Creating $DOMAIN, please wait... \e[0m"
sed "s/example.com/$DOMAIN/g" ../conf/nginx/singlesite/basic.conf \
> /etc/nginx/sites-available/$DOMAIN \
|| OwnError "Unable to create configuration file for $DOMAIN"
# Creating Symbolic Link
echo -e "\033[34m Creating symbolic link \e[0m"
ln -s /etc/nginx/sites-available/$DOMAIN /etc/nginx/sites-enabled/ \
|| OwnError "Unable to create symbolic link for $DOMAIN"
# Creating Htdocs & Logs Directory
echo -e "\033[34m Creating htdocs and logs directory \e[0m"
mkdir -p /var/www/$DOMAIN/{htdocs,logs} \
|| OwnError "Unable to create htdocs and logs directory"
# Creating Symbolic Links For Logs
echo -e "\033[34m Creating symbolic link for logs \e[0m"
ln -s /var/log/nginx/$DOMAIN.access.log /var/www/$DOMAIN/logs/access.log \
&& ln -s /var/log/nginx/$DOMAIN.error.log /var/www/$DOMAIN/logs/error.log \
|| OwnError "Unable to create symbolic link for $DOMAIN logs"
if [ "$4" = "--with-wordpress" ]
then
# Download Latest Wordpress
echo -e "\033[34m Downloading wordpress \e[0m"
wget -cO /var/www/$DOMAIN/htdocs/latest.tar.gz \
http://wordpress.org/latest.tar.gz
# Extracting Wordpress
tar --strip-components=1 -zxf /var/www/$DOMAIN/htdocs/latest.tar.gz \
-C /var/www/$DOMAIN/htdocs/
# Removing Wordpress Archive
rm /var/www/$DOMAIN/htdocs/latest.tar.gz
# Call GETINFO Function For MySQL & Nginx Values
GETINFO
# Modify wp-config.php
cp -v /var/www/$DOMAIN/htdocs/wp-config-sample.php \
/var/www/$DOMAIN/htdocs/wp-config.php
sed -i "s/username_here/$MYSQLUSER/" \
/var/www/$DOMAIN/htdocs/wp-config.php
sed -i "s/password_here/$MYSQLPASS/" \
/var/www/$DOMAIN/htdocs/wp-config.php
sed -i "s/localhost/$MYSQLHOST/" \
/var/www/$DOMAIN/htdocs/wp-config.php
printf '%s\n' "g/put your unique phrase here/d" \
a "$(curl -L https://api.wordpress.org/secret-key/1.1/salt/)" . w \
| ed -s wp-config.php
# Change Ownership
echo -e "\033[34m Changing ownership \e[0m"
chown -R $NGINXUSER:$NGINXUSER /var/www/$DOMAIN/
fi
# Reload Nginx Configuration
NGINXRELOAD
else
EngineHelp
fi
# Easy Engine Update
elif [ "$2" = "update" ]
then
if [ -n "$3" ]
then
# Remove http:// & www.
DOMAIN=$(echo $3 | sed "s'http://''" | sed "s'www.''")
if [ "$4" = "" ]
then
# Updating Site
else
EngineHelp
fi
else
EnginHelp
fi
# Easy Engine Delete
elif [ "$2" = "delete" ]
then
echo "Delete"
# Easy Engine Help
else
EngineHelp
fi
#echo "Under Developments !!"
# Easy Engine Config Settings
elif [ "$1" = "config" ]
then
echo "Under Developments !!"
# Easy Engine Help
else
EngineHelp
fi