Created
September 8, 2021 17:47
-
-
Save jfkhoury/636c314b4a7e40ddb2cf38bdbd43f09d to your computer and use it in GitHub Desktop.
ubuntu
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 | |
# Colors schemes for echo: | |
RD='\033[0;31m' # Red | |
GN='\033[0;32m' # Green | |
MG='\033[0;95m' # Magenta | |
NC='\033[0m' # No Color | |
ERROR_STRING="Installation error. Exiting" | |
CURRENT_PATH=$(pwd) | |
DEFAULT_PHP_VERSION="php7.4" | |
CURRENT_OS=$(grep -e VERSION_ID /etc/os-release | cut -d "=" -f 2 | cut -c 2-3) | |
ERROR_STRING="Installation error. Exiting" | |
CURRENT_PATH=$(pwd) | |
# CHECK FOR KEYS | |
while [[ -n $1 ]]; do | |
case "$1" in | |
--with-oracle) ORACLE=TRUE ;; | |
--with-mysql) MYSQL=TRUE ;; | |
--with-apache) APACHE=TRUE ;; | |
--with-db2) DB2=TRUE ;; | |
--with-cassandra) CASSANDRA=TRUE ;; | |
--with-tag=*) | |
DREAMFACTORY_VERSION_TAG="${1/--with-tag=/}" | |
;; | |
--with-tag) | |
DREAMFACTORY_VERSION_TAG="$2" | |
shift | |
;; | |
--debug) DEBUG=TRUE ;; | |
--help) HELP=TRUE ;; | |
-h) HELP=TRUE ;; | |
*) | |
echo -e "\n${RD}Invalid flag detected… aborting.${NC}" | |
HELP=TRUE | |
break | |
;; | |
esac | |
shift | |
done | |
if [[ $HELP == TRUE ]]; then | |
echo -e "\nList of available keys:\n" | |
echo " --with-oracle Install driver and PHP extensions for work with Oracle DB" | |
echo " --with-mysql Install MariaDB as default system database for DreamFactory" | |
echo " --with-apache Install Apache2 web server for DreamFactory" | |
echo " --with-db2 Install driver and PHP extensions for work with IBM DB2" | |
echo " --with-cassandra Install driver and PHP extensions for work with Cassandra DB" | |
echo " --with-tag=<tag name> Install DreamFactory with specific version. " | |
echo " --debug Enable installation process logging to file in /tmp folder." | |
echo -e " -h, --help Show this help\n" | |
exit 1 | |
fi | |
if [[ ! $DEBUG == TRUE ]]; then | |
exec 5>&1 # Save a copy of STDOUT | |
exec >/dev/null 2>&1 # Redirect STDOUT to Null | |
else | |
exec 5>&1 # Save a copy of STDOUT. Used because all echo redirects output to 5. | |
exec >/tmp/dreamfactory_installer.log 2>&1 | |
fi | |
clear >&5 | |
echo_with_color() { | |
case $1 in | |
Red | RED | red) | |
echo -e "${NC}${RD} $2 ${NC}" | |
;; | |
Green | GREEN | green) | |
echo -e "${NC}${GN} $2 ${NC}" | |
;; | |
Magenta | MAGENTA | magenta) | |
echo -e "${NC}${MG} $2 ${NC}" | |
;; | |
*) | |
echo -e "${NC} $2 ${NC}" | |
;; | |
esac | |
} | |
# Make sure script run as sudo | |
if ((EUID != 0)); then | |
echo -e "${RD}\nPlease run script with sudo: sudo bash $0 \n${NC}" >&5 | |
exit 1 | |
fi | |
# Retrieve executing user's username | |
CURRENT_USER=$(logname) | |
if [[ -z $SUDO_USER ]] && [[ -z $CURRENT_USER ]]; then | |
echo_with_color red " Enter username for installation DreamFactory:" >&5 | |
read -r CURRENT_USER | |
fi | |
if [[ -n $SUDO_USER ]]; then | |
CURRENT_USER=${SUDO_USER} | |
fi | |
### STEP 1. Install system dependencies | |
echo_with_color green "Step 1: Installing system dependencies...\n" >&5 | |
apt-get update | |
if [[ ! -f "/etc/localtime" ]]; then | |
echo -e "13\n33" | apt-get install -y tzdata | |
fi | |
apt-get install -y git \ | |
curl \ | |
zip \ | |
unzip \ | |
ca-certificates \ | |
apt-transport-https \ | |
software-properties-common \ | |
lsof \ | |
libmcrypt-dev \ | |
libreadline-dev \ | |
wget \ | |
sudo | |
# Check installation status | |
if (($? >= 1)); then | |
echo_with_color red "\n${ERROR_STRING}" >&5 | |
exit 1 | |
fi | |
echo_with_color green "The system dependencies have been successfully installed.\n" >&5 | |
else | |
echo_with_color green "Step 3: Installing Nginx...\n" >&5 ### Default choice | |
# Check nginx installation in the system | |
ps aux | grep -v grep | grep nginx | |
CHECK_NGINX_PROCESS=$? | |
dpkg -l | grep nginx | cut -d " " -f 3 | grep -E "nginx$" | |
CHECK_NGINX_INSTALLATION=$? | |
if ((CHECK_NGINX_PROCESS == 0)) || ((CHECK_NGINX_INSTALLATION == 0)); then | |
echo_with_color red "Nginx detected. Skipping installation. Configure Nginx manually.\n" >&5 | |
else | |
# Install nginx | |
# Checking running web server | |
lsof -i :80 | grep LISTEN | |
if (($? == 0)); then | |
echo_with_color red "Port 80 taken.\n " >&5 | |
echo_with_color red "Skipping Nginx installation. Install Nginx manually.\n " >&5 | |
else | |
apt-get install -y nginx ${PHP_VERSION}-fpm | |
if (($? >= 1)); then | |
echo_with_color red "\nCould not install Nginx. Exiting." >&5 | |
exit 1 | |
fi | |
# Change php fpm configuration file | |
sed -i 's/\;cgi\.fix\_pathinfo\=1/cgi\.fix\_pathinfo\=0/' "$(php -i | sed -n '/^Loaded Configuration File => /{s:^.*> ::;p;}' | sed 's/cli/fpm/')" | |
# Create nginx site entry | |
echo " | |
server { | |
listen 80 default_server; | |
listen [::]:80 default_server ipv6only=on; | |
root /var/www/api; | |
index index.php index.html index.htm; | |
server_name api.minimalisbliss.com; | |
gzip on; | |
gzip_disable \"msie6\"; | |
gzip_vary on; | |
gzip_proxied any; | |
gzip_comp_level 6; | |
gzip_buffers 16 8k; | |
gzip_http_version 1.1; | |
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; | |
location / { | |
try_files \$uri \$uri/ /index.php?\$args; | |
} | |
error_page 404 /404.html; | |
error_page 500 502 503 504 /50x.html; | |
location = /50x.html { | |
root /usr/share/nginx/html; | |
} | |
location ~ \.php$ { | |
try_files \$uri =404; | |
fastcgi_split_path_info ^(.+\.php)(/.+)$; | |
fastcgi_pass unix:/var/run/php/${PHP_VERSION}-fpm.sock; | |
fastcgi_index index.php; | |
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name; | |
include fastcgi_params; | |
} | |
}" >/etc/nginx/sites-available/default | |
service ${PHP_VERSION}-fpm restart && service nginx restart | |
echo_with_color green "Nginx installed.\n" >&5 | |
fi | |
fi | |
fi | |
### Install Node.js | |
node -v | |
if (($? >= 1)); then | |
curl -sL https://deb.nodesource.com/setup_10.x | bash - | |
apt-get install -y nodejs | |
if (($? >= 1)); then | |
echo_with_color red "\n${ERROR_STRING}" >&5 | |
exit 1 | |
fi | |
NODE_PATH=$(whereis node | cut -d" " -f2) | |
fi | |
### Step 6. Installing MySQL | |
if [[ $MYSQL == TRUE ]]; then ### Only with key --with-mysql | |
echo_with_color green "Step 6: Installing System Database for DreamFactory...\n" >&5 | |
dpkg -l | grep mysql | cut -d " " -f 3 | grep -E "^mysql" | grep -E -v "^mysql-client" | |
CHECK_MYSQL_INSTALLATION=$? | |
ps aux | grep -v grep | grep -E "^mysql" | |
CHECK_MYSQL_PROCESS=$? | |
lsof -i :3306 | grep LISTEN | |
CHECK_MYSQL_PORT=$? | |
if ((CHECK_MYSQL_PROCESS == 0)) || ((CHECK_MYSQL_INSTALLATION == 0)) || ((CHECK_MYSQL_PORT == 0)); then | |
echo_with_color red "MySQL Database detected in the system. Skipping installation. \n" >&5 | |
DB_FOUND=TRUE | |
else | |
if ((CURRENT_OS == 16)); then | |
apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8 | |
add-apt-repository 'deb [arch=amd64,arm64,i386,ppc64el] http://mariadb.petarmaric.com/repo/10.3/ubuntu xenial main' | |
elif ((CURRENT_OS == 18)); then | |
apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8 | |
add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://mariadb.petarmaric.com/repo/10.3/ubuntu bionic main' | |
else | |
echo_with_color red " The script support only Ubuntu 16 and 18 versions. Exit.\n " >&5 | |
exit 1 | |
fi | |
apt-get update | |
echo_with_color magenta "Please choose a strong MySQL root user password: " >&5 | |
read -r DB_PASS | |
if [[ -z $DB_PASS ]]; then | |
until [[ -n $DB_PASS ]]; do | |
echo_with_color red "The password can't be empty!" >&5 | |
read -r DB_PASS | |
done | |
fi | |
echo_with_color green "\nPassword accepted.\n" >&5 | |
# Disable interactive mode in installation mariadb. Set generated above password. | |
export DEBIAN_FRONTEND="noninteractive" | |
debconf-set-selections <<<"mariadb-server mysql-server/root_password password $DB_PASS" | |
debconf-set-selections <<<"mariadb-server mysql-server/root_password_again password $DB_PASS" | |
apt-get install -y mariadb-server | |
if (($? >= 1)); then | |
echo_with_color red "\n${ERROR_STRING}" >&5 | |
exit 1 | |
fi | |
service mariadb start | |
if (($? >= 1)); then | |
service mysql start | |
if (($? >= 1)); then | |
echo_with_color red "\nCould not start MariaDB.. Exit " >&5 | |
exit 1 | |
fi | |
fi | |
fi | |
echo_with_color green "Database for DreamFactory installed.\n" >&5 | |
### Step 7. Configuring DreamFactory system database | |
echo_with_color green "Step 7: Configure DreamFactory system database.\n" >&5 | |
DB_INSTALLED=FALSE | |
# The MySQL database has already been installed, so let's configure | |
# the DreamFactory system database. | |
if [[ $DB_FOUND == TRUE ]]; then | |
echo_with_color magenta "Is DreamFactory MySQL system database already configured? [Yy/Nn] " >&5 | |
read -r DB_ANSWER | |
if [[ -z $DB_ANSWER ]]; then | |
DB_ANSWER=Y | |
fi | |
if [[ $DB_ANSWER =~ ^[Yy]$ ]]; then | |
DB_INSTALLED=TRUE | |
# MySQL system database is not installed, but MySQL is, so let's | |
# prompt the user for the root password. | |
else | |
echo_with_color magenta "\nEnter MySQL root password: " >&5 | |
read -r DB_PASS | |
# Test DB access | |
mysql -h localhost -u root "-p$DB_PASS" -e"quit" | |
if (($? >= 1)); then | |
ACCESS=FALSE | |
TRYS=0 | |
until [[ $ACCESS == TRUE ]]; do | |
echo_with_color red "\nPassword incorrect!\n " >&5 | |
echo_with_color magenta "Enter root user password:\n " >&5 | |
read -r DB_PASS | |
mysql -h localhost -u root "-p$DB_PASS" -e"quit" | |
if (($? == 0)); then | |
ACCESS=TRUE | |
fi | |
TRYS=$((TRYS + 1)) | |
if ((TRYS == 3)); then | |
echo_with_color red "\nExit.\n" >&5 | |
exit 1 | |
fi | |
done | |
fi | |
fi | |
fi | |
# If the DreamFactory system database not already installed, | |
# let's install it. | |
if [[ $DB_INSTALLED == FALSE ]]; then | |
# Test DB access | |
mysql -h localhost -u root "-p$DB_PASS" -e"quit" | |
if (($? >= 1)); then | |
echo_with_color red "Connection to Database failed. Exit \n" >&5 | |
exit 1 | |
fi | |
echo_with_color magenta "\nWhat would you like to name your system database? (e.g. dreamfactory) " >&5 | |
read -r DF_SYSTEM_DB | |
if [[ -z $DF_SYSTEM_DB ]]; then | |
until [[ -n $DF_SYSTEM_DB ]]; do | |
echo_with_color red "\nThe name can't be empty!" >&5 | |
read -r DF_SYSTEM_DB | |
done | |
fi | |
echo "CREATE DATABASE ${DF_SYSTEM_DB};" | mysql -u root "-p${DB_PASS}" 2>&5 | |
if (($? >= 1)); then | |
echo_with_color red "\nCreating database error. Exit" >&5 | |
exit 1 | |
fi | |
echo_with_color magenta "\nPlease create a MySQL DreamFactory system database user name (e.g. dfadmin): " >&5 | |
read -r DF_SYSTEM_DB_USER | |
if [[ -z $DF_SYSTEM_DB_USER ]]; then | |
until [[ -n $DF_SYSTEM_DB_USER ]]; do | |
echo_with_color red "The name can't be empty!" >&5 | |
read -r DF_SYSTEM_DB_USER | |
done | |
fi | |
echo_with_color magenta "\nPlease create a secure MySQL DreamFactory system database user password: " >&5 | |
read -r DF_SYSTEM_DB_PASSWORD | |
if [[ -z $DF_SYSTEM_DB_PASSWORD ]]; then | |
until [[ -n $DF_SYSTEM_DB_PASSWORD ]]; do | |
echo_with_color red "The name can't be empty!" >&5 | |
read -r DF_SYSTEM_DB_PASSWORD | |
done | |
fi | |
# Generate password for user in DB | |
echo "GRANT ALL PRIVILEGES ON ${DF_SYSTEM_DB}.* to \"${DF_SYSTEM_DB_USER}\"@\"localhost\" IDENTIFIED BY \"${DF_SYSTEM_DB_PASSWORD}\";" | mysql -u root "-p${DB_PASS}" 2>&5 | |
if (($? >= 1)); then | |
echo_with_color red "\nCreating new user error. Exit" >&5 | |
exit 1 | |
fi | |
echo "FLUSH PRIVILEGES;" | mysql -u root "-p${DB_PASS}" | |
echo_with_color green "\nDatabase configuration finished.\n" >&5 | |
else | |
echo_with_color green "Skipping...\n" >&5 | |
fi | |
else | |
echo_with_color green "Step 6: Skipping DreamFactory system database installation.\n" >&5 | |
echo -e "Step 7: Skipping DreamFactory system database configuration.\n" >&5 | |
fi | |
chmod -R 2775 /var/www/api | |
chown -R "www-data:$CURRENT_USER" /var/www/api | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment