Last active
January 30, 2020 18:36
-
-
Save pentagonal/bac12664d99ecbc45d3fe13662cc8cab to your computer and use it in GitHub Desktop.
Ubuntu MySQL + Php (7.2) + Nginx (+Redis) Automated Installer
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 | |
# | |
# Pentagonal | |
# Ubuntu (Based) Php, Nginx, MariaDB Installer | |
# youtube: https://www.youtube.com/pentagonalOrg | |
# email: [email protected] | |
# | |
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 prerequired 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.2\e[0m '; | |
doCommand 'sudo apt-get -y install imagemagick php-imagick php7.2-fpm php7.2-opcache php7.2 php7.2-curl php7.2-gd php7.2-imap php7.2-intl php7.2-json php7.2-mbstring php7.2-mysql php7.2-pgsql php7.2-sqlite3 php7.2-xml php7.2-xmlrpc php7.2-xsl php7.2-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.2-fpm\e[0m '; | |
doCommand 'sudo service php7.2-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.2-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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install
wget -q -O - https://gist.github.com/pentagonal/bac12664d99ecbc45d3fe13662cc8cab/raw/installer.sh | bash
You can use automatically without prompt :
Download Installer first
And run installer
add
-y
as arguments to install all base required components.