This article demonstrates developing a shell script to deploy an ECommerce Application on a CentOS system.
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.
The following commands install and start Firewalld and MariaDB, update the firewall to allow traffic on port 3306, and launch the MySQL client:
Copy
Ask AI
sudo yum install -y firewalldsudo service firewalld startsudo systemctl enable firewalldsudo yum install -y mariadb-serversudo vi /etc/my.cnfsudo service mariadb startsudo systemctl enable mariadbsudo firewall-cmd --permanent --zone=public --add-port=3306/tcpsudo firewall-cmd --reloadmysql
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.
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:
Copy
Ask AI
sudo yum install -y firewalldsudo service firewalld startsudo systemctl enable firewalldsudo yum install -y mariadb-serversudo vi /etc/my.cnfsudo service mariadb startsudo systemctl enable mariadbsudo firewall-cmd --permanent --zone=public --add-port=3306/tcpsudo firewall-cmd --reloadmysql
Testing command blocks individually ensures that each part runs non-interactively (thanks to the -y option) and highlights any permission or manual input issues.
Create a SQL script to setup the database by creating a database, user, and assigning privileges:
Copy
Ask AI
cat > configure-db.sql <<EOFCREATE DATABASE ecomdb;CREATE USER 'ecomuser'@'localhost' IDENTIFIED BY 'ecompassword';GRANT ALL PRIVILEGES ON *.* TO 'ecomuser'@'localhost';FLUSH PRIVILEGES;EOFsudo mysql < configure-db.sql
For automated configuration, consider replacing manual edits with command-line utilities such as sed to modify files like /etc/my.cnf programmatically.
Install and configure Apache, PHP, and Git. Modify the server configuration to use index.php instead of index.html and clone the application repository.
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:
Copy
Ask AI
#!/bin/bash# deploy_ECommerce_Application.sh# This script deploys the ECommerce Application by installing and configuring######################################### Function: print_color# Description: Print a message in a specified color.# Usage: print_color "green" "Your message here"########################################function print_color(){ NC='\033[0m' # No Color case $1 in "green") COLOR='\033[0;32m' ;; "red") COLOR='\033[0;31m' ;; *) COLOR='\033[0m' ;; esac echo -e "${COLOR}$2${NC}"}######################################### Function: check_service_status# Description: Check if a service is active.# Usage: check_service_status firewalld########################################function check_service_status(){ service_name=$1 is_active=$(systemctl is-active "$service_name") if [ "$is_active" = "active" ]; then print_color "green" "$service_name Service is active" else print_color "red" "$service_name Service is not active" exit 1 fi}######################################### Function: check_firewalld_port# Description: Verify that a given port is configured in the public zone firewall.# Usage: check_firewalld_port "3306"########################################function check_firewalld_port(){ port=$1 firewall_ports=$(sudo firewall-cmd --list-all --zone=public | grep ports) if [[ $firewall_ports == *"$port"* ]]; then print_color "green" "Port $port is configured in the firewall" else print_color "red" "Port $port is not configured in the firewall" exit 1 fi}######################################### Function: check_item# Description: Check if a specific item appears in the web page content.# Usage: check_item "$web_page_content" "Laptop"########################################function check_item(){ web_page_content="$1" item="$2" if [[ "$web_page_content" == *"$item"* ]]; then print_color "green" "Item '$item' is present on the web page" else print_color "red" "Item '$item' is not present on the web page" fi}############################## Database and Service Setup############################## Install and configure Firewalldprint_color "green" "Installing and starting firewalld..."sudo yum install -y firewalldsudo service firewalld startsudo systemctl enable firewalldcheck_service_status firewalld# Install and configure MariaDBprint_color "green" "Installing and starting MariaDB..."sudo yum install -y mariadb-serversudo service mariadb startsudo systemctl enable mariadbcheck_service_status mariadb# Configure firewall for MariaDB (port 3306)print_color "green" "Adding firewall rule for MariaDB (port 3306)..."sudo firewall-cmd --permanent --zone=public --add-port=3306/tcpsudo firewall-cmd --reloadcheck_firewalld_port "3306"# Configure the Databaseprint_color "green" "Configuring the database..."cat > configure-db.sql <<EOFCREATE DATABASE ecomdb;CREATE USER 'ecomuser'@'localhost' IDENTIFIED BY 'ecompassword';GRANT ALL PRIVILEGES ON *.* TO 'ecomuser'@'localhost';FLUSH PRIVILEGES;EOFsudo mysql < configure-db.sql# Load inventory data into the databaseprint_color "green" "Loading inventory data into the database..."cat > db-load-script.sql <<EOFUSE ecomdb;CREATE TABLE products ( id mediumint(8) unsigned NOT NULL AUTO_INCREMENT, Name varchar(255) DEFAULT NULL, Price decimal(10,2) DEFAULT NULL, ImageUrl varchar(255) DEFAULT NULL, PRIMARY KEY (id));INSERT INTO products (Name,Price,ImageUrl) VALUES ("Laptop", "100", "c-1.png"), ("Drone", "200", "c-2.png"), ("VR", "300", "c-3.png"), ("Tablet", "5", "c-5.png"), ("Watch", "90", "c-6.png"), ("Phone", "80", "c-8.png"), ("Laptop", "150", "c-4.png");EOFsudo mysql < db-load-script.sql############################## Web Server Configuration#############################print_color "green" "Installing Apache, PHP, and configuring the web server..."sudo yum install -y httpd php php-mysqlsudo firewall-cmd --permanent --zone=public --add-port=80/tcpsudo firewall-cmd --reloadsudo sed -i 's/index.html/index.php/g' /etc/httpd/conf/httpd.confsudo service httpd startsudo systemctl enable httpdcheck_service_status httpdprint_color "green" "Cloning application repository..."sudo yum install -y gitsudo git clone https://github.com/kodekloudhub/learning-app-ecommerce.git /var/www/html/sudo sed -i 's/172.20.1.101/localhost/g' /var/www/html/index.php############################## Testing the Deployment#############################print_color "green" "Testing the web application..."web_page=$(curl http://localhost)for item in "Laptop" "Drone" "VR" "Watch" "Phone"do check_item "$web_page" "$item"doneprint_color "green" "Deployment complete. The ECommerce Application is up and running."
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.
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.
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.
Final Testing:
The script uses curl to fetch the web content and verifies the presence of key items such as “Laptop”, “Drone”, etc.
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!