Last active
June 22, 2017 20:42
-
-
Save mansouryaacoubi/9e918faf583e341e8cff4f35dfee906f to your computer and use it in GitHub Desktop.
Install LAMP on Linux Systems (Apache, MySQL, PHP)
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 | |
# @discription Installation script for lamp | |
# @author Mansour Yaacoubi | |
# @filename install-lamp.sh | |
# @todos add better documentation, add support for other distributions, | |
# create light-version of this tool | |
# | |
# Copyright 2017 myblog | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# | |
# Find out ubuntu version | |
lsb_release -r | grep '15' | |
if [ $? == 0 ]; then | |
ver=15 | |
else | |
ver=16 | |
fi | |
################# VARIABLES SECTION ################# | |
# Global paths vars | |
tmp="/tmp" | |
dpkglock="/var/lib/dpkg/lock" | |
# MySQL root password (can be changed here; default: q0sm0tec) | |
mysql_pass="q0sm0tec" | |
# First Terminal vars | |
col=61 | |
row=31 | |
size=$col"x"$row # default: 61x31 (61 characters and 31 lines) | |
# Second Terminal vars | |
statusterm="$tmp/tmp_newterminaltty" # Status Terminal ID Temp file | |
term2="/dev/pts/0" # Status Terminal ID | |
# Formatted strings (done; error; (deprecated)) | |
ok=" \e[1m-> \e[0;32m\e[1mdone\e[0m\e[0m\e[0m" | |
err=" \e[1m-> \e[0;31m\e[1merror\e[0m\e[0m\e[0m" | |
depr="(deprecated)" | |
cleanupstr="Cleanup finished" | |
nl='\\n' | |
# Files to be created | |
phpinfofile="index.php" | |
phpinfocontent='<?php phpinfo(); ?>' | |
mtestfile="mtest.html" # myblog Test File | |
mtestcontent='<!DOCTYPE html><html><head><title>myblog Test Page</title></head><body><h1>myblog HTML Test Page</h1><p>If you see this, you're Test was <span style="color:green;">successful.</span></p></body></html>' | |
# Dirconf vars | |
dirconfpath="/etc/apache2/mods-enabled" | |
dirconf="dir.conf" | |
# Banners | |
banner_lamp=" __ ___ __ _______ \\n / / / | / |/ / __ \\ \\n / / / /| | / /|_/ / /_/ /\\n / /___/ ___ |/ / / / ____/ \\n/_____/_/ |_/_/ /_/_/ \\n\\n" | |
banner_inst=" ____ __ ____ \\n / _/___ _____/ /_____ _/ / /__ _____\\n / // __ \/ ___/ __/ __ \`/ / / _ \/ ___/\\n _/ // / / (__ ) /_/ /_/ / / / __/ / \\n/___/_/ /_/____/\__/\__,_/_/_/\___/_/ \\n\\n" | |
# Banner count lines | |
blampn=$(grep -o "$nl" <<< "$banner_lamp" | wc -l); | |
binstn=$(grep -o "$nl" <<< "$banner_inst" | wc -l); | |
# Variables depending Ubuntu version | |
if [ $ver -ge 16 ]; then | |
htdocs="/var/www/html" | |
apachepack="apache2" | |
phppack="libapache2-mod-php7.0 php7.0 php-zip" | |
mysqlpack="php7.0-mysql mysql-server" | |
else | |
htdocs="/var/www" | |
apachepack="apache2" | |
phppack="libapache2-mod-php5 php5 php5.6-zip" | |
mysqlpack="php5-mysql mysql-server" | |
fi | |
################# FUNCTIONS SECTION ################# | |
# http://misc.flogisoft.com/bash/tip_colors_and_formatting | |
bold() { ansi 1 "$@"; } | |
italic() { ansi 3 "$@"; } | |
underline() { ansi 4 "$@"; } | |
inverted() { ansi 7 "$@"; } | |
strikethrough() { ansi 9 "$@"; } | |
red() { ansi 31 "$@"; } | |
green() { ansi 32 "$@"; } | |
yellow() { ansi 33 "$@"; } | |
blue() { ansi 34 "$@"; } | |
cyan() { ansi 36 "$@"; } | |
lgray() { ansi 37 "$@"; } | |
lblue() { ansi 94 "$@"; } | |
ansi() { echo -e "\e[${1}m${*:2}\e[0m"; } | |
sendmsg() { | |
if [ -z "$1" ]; then | |
echo > $term2 | |
else | |
echo -en "$1" > $term2 | |
fi | |
} | |
sendok() { | |
if [ -z "$1" ]; then | |
echo -e $ok > $term2 | |
else | |
okmsg=$(bold $1) | |
okmsg=$(green $okmsg) | |
echo -en "\\n$okmsg" > $term2 | |
fi | |
} | |
senderr() { | |
if [ -z "$1" ]; then | |
echo -e $err > $term2 | |
else | |
errmsg=$(bold $1) | |
errmsg=$(red $errmsg) | |
echo -e "\\n$errmsg" > $term2 | |
fi | |
} | |
sendcls() { | |
clear > $term2 | |
} | |
checkErrors() { # $1 = errorid, $2 = errormessage, $3 = exitProgramAfterErrorFlag (default=yes) | |
if [ $1 -eq 0 ]; then | |
sendok | |
else | |
senderr | |
if [ "$2" != "" ] && ([ -z "$3" ] || [ "$3" != "no" ]); then senderr "$2"; fi | |
if [ -z "$3" ]; then cleanup "$2"; exit 1; fi; | |
fi | |
} | |
cleanup() { # $1=Process Name | |
# Remove test files from htdocs | |
if [ -d $htdocs ]; then | |
cd $htdocs | |
if [ -f $phpinfofile ];then rm -f $phpinfofile; fi | |
if [ -f $mtestfile ]; then rm -f $mtestfile; fi | |
fi | |
# Remove test files from tmp-directory | |
if [ -d $tmp ]; then | |
cd $tmp | |
if [ -f $phpinfofile ];then rm -f $phpinfofile; fi | |
if [ -f $mtestfile ]; then rm -f $mtestfile; fi | |
if [ -f $statusterm ]; then rm -f $statusterm; fi | |
fi | |
if [ -z "$1" ] || [ "$1" == "" ]; then echo -e "$cleanupstr"; else echo -e "$cleanupstr ($1)"; fi; | |
} | |
repeat() { printf "$1"'%.s' $(eval "echo {1.."$(($2))"}"); } | |
showbanner() { | |
### BANNER | |
tmpcounter=$(($row - 1)) | |
sendcls | |
sendmsg "$banner_lamp"; | |
tmpcounter=$(($tmpcounter - $blampn)) | |
sleep 1 | |
sendmsg "$banner_inst"; | |
tmpcounter=$(($tmpcounter - $binstn)) | |
sleep 0.5 | |
tmpFooter=$(repeat "$nl" "$tmpcounter")" Copyright (c) 2017 myblog" | |
sendmsg "$tmpFooter" | |
sleep 2.5 | |
sendcls | |
} | |
################# FORMATTING TEXT ################# | |
# Font styles | |
banner_lamp=$(bold "$banner_lamp") | |
banner_inst=$(bold "$banner_inst") | |
depr=$(italic "$depr") | |
cleanupstr=$(bold "$cleanupstr") | |
# Font colors | |
banner_lamp=$(yellow "$banner_lamp") | |
banner_inst=$(lgray "$banner_inst") | |
cleanupstr=$(cyan "$cleanupstr") | |
################# CHECK WHETHER SCRIPT IS RUNNING AS ROOT ################# | |
# Run script as root to be able to install software like | |
# LAMP, composer and laravel | |
if [ "$EUID" -ne 0 ] | |
then echo "Please run as root" | |
exit | |
else | |
clear | |
echo | |
echo "Script is running with higher privileges as: $SUDO_USER -> $(whoami)" | |
echo | |
fi | |
################# DPKG-LOCK CHECK START ################# | |
procid=$(fuser $dpkglock 2>/dev/null) | |
while : | |
do | |
if [ "$procid" == "" ]; then | |
echo "$dpkglock is free to use. Continue..." | |
break | |
else | |
procname=$(ps -p $procid -o comm=) | |
procname=$(red $procname) | |
procname=$(bold $procname) | |
echo -e "$dpkglock temporarily unavailable.\\nProcess called $procname ($procid) is using it.\\n" | |
echo -en "Would you like to kill process ($procname) now? [y/n] " | |
read -n1 ans | |
echo "" | |
if [ "$ans" == "n" ] || [ "$ans" == "N" ]; then | |
echo "Exiting script without killing blocking process." | |
exit 1 | |
fi | |
procid=$(fuser -k $dpkglock 2>/dev/null) | |
fi | |
done | |
################## DPKG-LOCK CHECK END ################## | |
################# OPEN/FIND TERMINAL START ################# | |
# Open new windows to show progress output | |
echo "Open status update terminal" | |
gnome-terminal -e "bash -c \"tty>$statusterm;bash\"" --hide-menubar --geometry $size & disown | |
echo -ne "Find terminal pts" | |
while [ ! -f $statusterm ] | |
do | |
echo -ne "." | |
sleep 0.008 | |
done | |
echo | |
term2=$(cat $statusterm) | |
if [ "$term2" == "" ]; then echo -e $(bold $(red "\\nRetrying...")); sleep 2; term2=$(cat $statusterm); fi; | |
if [ "$term2" == "" ]; then echo -e $(bold $(red "Couldn't find second terminal.\\nPlease restart script.")); exit; fi; | |
cleanup "Open second terminal" | |
echo | |
echo "Found terminal ($term2)" | |
sleep 3 | |
sendcls | |
################## OPEN/FIND TERMINAL END ################## | |
################# PREPARATION & CLEANUP START ################# | |
# Start cleanup | |
sendmsg "Remove package files from local repo that can no longer\\n" | |
sendmsg "be downloaded, and are largely useless" | |
apt-get autoclean -y | |
checkErrors $? "Autocleaning local repository failed" | |
sendmsg | |
sendmsg "Update local repository and programs" | |
apt-get update -y | |
checkErrors $? "Updating local repository failed" | |
sleep 3 | |
################## PREPARATION & CLEANUP END ################## | |
###################################################### | |
################# INSTALL LAMP START ################# | |
showbanner "$banner_lamp" | |
# Installing LAMP (Linux Apache, MySQL and PHP) | |
sendmsg "## Installing LAMP-Server for $(lsb_release -ds) ##" | |
sendmsg | |
sendmsg " (1) Install Apache Server" | |
apt-get install -y $apachepack | |
checkErrors $? "Installing Apache packages failed" | |
sleep 2 | |
sendmsg | |
sendmsg " (2) Install MySQL DBMS" | |
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password $mysql_pass" | |
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $mysql_pass" | |
apt-get install -y $mysqlpack | |
checkErrors $? "Installing MySQL packages failed" | |
sleep 2 | |
tmpstr=" (2.1) "$(strikethrough "Activate MySQL")" $depr" | |
sendmsg "$tmpstr" | |
mysql_install_db | |
checkErrors $? "Activating MySQL failed" "no" | |
sendmsg " (2.1.1) Initializing MySQL Daemon" | |
mysqld --initialize | |
checkErrors $? "Initializing MySQL failed" "no" | |
sleep 2 | |
sendmsg " (2.2) Run MySQL setup-script\\n" | |
# To automate mysql_secure_installation, use expect | |
# START AUTOMATION | |
sendmsg " (2.2.1) Install expect-package for automation" | |
apt-get install -y expect | |
checkErrors $? "Installing expect-package failed" | |
SECURE_MYSQL=$(expect -c " | |
set timeout 10 | |
spawn /usr/bin/mysql_secure_installation | |
expect \"Enter current password for root (enter for none):\" | |
send \"$mysql_pass\r\" | |
expect \"Would you like to setup VALIDATE PASSWORD plugin?\" | |
send \"n\r\" | |
expect \"Change the root password?\" | |
send \"n\r\" | |
expect \"Remove anonymous users?\" | |
send \"y\r\" | |
expect \"Disallow root login remotely?\" | |
send \"y\r\" | |
expect \"Remove test database and access to it?\" | |
send \"y\r\" | |
expect \"Reload privilege tables now?\" | |
send \"y\r\" | |
expect eof | |
") | |
sendmsg " (2.2.2) Run setup-script with expect-syntax" | |
echo "$SECURE_MYSQL" | |
checkErrors $? "Running setup-script failed" | |
sendmsg " (2.2.3) Remove expect-package" | |
apt-get -y purge expect | |
checkErrors $? "Removing expect-package failed" | |
# END AUTOMATION | |
sleep 2 | |
sendmsg | |
sendmsg " (3) Install PHP" | |
apt-get install -y $phppack | |
checkErrors $? "Installing PHP packages failed" | |
cd $dirconfpath | |
sendmsg " (3.1) Backup $dirconf" | |
cp $dirconf $dirconf.bak | |
checkErrors $? "Backing up $dirconf failed" | |
sendmsg " (3.2) Add $phpfile to $dirconf" | |
exists=$(cat $dirconf | grep "index.php") | |
if [ "$exists" != "" ]; then sed -i "s/DirectoryIndex/& index.php/" $dirconf; fi; | |
checkErrors $? "Adding $phpfile to $dirconf failed" | |
sendmsg " (3.3) Install colordiff-package to show changes" | |
apt-get install -y colordiff | |
checkErrors $? "Installing colordiff-package failed" | |
sendmsg " (3.4) Show changes made in $dirconf" | |
colordiff $dirconf.bak $dirconf | |
if [ $? -ne 0 ]; then | |
sendok | |
else | |
senderr | |
senderr "Showing changes in $dirconf failed" | |
exit | |
fi | |
sendmsg " (3.5) Remove colordiff-package" | |
apt-get purge -y colordiff | |
checkErrors $? "Removing colordiff-package failed" | |
sendmsg | |
sendmsg " (4) Test Server\\n" | |
cd $htdocs | |
sendmsg " (4.1) Create test files\\n" | |
sendmsg " (4.1.1) Create phpinfo-file" | |
echo $phpinfocontent > $phpinfofile | |
checkErrors $? "Creating phpinfo-file failed" | |
sendmsg " (4.1.2) Create Qosmotec HTML Test webpage" | |
echo $mtestcontent > $mtestfile | |
checkErrors $? "Creating $mtestfile failed" | |
sendmsg " (4.2) Restart apache-server" | |
service apache2 restart | |
checkErrors $? "Restarting apache-server failed" | |
sendmsg " (4.3) Check if server is reachable" | |
ping -c1 localhost | |
checkErrors $? "Server localhost is not reachable" | |
sendmsg " (4.4) Check if webpage and phpinfo are reachable\\n" | |
cd $tmp | |
sendmsg " (4.4.1) Download $mtestfile (myblogTest)" | |
wget http://localhost/$mtestfile | |
checkErrors $? "$mtestfile could not be downloaded" | |
sendmsg " (4.4.2) Download $phpinfofile (phpinfo)" | |
wget http://localhost/$phpinfofile | |
checkErrors $? "$phpinfofile could not be downloaded" | |
sendmsg " (4.4.3) Check Downloads and compare" | |
if [ -f $phpinfofile ] && [ -f $mtestfile ] && cmp -s $mtestfile $htdocs/$mtestfile | |
then | |
sendok | |
else | |
senderr | |
senderr "Files could not be reached or are not the same" | |
exit | |
fi | |
sleep 1 | |
sendmsg " (4.4.4) Open phpinfo and $mtestfile in browser" | |
sleep 1 | |
#sensible-browser "http://localhost/" & | |
firefox "http://localhost/$phpinfofile" & | |
sleep 5 | |
firefox "http://localhost/$mtestfile" & | |
sleep 5 | |
killall firefox | |
sleep 2 | |
checkErrors $? "Opening phpinfo and $mtestfile in webbrowser failed" | |
sendok "LAMP-Server has been installed successfully." | |
cleanup "LAMP installation" | |
sleep 5 | |
################## INSTALL LAMP END ################## | |
###################################################### | |
# LAMP Installation finished |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment