Running PHP on localhost
Configure PHP in local environments like XAMPP, WAMP, and LAMP, and access PHP pages and projects via http://localhost.
PHP is one of the most commonly used server-side languages in web development. With Apache or Nginx, you can quickly run .php files and CMS projects on localhost.
Common Access Methods
| Scenario | Address Example |
|---|---|
| PHP files in the root directory | http://localhost/info.php |
| Subdirectory projects | http://localhost/myapp/index.php |
| Built-in development server | http://localhost:8000 |
Place your PHP files in the web root directory (such as htdocs/ for XAMPP or /var/www/html/ for LAMP), and access the corresponding path in your browser.
Local Environment Choices
- XAMPP / WAMP / MAMP / LAMP: Integrates Apache + PHP, suitable for CMS like WordPress and Drupal.
- PHP Built-in Server: Quick debugging for single files or small projects.
cd /path/to/project
php -S localhost:8000Verifying PHP is Working
Create an info.php file in the web root directory:
<?php phpinfo(); ?>Access http://localhost/info.php; if the PHP configuration page displays, the environment is functioning correctly. It is recommended to delete this file after debugging.
Common Issues
Page is downloaded instead of executed
Apache needs to load the PHP module (e.g., libapache2-mod-php); Nginx needs to configure fastcgi_pass to point to php-fpm.
500 Error or Blank Page
Check the Apache/Nginx and PHP error logs; verify display_errors in php.ini (can be set to On in development environments).
Missing Extensions
CMS often requires extensions like mysqli, pdo_mysql, gd, curl, etc., which should be enabled in the PHP extension management of the respective platform.
Summary
In stacks like XAMPP, PHP projects are typically accessed via http://localhost/projectname/; for standalone debugging, you can use php -S localhost:8000.