PHP Files Download Instead of Running in XAMPP – Fix for Windows, macOS & Linux

PHP Files Download Instead of Running in XAMPP

PHP file downloading instead of executing in XAMPP? Learn how to configure Apache and fix MIME types on Windows, macOS, and Linux/Ubuntu systems.


🧩 Introduction

Ever clicked on a .php file in your browser only for it to download instead of executing? This is a very common issue in XAMPP and typically happens because Apache isn’t configured to handle PHP properly.

In this blog, we’ll fix the issue step-by-step for Windows, macOS, and Ubuntu/Linux users.


❗ Why This Happens

  • PHP module isn’t loaded by Apache
  • MIME type for .php not handled
  • Wrong httpd.conf or php.ini configuration
  • PHP isn’t installed or linked correctly (on Linux/macOS)

🖥️ Fix on Windows

✅ Step-by-Step:

  1. Open httpd.conf:
    • XAMPP Control Panel > Apache > Config > httpd.conf
  2. Enable PHP Module:
    • Find: #LoadModule php_module "c:/xampp/php/php8apache2_4.dll"
    • Remove the #: LoadModule php_module "c:/xampp/php/php8apache2_4.dll"
  3. Add PHP Handler:
    • At the bottom of the file, add: AddHandler application/x-httpd-php .php PHPIniDir "C:/xampp/php"
  4. Save and restart Apache.
  5. Verify:
    • Create a test.php file in htdocs: <?php phpinfo(); ?>
    • Visit:
      http://localhost/test.php

🍏 Fix on macOS

✅ Step-by-Step:

  1. Edit Apache config: sudo nano /Applications/XAMPP/xamppfiles/etc/httpd.conf
  2. Enable PHP Module:
    Find and uncomment: LoadModule php_module modules/libphp.so
  3. Add PHP Handler:
    Add this if missing: AddHandler application/x-httpd-php .php
  4. Set PHP config path:
    Add: PHPIniDir "/Applications/XAMPP/xamppfiles/etc/"
  5. Restart Apache: sudo /Applications/XAMPP/xamppfiles/xampp restartapache
  6. Test it:
    • Add test.php in /Applications/XAMPP/htdocs/
    • Visit: http://localhost/test.php

🐧 Fix on Linux/Ubuntu

✅ Step-by-Step:

  1. Edit Apache config: sudo nano /opt/lampp/etc/httpd.conf
  2. Uncomment PHP Module:
    Find and ensure: LoadModule php_module modules/libphp.so
  3. Add Handler if missing: AddHandler application/x-httpd-php .php PHPIniDir "/opt/lampp/etc"
  4. Restart Apache: sudo /opt/lampp/lampp restartapache
  5. Verify:
    • Add test.php in /opt/lampp/htdocs/: <?php phpinfo(); ?>
    • Open: http://localhost/test.php

✅ Bonus Check (All Platforms)

If you’re using .htaccess, ensure it doesn’t override PHP handling with:

RemoveHandler .php
AddType application/x-httpd-php .php

Also verify your browser isn’t caching an old MIME type. Try opening the file in incognito/private window.


📌 Conclusion

The issue where PHP files download instead of run is always tied to a missing or misconfigured PHP handler in Apache. Whether you’re using Windows, macOS, or Ubuntu, follow the steps above to get your server running properly.


💬 Need Help?

Post your httpd.conf snippet or system info in the comments—I’ll guide you further!


Leave a Reply

Your email address will not be published. Required fields are marked *