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.
- Go to apachefriends.org and download the XAMPP installer for your operating system.
- Run the installer and follow the on-screen instructions. Accept the default component selection, which includes Apache, PHP, and MySQL.
- Once installed, open the XAMPP Control Panel and click Start next to both Apache and MySQL.
- Open your browser and visit
http://localhost. You should see the XAMPP welcome page, confirming that the web server is running. - Place your PHP files in the
htdocsfolder inside the XAMPP installation directory. On Windows this is typicallyC:\xampp\htdocs\; on macOS it is/Applications/XAMPP/htdocs/.
WAMP — Windows Only
WAMP (Windows, Apache, MySQL, PHP) is another popular option specifically for Windows.
- Download WampServer from wampserver.com.
- Run the installer. It will detect your installed version of Visual C++ Redistributable and install any missing components automatically.
- After installation, WampServer places an icon in the system tray. Click it and select Start All Services.
- Visit
http://localhostto confirm everything is running. - Place your PHP files in the
wwwfolder, typically atC:\wamp\www\.
MAMP — macOS (and Windows)
MAMP (macOS, Apache, MySQL, PHP) is designed for macOS developers but also has a Windows version.
- Download MAMP from mamp.info.
- Run the installer and open the MAMP application.
- Click Start Servers. MAMP opens a welcome page in your browser automatically.
- Place your PHP files in the
htdocsfolder 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
- 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.
- Extract the ZIP file to a folder such as
C:\php. - Add that folder to your system's
PATHenvironment variable so thephpcommand is available in any terminal window:- Open System Properties → Advanced → Environment Variables.
- Under System Variables, find
Path, click Edit, and addC:\php.
macOS
PHP is bundled with macOS, but the built-in version is often outdated. Installing via Homebrew is the recommended approach.
- Install Homebrew if you do not have it: open Terminal and run the command shown at brew.sh.
- 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
- Create a file called
index.phpinside the web root folder (htdocsfor XAMPP/MAMP,wwwfor WAMP). - Add the following content:
<?php
phpinfo();
?>
- 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.
- Open a terminal and navigate to the folder containing your PHP files:
cd /path/to/your/project
- Start the built-in server:
php -S localhost:8000
- Visit
http://localhost:8000in your browser. PHP will serve files from the current folder.
- Press
Ctrl + Cin 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 --versionin 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.inifile controls PHP's configuration settings; restart the web server after making any changes to it. - Set
display_errors = Onanderror_reporting = E_ALLinphp.iniduring development so you see all errors immediately.
