-
Create two VMs:
- VM-1: Install MariaDB in subnet
192.*
(e.g.,192.168.1.0/24
). - VM-2: Install Apache in subnet
10.*
(e.g.,10.0.1.0/24
).
- VM-1: Install MariaDB in subnet
-
Configure firewalls and routes to allow connectivity between the VMs.
-
Allow VM-2 to connect to MariaDB running on VM-1.
-
Google Cloud Platform Setup:
- GCP project with billing enabled.
- Install and configure
gcloud
CLI:gcloud init
-
Enable Required APIs:
gcloud services enable compute.googleapis.com
-
IAM Permissions: Ensure permissions to create VMs, VPC networks, subnets, and firewall rules.
-
Create a VPC Network:
gcloud compute networks create custom-vpc --subnet-mode=custom
-
Create Subnet-1 (192.*):
gcloud compute networks subnets create subnet-192 \ --network=custom-vpc \ --range=192.168.1.0/24 \ --region=us-central1
-
Create Subnet-2 (10.*):
gcloud compute networks subnets create subnet-10 \ --network=custom-vpc \ --range=10.0.1.0/24 \ --region=us-central1
gcloud compute instances create mariadb-vm \
--zone=us-central1-a \
--machine-type=e2-micro \
--subnet=subnet-192 \
--metadata=startup-script='#! /bin/bash
apt-get update
apt-get install -y mariadb-server
systemctl start mariadb
systemctl enable mariadb
mysql -e "CREATE DATABASE demo_db;"
mysql -e "CREATE USER '\''apacheuser'\''@'\''10.0.1.%'\'' IDENTIFIED BY '\''password123'\'';"
mysql -e "GRANT ALL PRIVILEGES ON demo_db.* TO '\''apacheuser'\''@'\''10.0.1.%'\'';"
mysql -e "FLUSH PRIVILEGES;"
echo "MariaDB setup complete."
' \
--tags=mariadb-server \
--boot-disk-size=10GB
gcloud compute instances create apache-vm \
--zone=us-central1-a \
--machine-type=e2-micro \
--subnet=subnet-10 \
--metadata=startup-script='#! /bin/bash
apt-get update
apt-get install -y apache2 php php-mysql
systemctl start apache2
systemctl enable apache2
echo "<?php
\$conn = new mysqli(\"192.168.1.2\", \"apacheuser\", \"password123\", \"demo_db\");
if (\$conn->connect_error) { die(\"Connection failed: \" . \$conn->connect_error); }
echo \"<h1>Data from MariaDB</h1>\";
\$result = \$conn->query(\"SHOW DATABASES\");
while (\$row = \$result->fetch_assoc()) {
echo \"Database: \" . \$row[\"Database\"] . \"<br>\";
}
?>" > /var/www/html/index.php
' \
--tags=apache-server \
--boot-disk-size=10GB
-
Allow MariaDB Traffic (Port 3306) to Subnet-10:
gcloud compute firewall-rules create allow-mariadb \ --network=custom-vpc \ --allow=tcp:3306 \ --source-ranges=10.0.1.0/24 \ --target-tags=mariadb-server
-
Allow HTTP Traffic to Apache Server:
gcloud compute firewall-rules create allow-http \ --network=custom-vpc \ --allow=tcp:80 \ --source-ranges=0.0.0.0/0 \ --target-tags=apache-server
-
Find Internal IPs:
- List the instances:
gcloud compute instances list
- Note the internal IP of
mariadb-vm
(e.g.,192.168.1.2
).
- List the instances:
-
Test Apache Server:
- Open the external IP of
apache-vm
in a browser:http://<APACHE-VM-EXTERNAL-IP>
- You should see the output confirming the connection to MariaDB and listing the databases:
Data from MariaDB Database: demo_db
- Open the external IP of
-
Delete VMs:
gcloud compute instances delete mariadb-vm apache-vm --zone=us-central1-a
-
Delete Firewall Rules:
gcloud compute firewall-rules delete allow-mariadb allow-http
-
Delete VPC Network:
gcloud compute networks delete custom-vpc
-
Two VMs were created in different subnets:
mariadb-vm
(Subnet: 192.168.1.0/24) hosting MariaDB.apache-vm
(Subnet: 10.0.1.0/24) hosting Apache with PHP.
-
Firewall Rules were configured to allow:
- Port
3306
for MariaDB from10.*
subnet. - Port
80
for HTTP access to Apache.
- Port
-
The PHP script on the Apache server successfully connected to the MariaDB server and displayed demo data.
Your setup is now complete! 🎉