PHP Install


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:

  1. A Web Server – Usually Apache or Nginx.

  2. PHP Interpreter – The actual PHP engine that executes the code.

  3. 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:

  • Apache (server)

  • MySQL (database)

  • PHP

  • phpMyAdmin (for managing databases)

Step 1: Download XAMPP

  1. Go to https://www.apachefriends.org.

  2. Choose the XAMPP version for your system (Windows, macOS, or Linux).

  3. Download the installer.

Step 2: Install XAMPP

  1. Run the downloaded setup file.

  2. Follow the installation steps — keep the default settings.

  3. 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

  1. Go to https://www.php.net/downloads.

  2. Download the latest stable version of PHP for your operating system.

  3. Unzip the folder to a location like:

    C:\php\
    
  4. 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:

  1. Create a file named index.php.

  2. Add:

    <?php echo "PHP is installed on macOS!"; ?>
    
  3. Run a local PHP server:

    php -S localhost:8000
    
  4. 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:

  1. Create a file in /var/www/html/ named info.php

    <?php phpinfo(); ?>
    
  2. 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.


Practice Questions

  1. Install XAMPP on your computer and start the Apache server. Create a PHP file named test.php inside the htdocs folder that prints “XAMPP is working!” and open it in your browser.

  2. Create a PHP file that prints “Hello, PHP installation successful!” using the echo statement. Place it in a local server folder and verify it runs in the browser.

  3. Using XAMPP, create a folder called myproject inside htdocs and add a PHP file that prints the current PHP version using the phpversion() function.

  4. Write a PHP script that uses phpinfo() to display all PHP configuration details in the browser. Explain why this is useful after installation.

  5. If Apache does not start in XAMPP because port 80 is busy, change the port to 8080. Then create a PHP file porttest.php that prints “Server running on port 8080!” and verify it in the browser.

  6. On macOS, use Homebrew to install PHP. Then create a PHP file that prints “PHP installed via Homebrew” and run it using the built-in PHP server (php -S localhost:8000).

  7. On Linux (Ubuntu), install Apache and PHP using apt. Create a file index.php in /var/www/html/ that prints “PHP on Linux working!” and open it in the browser.

  8. Write a PHP script that prints “Hello from manual PHP installation!” and explain what happens when the browser requests this file from the server.

  9. After installing PHP, check the version using the command line. Write a PHP script that also prints the PHP version and compare the outputs.

  10. Create a PHP file named info.php that runs phpinfo(). Then write a second PHP file named testdate.php that prints the current date using the date() function. Verify both files run correctly on your server.


Go Back Top