Before you start writing PHP code, you need to set up a working environment where PHP can run. Installing PHP isn’t complicated, but it’s important to understand how everything connects — the web server, PHP engine, and browser. Once you get this right, you’ll be ready to run your first PHP file in minutes.
What You Need to Run PHP
PHP is a server-side language, which means it needs a web server to execute. You can’t just open a PHP file directly in your browser like an HTML file. The browser alone can’t process PHP code — it must go through a server that has PHP installed.
To run PHP locally, you’ll need three things:
-
A Web Server – Usually Apache or Nginx.
-
PHP Interpreter – The actual PHP engine that executes the code.
-
A Database (optional) – Such as MySQL or MariaDB, if you want to store data.
Instead of installing each one manually, you can use an all-in-one package like XAMPP, WAMP, or MAMP.
Installing PHP Using XAMPP (Recommended)
The easiest way to install PHP on your computer is by using XAMPP. It’s free, works on all platforms, and includes:
Step 1: Download XAMPP
-
Go to https://www.apachefriends.org.
-
Choose the XAMPP version for your system (Windows, macOS, or Linux).
-
Download the installer.
Step 2: Install XAMPP
-
Run the downloaded setup file.
-
Follow the installation steps — keep the default settings.
-
Once installed, open the XAMPP Control Panel.
You’ll see modules like Apache, MySQL, FileZilla, etc.
Click Start next to Apache to start your local web server. If you plan to use databases later, start MySQL too.
Step 3: Verify Installation
After Apache starts, open your browser and type:
http://localhost
If everything is working, you’ll see the XAMPP welcome page.
This means your server is running successfully, and PHP is ready to use.
Running Your First PHP File
Now that your setup is ready, let’s run a PHP script.
Step 1: Locate the htdocs Folder
XAMPP stores all website files inside:
C:\xampp\htdocs\
Step 2: Create a New Folder
Create a new folder named myproject inside htdocs:
C:\xampp\htdocs\myproject
Step 3: Create a PHP File
Open any text editor (VS Code, Notepad++, Sublime, etc.) and create a new file named:
index.php
Add the following code:
<?php
echo "Hello, PHP is running!";
?>
Save it inside your myproject folder.
Step 4: Run the File in Browser
Open your browser and go to:
http://localhost/myproject/
You should see:
Hello, PHP is running!
Congratulations — PHP is successfully installed on your system.
Installing PHP Without XAMPP (Manual Method)
If you don’t want to use XAMPP, you can install PHP manually. This method gives you more control and is often used on production servers.
Step 1: Install a Web Server
Download and install Apache or Nginx.
Step 2: Install PHP
-
Go to https://www.php.net/downloads.
-
Download the latest stable version of PHP for your operating system.
-
Unzip the folder to a location like:
C:\php\
-
Add the PHP folder path to your system’s environment variables so you can use it from the command line.
Step 3: Configure PHP with Apache
Open the Apache configuration file (httpd.conf) and add:
LoadModule php_module "c:/php/php8apache2_4.dll"
AddType application/x-httpd-php .php
PHPIniDir "C:/php"
Restart Apache, and your PHP setup is ready.
Installing PHP on macOS
macOS used to come with PHP pre-installed, but that changed with newer versions. You can now install PHP using Homebrew, a package manager for macOS.
Run the following commands in the Terminal:
brew update
brew install php
After installation, check if PHP is working:
php -v
You’ll see the installed PHP version.
To test a script:
-
Create a file named index.php.
-
Add:
<?php echo "PHP is installed on macOS!"; ?>
-
Run a local PHP server:
php -S localhost:8000
-
Visit http://localhost:8000 in your browser.
Installing PHP on Linux
Most Linux distributions make it easy to install PHP with package managers.
For Ubuntu or Debian:
sudo apt update
sudo apt install apache2 php libapache2-mod-php
Check PHP version:
php -v
To verify that PHP is running:
-
Create a file in /var/www/html/ named info.php
<?php phpinfo(); ?>
-
Open your browser and go to:
http://localhost/info.php
You’ll see a page showing your PHP configuration.
Checking Your PHP Version
After installing, always check which PHP version you’re running.
Open the command prompt or terminal and type:
php -v
Example output:
PHP 8.2.12 (cli) (built: Oct 2024)
If you see this, PHP is correctly installed.
You can also check from a web browser using:
<?php
phpinfo();
?>
This function shows complete information about your PHP environment — version, configuration, and extensions.
Troubleshooting Common Installation Issues
1. Apache won’t start:
Check if another program (like Skype or IIS) is using port 80. Change Apache’s port to 8080 in the XAMPP control panel and access it at http://localhost:8080.
2. Blank page in browser:
Make sure your file is saved with .php extension and placed inside htdocs (for XAMPP).
3. PHP not recognized in terminal:
If you installed PHP manually, ensure the PHP folder path is added to your system environment variables.
4. phpinfo() not showing:
Confirm that the Apache module for PHP is enabled and Apache is restarted.
Summary of the Tutorial
Installing PHP is simple if you follow the right steps. The easiest option is using XAMPP, which bundles Apache, PHP, and MySQL together. Once installed, you can create .php files, place them in the htdocs folder, and run them through http://localhost.
For more control, you can install PHP manually or use tools like Homebrew (macOS) or apt (Linux). After setup, verify your installation with php -v or phpinfo() to make sure everything is working correctly.