Setting Up PHP Environment

Before you can write and run PHP code, you need a PHP environment — a setup that can interpret PHP scripts and serve web pages on your computer. PHP is a server-side language, which means it runs on a web server rather than directly in your browser. This topic walks you through getting a fully working PHP environment on your local machine, verifying the installation, understanding the main configuration file, and running your first PHP script.

Option 1: All-in-One Packages (Recommended for Beginners)

The easiest way to get started is to install a pre-packaged bundle that includes PHP, a web server (Apache or Nginx), and a MySQL database all at once. There is a different package available depending on your operating system.

XAMPP — Windows, macOS, and Linux

XAMPP is the most widely used local development package for beginners and is available for all major platforms.

  1. Go to apachefriends.org and download the XAMPP installer for your operating system.
  2. Run the installer and follow the on-screen instructions. Accept the default component selection, which includes Apache, PHP, and MySQL.
  3. Once installed, open the XAMPP Control Panel and click Start next to both Apache and MySQL.
  4. Open your browser and visit http://localhost. You should see the XAMPP welcome page, confirming that the web server is running.
  5. Place your PHP files in the htdocs folder inside the XAMPP installation directory. On Windows this is typically C:\xampp\htdocs\; on macOS it is /Applications/XAMPP/htdocs/.

WAMP — Windows Only

WAMP (Windows, Apache, MySQL, PHP) is another popular option specifically for Windows.

  1. Download WampServer from wampserver.com.
  2. Run the installer. It will detect your installed version of Visual C++ Redistributable and install any missing components automatically.
  3. After installation, WampServer places an icon in the system tray. Click it and select Start All Services.
  4. Visit http://localhost to confirm everything is running.
  5. Place your PHP files in the www folder, typically at C:\wamp\www\.

MAMP — macOS (and Windows)

MAMP (macOS, Apache, MySQL, PHP) is designed for macOS developers but also has a Windows version.

  1. Download MAMP from mamp.info.
  2. Run the installer and open the MAMP application.
  3. Click Start Servers. MAMP opens a welcome page in your browser automatically.
  4. Place your PHP files in the htdocs folder inside the MAMP installation directory, typically /Applications/MAMP/htdocs/.

Option 2: Installing PHP Directly

More experienced developers often install PHP as a standalone tool, which gives more control over the version and configuration.

Windows

  1. Visit php.net/downloads and download the latest stable Windows binary. Choose the Thread Safe ZIP for use with Apache, or Non-Thread Safe for use with PHP's built-in development server.
  2. Extract the ZIP file to a folder such as C:\php.
  3. Add that folder to your system's PATH environment variable so the php command is available in any terminal window:
    • Open System PropertiesAdvancedEnvironment Variables.
    • Under System Variables, find Path, click Edit, and add C:\php.

macOS

PHP is bundled with macOS, but the built-in version is often outdated. Installing via Homebrew is the recommended approach.

  1. Install Homebrew if you do not have it: open Terminal and run the command shown at brew.sh.
  2. Install the latest PHP version:
brew install php

Linux (Ubuntu / Debian)

sudo apt update
sudo apt install php php-cli php-common php-mysql

Verifying the Installation

After installing PHP through any method, verify it is working by checking the version from the command line.

php --version

You should see output similar to:

PHP 8.3.2 (cli) (built: Jan 16 2024 07:23:44)
Copyright (c) The PHP Group

If you see a version number, PHP is installed correctly. If you get an error such as "command not found", PHP is either not installed or not added to your system PATH.

Running Your First PHP Script

Through the Web Server

  1. Create a file called index.php inside the web root folder (htdocs for XAMPP/MAMP, www for WAMP).
  2. Add the following content:
<?php
  phpinfo();
?>
  1. Open your browser and visit http://localhost/index.php.

You should see a detailed page showing your PHP version, configuration settings, loaded extensions, and more. This page is generated by the built-in phpinfo() function.

Through PHP's Built-in Development Server

PHP includes its own lightweight web server — ideal for quick testing without Apache or Nginx.

  1. Open a terminal and navigate to the folder containing your PHP files:
cd /path/to/your/project
  1. Start the built-in server:
php -S localhost:8000
  1. Visit http://localhost:8000 in your browser. PHP will serve files from the current folder.
  1. Press Ctrl + C in the terminal to stop the server.

Running a PHP Script from the Command Line

PHP can also run scripts directly in the terminal without a web server, which is useful for testing logic quickly.

php index.php

Create a simple script and run it:

<?php
  echo "Hello, PHP environment is working!\n";

  $version = phpversion();
  echo "PHP Version: " . $version . "\n";
?>
php hello.php

Expected output:

Hello, PHP environment is working!
PHP Version: 8.3.2

Understanding php.ini

The php.ini file is PHP's main configuration file. It controls how PHP behaves: how long scripts are allowed to run, how large uploaded files can be, whether errors are displayed, and much more.

Finding php.ini

The location varies by installation. The easiest way to find it is to check the output of phpinfo() in the browser — look for the Loaded Configuration File row near the top of the page. You can also run this from the terminal:

php --ini

Common php.ini Settings

; Maximum time a script is allowed to run (seconds)
max_execution_time = 30

; Maximum amount of memory a script can consume
memory_limit = 128M

; Whether PHP errors are displayed in the browser
display_errors = On          ; Use On for development, Off for production

; Whether errors are written to a log file
log_errors = On

; Path to the error log file
error_log = /path/to/php_errors.log

; Which types of errors to report
error_reporting = E_ALL

; Maximum size of an uploaded file
upload_max_filesize = 2M

; Maximum size of POST data that PHP will accept
post_max_size = 8M

; Default timezone for date and time functions
date.timezone = "America/New_York"

After editing php.ini, you must restart Apache (or whatever web server you are using) for the changes to take effect. In XAMPP, use the Control Panel to stop and start Apache again.

Recommended Text Editors and IDEs

  • Visual Studio Code — free, lightweight, and widely used. Install the PHP Intelephense extension for autocompletion, error highlighting, and code navigation.
  • PhpStorm — the most feature-rich IDE for PHP development, with built-in debugging, database tools, and framework support. Paid, but free for students.
  • Sublime Text — fast and minimal; works well with PHP through community packages.

Key Points

  • XAMPP is the easiest way to set up PHP locally on any operating system — it bundles PHP, Apache, and MySQL together.
  • After installing, verify that PHP is working by running php --version in a terminal.
  • Use the phpinfo() function in a browser to see all configuration details about your PHP installation.
  • PHP includes a built-in development server started with php -S localhost:8000 — no Apache needed for quick testing.
  • The php.ini file controls PHP's configuration settings; restart the web server after making any changes to it.
  • Set display_errors = On and error_reporting = E_ALL in php.ini during development so you see all errors immediately.

Leave a Comment

Your email address will not be published. Required fields are marked *