Tutorials Logic, IN info@tutorialslogic.com

Install and Configure PHP for Web and Command-Line Development

Install PHP

A working PHP setup has three separate concerns: the PHP runtime, an optional web server integration, and the configuration loaded by that execution mode. Confirm each one instead of assuming that a successful installer configured every terminal and server process.

After this lesson, you can identify the PHP executable in use, find its loaded configuration, enable a required extension, and explain why a script may work in the terminal but fail through Apache or PHP-FPM.

Runtime Choices

Use a maintained operating-system package, the official Windows build, a development bundle such as XAMPP, or a container. Choose one primary setup for a project so that the terminal, editor, tests, and web server agree on the PHP version.

Setup Best Fit Check
XAMPP or similar bundle Fast local web setup Confirm the bundled php.exe and Apache module
Operating-system package Native Linux or macOS workflow Confirm package version and enabled extensions
Container Reproducible team environment Pin the image version and build configuration

Runtime Identity

Run these commands in the same terminal used for Composer and tests. On Windows with XAMPP, call C:\xampp\php\php.exe explicitly when php is not on PATH.

Inspect the Active CLI

Inspect the Active CLI
php --version
php --ini
php -m

--version identifies the executable version, --ini lists loaded configuration files, and -m lists available modules.

Configuration Loading

PHP reads php.ini when the runtime starts. A web server process can load a different file from the CLI, so compare php --ini with phpinfo() or php_ini_loaded_file() in the web request before changing settings.

Restart a long-running web server or PHP-FPM process after changing startup configuration. A new CLI command starts a new process and therefore reads its configuration again automatically.

Show Web Runtime Details

Show Web Runtime Details
<?php
echo 'PHP: ' . PHP_VERSION . PHP_EOL;
echo 'SAPI: ' . PHP_SAPI . PHP_EOL;
echo 'php.ini: ' . (php_ini_loaded_file() ?: 'not loaded') . PHP_EOL;

Remove this diagnostic page after setup because configuration output reveals server details.

Extension Checks

Enable only extensions the application needs, using the package manager or the correct php.ini. Verify the result in the same SAPI that runs the application. PDO may be loaded while a driver such as pdo_mysql is still missing.

Require a Database Driver

Require a Database Driver
<?php
if (!extension_loaded('pdo_mysql')) {
    throw new RuntimeException('The pdo_mysql extension is required.');
}

echo 'PDO MySQL ready';
Output
PDO MySQL ready

The output assumes the driver is installed; otherwise the deliberate exception names the missing capability.

Setup Diagnosis

  • Command not found: use the absolute executable path or correct PATH, then open a new terminal.
  • Raw PHP source in the browser: the web server is serving the file without a PHP handler.
  • Extension works in CLI only: compare the CLI and web php.ini paths and SAPI values.
  • Blank page: inspect the server error log and temporarily enable development error reporting locally.

Confirm the Active Runtime

0 of 2 checked

Q1. Which command shows the php.ini files used by the CLI?

Q2. Why can Apache and CLI expose different extensions?

Try this next

Audit Your PHP Setup

0 of 2 completed

  1. Record PHP_VERSION, PHP_SAPI, and php_ini_loaded_file() in CLI and through localhost.
  2. List the application-required extensions and confirm each one without exposing phpinfo() publicly.
Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.