Since PHP is a server-side language, you cannot simply open a PHP file in your browser. You need a functioning web server (like Apache), a PHP interpreter, and a database (like MySQL) running on your computer.
Instead of installing each component separately, we use an integrated package known as a “Stack.”
Understanding the Essential Stack
The most popular stacks package the three essential tools needed for local PHP development:
| Component | Role | Description |
| PHP | The Language | Executes the PHP code written in your tutorials. |
| MySQL/MariaDB | The Database | Stores data (user profiles, post content, etc.) for dynamic websites. |
| Apache | The Web Server | Receives requests from your browser and serves up the correct PHP file. |
You have three main options for installing this stack, depending on your operating system:
1. XAMPP (Windows, macOS, Linux)
XAMPP is the most popular, cross-platform solution.
- X (Cross-Platform)
- Apache (Web Server)
- MariaDB (Database)
- PHP (Scripting Language)
- Perl (Optional Scripting Language)
Installation Steps (General)
- Download: Go to the official Apache Friends website and download the XAMPP installer for your operating system (Windows, macOS, or Linux).
- Install: Run the installer and follow the on-screen prompts. It is usually best to install it in the default directory (e.g.,
C:\xamppon Windows). - Start Control Panel: Open the XAMPP Control Panel.
- Launch Services: Click the Start buttons next to Apache and MySQL. The modules should turn green, indicating they are running.
- Verification: Open your web browser and navigate to
http://localhost. If XAMPP is working correctly, you will see the XAMPP dashboard.
2. MAMP (macOS and Windows)
MAMP (My Apache MySQL PHP) is another user-friendly package, particularly popular among Mac users.
- Mac (or Windows)
- Apache
- MySQL
- PHP
The installation process is very similar to XAMPP: download, install, and then launch the MAMP Manager application to start the server components.
3. LAMP/WAMP/LEMP (Advanced)
- LAMP (Linux)
- WAMP (Windows)
- LEMP (Linux with Nginx instead of Apache)
These are used by advanced developers who prefer to install and configure each component manually. For beginners, XAMPP is strongly recommended.
Running Your First PHP File
Once your local server (Apache) is running via XAMPP or MAMP, you can test your code.
- Locate the Web Root:
- XAMPP: The main folder for all websites is usually called
htdocs(e.g.,C:\xampp\htdocs). - MAMP: The main folder is usually called
htdocsorwwwwithin the MAMP installation folder.
- XAMPP: The main folder for all websites is usually called
- Create Your File: Inside the
htdocsfolder, create a new file namedhello.php. - Add the Code: Paste the following content into
hello.php:
PHP
<?php
echo "<h1>Local Server Test Successful!</h1>";
?>
- View the Result: Open your web browser and navigate to the following address:
http://localhost/hello.php
If you see the large heading “Local Server Test Successful!”, your PHP environment is set up and ready for development.
Next Steps
Now that you have your server running, we can dive into the fundamental rules of the language. The next chapter will cover “PHP Basic Syntax, Variables, and Comments.”
