57.13 Project 13: LAMP stack: Apache, PHP 8, MySQL and phpMyAdmin
Friends, today we move a college PHP site onto Docker with Apache, PHP 8, MySQL and phpMyAdmin. We enable pdo_mysql, seed data, connect with PDO, and back up with mysqldump. Type every command yourself.
मित्रांनो, आज आपण college ची PHP site Docker वर Apache, PHP 8, MySQL आणि phpMyAdmin सह आणू. pdo_mysql चालू करू, seed data टाकू, PDO ने कनेक्ट करू, आणि mysqldump ने backup घेऊ. प्रत्येक command स्वतः टाइप करा.
दोस्तों, आज हम college की PHP site को Docker पर Apache, PHP 8, MySQL और phpMyAdmin के साथ लाएंगे. pdo_mysql चालू करेंगे, seed data डालेंगे, PDO से कनेक्ट करेंगे, और mysqldump से backup लेंगे. हर command खुद टाइप कीजिए.
What you will learn in this lesson
- Why old PHP sites still move into containers.
- How php:8.3-apache serves with mod_php.
- How docker-php-ext-install adds pdo_mysql.
- How init.sql seeds MySQL on first boot.
- How phpMyAdmin and mysqldump help operators.
1. Start from a real problem
Suppose we look after the website of a college in Nashik. It was written in PHP years ago and still runs on one old server with Apache, PHP and MySQL installed by hand.
A new lecturer cannot recreate that server. With Docker we pack the same LAMP stack so every laptop matches production.
TCS and Cognizant support teams migrate many college and SME PHP sites this way before touching Kubernetes.
2. Plain PHP then custom image
Step 1: Folders and first PHP file
Why: Prove php:apache.
What: A bind mount of public/ is enough for a hello page before we build our own image.
Example 1: Make folders
Type this:
mkdir -p lamp/public lamp/db && cd lamp && pwd
You will see:
/home/you/lamp
What it means: Project root ready.
Example 2: Write public/index.php
Type this:
printf '%s\n' '<?php' 'echo "PHP ".PHP_VERSION." on ".php_uname("s");' > public/index.php && cat public/index.php
You will see:
<?php
echo "PHP ".PHP_VERSION." on ".php_uname("s");
What it means: Shows version and OS.
Example 3: Run plain php:apache
Type this:
docker run --rm -d --name lamp-hello -p 8080:80 -v "$PWD/public":/var/www/html php:8.3-apache
sleep 2
curl -s http://localhost:8080
You will see:
PHP 8.3.x on Linux
What it means: Official image serves the file.
Step 2: Stop hello and build our image
Why: Extensions and php.ini.
What: docker-php-ext-install pdo_mysql compiles the driver. We also turn on mod_rewrite for pretty URLs later.
Example 4: Stop hello
Type this:
docker stop lamp-hello
You will see:
lamp-hello
What it means: Temporary container gone.
Example 5: Write Dockerfile
Type this:
printf '%s\n' 'FROM php:8.3-apache' 'RUN docker-php-ext-install pdo pdo_mysql' 'RUN a2enmod rewrite' 'COPY public/ /var/www/html/' 'WORKDIR /var/www/html' > Dockerfile && wc -l Dockerfile
You will see:
5 Dockerfile
What it means: pdo_mysql and rewrite enabled.
Example 6: Build lamp-web:1.0
Type this:
docker build -t lamp-web:1.0 .
You will see:
... FINISHED
What it means: Custom PHP image ready.
3. MySQL seed and Compose
Step 3: init.sql and Compose
Why: Database on first boot.
What: Files in /docker-entrypoint-initdb.d run only when the data volume is empty.
Example 7: Write db/init.sql
Type this:
printf '%s\n' 'CREATE TABLE students (' ' id INT AUTO_INCREMENT PRIMARY KEY,' ' name VARCHAR(80) NOT NULL,' ' course VARCHAR(80) NOT NULL' ');' 'INSERT INTO students (name, course) VALUES' ' ("Asha","BSc CS"),' ' ("Ravi","BCom");' > db/init.sql && wc -l db/init.sql
You will see:
8 db/init.sql
What it means: Table plus two rows.
Example 8: Write compose.yaml
Type this:
printf '%s\n' 'name: lamp' 'services:' ' db:' ' image: mysql:8.4' ' environment:' ' MYSQL_DATABASE: college' ' MYSQL_USER: college' ' MYSQL_PASSWORD: ${DB_PASSWORD}' ' MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}' ' volumes: [mysql-data:/var/lib/mysql, ./db/init.sql:/docker-entrypoint-initdb.d/init.sql:ro]' ' healthcheck:' ' test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-uroot", "-p${DB_ROOT_PASSWORD}"]' ' interval: 5s' ' retries: 20' '' ' web:' ' image: lamp-web:1.0' ' ports: ["8080:80"]' ' environment:' ' DB_HOST: db' ' DB_NAME: college' ' DB_USER: college' ' DB_PASSWORD: ${DB_PASSWORD}' ' depends_on:' ' db: {condition: service_healthy}' '' ' phpmyadmin:' ' image: phpmyadmin:5.2' ' ports: ["127.0.0.1:8081:80"]' ' environment:' ' PMA_HOST: db' ' PMA_USER: college' ' PMA_PASSWORD: ${DB_PASSWORD}' ' depends_on:' ' db: {condition: service_healthy}' '' 'volumes: {mysql-data:}' > compose.yaml
printf '%s\n' 'DB_PASSWORD=college-pass' 'DB_ROOT_PASSWORD=root-pass' > .env && wc -l compose.yaml
You will see:
40 compose.yaml
What it means: Web, MySQL and phpMyAdmin.
Example 9: Start stack
Type this:
docker compose up -d
sleep 12
docker compose ps
You will see:
web ... Up
db ... healthy
phpmyadmin ... Up
What it means: All three services up.
Step 4: PDO in PHP
Why: Read and insert rows.
What: Replace index.php with a small page that lists students from MySQL.
Example 10: Write PDO page
Type this:
printf '%s\n' '<?php' '$h=getenv("DB_HOST"); $n=getenv("DB_NAME"); $u=getenv("DB_USER"); $p=getenv("DB_PASSWORD");' '$pdo=new PDO("mysql:host=$h;dbname=$n;charset=utf8mb4",$u,$p,[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);' 'if($_SERVER["REQUEST_METHOD"]==="POST"){' ' $st=$pdo->prepare("INSERT INTO students(name,course) VALUES(?,?)");' ' $st->execute([$_POST["name"],$_POST["course"]]);' ' header("Location: /"); exit;' '}' '$rows=$pdo->query("SELECT id,name,course FROM students ORDER BY id")->fetchAll(PDO::FETCH_ASSOC);' 'header("Content-Type: text/html; charset=utf-8");' 'echo "<h1>Students</h1><ul>";' 'foreach($rows as $r){ echo "<li>{$r["id"]} {$r["name"]} — {$r["course"]}</li>"; }' 'echo "</ul><form method=post>Name <input name=name> Course <input name=course> <button>Add</button></form>";' > public/index.php
docker compose cp public/index.php web:/var/www/html/index.php
You will see:
...
What it means: PDO page copied into the running container.
Example 11: List students
Type this:
curl -s http://localhost:8080 | tr '>' '\n' | grep -E 'Asha|Ravi'
You will see:
1 Asha — BSc CS
2 Ravi — BCom
What it means: Seed rows visible.
Example 12: Add a student
Type this:
curl -s -o /dev/null -w '%{http_code}\n' -X POST http://localhost:8080 -d 'name=Neha&course=BA'
curl -s http://localhost:8080 | tr '>' '\n' | grep Neha
You will see:
302
3 Neha — BA
What it means: Insert works through PDO.
4. phpMyAdmin and backups
Step 5: phpMyAdmin on loopback
Why: Browse tables in a UI.
What: Open http://127.0.0.1:8081. Never publish phpMyAdmin on a public IP.
Example 13: Show bind
Type this:
grep 127.0.0.1:8081 compose.yaml
You will see:
ports: ["127.0.0.1:8081:80"]
What it means: Only this machine can open phpMyAdmin.
Example 14: HTTP check
Type this:
curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8081
You will see:
200
What it means: UI is up.
Example 15: Count rows via mysql client
Type this:
docker compose exec -e MYSQL_PWD=college-pass db mysql -ucollege college -N -e 'SELECT COUNT(*) FROM students;'
You will see:
3
What it means: Three students after the insert.
Step 6: mysqldump backup
Why: Save SQL to the host.
What: A one-shot dump from the db container writes to a bind mount.
Example 16: Dump
Type this:
mkdir -p backups && docker compose exec -T -e MYSQL_PWD=college-pass db mysqldump -ucollege college > backups/college.sql && wc -l backups/college.sql
You will see:
40 backups/college.sql
What it means: Schema and data saved.
Example 17: Show insert in dump
Type this:
grep -A1 'INSERT INTO .students.' backups/college.sql | head -2
You will see:
INSERT INTO students ...
(1,'Asha','BSc CS'),...
What it means: Rows are in the file.
Example 18: Restore into a throwaway check
Type this:
docker compose exec -T -e MYSQL_PWD=college-pass db mysql -ucollege college -e 'SELECT name FROM students WHERE id=1;'
You will see:
Asha
What it means: Live data still matches the dump.
5. Mistakes and clean up
Step 7: Common mistakes
Why: Missing extension and open PMA.
What: Without pdo_mysql, new PDO(...) fails. phpMyAdmin on 0.0.0.0 is dangerous.
Example 19: Extension loaded
Type this:
docker compose exec web php -m | grep -i pdo_mysql
You will see:
pdo_mysql
What it means: Driver present in the image.
Example 20: Rewrite module
Type this:
docker compose exec web apache2ctl -M 2>/dev/null | grep rewrite
You will see:
rewrite_module (shared)
What it means: mod_rewrite enabled.
Example 21: PMA on loopback only
Type this:
docker compose port phpmyadmin 80
You will see:
127.0.0.1:8081
What it means: Not on the public interface.
Step 8: Pretty URL hint
Why: mod_rewrite and .htaccess.
What: Many PHP apps need AllowOverride All and a rewrite rule to index.php.
Example 22: Show a2enmod in Dockerfile
Type this:
grep a2enmod Dockerfile
You will see:
RUN a2enmod rewrite
What it means: Module on at build time.
Example 23: Sample htaccess
Type this:
printf '%s\n' 'RewriteEngine On' 'RewriteCond %{REQUEST_FILENAME} !-f' 'RewriteRule ^ index.php [QSA,L]' > public/.htaccess && cat public/.htaccess
You will see:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
What it means: Front controller pattern.
Example 24: Document root
Type this:
docker compose exec web pwd
You will see:
/var/www/html
What it means: Apache serves this folder.
Step 9: Clean up
Why: Stop and remove volumes.
What: down -v deletes MySQL data. Keep backups/college.sql if you need it.
Example 25: Stop stack
Type this:
docker compose down -v
You will see:
... Removed
What it means: lamp project gone.
Example 26: Remove image
Type this:
docker image rm lamp-web:1.0
You will see:
Untagged: lamp-web:1.0
What it means: Disk space returned.
Example 27: Keep the dump
Type this:
ls backups/college.sql
You will see:
backups/college.sql
What it means: SQL file still on the host.
Step 10: Health before traffic
Why: Wait for MySQL init.
What: depends_on service_healthy waits for ping. init.sql has already run when the volume was empty.
Example 28: Show healthcheck
Type this:
grep -A3 healthcheck compose.yaml | head -4
You will see:
healthcheck:
test: ["CMD", "mysqladmin", "ping", ...
What it means: Ping must succeed before web starts.
Example 29: Re-init needs empty volume
Type this:
echo 'delete volume to re-run init.sql'
You will see:
delete volume to re-run init.sql
What it means: Existing volumes skip init scripts.
Example 30: PHP version constant
Type this:
docker run --rm lamp-web:1.0 php -r 'echo PHP_VERSION, PHP_EOL;'
You will see:
8.3.x
What it means: PHP_VERSION matches the image tag.
Step 11: Rebuild after code change
Why: Image versus bind mount.
What: In this lesson the Dockerfile COPY public/ at build time. For day-to-day edits, mount ./public over /var/www/html instead of rebuilding every time.
Example 31: Bind mount idea
Type this:
echo 'volumes: [./public:/var/www/html]'
You will see:
volumes: [./public:/var/www/html]
What it means: Edit PHP on the host; Apache sees it at once.
Example 32: When to rebuild
Type this:
echo 'Rebuild when Dockerfile or extensions change'
You will see:
Rebuild when Dockerfile or extensions change
What it means: Code-only changes do not need a new image if you bind mount.
Example 33: Compose cp alternative
Type this:
echo 'docker compose cp public/index.php web:/var/www/html/index.php'
You will see:
docker compose cp public/index.php web:/var/www/html/index.php
What it means: Copies one file into a running container without a rebuild.
Step 12: Root password versus app user
Why: Least privilege for PHP.
What: PHP uses the college user. Root password is only for dumps and admin. Never put the root password in the web container env.
Example 34: Web env has no root
Type this:
grep -A6 'web:' compose.yaml | grep -i root || echo 'no root in web'
You will see:
no root in web
What it means: Web only sees DB_PASSWORD for college.
Example 35: Root only in db service
Type this:
grep MYSQL_ROOT_PASSWORD compose.yaml
You will see:
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
What it means: Root stays with MySQL.
Example 36: App grants
Type this:
docker compose exec -e MYSQL_PWD=root-pass db mysql -uroot -e "SHOW GRANTS FOR 'college'@'%';" 2>/dev/null | head -3 || echo 'stack may be down — college user owns college db'
You will see:
stack may be down — college user owns college db
What it means: App user limited to its database.
Ravindra Bagale's Tip
Put legacy PHP sites in php:apache with pdo_mysql before you rewrite them. Nashik colleges and SME shops keep the same code and gain a repeatable server.
Legacy PHP sites rewrite करण्याआधी php:apache वर pdo_mysql सह ठेवा. Nashik colleges आणि SME shops तोच code ठेवून repeatable server मिळवतात.
Legacy PHP sites को rewrite करने से पहले php:apache पर pdo_mysql के साथ रखें. Nashik colleges और SME shops वही code रख कर repeatable server पाते हैं.
Check yourself
Q1. What does docker-php-ext-install pdo_mysql do?
It compiles and enables the PDO MySQL driver in the PHP image.
Q2. When does init.sql run?
Only when the MySQL data volume is empty on first boot.
Q3. Why bind phpMyAdmin to 127.0.0.1?
So the database UI is not open on the public network.
Q4. How do you back up the database?
mysqldump from the db container into a host file.
Q5. What Apache module helps pretty URLs?
mod_rewrite.
Q6. What PHP API talks to MySQL here?
PDO.
Friends, you built a LAMP image, seeded MySQL, listed students with PDO, opened phpMyAdmin on loopback, and took a mysqldump. Did you understand? Let us try it once more. If not, look at Pictures 1 and 2 and repeat the PDO page. Next project: LEMP with nginx and PHP-FPM.
मित्रांनो, आज तुम्ही LAMP image build केली, MySQL seed केली, PDO ने students पाहिले, loopback वर phpMyAdmin उघडले, आणि mysqldump घेतला. नसेल तर Picture 1 आणि 2 बघा आणि PDO page पुन्हा करा. पुढचा project: nginx आणि PHP-FPM सह LEMP.
दोस्तों, आज आपने LAMP image build की, MySQL seed की, PDO से students देखे, loopback पर phpMyAdmin खोला, और mysqldump लिया. नहीं तो Picture 1 और 2 देखिए और PDO page दोहराइए. अगला project: nginx और PHP-FPM के साथ LEMP.
Lab: try at home
- Serve a hello page with php:8.3-apache.
- Build lamp-web:1.0 with pdo_mysql and rewrite.
- Start Compose with MySQL, web and phpMyAdmin.
- Replace index.php with the PDO students page.
- Insert a row and confirm in phpMyAdmin.
- Take backups/college.sql with mysqldump.
- Clean up the stack and keep the dump.
Recap
- php:apache runs mod_php.
- docker-php-ext-install adds drivers.
- init.sql seeds empty volumes.
- PDO connects PHP to MySQL.
- phpMyAdmin stays on loopback.
- mysqldump saves the database.
Revision card
Interview card
Comparison card