How to Run PHP Files Locally Using XAMPP: Step-by-Step Tutorial for Beginners

Running PHP files on your local computer is one of the first practical skills every web developer needs to acquire. Without a local server environment, PHP code simply cannot execute because PHP is a server-side language that requires a web server to process it before sending the result to a browser. When you open a raw PHP file directly in a browser, all you see is the unprocessed code rather than the intended output. XAMPP solves this problem by providing a complete local server environment that includes Apache, MySQL, and PHP all bundled into a single installation package that works on Windows, macOS, and Linux. It is free, open source, and widely regarded as the most beginner-friendly way to get a local PHP development environment running quickly.

The name XAMPP is an acronym that breaks down as follows: X stands for cross-platform, A stands for Apache, M stands for MariaDB, and the two P letters stand for PHP and Perl. Together these components give developers everything they need to build, test, and debug PHP-powered websites on their own machine without needing an internet connection or a paid hosting plan. This tutorial walks through every step of the process from downloading and installing XAMPP all the way through writing your first PHP file and viewing it in a browser, with clear explanations designed specifically for those who are new to local development environments.

Reasons to Use XAMPP Locally

Before getting into the installation steps, it is worth understanding why XAMPP is the tool of choice for so many beginners and experienced developers alike. The most important reason is that it replicates a real web server environment on your local machine, meaning the behavior you observe during local development closely matches what will happen when your code runs on a live hosting server. This consistency reduces the risk of encountering unexpected problems when you eventually deploy your project to the web. Other local development approaches that simply open HTML files in a browser do not provide this server-like behavior for PHP code.

XAMPP also bundles phpMyAdmin, a web-based graphical interface for managing MySQL and MariaDB databases. This is enormously useful for PHP projects that interact with databases, as it allows you to create tables, insert data, run queries, and inspect database contents without writing raw SQL commands from the terminal. The all-in-one nature of XAMPP means that installing it gives you a complete development stack in a single step, rather than requiring you to separately install and configure Apache, PHP, and a database server. For beginners, this simplicity is a major advantage that removes a significant barrier to getting started with PHP development.

System Requirements Before Installation

XAMPP is a lightweight application that runs comfortably on most modern computers, but there are a few system requirements worth confirming before you begin the download. On Windows, XAMPP requires Windows 7 or later and works on both 32-bit and 64-bit systems. On macOS, the supported versions depend on the XAMPP release you choose, and it is generally advisable to download the latest stable version to ensure compatibility with current macOS releases. On Linux, XAMPP supports a wide range of distributions including Ubuntu, Debian, and Fedora. In all cases, you will need at least 512 megabytes of RAM and approximately 1.5 gigabytes of free disk space for the base installation.

It is also important to check whether any other application on your computer is already using port 80 or port 443, as these are the default ports that Apache uses to serve web pages. Conflicts on these ports are one of the most common causes of Apache failing to start in XAMPP. On Windows, Skype and Internet Information Services are two common applications that may already be occupying port 80. On macOS, the built-in Apache service can sometimes conflict as well. If you know that these applications are running on your system, you will either need to stop them before starting XAMPP or reconfigure XAMPP to use a different port. The XAMPP control panel makes port conflict diagnosis straightforward once installation is complete.

Downloading XAMPP Installation Package

The official source for XAMPP downloads is the Apache Friends website, located at apachefriends.org. Navigating to this site presents you with download buttons for Windows, Linux, and macOS versions of XAMPP. You should always download from this official source rather than from third-party download aggregators, as unofficial sources may bundle unwanted software or provide outdated versions. The Apache Friends website lists several XAMPP versions corresponding to different PHP versions, and for most beginners the recommended approach is to download the latest stable version unless your project has a specific PHP version requirement.

On Windows, the download is an executable installer file with a .exe extension that is typically around 150 to 160 megabytes in size. On macOS, the download is a .dmg disk image file, and on Linux it is a .run installer script. The download size may seem modest, but the installed XAMPP environment expands considerably because it includes Apache, MariaDB, PHP, Perl, phpMyAdmin, and several other bundled tools and libraries. Before downloading, take note of the PHP version included in your chosen XAMPP release, as this can be important when working with PHP frameworks or libraries that have specific minimum PHP version requirements.

Installing XAMPP on Windows Systems

Once the Windows installer has finished downloading, locate the .exe file in your downloads folder and right-click it to run it as an administrator. Running the installer with administrator privileges helps avoid permission issues that can sometimes interfere with the installation process. When the installer launches, you may see a warning from Windows User Account Control asking whether you want to allow the application to make changes to your device. Click yes to proceed. If your system has an active antivirus program, you may also see a warning that the antivirus could affect the installation. It is generally safe to continue past this warning, though you may want to temporarily disable real-time scanning if you encounter installation errors.

