How to Install PHP for Windows

Before writing the first line of code in PHP language, we need to install AMP (Apache, MySQL, PHP/Perl/Python) software stack and a visual studio code editor.

There are several AMP stack options available in the market to set up a local development environment. Some of the most popular ones include:

  • XAMPP
    • Available for Windows, Mac OS, and Linux.
    • Includes Apache, MySQL/MariaDB, PHP, and Perl.
  • MAMP
    • Designed for Mac OS and Windows.
    • Includes Apache, MySQL, and PHP.
  • WAMP
    • Specifically for Windows.
    • Includes Apache, MySQL, and PHP.
  • LAMP
    • Typically used for Linux.
    • Includes Apache, MySQL, and PHP.
    • You can set up manually or using package managers.
  • LAPP
    • Similar to LAMP but uses PostgreSQL instead of MySQL.

A PHP IDE (Integrated Development Environment) is a program that allows us to easily write the PHP code. Here are some popular code editors and Integrated Development Environments (IDEs) for PHP development:

  • Visual Studio Code
    • A free and open-source code editor developed by Microsoft.
    • Features include syntax highlighting, IntelliSense, debugging, and a vast library of extensions, including PHP support.
  • PHPStorm
    • A commercial IDE from JetBrains, specifically designed for PHP development.
    • Provides advanced features like code navigation, refactoring, debugging, and integration with version control systems.
  • Sublime Text
    • A lightweight, fast, and highly customizable text editor.
    • Supports various plugins and packages to enhance PHP development.
  • NetBeans:
    • A free, open-source IDE that supports PHP, Java, and other languages.
    • Provides features like syntax highlighting, code templates, debugging, and project management.
  • Atom:
    • An open-source text editor by GitHub.
    • Highly customizable with a large community of developers creating packages and themes.
  • Eclipse PDT (PHP Development Tools):
    • A free and open-source IDE customized for PHP.
    • Offers features like syntax highlighting, code completion, and integration with other Eclipse plugins.

As you can see that there are so many options for AMP stack and code editors. Each of these options provides a convenient way to set up a local server environment for PHP. The best choice depends on your operating system and specific needs. But we will suggest for beginners to go with XAMPP. why? Let’s understand it.

What is XAMPP?


XAMPP is a popular and open-source web server that is freely available for Windows, Mac, and Linux. It includes Apache (web server), MySQL database engine, PHP, and Perl as well as some other useful components such as FileZilla, OpenSSL, Webalizer, Mercury Mail, etc.

Therefore, it is the best choice for everyone, including beginners. The acronym XAMPP stands for

  • X – Cross platform operating system, meaning that it can run on any operating system such as Windows, Mac OS, and Linux.
  • A – Apache which is a web server software.
  • M – MySQL (Database)
  • P – PHP
  • P – Perl (Scripting language)

Why Use XAMPP?


In order to use PHP, you will need to install PHP, Apache web server, and may be even MySQL database. Although, it is not easy for beginners to install Apache, and configure it.

If you go to install Apache on your computer system, you will still have to set it up after installation and integrate it with PHP. XAMPP makes these complex tasks easy for beginners. It deals with all the complexity of setting up and integrating with PHP and Perl. The Apache Friends organization created it to help promote the use of the Apache web server in web development environments.

XAMPP offers an easy to use control panel to manage Apache, MySQL database and other programs such as FileZilla FTP server, Mercury mail server, and Tomcat. However, it is your choice to use or not. But I will recommend using it, especially for beginners.

How to Download and Install XAMPP Server for PHP?


In this section, we will learn how to download and install the XAMPP server on Windows platform step by step. Follow the below steps and install the XAMPP server for PHP on your computer system. For other operating systems, installation steps process are similar.

Step 1: Download XAMPP Server

(1) Visit the XAMPP website:

(2) Choose your platform:

  • On the homepage, you’ll see download links for Windows, Linux, and macOS. Click the appropriate download button according to your operating system.

(3) Select Version:

  • On clicking, it will be taken you to a download page where there are several versions available. It is recommended that you should download the latest version unless you need a specific older version for compatibility reasons.


(4) Download XAMPP Installer:

  • Click the download button, and the installer file will start downloading on your computer system. Save it to a location you can easily access.

Download XAMPP installer.

Step 2: Install XAMPP Installer

(1) Run the Installer:

  • Navigate to the location where you downloaded the XAMPP installer file on your system.
  • Double-click the installer file to start the installation process. You might see a security warning message dialog window or User Account Control prompt. Click “Yes” or “Run” to continue.

(2) Welcome Wizard:

  • The start screen of the XAMPP setup wizard will appear automatically. Click on “Next” button to proceed.

Start up screen of XAMPP setup wizard.

(3) Select Software Components:

Now you’ll see a list of software components you can install. By default, Apache, MySQL, PHP, and some other essential components are selected. If you’re not sure what you need, it is best to leave the default selections. Click on the “Next” button.

