Skip to content

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

XAMPP (Cross-Platform)

XAMPP includes Apache, MySQL, PHP, and phpMyAdmin in one package.

Windows/macOS/Linux:

  1. Download from https://www.apachefriends.org/
  2. Run the installer
  3. Start Apache and MySQL from the control panel
  4. Visit http://localhost to test

WAMP (Windows)

Windows-specific solution with Apache, MySQL, and PHP.

Installation:

  1. Download from http://www.wampserver.com/
  2. Install and start services
  3. Access via system tray icon

MAMP (macOS)

Mac-specific development environment.

Installation:

  1. Download from https://www.mamp.info/
  2. Install and configure ports
  3. Start server from the application

Option 2: Standalone Installation

Windows

Using Chocolatey:

powershell
# First install Chocolatey, then:
choco install php
choco install composer

Manual Installation:

  1. Download PHP from https://windows.php.net/download/
  2. Extract to C:\php
  3. Add C:\php to system PATH environment variable
  4. Copy php.ini-development to php.ini

macOS

Using Homebrew:

bash
# First install Homebrew, then:
brew install php
brew install composer

Using MacPorts:

bash
sudo port install php81 +apache2
sudo port install php81-mysql

Linux (Ubuntu/Debian)

bash
# 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/composer

Linux (CentOS/RHEL)

bash
# 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-curl

Verifying Installation

Check PHP Version

bash
php --version

Expected 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 Technologies

Check PHP Configuration

bash
php --ini

Test PHP in Browser

Create a file named info.php:

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:

bash
sudo apt install php-gd php-zip php-intl

Windows (Using Composer):

bash
composer require ext-gd ext-zip ext-intl

Development 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:

bash
composer --version

Basic Usage:

bash
# Initialize new project
composer init

# Install a package
composer require monolog/monolog

# Install dependencies
composer install

Xdebug (Debugger)

Installation:

bash
# Ubuntu/Debian
sudo apt install php-xdebug

# macOS with Homebrew
brew install php-xdebug

Configure in php.ini:

ini
zend_extension=xdebug
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_port=9003

Web Server Configuration

Built-in Development Server

PHP includes a built-in web server for development:

bash
# 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.php

Apache Configuration

Enable PHP Module:

apache
LoadModule php_module modules/libphp.so
AddType application/x-httpd-php .php

Virtual Host Example:

apache
<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:

nginx
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:

bash
# Ubuntu/Debian
sudo apt install mysql-server

# macOS
brew install mysql

# Start service
sudo systemctl start mysql

Basic Configuration:

bash
sudo mysql_secure_installation

PostgreSQL

Installation:

bash
# Ubuntu/Debian
sudo apt install postgresql postgresql-contrib

# macOS
brew install postgresql

Development Workflow Setup

Project Structure

my-php-project/
├── public/
│   ├── index.php
│   └── .htaccess
├── src/
├── tests/
├── vendor/
├── composer.json
└── .gitignore

Composer Configuration

json
{
    "name": "myname/my-php-project",
    "description": "My PHP Project",
    "type": "project",
    "require": {
        "php": ">=8.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    }
}

Git Configuration

bash
git init
git add .
git commit -m "Initial commit"

Common Troubleshooting

Permission Issues (Linux/macOS)

bash
# Fix web server permissions
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

PHP Extension Not Loading

  1. Check php.ini location: php --ini
  2. Verify extension path in php.ini
  3. Restart web server after modifications

Port Conflicts

bash
# Check what's using port 80
sudo netstat -tulpn | grep :80

# Use a different port
php -S localhost:8080

Next 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

Content is for learning and research only.