BASH
Automate Simple Web Application Deployment
Streamline web application deployment by automating Git pull, installing project dependencies, and restarting web server services.
#!/bin/bash
# Configuration
PROJECT_ROOT="/var/www/mywebapp" # Path to your web application
GIT_BRANCH="main" # Branch to pull from
WEB_SERVER_SERVICE="nginx" # Or apache2, httpd, etc.
PHP_FPM_SERVICE="php8.1-fpm" # If applicable, e.g., php-fpm
echo "Starting deployment for ${PROJECT_ROOT}..."
# Navigate to project directory
cd "${PROJECT_ROOT}" || { echo "Error: Project directory not found."; exit 1; }
# Pull latest changes from Git
echo "Pulling latest changes from Git branch: ${GIT_BRANCH}"
git pull origin "${GIT_BRANCH}"
if [ $? -eq 0 ]; then
echo "Git pull successful."
else
echo "Error: Git pull failed."
exit 1
fi
# Install/update dependencies (e.g., Node.js, PHP Composer)
if [ -f "package.json" ]; then
echo "Installing Node.js dependencies..."
npm install --omit=dev # Or yarn install
fi
if [ -f "composer.json" ]; then
echo "Installing PHP Composer dependencies..."
composer install --no-dev --optimize-autoloader
fi
# Clear cache or run migrations (example, adjust as needed)
# php artisan cache:clear
# php artisan migrate --force
# Restart services
echo "Restarting web server and PHP-FPM services..."
sudo systemctl restart "${WEB_SERVER_SERVICE}"
sudo systemctl restart "${PHP_FPM_SERVICE}"
echo "Deployment complete for ${PROJECT_ROOT}!"
How it works: This script automates a basic web application deployment process. It navigates to the project directory, pulls the latest code from a specified Git branch, installs Node.js (`npm install`) and PHP Composer (`composer install`) dependencies if their respective manifest files are present, and finally restarts the web server and PHP-FPM services to apply changes.