Dialog window for selecting software components.


(4) Choose Installation Folder:

In the next step, you have to choose a folder in your system where you would like to install XAMPP software packet. If you select the default standard setup, which is usually fine for beginners, a folder with the name XAMPP will be created under the drive C:\ for you. After you’ve chosen a location, click on the ‘Next’ button.

Dialog window to choose the installation folder.

Once you click the “Next” button in the XAMPP installation wizard, the setup process will begin. The setup wizard will unpack the selected components and install them into the designated folder (e.g., C:\xampp). You can monitor the progress of the installation by watching the green loading bar in the middle of the screen.

Start of XAMPP installation process.

This indicates how much of the installation process has been completed. The entire process may take several minutes, depending on your system’s speed and the number of components being installed. Once the progress bar reaches the end, the installation will be complete, and you will be prompted to finish the setup.

During the installation of the XAMPP server, your firewall might prompt you with a message about blocking some components, especially the Apache server. Here’s what you should do:

  • You may see a notification from your firewall asking if you want to allow Apache HTTP Server to communicate through the firewall.
  • Tick the checkboxes to allow Apache to communicate with private or work networks. This is necessary for the server to function correctly on your local network.
  • Generally, it is not recommended to make your XAMPP server available on public networks, as this could expose your system to security risks.

(5) Bitnami for XAMPP:

You may see a dialog window offering to install Bitnami for XAMPP, which helps to install WordPress, Joomla, and other CMS applications. You can uncheck this box if you’re not interested. Click “Next.”

Bitnami for XAMPP.

(6) Ready to Install:

The wizard is ready to install the XAMPP on your computer system. Click “Next” button to start the installation process of XAMPP.

XAMPP is ready to install.

(7) Installation Process:

The installer will extract files and set up XAMPP. This may take a few minutes.

(8) Finish Installation:

Once the installation of all components is complete, you will see a completion dialog window. Check the box to launch the XAMPP Control Panel if you want to start it immediately, and click “Finish” button.

XAMPP installation completed.

(9) Preferred Language:

Choose your preferred language.

Choose the preferred language.

Step 3: Start Using XAMPP

(1) Open XAMPP Control Panel:

XAMPP is ready to use. If you didn’t choose to launch the Control Panel during installation, you can open it manually. Go to the XAMPP installation directory or folder (i.e. C:\xampp) and double-click on xampp-control.exe.

(2) Start Apache and MySQL:

The control panel is used to manage programs installed though XAMPP. With the control panel, you can do the following things:

  • Install and uninstall services, such as Apache, MySQL, etc. that are installed by XAMPP software.
  • Start and stop services.
  • Open configure files for Apache, MySQL etc. to make changes.
  • View log files for the modules.

In the XAMPP Control Panel, you’ll see a list of installed services, modules and the process IDs PID(s). Click “Start” button next to Apache and MySQL to start the web server and database server.

XAMPP control panel.

If no error appears, then the XAMPP server is running successfully on your computer system.

XAMPP server is running successfully on your computer system.

A green mark indicates that the module has started as a service. A red mark indicates that it has not been started as a service. To install the service, click on the red mark.

(3) Verify Installation:

Open your favorite web browser and type URL http://localhost/ into the address bar. If XAMPP is installed correctly, you should see the XAMPP welcome page.

Basic Web Server Configuration


We assume that you have successfully installed XAMPP on drive C in Windows using the above steps remarked. The following is the list of the basic directories or folders that you are supposed to be aware of:

List of basic XAMPP web server configuration.

What is ‘htdocs’?

The ‘htdocs’ is a root directory or main folder in the XAMPP where all of your PHP code will be placed in this directory. It is a default root directory where the server looks for files to serve. This means that any file placed in this directory will be accessed through a web browser.

The htdocs folder is the main place where you will develop and test your web applications when using XAMPP. When you install XAMPP, the htdocs folder is created in the XAMPP installation directory. For example, C:\xampp\htdocs on Windows or /Applications/XAMPP/htdocs on Mac OS.

How htdocs Works?

(1) Accessing Files:

Files and folders you place inside htdocs are easily accessible through a web browser using http://localhost/. For example, if you have a file named index.php in htdocs, you can access it by typing this URL http://localhost/index.php on the browser’s address bar.

(2) Organizing Projects:

You can create subdirectories within htdocs for your different projects to avoid file conflicts and maintain a clean working environment. For example, if you have a project called mywebsite, you can create a folder mywebsite inside htdocs and access it via http://localhost/mywebsite/.

(3) Developing PHP Applications:

Since XAMPP includes PHP, you can place your PHP files in htdocs to execute and test them using the Apache server.

(4) Static and Dynamic Files:

You can create both static files (HTML, CSS, JavaScript) and dynamic files (PHP) from this directory.

