BASH
Basic Git-based Website Deployment
Streamline your web project deployments with a Bash script that pulls the latest code from Git, installs dependencies, and sets file permissions.
#!/bin/bash
# Configuration
PROJECT_ROOT="/var/www/myproject"
GIT_REPO="[email protected]:myuser/myproject.git" # or HTTPS: https://github.com/myuser/myproject.git
BRANCH="main" # or master, develop etc.
WEB_USER="www-data" # Common web server user (e.g., apache, nginx)
WEB_GROUP="www-data"
# Navigate to project root
cd "$PROJECT_ROOT" || { echo "ERROR: Project directory not found: $PROJECT_ROOT"; exit 1; }
echo "Starting deployment for $PROJECT_ROOT..."
# 1. Pull latest code from Git
echo "Pulling latest code from Git branch '$BRANCH'..."
git pull origin "$BRANCH"
if [ $? -eq 0 ]; then
echo "Git pull successful."
else
echo "ERROR: Git pull failed."
exit 1
fi
# 2. Install/Update PHP Composer dependencies (if composer.json exists)
if [ -f "composer.json" ]; then
echo "Installing Composer dependencies..."
composer install --no-dev --optimize-autoloader
if [ $? -eq 0 ]; then
echo "Composer install successful."
else
echo "WARNING: Composer install failed. Continuing deployment."
fi
fi
# 3. Install/Update Node.js dependencies (if package.json exists)
if [ -f "package.json" ]; then
echo "Installing Node.js dependencies..."
npm install
if [ $? -eq 0 ]; then
echo "NPM install successful."
echo "Running NPM build (if applicable)..."
npm run build # Assuming a build script exists
if [ $? -ne 0 ]; then
echo "WARNING: NPM build failed. Continuing deployment."
fi
else
echo "WARNING: NPM install failed. Continuing deployment."
fi
fi
# 4. Set appropriate permissions
echo "Setting file permissions..."
chown -R "$WEB_USER":"$WEB_GROUP" "$PROJECT_ROOT"
chmod -R ug+rwx "$PROJECT_ROOT"/storage "$PROJECT_ROOT"/bootstrap/cache # Example for Laravel
chmod -R ugo+rX "$PROJECT_ROOT" # Read/execute for all, write for owner
# Optional: Clear cache, restart services, etc.
# php artisan cache:clear
# sudo systemctl restart php8.1-fpm.service
echo "Deployment finished."
How it works: This script provides a basic automated deployment workflow. It navigates to the project directory, pulls the latest code from a specified Git branch, and conditionally installs PHP (Composer) and Node.js (NPM) dependencies if their respective manifest files are present. Finally, it sets appropriate file ownership and permissions for the web server, which is crucial for web applications.