Skip to content

PHP Installation — Setting Up PHP on Windows, macOS, and Linux

DodaTech Updated 2026-06-28 6 min read

In this tutorial, you will learn about PHP Installation. We cover key concepts, practical examples, and best practices to help you master this topic.

Installing PHP requires a web server (Apache or Nginx), the PHP interpreter itself, and optionally a database like MySQL, all configured to work together for serving dynamic web applications.

Why It Matters

PHP is useless without a proper runtime environment. You need the PHP interpreter, a web server to handle HTTP requests, and database connectivity. A correct setup lets you test locally before deploying to production. The most common frustration for beginners is not understanding how these pieces fit together. Once you get PHP running with a single phpinfo() page, you have a foundation for every future PHP project.

Real-World Use

Every production PHP deployment uses the same stack: a web server, PHP-FPM for processing, and a database. Shared hosting providers give you Apache with mod_php pre-configured. Laravel Forge and Docker automate production setups. DodaTech's website uses PHP with Nginx and PHP-FPM for the user portal.

What You Will Learn

  • Installing PHP 8 on Windows, macOS, and Linux
  • Configuring Apache and Nginx to work with PHP
  • Installing Composer for dependency management
  • Setting up a local development environment
  • Troubleshooting common installation issues

Learning Path

flowchart LR
  A[What Is PHP] --> B[Installation
You are here] B --> C[Syntax and Variables] C --> D[Control Flow and Loops] D --> E[Functions] style B fill:#f90,color:#fff

Installing on Linux

Ubuntu and Debian

Ubuntu includes PHP in its package repositories. Install PHP 8.3 with common extensions:

sudo apt update
sudo apt install php8.3 php8.3-cli php8.3-common php8.3-mysql php8.3-zip php8.3-gd php8.3-mbstring php8.3-curl php8.3-xml php8.3-bcmath

Verify the installation:

php -v

Expected output:

PHP 8.3.x (cli) (built: ...)
Copyright (c) The PHP Group
Zend Engine v4.3.x

The php8.3-cli package gives you the command-line interface. The other packages add database, image processing, and HTTP support.

Installing Apache with PHP

sudo apt install apache2 libapache2-mod-php8.3
sudo systemctl enable apache2
sudo systemctl start apache2

Create a test file:

echo "<?php phpinfo();" | sudo tee /var/www/html/info.php

Open http://localhost/info.php in your browser. You should see the PHP information page with version, extensions, and configuration.

Installing Nginx with PHP-FPM

Nginx does not embed PHP. It uses PHP-FPM (FastCGI Process Manager) as a separate service:

sudo apt install nginx php8.3-fpm

Create an Nginx server block:

server {
    listen 80;
    server_name localhost;
    root /var/www/html;
    index index.php;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
    }
}
sudo systemctl restart nginx

Installing on macOS

macOS ships with PHP removed since macOS Ventura. Install PHP via Homebrew:

brew install php

This installs PHP 8.3 with PHP-FPM built in. Verify:

php -v

To start PHP-FPM automatically:

brew services start php

For a full local development environment, install Laravel Valet:

composer global require laravel/valet
valet install

Valet starts PHP-FPM and Nginx automatically when your Mac boots. It proxies requests to your sites automatically.

Installing on Windows

XAMPP bundles Apache, PHP, MySQL, and phpMyAdmin in a single installer:

  1. Download XAMPP from apachefriends.org
  2. Run the installer with default options
  3. Open the XAMPP Control Panel and start Apache and MySQL
  4. Place your PHP files in C:\xampp\htdocs
  5. Open http://localhost in your browser

Option 2: Manual Installation

Download the PHP ZIP package from windows.php.net. Extract it to C:\php. Add C:\php to your system PATH. Configure PHP by copying php.ini-development to php.ini and enabling extensions like extension=mysqli.

Option 3: WSL

Install Windows Subsystem for Linux, then follow the Ubuntu instructions. This gives you a production-like Linux environment on Windows.

Installing Composer

Composer is PHP's package manager. Install it globally:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer

Verify:

composer --version

Now you can install packages:

composer require guzzlehttp/guzzle

Testing Your Setup

Create a PHP file that tests everything:

<?php
echo "PHP Version: " . phpversion() . "\n";
echo "Extensions loaded: " . implode(', ', get_loaded_extensions()) . "\n";
echo "MySQL available: " . (extension_loaded('mysqli') ? 'Yes' : 'No') . "\n";
echo "JSON available: " . (extension_loaded('json') ? 'Yes' : 'No') . "\n";

Run it from the command line:

php test.php

Common Mistakes

1. Forgetting to Restart the Web Server

After installing PHP or changing configuration, restart Apache or PHP-FPM. Otherwise changes do not take effect.

2. Using Wrong PHP Version

Multiple PHP versions can coexist. Ensure your web server uses the correct version by checking phpinfo() output.

3. Missing Extensions

Many PHP features require specific extensions. Run php -m to list loaded extensions. Install missing ones with your package manager.

4. File Permission Issues

PHP needs read access to execute files and write access for uploads and sessions. Set permissions carefully: directories should be 755, files should be 644.

5. Not Setting the Document Root Correctly

Your web server must point to the directory containing your PHP files. Apache uses DocumentRoot. Nginx uses root. If you get a 404, your files may be in the wrong place.

Practice Questions

  1. What is the difference between mod_php and PHP-FPM? mod_php runs PHP inside Apache. PHP-FPM runs PHP as a separate process, used with Nginx.

  2. How do you check which PHP version is installed? Run php -v from the command line.

  3. What is Composer and why do you need it? Composer manages PHP dependencies, handles autoloading, and downloads libraries from Packagist.

  4. What is the purpose of the document root? It is the directory the web server serves files from. Only files inside it are publicly accessible.

  5. Challenge: Install a complete LAMP or LEMP stack on your system, create a phpinfo page, and verify all extensions are working.

Mini Project: Environment Checker

Create a script that checks your PHP environment:

<?php
$checks = [
    'PHP Version >= 8.0' => version_compare(phpversion(), '8.0.0', '>='),
    'PDO extension' => extension_loaded('pdo'),
    'JSON extension' => extension_loaded('json'),
    'MBString extension' => extension_loaded('mbstring'),
    'CURL extension' => extension_loaded('curl'),
];

$allPass = true;
foreach ($checks as $name => $result) {
    echo ($result ? 'PASS' : 'FAIL') . ": $name\n";
    if (!$result) $allPass = false;
}
echo $allPass ? "All checks passed.\n" : "Some checks failed. Install missing extensions.\n";

FAQ

Do I need Apache or Nginx to run PHP?

For command-line scripts, no. Just type 'php script.php'. For web applications, yes -- you need a web server to handle HTTP requests and serve PHP files.

What is the difference between php and php-fpm?

php is the CLI binary for running scripts. php-fpm is the FastCGI process manager used by Nginx to handle web requests.

Can I install multiple PHP versions?

Yes. Linux repositories support co-installation. Use php8.2 and php8.3 side by side. Your web server can switch between them.

Why do I get 'Connection refused' when connecting to MySQL?

MySQL may not be running. Check with 'systemctl status mysql' or 'brew services list'. Also verify the host and port in your PHP configuration.

What is php.ini and how do I find it?

php.ini is the PHP configuration file. Run 'php --ini' to find its location. Common settings include memory_limit, upload_max_filesize, and display_errors.

What is Next

Now that PHP is installed, proceed to PHP Syntax and Variables to learn the basic syntax, variable types, and how to write your first PHP scripts. After that, read Control Flow and Loops to understand conditional logic in PHP.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro