Installing PHP
Before you can start writing PHP code, you need to set up a development environment. This lesson will guide you through installing PHP on Windows, Mac, and Linux, plus setting up a local web server.
Installation Options
There are two main approaches to installing PHP:
| Method | Pros | Cons | Best For |
|---|---|---|---|
| All-in-One Package (XAMPP, MAMP, WAMP) |
Easy setup, includes Apache & MySQL, GUI tools | Larger download, may include unused features | Beginners, quick setup |
| Standalone PHP | Lightweight, full control, latest version | Manual configuration, command-line only | Advanced users, production-like setup |
Option 1: XAMPP (Recommended for Beginners)
XAMPP is a free, cross-platform package that includes Apache, MySQL, PHP, and Perl. It's the easiest way to get started.
- Apache Web Server
- PHP (latest version)
- MySQL Database
- phpMyAdmin (database management)
- Control panel for easy management
Installing XAMPP on Windows
- Download XAMPP from apachefriends.org
- Run the installer (xampp-windows-x64-installer.exe)
- Choose installation directory (default: C:\xampp)
- Select components (keep all defaults)
- Click "Next" through the installation
- Launch XAMPP Control Panel
- Click "Start" next to Apache and MySQL
Installing XAMPP on Mac
- Download XAMPP for Mac from apachefriends.org
- Open the .dmg file
- Drag XAMPP to Applications folder
- Open XAMPP from Applications
- Click "Start" for Apache and MySQL
Installing XAMPP on Linux
# Download XAMPP
wget https://www.apachefriends.org/xampp-files/8.2.12/xampp-linux-x64-8.2.12-0-installer.run
# Make it executable
chmod +x xampp-linux-x64-8.2.12-0-installer.run
# Run installer
sudo ./xampp-linux-x64-8.2.12-0-installer.run
# Start XAMPP
sudo /opt/lampp/lampp start
Option 2: Standalone PHP Installation
Windows
- Download PHP from windows.php.net
- Choose "Thread Safe" version (VS16 x64)
- Extract to C:\php
- Add C:\php to system PATH
- Rename php.ini-development to php.ini
Mac (using Homebrew)
# Install Homebrew if not installed
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install PHP
brew install php
# Verify installation
php -v
Linux (Ubuntu/Debian)
# Update package list
sudo apt update
# Install PHP and common extensions
sudo apt install php php-cli php-mysql php-mbstring php-xml
# Verify installation
php -v
Verifying Your Installation
After installation, verify PHP is working correctly:
Method 1: Command Line
# Check PHP version
php -v
# Run PHP code directly
php -r "echo 'Hello from PHP!';"
Method 2: Create a Test File
Create a file named test.php in your web server's document root:
- XAMPP Windows: C:\xampp\htdocs\test.php
- XAMPP Mac: /Applications/XAMPP/htdocs/test.php
- XAMPP Linux: /opt/lampp/htdocs/test.php
<?php
phpinfo();
?>
Then visit http://localhost/test.php in your browser. You should see
a detailed PHP information page.
Setting Up a Code Editor
While you can write PHP in any text editor, these are recommended:
| Editor | Features | Best For |
|---|---|---|
| VS Code | Free, extensions, IntelliSense, debugging | Most developers |
| PHPStorm | Professional IDE, advanced features, paid | Professional development |
| Sublime Text | Fast, lightweight, extensible | Quick editing |
| Atom | Free, hackable, GitHub integration | Customization lovers |
- PHP Intelephense - IntelliSense and code navigation
- PHP Debug - Debugging support
- PHP DocBlocker - Auto-generate documentation
- Prettier - Code formatting
Common Installation Issues
Port 80 Already in Use
Problem: Apache won't start because port 80 is occupied (often by Skype or IIS)
Solution:
- Close applications using port 80
- Or change Apache port in httpd.conf (Listen 8080)
- Then access via http://localhost:8080
PHP Not Found in Command Line
Problem: "php is not recognized" error
Solution: Add PHP to your system PATH
- Windows: System Properties → Environment Variables → Path → Add C:\xampp\php
- Mac/Linux: Add to ~/.bashrc or ~/.zshrc: export PATH="/path/to/php:$PATH"
Summary
- XAMPP: All-in-one package with Apache, PHP, MySQL - easiest for beginners
- Standalone: Install PHP directly for more control
- Verify: Use php -v or phpinfo() to confirm installation
- Document Root: Place PHP files in htdocs folder (XAMPP)
- Editor: VS Code with PHP extensions recommended
- Troubleshooting: Check ports, PATH, and firewall settings
What's Next?
Now that PHP is installed, you're ready to write your first program! In the next lesson, we'll create a complete PHP file, understand the basic syntax, and run it on your local server.
Enjoying these tutorials?