Session 3.5 – Setting up XAMPP Environment
Module 3: PHP Basics | Duration: 1 hr
Learning Objectives
By the end of this session, students will be able to:
- Understand XAMPP components and architecture
- Install XAMPP on different operating systems
- Configure Apache and MySQL servers
- Create and test PHP scripts locally
- Manage databases with phpMyAdmin
- Troubleshoot common XAMPP issues
Introduction
XAMPP is a free, open-source cross-platform web server solution that includes Apache HTTP Server, MariaDB/MySQL database, and interpreters for PHP and Perl. It's perfect for local development and testing.
Key Insight
XAMPP provides everything you need to run a local web server on your computer, making it ideal for learning PHP and MySQL without needing to deploy code to a live server.
What is XAMPP?
XAMPP stands for Cross-Platform (X), Apache (A), MariaDB (M), PHP (P), and Perl (P).
XAMPP Components
Apache HTTP Server
- Most popular web server
- Handles HTTP requests
- Serves PHP files
- Port 80 (HTTP), 443 (HTTPS)
MariaDB/MySQL
- Relational database system
- Stores application data
- SQL query support
- Port 3306
PHP
- Server-side scripting language
- Processes dynamic content
- Database connectivity
- Session management
phpMyAdmin
- Web-based MySQL admin
- GUI for database operations
- Import/export data
- SQL query interface
Installation Guide
Windows Installation
1. Download XAMPP from https://www.apachefriends.org/
2. Run the installer (xampp-windows-x64-installer.exe)
3. Select components:
- Apache (Required)
- MySQL (Required)
- PHP (Required)
- phpMyAdmin (Recommended)
4. Choose installation directory (default: C:\xampp)
5. Complete installation wizard
6. Launch XAMPP Control Panel
Linux Installation
# Download XAMPP for Linux
wget https://downloadsapachefriends.global.ssl.fastly.net/xampp-files/8.2.4/xampp-linux-x64-8.2.4-0-installer.run
# Make installer executable
chmod +x xampp-linux-x64-8.2.4-0-installer.run
# Run installer
sudo ./xampp-linux-x64-8.2.4-0-installer.run
# Start XAMPP
sudo /opt/lampp/lampp start
# Stop XAMPP
sudo /opt/lampp/lampp stop
macOS Installation
1. Download XAMPP for macOS from official website
2. Open the DMG file
3. Drag XAMPP to Applications folder
4. Open Applications > XAMPP
5. Click "Start" for Apache and MySQL
6. Access via http://localhost
System Requirements
- Windows: Windows 7/8/10/11
- Linux: Ubuntu, Debian, Fedora, etc.
- macOS: OS X 10.9 or higher
- RAM: Minimum 512 MB (2 GB recommended)
- Disk Space: 1 GB free space
Configuration
Starting Services
1. Open XAMPP Control Panel
2. Click "Start" next to Apache
3. Click "Start" next to MySQL
4. Services should show "Running" in green
5. Access control panel at http://localhost/dashboard
Apache Configuration (httpd.conf)
# Location: xampp/apache/conf/httpd.conf
# Change default port (if 80 is occupied)
Listen 8080
# Document root (where files are served from)
DocumentRoot "C:/xampp/htdocs"
<Directory "C:/xampp/htdocs">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
# Enable mod_rewrite for URL rewriting
LoadModule rewrite_module modules/mod_rewrite.so
PHP Configuration (php.ini)
; Location: xampp/php/php.ini
; Display errors (useful for development)
display_errors = On
error_reporting = E_ALL
; Maximum execution time
max_execution_time = 300
; Memory limit
memory_limit = 256M
; File upload settings
upload_max_filesize = 64M
post_max_size = 64M
; Time zone
date.timezone = "America/New_York"
; Extensions (enable as needed)
extension=mysqli
extension=pdo_mysql
extension=curl
extension=gd
extension=mbstring
MySQL Configuration (my.ini)
# Location: xampp/mysql/bin/my.ini
[mysqld]
port = 3306
socket = "C:/xampp/mysql/mysql.sock"
basedir = "C:/xampp/mysql"
datadir = "C:/xampp/mysql/data"
max_allowed_packet = 64M
default-storage-engine = InnoDB
Testing Your Setup
Test 1: Apache Server
1. Open browser
2. Go to http://localhost
3. You should see XAMPP dashboard
4. If successful, Apache is working
Test 2: PHP Installation
1. Create file: C:\xampp\htdocs\test.php
2. Add content:
<?php phpinfo(); ?>
3. Open: http://localhost/test.php
4. Should display PHP configuration page
Test 3: MySQL Database
1. Go to http://localhost/phpmyadmin
2. Login with username: root, password: (blank)
3. You should see phpMyAdmin interface
4. Can create databases and tables
Create Your First PHP Page
<!-- File: C:\xampp\htdocs\hello.php -->
<!DOCTYPE html>
<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<h1>Welcome to PHP</h1>
<?php
echo "<p>Hello from PHP!</p>";
echo "<p>Current date: " . date("Y-m-d H:i:s") . "</p>";
echo "<p>Server: " . $_SERVER['SERVER_NAME'] . "</p>";
?>
</body>
</html>
Test MySQL Connection
<?php
// File: C:\xampp\htdocs\db_test.php
$servername = "localhost";
$username = "root";
$password = "";
try {
$conn = new PDO("mysql:host=$servername", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully to MySQL!";
// Show MySQL version
$stmt = $conn->query("SELECT VERSION()");
$version = $stmt->fetchColumn();
echo "<br>MySQL version: " . $version;
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
$conn = null;
?>
Troubleshooting
Common Issues and Solutions
Problem: Apache won't start - Port 80 is occupied
Solution:
1. Check what's using port 80:
netstat -ano | findstr :80
2. Change Apache port in httpd.conf:
Listen 8080
3. Restart Apache
4. Access via http://localhost:8080
Problem: MySQL won't start - Port 3306 in use
Solution:
1. Check for other MySQL instances
2. Stop other MySQL services
3. Or change MySQL port in my.ini:
port = 3307
4. Update PHP connection strings accordingly
Problem: .php files download instead of running
Solution:
1. Check Apache is running
2. Verify PHP module is loaded in httpd.conf:
LoadModule php_module "C:/xampp/php/php8apache2_4.dll"
3. Check PHP handler:
AddType application/x-httpd-php .php
4. Restart Apache
Useful Commands
# Windows - Check if Apache is running
netstat -ano | findstr :80
# Windows - Check if MySQL is running
netstat -ano | findstr :3306
# Linux - Start XAMPP
sudo /opt/lampp/lampp start
# Linux - Stop XAMPP
sudo /opt/lampp/lampp stop
# Linux - Check XAMPP status
sudo /opt/lampp/lampp status
# Test PHP from command line
php -v
php -m # Show loaded modules
php -r "echo 'Hello PHP';"
Session Summary
Key Points
- XAMPP: Cross-platform web server package for local development
- Components: Apache, MySQL/MariaDB, PHP, phpMyAdmin, Perl
- Installation: Simple wizard-based process for all platforms
- Configuration: Customize ports, PHP settings, MySQL options
- htdocs: Default web root directory for PHP files
- phpMyAdmin: Web interface for database management
- Testing: Verify setup with simple PHP and MySQL tests
Best Practices
- Always stop services when not in use to free up resources
- Keep XAMPP updated to latest version
- Use different folders in htdocs for different projects
- Set up virtual hosts for multiple projects
- Backup MySQL databases regularly
- Never use XAMPP in production environments
Next Session Preview
In the next session, we'll explore PHP Forms, learning how to handle form data using GET and POST methods, process user input, and create interactive web applications.