This article introduces a project to develop Ansible playbooks for deploying a fictional ecommerce website using a LAMP stack architecture.
In this article, we introduce the project that you will work on throughout this course. You will develop Ansible playbooks to deploy the KodeKloud ecommerce website—a fictional online store selling electronic devices. This project is divided into stages, starting with setting up a lab environment and creating simple playbooks, and then progressing to advanced best practices using includes and roles.The KodeKloud ecommerce website uses a LAMP stack architecture:
Linux as the operating system
Apache HTTP Server for web services
MariaDB as the database (a community fork of MySQL)
PHP for server-side scripting
The focus of this project is on automating the deployment process with Ansible, rather than making changes to the application code itself.
Before you automate the deployment, it is important to be familiar with the manual configuration steps for setting up each component. This lesson reviews these essential tasks so you understand how each piece fits into the overall process.The tasks include:
Choosing the deployment system (we use a CentOS Linux machine).
Installing and configuring the Apache HTTP server, then enabling and starting the service.
Installing and configuring the MariaDB database, then enabling and starting the service.
Installing and configuring PHP.
Downloading and setting up the application code such that it correctly connects to Apache and PHP.
Configuring the system further by setting up the firewall and creating the necessary rules.
For better logical flow, the guide begins by setting up and configuring the database before moving on to Apache and PHP.
Begin by installing the MariaDB server. Next, update the /etc/my.cnf file to change port settings if needed (remember that although the file is named my.cnf, it is also used by MariaDB). Then, start and enable the MariaDB service.
Copy
Ask AI
sudo yum install mariadb-serversudo vi /etc/my.cnf # Adjust port settings as requiredsudo service mariadb startsudo systemctl enable mariadb
After starting the database service, add the necessary firewall rules to allow external access on port 3306:
Next, access the database with the MySQL client to create the database, user, and assign privileges. Use the following SQL commands:
Copy
Ask AI
MariaDB > CREATE DATABASE ecomdb;MariaDB > CREATE USER 'ecomuser'@'localhost' IDENTIFIED BY 'ecompassword';MariaDB > GRANT ALL PRIVILEGES ON *.* TO 'ecomuser'@'localhost';MariaDB > FLUSH PRIVILEGES;
Finally, load the inventory data for the products with the provided database load script.
Make sure the database credentials and port settings in your configuration match those specified in your Ansible playbooks.
This step involves installing Apache, PHP, and PHP-MySQL to enable database connectivity. Then, update the firewall rules to allow HTTP traffic and modify Apache’s configuration to use index.php as the default file.Install the necessary packages:
Before testing, update the index.php file with the correct database details (address, name, user ID, and password). Finally, verify the deployment with a simple test:
Copy
Ask AI
curl http://localhost
This setup demonstrates how to deploy the LAMP stack on a single node where the database, Apache, and PHP all reside on the same system.In a multi-node configuration, the components are distributed on separate systems. Although the steps remain similar, connectivity details must be adjusted:
Update index.php on the web server with the database server’s IP address.
In the database server, specify the web server’s IP address when configuring user access. This ensures that only the authorized web server can connect.
For example, on the database server, use the following configuration:
Copy
Ask AI
MariaDB > CREATE DATABASE ecomdb;MariaDB > CREATE USER 'ecomuser'@'172.20.1.102' IDENTIFIED BY 'ecompassword';MariaDB > GRANT ALL PRIVILEGES ON *.* TO 'ecomuser'@'172.20.1.102';MariaDB > FLUSH PRIVILEGES;
And, update the PHP connection code on the web server accordingly:
Copy
Ask AI
$link = mysqli_connect('172.20.1.101', 'ecomuser', 'ecompassword');if ($link) { $res = mysqli_query($link, "SELECT * FROM products;"); while ($row = mysqli_fetch_assoc($res)) { // Process each row }}
Always ensure that your database user permissions and firewall settings are secured in both single and multi-node setups to prevent unauthorized access.
This line specifies the IP address of the database server, the database name, the user ID, and the password. You will modify this connection string as needed throughout the project.
Create Ansible playbooks to automate the deployment.
Practice deploying the KodeKloud ecommerce application following the steps provided.
For further reading on Kubernetes concepts and container orchestration (if relevant to your deployment automation), visit the Kubernetes Documentation.Happy automating!