Skip to content

Instantly share code, notes, and snippets.

@seanquijote
Created December 3, 2020 11:31
Show Gist options
  • Save seanquijote/562aa2fb1a8e03e54428293b18333786 to your computer and use it in GitHub Desktop.
Save seanquijote/562aa2fb1a8e03e54428293b18333786 to your computer and use it in GitHub Desktop.
Simple Auto-Setup Script for NodeJS and Laravel
#!/usr/bin/env bash
#title : Simple Auto-Setup Script for NodeJS and Laravel
#description : Installs all dependencies for NodeJS and Laravel web apps
#author : seanquijote
#date_created : 20200428
#date_lastupdated : 20200521
#version : 0.0.4
#==============================================================================
read -e -p "Enter Username: " UNAME
read -s -p "Enter Password: " PASSWD
printf "\n"
if [ -z "$UNAME" ] || [ -z "$PASSWD" ]; then
echo "Error: username and password are required"
echo "Usage: bash laravel-auto-setup.sh [username] [password]"
return 1
fi
printf "\n"
echo "Starting Laravel Auto-setup Script"
printf "\n"
echo "==================== Apt Update ===================="
echo "Updating package manager cache..."
apt update
if [ $? -eq 0 ]; then
echo -e "\e[92mSUCCESS\e[0m (apt packages updated)"
else
echo -e "\e[91mFAIL\e[0m (unable to update apt package)"
fi
echo "==================== Apt Fix Broken Packages ===================="
echo "Fixing all broken packages..."
apt --fix-broken install
if [ $? -eq 0 ]; then
echo -e "\e[92mSUCCESS\e[0m (apt --fix-broken install)"
else
echo -e "\e[91mFAIL\e[0m (apt --fix-broken install)"
fi
echo "==================== cURL ===================="
echo "Checking if cURL is installed..."
command -v curl
if [ $? -eq 1 ]; then
echo "cURL is not yet installed"
echo "Installing cURL..."
apt install -y curl
command -v curl
if [ $? -eq 0 ]; then
echo -e "\e[92mSUCCESS\e[0m (cURL installed)"
else
echo -e "\e[91mFAIL\e[0m (unable to install cURL)"
return 1
fi
else
echo -e "\e[92mSUCCESS\e[0m (cURL is already installed)"
fi
echo "==================== WGET ===================="
echo "Checking if WGET is installed..."
command -v wget
if [ $? -eq 1 ]; then
echo "WGET is not yet installed"
echo "Installing WGET..."
apt install -y wget
command -v wget
if [ $? -eq 0 ]; then
echo -e "\e[92mSUCCESS\e[0m (WGET installed)"
else
echo -e "\e[91mFAIL\e[0m (unable to install WGET)"
return 1
fi
else
echo -e "\e[92mSUCCESS\e[0m (WGET is already installed)"
fi
echo "==================== Apache ===================="
echo "Checking if Apache is installed..."
command -v apache2
if [ $? -eq 1 ]; then
echo "Apache is not yet installed"
echo "Installing Apache..."
apt install -y apache2
command -v apache2
if [ $? -eq 0 ]; then
ufw allow 'Apache'
apache2 -v
systemctl status apache2
echo -e "\e[92mSUCCESS\e[0m (Apache installed)"
else
echo -e "\e[91mFAIL\e[0m (unable to install Apache)"
return 1
fi
else
echo -e "\e[92mSUCCESS\e[0m (Apache is already installed)"
fi
echo "==================== MySQL ===================="
echo "Checking if MySQL is installed..."
command -v mysql
if [ $? -eq 1 ]; then
echo "MySQL is not yet installed"
echo "Installing MySQL..."
apt install -y mysql-server
command -v mysql
if [ $? -eq 0 ]; then
mysql --version
systemctl status mysql
echo -e "\e[92mSUCCESS\e[0m (MySQL installed)"
else
echo -e "\e[91mFAIL\e[0m (unable to install MySQL)"
return 1
fi
else
echo -e "\e[92mSUCCESS\e[0m (MySQL is already installed)"
fi
echo "==================== Node.js Engine ===================="
echo "Checking if Node.js is installed..."
command -v node
if [ $? -eq 1 ]; then
echo "Node.js is not yet installed"
echo "Installing Node.js..."
curl -sL https://deb.nodesource.com/setup_13.x | sudo -E bash -
apt install -y nodejs
if [ $? -eq 0 ]; then
node -v
echo -e "\e[92mSUCCESS\e[0m (Node.js installed)"
else
echo -e "\e[91mFAIL\e[0m (unable to install Node.js)"
return 1
fi
else
echo -e "\e[92mSUCCESS\e[0m (Node.js is already installed)"
fi
echo "==================== NPM ===================="
echo "Checking if NPM is installed..."
command -v npm
if [ $? -eq 1 ]; then
echo "NPM is not yet installed"
echo "Installing NPM..."
apt install -y npm
if [ $? -eq 0 ]; then
npm -v
echo -e "\e[92mSUCCESS\e[0m (NPM installed)"
else
echo -e "\e[91mFAIL\e[0m (unable to install NPM)"
return 1
fi
else
echo -e "\e[92mSUCCESS\e[0m (NPM is already installed)"
fi
echo "==================== PM2 ===================="
echo "Checking if PM2 is installed..."
command -v pm2
if [ $? -eq 1 ]; then
echo "PM2 is not yet installed"
echo "Installing PM2..."
npm install -g pm2
command -v pm2
if [ $? -eq 0 ]; then
echo -e "\e[92mSUCCESS\e[0m (PM2 installed)"
else
echo -e "\e[91mFAIL\e[0m (unable to install PM2)"
return 1
fi
else
echo -e "\e[92mSUCCESS\e[0m (PM2 is already installed)"
fi
echo "==================== MongoDB Database ===================="
echo "Checking if MongoDB is installed..."
command -v mongod
if [ $? -eq 1 ]; then
echo "Installing MongoDB..."
apt install -y mongodb
command -v mongod
if [ $? -eq 0 ]; then
mongod --version
echo -e "\e[92mSUCCESS\e[0m (mongoDB installed)"
echo "Starting MongoDB..."
systemctl start mongodb
if [ $? -eq 0 ]; then
echo -e "\e[92mSUCCESS\e[0m (mongoDB is now active)"
else
echo -e "\e[91mFAIL\e[0m (Error! Please see MongoDB status)"
fi
else
echo -e "\e[91mFAIL\e[0m (unable to install MongoDB)"
return 1
fi
else
echo -e "\e[92mSUCCESS\e[0m (mongoDB is already installed)"
fi
echo "==================== Create MongoDB Database ===================="
mongo "DATABASE_NAME_HERE" --eval 'db.createCollection("COLLECTION_NAME_HERE"); db.createCollection("COLLECTION_NAME_HERE");'
if [ $? -eq 0 ]; then
echo -e "\e[92mSUCCESS\e[0m (MongoDB database "DATABASE_NAME_HERE" and its collections created)"
else
echo -e "\e[91mFAIL\e[0m (Unable to create MongoDB database "DATABASE_NAME_HERE" and its collections)"
return 1
fi
echo "==================== Git ===================="
echo "Checking if Git is installed..."
command -v git
if [ $? -eq 1 ]; then
echo "Git is not yet installed"
echo "Installing Git..."
apt install -y git
if [ $? -eq 0 ]; then
git --version
echo -e "\e[92mSUCCESS\e[0m (git installed)"
else
echo -e "\e[91mFAIL\e[0m (unable to install Git)"
return 1
fi
else
echo -e "\e[92mSUCCESS\e[0m (Git is already installed)"
fi
echo "==================== Clone NodeJS web app repository ===================="
echo "Checking if repository folder exists..."
if [ ! -d "DIRECTORY_PATH_HERE" ]; then
echo "Cloning repository..."
mkdir DIRECTORY_PATH_HERE
cd DIRECTORY_PATH_HERE
git clone --single-branch --branch BRANCH_NAME https://"${UNAME}":"${PASSWD}"@"GIT_REPO_URL"
if [ $? -eq 0 ]; then
echo -e "\e[92mSUCCESS\e[0m (repository cloned)"
else
echo -e "\e[91mFAIL\e[0m (unable to clone repository)"
return 1
fi
else
echo -e "\e[92mSUCCESS\e[0m (repository folder already exists)"
fi
echo "==================== NPM Install Packages ===================="
echo "Installing NPM packages..."
if [ -d "REPO_PATH_HERE" ]; then
cd REPO_PATH_HERE
npm install
if [ $? -eq 0 ]; then
echo -e "\e[92mSUCCESS\e[0m (NPM packages installed)"
else
echo -e "\e[91mFAIL\e[0m (unable to install NPM packages)"
fi
else
echo -e "\e[91mFAIL\e[0m (Repository folder does not exist. Please clone this repository.)"
fi
echo "==================== Create NodeJS web app .env ===================="
echo "Creating .env file..."
if [ ! -f "REPO_PATH_HERE/.env" ]; then
cd REPO_PATH_HERE
echo 'PORT=8080
DBURL=mongodb://localhost:27017
DATABASE=DATABASE_NAME_HERE' >> .env
if [ -f "REPO_PATH_HERE/.env" ]; then
echo -e "\e[92mSUCCESS\e[0m (.env file created)"
else
echo -e "\e[91mFAIL\e[0m (unable to create .env file)"
fi
else
echo -e "\e[92mSUCCESS\e[0m (.env file already exists)"
fi
echo "==================== Start NodeJS web app Server ===================="
echo "Starting node server..."
cd REPO_PATH_HERE
pm2 start server.js
if [ $? -eq 0 ]; then
echo -e "\e[92mSUCCESS\e[0m (Node Server packages started)"
else
echo -e "\e[91mFAIL\e[0m (unable to start Node Server)"
fi
echo "==================== PHP ===================="
echo "Checking if php is installed..."
cd /
command -v php
if [ $? -eq 1 ]; then
echo "Installing php and php mods..."
apt install -y php7.2 php7.2-curl php7.2-mysql php7.2-zip php7.2-gd php7.2-mbstring php7.2-xml
if [ $? -eq 0 ]; then
echo -e "\e[92mSUCCESS\e[0m (apt install -y php7.2 php7.2-curl php7.2-mysql php7.2-zip php7.2-gd php7.2-mbstring php7.2-xml)"
else
echo -e "\e[91mFAIL\e[0m (apt install -y php7.2 php7.2-curl php7.2-mysql php7.2-zip php7.2-gd php7.2-mbstring php7.2-xml)"
return 1
fi
else
echo -e "\e[92mSUCCESS\e[0m (php7.2 php7.2-curl php7.2-mysql php7.2-zip php7.2-gd php7.2-mbstring php7.2-xml is already installed)"
fi
echo "==================== Composer ===================="
echo "Checking if composer is installed..."
command -v composer
if [ $? -eq 1 ]; then
echo "Composer is not yet installed"
echo "Installing composer..."
cd ~
curl -sS https://getcomposer.org/installer -o composer-setup.php
php -r "if (hash_file('SHA384', 'composer-setup.php') === 'e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
composer
if [ $? -eq 0 ]; then
echo -e "\e[92mSUCCESS\e[0m (composer installation)"
else
echo -e "\e[91mFAIL\e[0m (composer installation)"
return 1
fi
else
echo -e "\e[92mSUCCESS\e[0m (composer is already installed)"
fi
echo "==================== Clone Laravel web app repository ===================="
echo "Checking if repo exists..."
if [ ! -d "/var/www/html/REPO_DIR_NAME" ]; then
echo "Cloning repo..."
cd /var/www/html/
git clone --single-branch --branch sap https://"${UNAME}":"${PASSWD}"@REPO_URL_HERE
if [ $? -eq 0 ]; then
# This is not recommended though. Will update this in the future
chmod 777 -R /var/www/html/REPO_DIR_NAME/storage/framework
chmod 777 -R /var/www/html/REPO_DIR_NAME/storage/logs
echo -e "\e[92mSUCCESS\e[0m (git clone)"
else
echo -e "\e[91mFAIL\e[0m (git clone)"
return 1
fi
else
echo -e "\e[92mSUCCESS\e[0m (repo already exists)"
fi
echo "==================== Composer Install Packages ===================="
echo "Installing composer packages..."
if [ -d "/var/www/html/REPO_DIR_NAME" ]; then
cd /var/www/html/REPO_DIR_NAME
composer install
if [ $? -eq 0 ]; then
echo -e "\e[92mSUCCESS\e[0m (composer install)"
else
echo -e "\e[91mFAIL\e[0m (composer install)"
return 1
fi
else
echo -e "\e[91mFAIL\e[0m (repo does not exist)"
return 1
fi
echo "==================== Create Laravel web app .env ===================="
echo "Creating .env file..."
if [ ! -f "/var/www/html/REPO_DIR_NAME/.env" ]; then
cd /var/www/html/REPO_DIR_NAME/
echo 'APP_NAME=Laravel
APP_ENV=local
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=' >> .env
if [ -f "/var/www/html/REPO_DIR_NAME/.env" ]; then
echo -e "\e[92mSUCCESS\e[0m (.env file created)"
else
echo -e "\e[91mFAIL\e[0m (unable to create .env file)"
return 1
fi
else
echo -e "\e[92mSUCCESS\e[0m (.env file already exists)"
fi
echo "==================== Laravel Config Cache ===================="
echo "Conducting laravel config:cache..."
cd /var/www/html/REPO_DIR_NAME/
php artisan config:cache
if [ $? -eq 0 ]; then
echo -e "\e[92mSUCCESS\e[0m (php artisan config:cache)"
else
echo -e "\e[91mFAIL\e[0m (php artisan config:cache)"
fi
echo "==================== Create Apache2 sites .conf file ===================="
if [ ! -f "/etc/apache2/sites-available/example.com.conf" ]; then
cd /etc/apache2/sites-available/
echo '<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html/REPO_DIR_NAME/public
ServerName example.com
<Directory /var/www/html/REPO_DIR_NAME>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>' >> example.com.conf
if [ -f "/etc/apache2/sites-available/example.com.conf" ]; then
echo -e "\e[92mSUCCESS\e[0m (example.com.conf file creation)"
else
echo -e "\e[91mFAIL\e[0m (example.com.conf file creation)"
return 1
fi
else
echo -e "\e[92mSUCCESS\e[0m (example.com.conf file already exists)"
fi
echo "==================== Change Mod and Site Configuration ===================="
cd /etc/apache2/sites-available/
echo "Enabling mod rewrite"
a2enmod rewrite
if [ $? -eq 1 ]; then
echo -e "\e[91mFAIL\e[0m (Error during enabling of mod rewrite)"
return 1
fi
a2dissite 000-default.conf
if [ $? -eq 1 ]; then
echo -e "\e[91mFAIL\e[0m (Error during disabling of 000-default.conf)"
return 1
fi
a2ensite example.com.conf
if [ $? -eq 1 ]; then
echo -e "\e[91mFAIL\e[0m (Error during enabling of example.com.conf)"
return 1
fi
systemctl restart apache2
if [ $? -eq 1 ]; then
echo -e "\e[91mFAIL\e[0m (Error during restarting of example.com.conf)"
return 1
fi
echo -e "\e[92mSUCCESS\e[0m (mod and site config changed)"
printf "\n"
echo "==================== Setup Complete ===================="
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment