Skip to main content
PHP Backend Development Tutorial
CHAPTER 03 Beginner

Setting Up PHP and Local Development Environment

Updated: May 14, 2026
15 min read

# CHAPTER 3

Setting Up PHP and Local Development Environment

1. Introduction

You cannot run PHP code by simply double-clicking a file like you can with an HTML file. PHP requires a Web Server (to intercept HTTP requests) and a PHP Engine (to process the code). Since you don't want to buy a live internet server just to learn, we will turn your personal computer into a "Local Server." In this chapter, we will install and configure XAMPP, providing you with a fully functional local development environment.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand the components of a LAMP/XAMPP stack.
  • Install XAMPP on your local machine.
  • Start a local Apache web server and MySQL database.
  • Navigate phpMyAdmin for database management.
  • Create and run your first PHP file locally.

3. Beginner-Friendly Explanation

If you try to open a .php file in Chrome directly from your Desktop (e.g., file:///C:/Users/Desktop/index.php), the browser will just show you the raw code. It doesn't know how to run it. You need a Web Server. XAMPP is a free, all-in-one software package. Think of it as an "Internet Simulator." When you turn on XAMPP, your laptop pretends it is a powerful server in a massive data center. You can type http://localhost into your browser, and your browser will talk to your laptop's internal server, allowing the PHP code to run properly.

4. What is XAMPP?

XAMPP stands for:
  • X: Cross-Platform (works on Windows, Mac, Linux).
  • A: Apache (The Web Server software).
  • M: MariaDB/MySQL (The Database).
  • P: PHP (The programming language).
  • P: Perl (Another language, rarely used by beginners).

5. Installing XAMPP

Step-by-Step Installation:
  1. 1. Go to apachefriends.org and download XAMPP for your OS.
  1. 2. Run the installer. Leave all the default settings checked.
  1. 3. Install it in the default directory (usually C:\xampp on Windows or /Applications/XAMPP on Mac).
  1. 4. Open the XAMPP Control Panel.
  1. 5. Click Start next to Apache and MySQL. (If they turn green, your local server is running!).

6. Running Local Servers (htdocs)

Where do you put your code? Apache only looks for files in one specific folder.
  • Windows: C:\xampp\htdocs\
  • Mac: /Applications/XAMPP/htdocs/
This folder is the "root" of your server.

Creating Your First App:

  1. 1. Open the htdocs folder.
  1. 2. Create a new folder named myproject.
  1. 3. Inside myproject, create a file named index.php.
  1. 4. Open your web browser and type: http://localhost/myproject/index.php

7. Coding Tutorial: Your First Local Script

Open index.php in your code editor (like VS Code) and add this code:
php
1234
<?php
// PHP has a built-in function to display all information about your server setup
phpinfo();
?>

Save the file and refresh your browser (http://localhost/myproject/index.php). You will see a massive purple page displaying all your PHP configuration details. Your local environment works!

8. Using phpMyAdmin

XAMPP comes with a database management tool called phpMyAdmin.
  1. 1. Open your browser and type: http://localhost/phpmyadmin
  1. 2. This provides a visual interface (frontend) for your MySQL database.
  1. 3. You can click "New" to create databases, tables, and insert data without having to write complex SQL code in a command-line terminal.

9. Best Practices

  • Use a Code Editor: Do not use Notepad. Download a professional Code Editor like Visual Studio Code (VS Code). It provides syntax highlighting for PHP, autocompletion, and helps catch errors before you run the code.

10. Common Mistakes

  • Forgetting to Start Apache: If your browser says "Site cannot be reached" when you go to localhost, you probably forgot to open the XAMPP Control Panel and click "Start" on Apache.
  • Double-Clicking the File: Never double-click a .php file in your file explorer to open it. It must be accessed through the browser using the http://localhost/ URL.

11. Exercises

  1. 1. Start your XAMPP server, create a folder called testapp in the htdocs directory, and write a PHP script that prints the phrase "Server is running!" to the browser.

12. Coding Challenges

  • Challenge: Explore http://localhost/phpmyadmin. Create a new database named testdb and a table named users with two columns: id and name.

13. MCQs with Answers

Question 1

In the XAMPP stack, what role does Apache play?

Question 2

Where must you save your PHP files in a default Windows XAMPP installation for Apache to serve them to the browser?

14. Interview Questions

  • Q: What is a LAMP/XAMPP stack, and describe the specific function of each component (Linux, Apache, MySQL, PHP).
  • Q: Explain why opening a .php file directly in a browser via the file:// protocol results in raw code display, whereas accessing it via http://localhost/ executes the script.

15. FAQs

Q: Can other people on the internet see my localhost website? A: No. localhost is completely private to your specific computer. It is a closed loop. No one on the internet can see your work until you purchase a domain name and upload your code to a live hosting provider (covered in Chapter 18).

16. Summary

In Chapter 3, we transformed your computer into a backend development laboratory. By installing XAMPP, you now possess a local Apache Web Server, a MySQL Database, and the PHP Engine. By saving your files in the htdocs directory and accessing them via http://localhost, you can write, test, and debug powerful backend applications in a completely safe, private environment.

17. Next Chapter Recommendation

Your environment is ready. Now it's time to learn the language. Proceed to Chapter 4: PHP Syntax and Backend Basics.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·