Skip to main content
This guide shows how to build a safe, testable shell script to deploy the KodeKloud e-commerce application on CentOS (7/8). It documents the development approach — start with verified manual commands, then convert them into a small, well-structured script with checks, meaningful output, and idempotent operations. Follow the sequence: firewall → database → database population → web server → application clone & configuration → verification. Why this approach?
  • Verifying commands manually first avoids surprises when automating.
  • Small reusable functions make the script easier to test and maintain.
  • Basic runtime checks make failures easy to detect and debug.
Prerequisites Ports and components used Callout about privileges:
This script performs package installation and service configuration and uses sudo throughout. Run it from an account with sudo privileges or run the script as root.
Initial manual commands Start by copying and running these commands manually on a test VM to confirm behavior before automating:
Development approach (recommended)
  • Build a minimal working script by grouping the verified manual commands.
  • Test each logical block in a disposable/test VM to ensure it runs non-interactively.
  • Improve iteratively:
    • Add comments and colored, informative output.
    • Add service and firewall checks.
    • Verify DB contents and web page content.
    • Keep functions small and reusable.
  • Use safe file modifications (for example, sed with backups) if you need to edit config files.
Database configuration best practices
  • Use a separate SQL file for creation of DB/users (configure-db.sql) and another for loading data (db-load-script.sql).
  • Invoke these files non-interactively: sudo mysql < configure-db.sql and sudo mysql < db-load-script.sql.
  • This guide uses sudo mysql so no password is required (root login). If production uses authentication, parameterize credentials and secure them properly.
Sample SQL used by the script — configure-db.sql
— db-load-script.sql
Web server configuration notes
  • Install Apache (httpd), PHP, and the PHP MySQL extension (php-mysql on CentOS).
  • Open port 80 in FirewallD.
  • Configure Apache to prefer index.php over index.html:
    • sudo sed -i 's/index.html/index.php/g' /etc/httpd/conf/httpd.conf
  • Clone the web app into /var/www/html/ and replace hard-coded database IPs with localhost in index.php.
A polished, production-aware script Below is a consolidated, improved script that demonstrates:
  • Shebang and header comments.
  • Reusable functions for colored output, service checks, firewall checks, DB verification, and content checks.
  • Clear sequence: firewall → MariaDB → DB creation & load → web server → app clone & config → verification.
Save this as deploy-ecommerce-application.sh, make it executable, and run it in a test VM: chmod +x deploy-ecommerce-application.sh && sudo ./deploy-ecommerce-application.sh
Verification checklist
  • Services active: firewalld, mariadb, httpd
  • Firewall ports permanent: 3306/tcp, 80/tcp
  • Database exists and products table populated (script checks count)
  • App files present in /var/www/html/
  • Web page contains expected item names (script checks with curl)
Testing and teardown suggestions
  • Revert VM snapshots between runs for quick testing.
  • Alternatively, create a teardown script to:
    • stop services: sudo systemctl stop httpd mariadb firewalld
    • remove installed packages (e.g., sudo yum remove -y httpd mariadb-server git php)
    • remove app files: sudo rm -rf /var/www/html/*
    • restore changed config files from backups you create before editing them
  • If using an ephemeral test environment, snapshots are simpler and safer.
Additional improvements to consider
  • Add a log file for stdout/stderr (redirect to /var/log/deploy-ecommerce.log).
  • Add retries for transient failures (yum repos, network timeouts).
  • Parameterize script variables: DB name, user, password, repo URL, and target directory.
  • Use a configuration management tool (Ansible, Puppet, Chef) for production deployments and drift management.
  • Secure credentials with a vault or environment variables rather than embedding sensitive values.
Links and references
Tip: Keep scripts modular (small reusable functions) and add clear messages and checks so failures are visible and easy to debug.

Watch Video