Ubuntu环境下LAMP部署后PhpMyAdmin显示PHP代码而非解析
Hey there, I get why this is frustrating—you’ve done your research, confirmed PHP 7 is installed, but phpMyAdmin is spitting out raw PHP code instead of rendering the interface. Let’s break down what’s going on and fix this together.
The core issue here is that Apache isn’t configured to process PHP files with your PHP 7 installation. Even if PHP is installed on your system, Apache needs explicit instructions to pass .php files to the PHP interpreter instead of just sending the raw code straight to your browser.
Step-by-Step Solutions
Step 1: Enable the Correct PHP 7 Module for Apache
First, you need to make sure Apache is using the PHP 7 module matching your installed version.
Check your exact PHP 7 version with this command:
php -vYou’ll see output like
PHP 7.4.33-1+ubuntu20.04.1+deb.sury.org+1—note the version number (e.g.,7.4).Enable the corresponding Apache module:
sudo a2enmod php7.xReplace
xwith your specific version (e.g.,sudo a2enmod php7.4).Restart Apache to apply the change:
sudo systemctl restart apache2
Step 2: Install the Apache PHP Module (If Missing)
If enabling the module throws an error, you probably don’t have the Apache-PHP integration package installed. Fix that with:
sudo apt install libapache2-mod-php7.x
Again, replace x with your PHP version. Then repeat the enable and restart steps from Step 1.
Step 3: Verify Apache’s PHP Handling Rules
Ensure Apache is set to process .php files correctly. Open the PHP module config file for your version:
sudo nano /etc/apache2/mods-available/php7.x.conf
Look for this section—if it’s missing, add it:
<FilesMatch ".+\.ph(ar|p|tml)$"> SetHandler application/x-httpd-php </FilesMatch> <FilesMatch ".+\.phps$"> SetHandler application/x-httpd-php-source Require all denied </FilesMatch> <FilesMatch "^\.ph(ar|p|ps|tml)$"> Require all denied </FilesMatch>
Save the file (Ctrl+O, press Enter) and exit (Ctrl+X), then restart Apache again.
Step 4: Enable phpMyAdmin’s Apache Configuration
Sometimes phpMyAdmin’s Apache config isn’t enabled by default. Fix that with:
sudo a2enconf phpmyadmin sudo systemctl restart apache2
A Quick Note on Old PHP 5 Posts
You can ignore those 2016 threads about PHP 5—they’re totally irrelevant for your PHP 7 setup. The problem here has nothing to do with missing PHP 5; it’s all about getting Apache and PHP 7 talking to each other properly.
Let me know if any of these steps fix your issue, or if you run into specific errors while executing them!
内容的提问来源于stack exchange,提问作者Voodoobear