MySQL Directory:

This directory contains all the information related to the MySQL database engine. By default, it runs on the port 3306. MySQL uses a structured format to store data in tables that are related to each other. It supports structured query language (SQL) to manage and manipulate data.

MySQL generates various logs, such as error logs, general query logs, and binary logs, which are stored in this directory. These logs are essential for troubleshooting and monitoring server activities.

PHP Directory:

The “PHP directory” usually refers to the directory where the PHP installation files are located. This directory contains the PHP executable, configuration files, and extensions necessary to run PHP scripts. In this directory, you will get an important file named php.ini which controls PHP behavior, such as error reporting, upload limits, and extension loading.

By default, the Apache web server runs on the main port 80 and the SSL port 443. If another web server has already taken the port 80, you can use a different port number. We assume that you are using the port 80.

Managing Databases for Web Projects


To manage databases for your web projects, you can use phpMyAdmin. It is a free, open-source web-based tool written in PHP, which is designed to handle the MySQL and MariaDB databases. It provides a user-friendly graphical interface for managing databases. This interface makes it easier for users to perform database operations with no need to use the command line.

To access phpMyAdmin, open the XAMPP control panel and click on the “Admin” button next to the MySQL module. This will open phpMyAdmin in your default web browser, allowing you to manage your databases of your web projects that you’re testing on your XAMPP.

Alternatively, you can also access phpMyAdmin for databases by entering http://localhost/phpmyadmin/ in your web browser. This will take you to the phpMyAdmin login page, where you can log in and manage your MySQL databases. Use your MySQL username and password to log in. Typically, the root is the default username for local installations.

Download and Install Visual Studio Code (VS Code)


Here’s a step-by-step guide on how to download and install Visual Studio Code (VS Code) for PHP development:

  • Open your web browser and go to the official Visual Studio Code website.
  • On the homepage, you’ll see download options for Windows, Mac OS, and Linux. Click the appropriate download button for your operating system.
  • Save the installer file to a location where you can easily access on your computer system.
  • Navigate to the location where you downloaded the VS Code installer.
  • Double-click the installer file to start the installation process. You might see a security warning; click “Yes” or “Run” to continue.
  • Read and accept the license agreement, then click “Next” button.
  • Select the folder where you want to install VS Code. The default location is usually fine. Click on the “Next” button.
  • Choose additional tasks like creating a desktop icon, adding to PATH, and more. It’s recommended to check the options to add to  PATH for easier command line access. Click on the “Next” button.
  • Click on the “Install” button to start the installation process. This may take a few minutes.
  • Once the installation is complete, click on the “Finish” button choose to launch VS Code immediately.

Install Extensions In VS Code


Now you install two extensions in your VS code editor. They are:

  • All-in-One PHP support
  • Live Server
  • Auto Rename Tag
  • Code Runner
  • Code Spell Checker
  • Color Highlight

For this, click on the extension icon and type All-in-one PHP support and install it. After installing, type Live server and install it.

Steps to Configure the Correct PHP Executable Path in VS Code


There are the following steps through which we can configure the correct PHP executable path in VS Code. They are:

1. Find the Correct PHP Executable Path:

  • If you’re using XAMPP, the default path to the PHP executable is usually something like D:\xampp\php\php.exe.
  • If you have installed XAMPP on a different drive, change the drive letter with your drive letter. I have installed XAMPP in the D drive. Therefore, the default path is D:\xampp\php\php.exe.

2. Add PHP to Your PATH

  • Open the Start menu, search for “Environment Variables,” and select “Edit the system environment variables.”
  • In the System Properties window, click on the “Environment Variables” button.
  • In the Environment Variables window, find the “Path” variable under “System variables” and click “Edit.”
  • Click “New” and add the path “D:\xampp\php” to the PHP directory. Do not use the double quotes.
  • Click “OK” to close all dialog boxes and save the changes.

3. Configure VS Code

  • Open VS Code.
  • Press ctrl+, to open setting.

4. Set the PHP Executable Path

  • Add or modify the php.validate.executablePath setting in the settings.json file. For example:
{
"php.validate.executablePath": "D:\\xampp\\php\\php.exe",
"php.debug.executablePath": "D:\\xampp\\php\\php.exe",
"php.executables": ["D:\\xampp\\php\\php.exe"]
}
  • Note that you need to use double backslashes (\\) on Windows to escape the backslash character.

5. Save and Restart:

  • Save the settings.json file.
  • Restart VS Code to ensure the changes take effect.

6. Verify the Setup

  • Open a PHP file in VS Code and check if the error message is resolved.
  • Ensure that the path specified in your settings matches the location of your php.exe.

In this tutorial, you have learned how to download and install PHP for Windows. You also learned how to install XAMPP server and configure it with visual studio code. I hope that you will have successfully installed XAMPP for PHP and configured it VS code.