Apache24本地服务器PHP脚本未执行且无法连接SQL服务器求助
Hey there, let's work through your two core problems step by step—these are super common hurdles when setting up a local web stack, so we'll get you up and running in no time.
1. PHP Code Displays as Plain Text Instead of Executing
This happens when Apache isn't configured to recognize and process PHP files, so it just serves them like regular text files. Here's how to fix it:
- Confirm your file extensions: Make sure the script your form submits to has a
.phpsuffix (e.g.,submit.php), not.html. Your form'sactionattribute should point directly to this file, like<form action="submit.php" method="POST">. - Check Apache loads the PHP module: Open your Apache
httpd.conffile (usually located inApache24/conf). Look for a line starting withLoadModule php[version]_module—for PHP 8.x, it might look like:
If this line is commented out (starts withLoadModule php8_module "C:/php/php8apache2_4.dll"#), remove the#to enable it. Then add this line right below to tell Apache to handle.phpfiles:AddHandler application/x-httpd-php .php - Restart Apache: After editing
httpd.conf, runhttpd -k restartin your Apache directory to apply changes. - Test PHP functionality: Create a simple
info.phpfile with:
Access it via your browser (e.g.,<?php phpinfo(); ?>http://localhost/info.php). If you see a detailed PHP info page, PHP is working—if not, double-check your module loading and handler setup.
2. Apache Port 0 Error When Running httpd -k start
A port 0 error means Apache can't bind to a valid network port, usually due to misconfiguration or permission issues. Try these fixes:
- Verify the
Listendirective: Openhttpd.confand look for a line likeListen 80. If it's set toListen 0or missing entirely, that's the problem. Change it to a valid port (80 is standard, but use 8080 or another free port if 80 is occupied):Listen 80 # Alternative if port 80 is taken: Listen 8080 - Check for port conflicts: On Windows, run
netstat -ano | findstr :80in CMD to see which process is using port 80. Note the PID at the end, open Task Manager, find that PID, and end the process. On Linux/macOS, uselsof -i :80to locate the conflicting process. - Validate your Apache config: Run
httpd -tin your Apache directory to check for syntax errors inhttpd.conf(like typos in file paths or missing brackets). Fix any errors it reports before restarting. - Run CMD as administrator: Sometimes Apache can't bind to ports due to insufficient permissions. Right-click CMD, select "Run as administrator", navigate to your Apache directory, and run
httpd -k startagain.
Once both issues are resolved, your form should submit to the PHP script, which can then connect to your phpMyAdmin-managed MySQL database to store data. For the database connection, make sure your PHP script uses mysqli or PDO with the correct credentials (localhost, your MySQL username/password, and target database name).
内容的提问来源于stack exchange,提问作者Reginald1234