The XAMPP setup wizard presents a component selection screen where you can choose which elements to install. For PHP development, you need Apache, MySQL, PHP, and phpMyAdmin at a minimum. The other components such as Perl, FileZilla FTP server, and Mercury mail server are optional and can be deselected if you want a leaner installation. After selecting components, the wizard asks you to choose an installation directory. The default installation path is C:\xampp on Windows, and it is strongly recommended to accept this default rather than choosing a path with spaces in the folder names, as spaces in the XAMPP installation path can cause configuration issues with some PHP applications. Complete the installation by clicking through the remaining wizard screens and waiting for the file extraction to finish.

Installing XAMPP on macOS Systems

Installing XAMPP on macOS begins by opening the downloaded .dmg file from your downloads folder. This mounts a virtual disk that contains the XAMPP installer application. Double-click the installer application to launch it, and if macOS displays a security warning because the application was downloaded from the internet, navigate to System Preferences, then Security and Privacy, and click the option to open the application anyway. The XAMPP installer on macOS uses the same wizard-based interface as the Windows version, presenting component selection options and asking for confirmation before beginning the installation.

On macOS, XAMPP installs into the /Applications/XAMPP directory by default. Unlike the Windows installation, the macOS version does not require administrator-level installation adjustments related to UAC, but you will need to provide your macOS user password when prompted during the installation to authorize the creation of files in the applications directory. After the installation completes, XAMPP places a manager application in your Applications folder called XAMPP that serves the same purpose as the Windows control panel, allowing you to start and stop the Apache and MySQL services. If you are running a recent version of macOS with stricter security restrictions, you may need to grant additional permissions to XAMPP components through the Privacy and Security settings.

Launching the XAMPP Control Panel

On Windows, the XAMPP control panel can be launched from the Start menu by searching for XAMPP or by navigating to the C:\xampp folder and running the xampp-control.exe file directly. It is convenient to right-click this executable and create a desktop shortcut so that you can start the control panel without navigating through folders every time. The control panel displays a list of modules including Apache, MySQL, FileZilla, Mercury, and Tomcat, each with start and stop buttons alongside status indicators that show whether each service is currently running. For PHP development, the two modules you need to start are Apache and MySQL.

On macOS, the XAMPP manager application in your Applications folder provides a graphical interface that is functionally equivalent to the Windows control panel. The manager has three tabs: Welcome, Manage Servers, and Welcome, with the Manage Servers tab being the most important as it lists the Apache Web Server, MySQL Database, and ProFTPD services along with start, stop, and restart controls for each. To start running your local PHP environment on either operating system, click the start button next to Apache. If Apache starts successfully, its status indicator turns green and you should be able to open a browser and navigate to localhost to see the XAMPP welcome page, which confirms that your local server is running correctly.

Resolving Common Port Conflict Issues

Port conflicts are the most frequent obstacle beginners encounter when first starting Apache in XAMPP, and knowing how to diagnose and resolve them saves considerable frustration. The clearest sign of a port conflict is that Apache fails to start and its status indicator in the control panel remains red or orange after you click the start button. On Windows, clicking the Logs button next to the Apache module in the control panel and reviewing the error log often reveals a message indicating that the port is already in use. The Windows control panel also includes a Netstat button that lists all currently active network connections and the processes using each port, which can help you identify what is occupying port 80.

The simplest resolution for a port conflict is to change the port that Apache uses to serve web content. This is done by editing the Apache configuration file called httpd.conf, which is located at C:\xampp\apache\conf\httpd.conf on Windows. Open this file in a text editor and find the line that reads Listen 80, then change 80 to an unused port number such as 8080. You must also find and update the ServerName directive to reflect the new port. After saving the file and restarting Apache from the control panel, you access your local server by typing localhost:8080 in the browser instead of simply localhost. This approach resolves the vast majority of port conflict situations without requiring changes to any other software on your system.

Locating the htdocs Root Folder

The htdocs folder is the document root of your local Apache web server, which means it is the folder where you must place your PHP files for Apache to be able to serve them through the browser. Files placed anywhere outside of this folder cannot be accessed through the browser using the localhost address. On Windows with a default XAMPP installation, the htdocs folder is located at C:\xampp\htdocs. On macOS, it is at /Applications/XAMPP/htdocs. Understanding the location and purpose of this folder is fundamental to working with XAMPP, and every PHP project you create during local development will live inside this directory.

When you first navigate to the htdocs folder, you will find that it already contains several files and folders placed there by the XAMPP installer, including the index.php file that generates the XAMPP welcome page you see when you visit localhost in the browser. You can leave these files in place as they do not interfere with your own projects. The recommended approach for organizing your work is to create a new subfolder inside htdocs for each PHP project you work on. For example, creating a folder named myproject inside htdocs and placing your PHP files inside it means you can access those files by navigating to localhost/myproject in the browser. This folder-based organization keeps your projects separate and makes it easy to manage multiple projects simultaneously.

