Skip to main content
In this lesson, we demonstrate how to develop a shell script to deploy the ECommerce Application on a CentOS system. The deployment process includes installing and configuring Firewalld, MariaDB, Apache (httpd), PHP, and Git; setting up the database with inventory data; and verifying that the web server serves the application correctly. Below is an enhanced walkthrough that maintains the original command order and details while improving readability, flow, and technical accuracy.

1. Initial Command Overview

The following commands install and start Firewalld and MariaDB, update the firewall to allow traffic on port 3306, and launch the MySQL client:
Although the instructions include manual editing of /etc/my.cnf using vi, you can automate configuration changes with tools like sed for a fully automated deployment.

2. Developing the Deployment Script

Create the deployment script locally using your favorite IDE (e.g., PyCharm) and test it in your lab environment. Name the script file, for example, deploy_ECommerce_Application.sh. Start by copying the essential commands from your Git repository before gradually improving and testing the script. An initial version might look like this:
Testing command blocks individually ensures that each part runs non-interactively (thanks to the -y option) and highlights any permission or manual input issues.

3. Database Configuration

3.1. Configuring the Database

Create a SQL script to setup the database by creating a database, user, and assigning privileges:
For automated configuration, consider replacing manual edits with command-line utilities such as sed to modify files like /etc/my.cnf programmatically.

3.2. Loading Inventory Data

Load sample inventory data by creating a SQL script that creates a table and populates it with product details:
If you face access issues (e.g., “Access denied for user … to database ‘ecomdb’”), try running the command with sudo.

4. Web Server Configuration

Install and configure Apache, PHP, and Git. Modify the server configuration to use index.php instead of index.html and clone the application repository.
Clone the repository and update the configuration to replace the database IP with localhost:
You can test the web application with:

5. Enhancing the Script with Checks and User-Friendly Messages

For a production-grade script, it is ideal to include functions that provide colored status messages, verify service activity, and confirm that firewall rules and web content are as expected. Below is an example of an enhanced deployment script:

Explanation

  1. Functions for User-Friendly Output:
    The functions print_color, check_service_status, and check_firewalld_port offer colored output and validate that necessary services and firewall rules are active.
  2. Database Setup:
    SQL scripts (configure-db.sql and db-load-script.sql) configure the database and load inventory data. Executing these with sudo mysql ensures proper permission handling.
  3. Web Server Setup:
    Apache is installed and configured to use index.php via sed, and the application repository is cloned into /var/www/html. The script updates the IP address in index.php with localhost, standardizing the environment.
  4. Final Testing:
    The script uses curl to fetch the web content and verifies the presence of key items such as “Laptop”, “Drone”, etc.

6. Final Testing and Teardown

After executing the script, verify the deployment by visiting http://localhost in your browser to confirm that all products are listed. For further improvements, consider adding robust error checking and a teardown script to stop services, clean up changes, or restore your test VM to a clean snapshot. Happy deploying!

Watch Video