Skip to content

Instantly share code, notes, and snippets.

@pentagonal
Last active February 21, 2019 20:14
Show Gist options
  • Save pentagonal/c404fede46a543466325d8436dc0ff06 to your computer and use it in GitHub Desktop.
Save pentagonal/c404fede46a543466325d8436dc0ff06 to your computer and use it in GitHub Desktop.
Ubuntu MySQL + Php (7.1) + Nginx (+Redis) Automated Installer
#!/bin/bash
#
# Pentagonal
# Ubuntu (Based) Php, Nginx, MariaDB Installer
# youtube: https://www.youtube.com/pentagonalOrg
# email: org@pentagonal.org
#
isDefaultOk=false;
if [[ "$#" -eq 1 && ( $1 == '-y' || $1 == '-Y' ) ]]; then
isDefaultOk=true
fi
function cancel()
{
echo
echo -e '\e[41m \e[0m';
echo -e '\e[41m Installation Aborted \e[0m';
echo -e '\e[41m \e[0m';
echo
exit 1;
}
function spinPid()
{
pid=$1
sp="/-\|"
sc=0
echo -n " "
until [[ -z $(ps -A | grep $pid) ]];do
printf "\b\b\b[${sp:sc++:1}]"
((sc==${#sp})) && sc=0
sleep .03
done
printf "\b\b\b[\e[32m✓\e[0m]\n"
}
function doCommand()
{
$1 &> /dev/null &
spinPid $!
}
ask() {
# https://djm.me/ask
local prompt default REPLY
while true; do
if [ "${2:-}" = "Y" ]; then
prompt="Y/n"
default=Y
elif [ "${2:-}" = "N" ]; then
prompt="y/N"
default=N
else
prompt="y/n"
default=
fi
# Ask the question (not using "read -p" as it uses stderr not stdout)
echo -n "$1 [$prompt] ";
# Read the answer (use /dev/tty in case stdin is redirected from somewhere else)
read REPLY </dev/tty
# Default?
if [ -z "$REPLY" ]; then
REPLY=$default
fi
# Check if the reply is valid
case "$REPLY" in
Y*|y*) return 0 ;;
N*|n*) return 1 ;;
esac
done
}
# clearing screen
printf "\033c"
echo '█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█';
echo '█ █';
echo '█ Installer █';
echo '█ █';
echo '█▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█';
echo
sudo ls ~ &> /dev/null
if [ $? -eq 1 ]; then
cancel
fi
unknown=""
isInstallMariaDB=false
echo -en '\e[32mTesting connectivity \e[0m';
unknown=$(ping -q -w 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` > /dev/null && echo -en 'true' || echo -en)
if [[ -z $unknown ]]; then
echo -e ' [\e[31mx\e[0m]';
echo
echo -e '\e[41m \e[0m';
echo -e '\e[41m Please check your connection. \e[0m';
echo -e '\e[41m \e[0m';
echo
exit 1;
fi
echo -e " [\e[32m✓\e[0m]"
# install common usage
echo -en '\e[32mInstalling pre - required dependencies\e[0m';
doCommand "sudo apt-get -y install software-properties-common curl openssl"
# Install Lsb-Release
echo -en '\e[32mDetecting Operating System\e[0m ';
doCommand "sudo apt-get -y install lsb-release"
LSBRelase=false
if [ -f /etc/upstream-release/lsb-release ]; then
LSBRelase="/etc/upstream-release/lsb-release";
elif [ -f /etc/lsb-release ]; then
LSBRelase="/etc/lsb-release";
fi
if [ LSBRelase == false ]; then
echo
echo -e '\e[41m \e[0m';
echo -e '\e[41m Could not detect Ubuntu versions ! \e[0m';
echo -e '\e[41m \e[0m';
echo
exit 1
fi
A_TYPE=`grep -oiP '(?<=DISTRIB_ID=)(Ubuntu)$' $LSBRelase`
A_CODE=`grep -oiP '(?<=DISTRIB_CODENAME=)(.+)$' $LSBRelase`
A_RELEASE=`grep -oiP '(?<=DISTRIB_RELEASE=)(.+)$' $LSBRelase`
A_DESCRIPTION=`grep -oiP '(?<=DISTRIB_DESCRIPTION=)(.+)$' $LSBRelase`
if [[ -z $A_TYPE || -z $A_CODE ]]; then
echo
echo -e '\e[41m \e[0m';
echo -e '\e[41m Could not detect Ubuntu versions ! \e[0m';
echo -e '\e[41m \e[0m';
echo
exit 1;
fi
if [ $isDefaultOk != true ]; then
echo
echo '▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀';
echo
echo -e "- Operating System : \\e[33m${A_TYPE}\\e[0m";
echo -e "- Code Name : \\e[33m${A_CODE}\\e[0m";
echo -e "- Release : \\e[34m$A_RELEASE\\e[0m";
echo
echo '▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀';
echo
if ! ask "This is correct your \"Operating System\" ?" Y; then
cancel
fi
echo
echo '█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█';
echo '█ █';
echo '█ Begin Installing Php MySQL Nginx █';
echo '█ █';
echo '█▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█';
echo
else
echo
echo '█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█';
echo '█ █';
echo '█ Begin Installing Php MySQL Nginx █';
echo '█ █';
echo '█▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█';
echo
echo -e "- Operating System : \\e[33m${A_TYPE}\\e[0m";
echo -e "- Code Name : \\e[33m${A_CODE}\\e[0m";
echo -e "- Release : \\e[34m$A_RELEASE\\e[0m";
echo
fi
if ! which mysql > /dev/null 2>&1; then
# installing MariaDB
echo -en '\e[32mAdding MariaDB Key\e[0m ';
doCommand "sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8"
# try to remove
echo -en '\e[32mRemoving Old MariaDB Repository\e[0m ';
sudo add-apt-repository -y --remove "deb [arch=amd64,i386,ppc64el] http://sgp1.mirrors.digitalocean.com/mariadb/repo/10.2/ubuntu ${A_CODE} main" &> /dev/null &
spinPid $!
# try to add
echo -en '\e[32mAdding MariaDB Repository\e[0m ';
sudo add-apt-repository -y "deb [arch=amd64,i386,ppc64el] http://sgp1.mirrors.digitalocean.com/mariadb/repo/10.2/ubuntu ${A_CODE} main" &> /dev/null &
spinPid $!
isInstallMariaDB=true
fi
# try to remove
echo -en '\e[32mRemoving Old PPA PHP Repository\e[0m ';
doCommand 'sudo apt-add-repository -y --remove ppa:ondrej/php'
# installing Php
echo -en '\e[32mAdding Php Repository\e[0m ';
doCommand 'sudo apt-add-repository -y ppa:ondrej/php'
# Update
echo -en '\e[32mUpdating dependencies\e[0m ';
doCommand 'sudo apt-get -y update'
if ! which nginx > /dev/null 2>&1; then
# install nginx
echo -en '\e[32mInstalling nginx\e[0m ';
doCommand 'sudo apt-get -y install nginx'
else
echo -en '\e[32mNginx is Installed\e[0m ';
echo -e " [\e[32m✓\e[0m]"
fi
echo -en '\e[32mInstalling Php 7.1\e[0m ';
doCommand 'sudo apt-get -y install imagemagick php-imagick php7.1-fpm php7.1-opcache php7.1-bz2 php7.1-curl php7.1-gd php7.1-imap php7.1-intl php7.1-json php7.1-mbstring php7.1-mcrypt php7.1-mysql php7.1-pgsql php7.1-sqlite3 php7.1-xml php7.1-xmlrpc php7.1-xsl php7.1-zip'
if ! which mysql > /dev/null 2>&1; then
export DEBIAN_FRONTEND="noninteractive"
sudo debconf-set-selections <<< "mariadb-server mysql-server/root_password password mysql"
sudo debconf-set-selections <<< "mariadb-server mysql-server/root_password_again password mysql"
echo -en '\e[32mInstalling MySQL(MariaDB)\e[0m ';
doCommand "sudo apt-get -y install mariadb-server"
echo -en '\e[32mBringing Up MySQL\e[0m ';
doCommand "sudo service mysql restart"
echo -en '\e[32mChange MySQL To Native Password\e[0m ';
sudo -u root mysql -uroot -pmysql -e "use mysql;UPDATE user SET plugin='mysql_native_password';" &> /dev/null &
spinPid $!
echo -en '\e[32mChange MySQL Password to: mysql\e[0m ';
sudo -u root mysql -uroot -pmysql -e "use mysql;UPDATE user SET password=PASSWORD('mysql') WHERE user='root';FLUSH PRIVILEGES;" &> /dev/null &
spinPid $!
isInstallMariaDB=true
else
echo -en '\e[32mMySQL is Installed\e[0m ';
echo -e " [\e[32m✓\e[0m]"
fi
# redis
isRedis=false
if [ $isDefaultOk != true ];then
# Additional Extension
echo
echo -e '\e[32mCommon php extension libraries : \e[0m'
echo -e " \e[34mphp-gettext php-yaml php-phalcon\e[0m"
echo
if ask "Install common php extension libraries ?" Y; then
echo
echo -en '\e[32mInstalling additional libraries\e[0m ';
doCommand "sudo apt-get -y install php-gettext php-yaml php-phalcon"
fi
# Redis
echo
echo -e '\e[32mRedis Cache : \e[0m'
echo -e " \e[34mredis-server php-redis\e[0m"
echo
if ask "Install Redis Server & Php Extension ?" Y; then
if ! which redis-server > /dev/null 2>&1; then
# install redis
echo
echo -en '\e[32mInstalling Php Redis\e[0m ';
doCommand "sudo apt-get -y install redis-server"
else
echo
echo -en '\e[32mRedis Server is Installed\e[0m ';
echo -e " [\e[32m✓\e[0m]"
echo -en '\e[32mInstalling Php Redis\e[0m ';
doCommand "sudo apt-get -y install php-redis"
fi
isRedis=true
fi
else
echo -en '\e[32mInstalling additional resource\e[0m ';
if ! which redis-server > /dev/null 2>&1; then
doCommand "sudo apt-get -y install php-gettext php-yaml php-phalcon redis-server php-redis"
else
doCommand "sudo apt-get -y install php-gettext php-yaml php-phalcon php-redis"
fi
isRedis=true
fi
echo -en '\e[32mRestarting Nginx\e[0m ';
doCommand 'sudo service nginx restart'
echo -en '\e[32mRestarting Php7.1-fpm\e[0m ';
doCommand 'sudo service php7.1-fpm restart'
if [ $isInstallMariaDB == true ];then
echo -en '\e[32mRestarting MySQL\e[0m ';
doCommand "sudo service mysql restart"
fi
if [ $isRedis == true ];then
echo -en '\e[32mRestarting Redis Server\e[0m ';
doCommand "sudo service redis-server restart"
fi
P_CONFIGURATION=$(cat <<CONFIG
include snippets/fastcgi-php.conf;
CONFIG
)
if [ ! -f /etc/nginx/snippets/fastcgi-php.conf ]; then
P_CONFIGURATION=$(cat <<CONFIG
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)\$;
# Check that the PHP script exists before passing it
try_files \$fastcgi_script_name =404;
# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set \$path_info \$fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi.conf;
CONFIG
)
fi
_CONFIGS=$(cat <<CONFIG
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files \$uri \$uri/ /index.php?\$args;
}
location ~ \.php\$ {
${P_CONFIGURATION}
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
}
}
CONFIG
)
FILE_LOCAL='localhost.generated.conf'
NGINX_FILE_CONFIG=`eval echo ~$USER/$FILE_LOCAL`
echo "$_CONFIGS" > $NGINX_FILE_CONFIG
echo
echo -e '\e[44m \e[0m';
echo -e '\e[44m ALL DONE \e[0m';
echo -e '\e[44m \e[0m';
echo
echo "You can put :";
echo " ${NGINX_FILE_CONFIG} to /etc/nginx/sites-enabled/";
echo "Document root :";
echo " /var/www/html"
echo
echo '▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀';
echo
echo "sudo cp ${NGINX_FILE_CONFIG} /etc/nginx/sites-available/";
echo "sudo ln -s /etc/nginx/sites-available/${FILE_LOCAL} /etc/nginx/sites-enabled/";
echo "sudo service nginx restart"
echo
echo '▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀';
echo
if [ $isInstallMariaDB == true ];then
echo
echo 'Your MySQL Password is:'
echo ' mysql'
echo 'you can change root password with:'
echo " mysql -uroot -pmysql -e \"SET PASSWORD = PASSWORD('YourNewPassword');FLUSH PRIVILEGES;\""
echo
fi
@pentagonal
Copy link
Author

pentagonal commented Sep 2, 2017

Install

wget -q -O - https://gist.github.com/pentagonal/c404fede46a543466325d8436dc0ff06/raw/installer.sh | bash

You can use automatically without prompt :

Download Installer first

wget https://gist.github.com/pentagonal/c404fede46a543466325d8436dc0ff06/raw/installer.sh

And run installer

bash installer.sh -y

add -y as arguments to install all base required components.

@pentagonal
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment