Environment Setup
Overview
Before you can start PHP development, you need to set up a development environment. This chapter will guide you through installing PHP and the necessary tools on different operating systems.
Installation Options
Option 1: All-in-One Solutions (Recommended for Beginners)
XAMPP (Cross-Platform)
XAMPP includes Apache, MySQL, PHP, and phpMyAdmin in one package.
Windows/macOS/Linux:
- Download from https://www.apachefriends.org/
- Run the installer
- Start Apache and MySQL from the control panel
- Visit
http://localhostto test
WAMP (Windows)
Windows-specific solution with Apache, MySQL, and PHP.
Installation:
- Download from http://www.wampserver.com/
- Install and start services
- Access via system tray icon
MAMP (macOS)
Mac-specific development environment.
Installation:
- Download from https://www.mamp.info/
- Install and configure ports
- Start server from the application
Option 2: Standalone Installation
Windows
Using Chocolatey:
# First install Chocolatey, then:
choco install php
choco install composerManual Installation:
- Download PHP from https://windows.php.net/download/
- Extract to
C:\php - Add
C:\phpto system PATH environment variable - Copy
php.ini-developmenttophp.ini
macOS
Using Homebrew:
# First install Homebrew, then:
brew install php
brew install composerUsing MacPorts:
sudo port install php81 +apache2
sudo port install php81-mysqlLinux (Ubuntu/Debian)
# Update package list
sudo apt update
# Install PHP and common extensions
sudo apt install php php-cli php-mysql php-xml php-mbstring php-curl
# Install Composer
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composerLinux (CentOS/RHEL)
# Install PHP
sudo yum install php php-cli php-mysql php-xml php-mbstring php-curl
# Or use dnf on newer versions
sudo dnf install php php-cli php-mysql php-xml php-mbstring php-curlVerifying Installation
Check PHP Version
php --versionExpected output:
PHP 8.2.0 (cli) (built: Dec 6 2022 15:31:23) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.0, Copyright (c) Zend TechnologiesCheck PHP Configuration
php --iniTest PHP in Browser
Create a file named info.php:
<?php
phpinfo();
?>Place it in your web server's document root, then visit http://localhost/info.php
Essential Extensions
Core Extensions (Usually Included)
- mysqli/pdo: Database connectivity
- curl: HTTP client functionality
- json: JSON data handling
- mbstring: Multibyte string handling
- xml: XML processing
Installing Additional Extensions
Ubuntu/Debian:
sudo apt install php-gd php-zip php-intlWindows (Using Composer):
composer require ext-gd ext-zip ext-intlDevelopment Tools
Code Editors and IDEs
Visual Studio Code (Free)
Recommended Extensions:
- PHP Intelephense
- PHP Debug
- Bracket Pair Colorizer
- GitLens
PhpStorm (Paid)
Professional IDE with advanced features:
- Intelligent code completion
- Built-in debugger
- Database tools
- Framework support
Sublime Text (Paid)
Lightweight with PHP packages:
- SublimeLinter-php
- PHPCompanion
- DocBlockr
Composer (Dependency Manager)
Composer is essential for modern PHP development.
Verify Installation:
composer --versionBasic Usage:
# Initialize new project
composer init
# Install a package
composer require monolog/monolog
# Install dependencies
composer installXdebug (Debugger)
Installation:
# Ubuntu/Debian
sudo apt install php-xdebug
# macOS with Homebrew
brew install php-xdebugConfigure in php.ini:
zend_extension=xdebug
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_port=9003Web Server Configuration
Built-in Development Server
PHP includes a built-in web server for development:
# Start server on localhost:8000
php -S localhost:8000
# Start with specified document root
php -S localhost:8000 -t /path/to/document/root
# Start with router script
php -S localhost:8000 router.phpApache Configuration
Enable PHP Module:
LoadModule php_module modules/libphp.so
AddType application/x-httpd-php .phpVirtual Host Example:
<VirtualHost *:80>
DocumentRoot "/var/www/myproject"
ServerName myproject.local
<Directory "/var/www/myproject">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>Nginx Configuration
Basic PHP Configuration:
server {
listen 80;
server_name myproject.local;
root /var/www/myproject;
index index.php index.html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}Database Setup
MySQL/MariaDB
Installation:
# Ubuntu/Debian
sudo apt install mysql-server
# macOS
brew install mysql
# Start service
sudo systemctl start mysqlBasic Configuration:
sudo mysql_secure_installationPostgreSQL
Installation:
# Ubuntu/Debian
sudo apt install postgresql postgresql-contrib
# macOS
brew install postgresqlDevelopment Workflow Setup
Project Structure
my-php-project/
├── public/
│ ├── index.php
│ └── .htaccess
├── src/
├── tests/
├── vendor/
├── composer.json
└── .gitignoreComposer Configuration
{
"name": "myname/my-php-project",
"description": "My PHP Project",
"type": "project",
"require": {
"php": ">=8.0"
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
}Git Configuration
git init
git add .
git commit -m "Initial commit"Common Troubleshooting
Permission Issues (Linux/macOS)
# Fix web server permissions
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/htmlPHP Extension Not Loading
- Check
php.inilocation:php --ini - Verify extension path in
php.ini - Restart web server after modifications
Port Conflicts
# Check what's using port 80
sudo netstat -tulpn | grep :80
# Use a different port
php -S localhost:8080Next Steps
Now that your environment is set up, let's create your first PHP script in Quick Start.
Verification Checklist
- [ ] PHP is installed and accessible from command line
- [ ] Web server is running (Apache/Nginx or built-in server)
- [ ] Composer is installed and working
- [ ] Code editor is configured with PHP extensions
- [ ] Database server is running (if needed)
- [ ]
phpinfo()page is accessible in browser