- 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.
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.- 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,
sedwith backups) if you need to edit config files.
- 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.sqlandsudo mysql < db-load-script.sql. - This guide uses
sudo mysqlso no password is required (root login). If production uses authentication, parameterize credentials and secure them properly.
- Install Apache (
httpd), PHP, and the PHP MySQL extension (php-mysqlon CentOS). - Open port 80 in FirewallD.
- Configure Apache to prefer
index.phpoverindex.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 withlocalhostinindex.php.
- 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.
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
- Services active:
firewalld,mariadb,httpd - Firewall ports permanent:
3306/tcp,80/tcp - Database exists and
productstable populated (script checks count) - App files present in
/var/www/html/ - Web page contains expected item names (script checks with
curl)
- 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
- stop services:
- If using an ephemeral test environment, snapshots are simpler and safer.
- 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.
- MariaDB / MySQL documentation
- Apache HTTP Server documentation
- FirewallD documentation
- KodeKloud learning-app-ecommerce repository
Tip: Keep scripts modular (small reusable functions) and add clear messages and checks so failures are visible and easy to debug.