Writing Your First PHP File

With XAMPP installed and Apache running, you are ready to write and run your first PHP file. Open a plain text editor such as Notepad on Windows or TextEdit on macOS, though using a dedicated code editor such as Visual Studio Code, Sublime Text, or Notepad++ is strongly recommended even for beginners because these editors provide syntax highlighting that makes PHP code easier to read and debug. In your editor, create a new file and type the following content exactly as shown: start with the opening PHP tag which is written as a less-than sign followed by a question mark and the letters php, then on the next line write echo “Hello, World!”; and on the final line close with a question mark followed by a greater-than sign.

Save this file with the name index.php inside a new folder called myfirstproject that you create inside the htdocs directory. The .php file extension is essential because it tells Apache that this file contains PHP code that needs to be processed by the PHP interpreter before the output is sent to the browser. Files saved with a .txt or .html extension will not be processed as PHP even if they contain valid PHP code. Once the file is saved in the correct location, open your browser and type localhost/myfirstproject in the address bar and press enter. If everything is configured correctly, the browser displays the text Hello, World! which is the output of your PHP echo statement, confirming that your local PHP development environment is fully functional.

Testing PHP Configuration Details

After successfully running your first PHP file, it is useful to verify the details of your PHP configuration using a built-in PHP function called phpinfo. This function outputs a detailed page containing information about the PHP version installed, all enabled extensions, configuration values, environment variables, and server information. To use it, create a new PHP file in your project folder, give it a name such as info.php, and write a PHP block containing only phpinfo followed by a pair of parentheses and a semicolon. Save the file and access it through the browser using the localhost address for your project folder.

The phpinfo output page is an invaluable reference when you need to confirm whether a specific PHP extension is enabled, check the location of your php.ini configuration file, or verify that PHP is reading the configuration values you expect. For example, if you are working with a PHP framework that requires specific extensions such as PDO for database connectivity or GD for image processing, the phpinfo page shows at a glance whether these extensions are active. One important security note is that the phpinfo function reveals detailed information about your server environment, so you should never leave an info.php file on a publicly accessible web server. For local development purposes this is completely safe, but the file should be deleted or removed from any project before it is deployed to a live hosting environment.

Connecting PHP Files to MySQL

One of the most powerful aspects of the XAMPP environment is that it includes a fully functional MySQL database server alongside Apache and PHP, enabling you to build PHP applications that store and retrieve data from a database on your local machine. Before connecting PHP to MySQL, you first need to create a database using phpMyAdmin, the browser-based database management tool included with XAMPP. Access phpMyAdmin by navigating to localhost/phpmyadmin in your browser while Apache and MySQL are both running. The phpMyAdmin interface allows you to create a new database by clicking the New option in the left sidebar, entering a database name, and clicking Create.

Once a database exists, you can connect to it from PHP using the MySQLi extension or the PDO extension, both of which are included and enabled by default in XAMPP. A basic database connection using MySQLi requires four pieces of information: the hostname which is localhost for a local XAMPP database, the username which defaults to root in XAMPP, the password which is empty by default in a fresh XAMPP installation, and the database name you created in phpMyAdmin. Writing a PHP file that establishes this connection and checks for errors is a good early exercise that verifies your database configuration is correct and prepares you for building PHP applications that interact with stored data.

Organizing Multiple PHP Projects

As you become more comfortable with XAMPP and begin working on multiple PHP projects simultaneously, developing a consistent folder organization strategy inside the htdocs directory becomes increasingly important. The simplest approach is to create one top-level folder per project inside htdocs, with each folder named after the project it contains. Each project folder then holds all the PHP files, CSS stylesheets, JavaScript files, and image assets that belong to that project. With this structure, each project is accessed through a distinct URL path such as localhost/projectname, keeping the projects completely separate from one another in both the file system and the browser address bar.

For more complex projects that use a PHP framework such as Laravel or CodeIgniter, additional folder structure conventions may be dictated by the framework itself. Laravel, for example, places the publicly accessible files in a subfolder called public within the project directory, and accessing a Laravel project through XAMPP requires either pointing your browser to localhost/projectname/public or making a configuration change in Apache to set the project’s public folder as the document root for a virtual host. Learning to configure Apache virtual hosts in XAMPP is a useful skill for intermediate PHP developers as it allows each project to be accessed through a clean custom domain name like myproject.local rather than through a subfolder path, more closely simulating a real hosting environment.

Troubleshooting Common PHP Errors

