Back to Blog
Lesson 1 of the PHP: Modern PHP from the Ground Up course
PHPJuly 12, 20264 min read

Setting Up the Local Development Environment for PHP

Master your PHP installation and local development environment setup. Learn to configure a web server and verify your CLI installation to start coding.

PHPWeb DevelopmentBackendSetupCLI

Welcome to "PHP: Modern PHP from the Ground Up." In this course, we are going to build a functional MVC web application from scratch. Before we write our first line of logic, we must ensure your computer is prepared to act as a server.

Setting up a robust local development environment is the most important step in your backend journey. If you are coming from other ecosystems, you might be used to specialized tooling, but for raw PHP, we need a reliable foundation. Whether you are Setting Up the Local Development Environment for Laravel or building a custom project, the principles remain the same: you need an interpreter, a server, and a code editor.

Understanding the PHP Stack

To execute PHP, your machine needs three primary components:

  1. The PHP Interpreter: This is the engine that reads your code and turns it into HTML or data for the browser.
  2. A Web Server: Tools like Apache or Nginx listen for HTTP requests from your browser and route them to the PHP interpreter.
  3. A Code Editor: You need a workspace that understands PHP syntax to help you write clean, efficient code.

While there are "all-in-one" installers like XAMPP or MAMP, I recommend installing these components natively or using a lightweight tool like php -S (the built-in development server) to understand how the moving parts actually fit together.

Step 1: Installing PHP

First, verify if you already have PHP installed. Open your terminal (or Command Prompt on Windows) and type:

Bash
php -v

If you see a version number (e.g., PHP 8.2.x), you are ready to go. If not, follow the instructions for your OS:

  • macOS: The easiest path is using Homebrew. Run brew install php.
  • Windows: Download the "Thread Safe" zip file from windows.php.net, extract it to C:\php, and add that folder to your System PATH.
  • Linux: Use your distribution's package manager (e.g., sudo apt install php on Ubuntu).

After installation, run php -v again to confirm the version is recognized by your shell.

Step 2: Configuring the Local Development Server

For this course, we will use PHP’s built-in web server. It is perfect for local development because it requires no complex configuration files.

  1. Create a folder for your project: mkdir my-php-app && cd my-php-app.
  2. Create a file named index.php inside that folder.
  3. Open index.php in your code editor (I recommend VS Code) and add this line: <?php echo "Hello, Server!"; ?>
  4. Start the server by running this command in your terminal:
Bash
php -S localhost:8000

Now, open your web browser and navigate to http://localhost:8000. You should see "Hello, Server!" rendered on your screen. You have successfully configured a local development environment.

Hands-on Exercise

To verify your setup, try this:

  1. Stop the server by pressing Ctrl + C in your terminal.
  2. Change the text in index.php to <?php echo "My first MVC app is running!"; ?>.
  3. Restart the server with the same command.
  4. Refresh your browser. If you see the updated text, your environment is correctly watching your files.

Common Pitfalls

  • Path Issues: If the terminal says php: command not found, the PHP executable isn't in your system's PATH. You must add the directory containing the php binary to your environment variables.
  • Port Conflicts: If you get an error like "Address already in use," another process is likely using port 8000. Simply change the port in the command: php -S localhost:8080.
  • File Extensions: Always ensure your files end in .php. The server will treat a .html file as plain text and will not execute the PHP code inside it.

FAQ

Do I need Apache or Nginx? Not for this course. PHP's built-in php -S server is sufficient for local development. You only need Apache or Nginx when deploying to a production server.

Is VS Code the best editor for PHP? It is excellent. Install the "PHP Intelephense" extension for better code completion and error checking.

Recap

We have successfully installed the PHP interpreter, validated the installation via CLI, and launched a local development server to serve our files. You now have a working sandbox for our upcoming lessons.

Up next: PHP Syntax and Browser Output.

Similar Posts