-
Hajipur, Bihar, 844101
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.
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.
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)
Choose the XAMPP version for your system (Windows, macOS, or Linux).
Download the installer.
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.
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.
Now that your setup is ready, let’s run a PHP script.
XAMPP stores all website files inside:
C:\xampp\htdocs\
Create a new folder named myproject
inside htdocs
:
C:\xampp\htdocs\myproject
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.
Open your browser and go to:
http://localhost/myproject/
You should see:
Hello, PHP is running!
Congratulations — PHP is successfully installed on your system.
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.
Download and install Apache or Nginx.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Using XAMPP, create a folder called myproject
inside htdocs
and add a PHP file that prints the current PHP version using the phpversion()
function.
Write a PHP script that uses phpinfo()
to display all PHP configuration details in the browser. Explain why this is useful after installation.
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.
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
).
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.
Write a PHP script that prints “Hello from manual PHP installation!” and explain what happens when the browser requests this file from the server.
After installing PHP, check the version using the command line. Write a PHP script that also prints the PHP version and compare the outputs.
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.