Encountering errors when running PHP files locally is a normal part of the learning process, and XAMPP provides several tools and configuration options that make debugging easier. By default, PHP may be configured to suppress error messages from appearing in the browser, which can make it difficult to understand why a script is not working as expected. To enable visible error reporting during development, open the php.ini file located at C:\xampp\php\php.ini on Windows or /Applications/XAMPP/etc/php.ini on macOS, find the line that reads display_errors and ensure its value is set to On, then restart Apache from the control panel for the change to take effect.

With error display enabled, PHP will show detailed error messages directly in the browser when your code contains syntax errors, references to undefined variables, or calls to functions that do not exist. These error messages include the file name and line number where the problem occurred, making them very useful for pinpointing the source of a bug. Common beginner errors include missing semicolons at the end of PHP statements, mismatched quotation marks in string values, and attempting to use a variable before it has been assigned a value. The Apache error log, accessible through the XAMPP control panel by clicking the Logs button next to the Apache module, provides additional diagnostic information for errors that occur at the server level rather than within the PHP code itself.

Stopping and Restarting XAMPP Services

Knowing how to properly stop XAMPP services is just as important as knowing how to start them. When you are finished with a development session, it is good practice to stop both Apache and MySQL from the control panel rather than simply closing the control panel window or shutting down your computer while the services are still running. Stopping the services gracefully allows Apache and MySQL to close any open connections and write any pending data to disk before shutting down, reducing the small but non-zero risk of database corruption that can occur when a database server is terminated abruptly.

On Windows, closing the XAMPP control panel window does not automatically stop the running services. You must click the Stop button next to each active module before closing the control panel, or use the Quit option in the control panel to stop all services and exit simultaneously. On macOS, the XAMPP manager application provides a Stop All button that halts all running services at once. Restarting individual services is often necessary after making configuration changes to Apache or PHP, as changes to configuration files only take effect when the service reads them again at startup. The Restart button in the control panel handles this cleanly without requiring a full system reboot, making the configuration adjustment and testing cycle efficient during development.

Advancing Beyond Basic Local Development

Once you are comfortable running PHP files through XAMPP and connecting them to a local MySQL database, there are several natural next steps that will broaden your PHP development capabilities. Learning to use a dedicated code editor with PHP debugging integration, such as Visual Studio Code with the PHP Debug extension, allows you to set breakpoints in your PHP code and step through it line by line to observe variable values and execution flow in real time. This capability is significantly more powerful than relying on echo statements for debugging and becomes increasingly valuable as your scripts grow in complexity.

Exploring the configuration options available in the php.ini file is another productive area of advancement. This file controls a wide range of PHP behavior including file upload size limits, maximum execution time for scripts, timezone settings, and which extensions are loaded. Many PHP projects and frameworks have specific php.ini requirements, and knowing how to locate and modify this file in your XAMPP installation prepares you to meet those requirements when they arise. As your skills develop further, you may eventually want to explore alternative local development environments such as Docker-based setups that provide even greater flexibility and closer parity with production server configurations, but XAMPP remains an excellent foundation and a tool that many professional developers continue to use throughout their careers for its simplicity and reliability.

Conclusion

Getting PHP files running locally through XAMPP is one of the most important and enabling steps a new web developer can take. The process of installing XAMPP, starting Apache and MySQL, placing PHP files in the htdocs folder, and accessing them through the browser establishes a development workflow that serves as the foundation for everything that follows in PHP development. Every PHP concept you learn, every database-driven application you build, and every framework you explore can be developed and tested locally in this environment before any code ever reaches a live server.

The skills covered in this tutorial extend well beyond the mechanics of running a single PHP file. Learning to manage your project folder structure within htdocs, troubleshoot Apache port conflicts, enable PHP error reporting, connect to a local MySQL database through phpMyAdmin, and stop and restart services cleanly are all practical skills that contribute to a professional development workflow. These habits, established early in the learning process, save time and prevent frustration as projects grow in complexity and as the consequences of configuration mistakes become more significant.

XAMPP has maintained its position as the preferred local development environment for PHP beginners for well over a decade, and that longevity reflects genuine usefulness rather than mere inertia. Its combination of simplicity, completeness, and cross-platform availability makes it the right choice for anyone starting their PHP development journey, regardless of whether their ultimate goal is to build simple personal websites, data-driven web applications, or enterprise-grade systems using modern PHP frameworks. The investment of time required to get comfortable with XAMPP pays returns immediately and continues to deliver value throughout a developer’s career.

As you move forward from this foundation, remember that local development is about more than convenience. It is about having a safe, controlled environment where you can experiment freely, make mistakes without consequences, and build confidence before deploying work to the public internet. XAMPP provides exactly that environment, and the developers who use it most effectively are those who treat their local setup as a genuine professional tool rather than merely a temporary stepping stone. Take the time to organize your projects thoughtfully, keep your XAMPP installation updated, and continue learning the configuration options that give you greater control over your local environment, and you will find that XAMPP grows with you as your PHP development skills advance.

img