BASH

Basic Web App Deployment Script

A foundational bash script for deploying web applications, covering code pull, dependency installation, build process, and service restart.

#!/bin/bash

# Basic web application deployment script

APP_DIR="/var/www/mywebapp"
SERVICE_NAME="mywebapp.service" # e.g., systemd service name for your app

echo "Starting deployment for $APP_DIR"

# 1. Navigate to application directory
cd "$APP_DIR" || { echo "Error: Application directory not found."; exit 1; }

# 2. Pull latest code from Git
echo "Pulling latest code..."
git pull origin main || { echo "Error: Git pull failed."; exit 1; }

# 3. Install dependencies (e.g., npm, composer)
# Uncomment and modify based on your project type
# echo "Installing Node.js dependencies..."
# npm install || { echo "Error: npm install failed."; exit 1; }

# echo "Installing PHP dependencies..."
# composer install --no-dev --optimize-autoloader || { echo "Error: Composer install failed."; exit 1; }

# 4. Run build process (if applicable, e.g., frontend build)
# Uncomment and modify based on your project type
# echo "Running build process..."
# npm run build || { echo "Error: Build failed."; exit 1; }

# 5. Clear cache/run migrations (if applicable)
# Uncomment and modify based on your project type
# echo "Clearing cache and running migrations..."
# php artisan cache:clear
# php artisan migrate --force

# 6. Restart application service
echo "Restarting application service: $SERVICE_NAME"
sudo systemctl restart "$SERVICE_NAME" || { echo "Error: Failed to restart service."; exit 1; }

echo "Deployment completed successfully!"
How it works: This script outlines a common workflow for deploying a web application. It navigates to the application directory, pulls the latest code from a Git repository, and includes placeholders for installing dependencies (like `npm install` or `composer install`), running build processes, and clearing caches or running database migrations. Finally, it restarts the application's systemd service to apply the updates. This script provides a customizable foundation for automating web application deployments, reducing manual errors.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs