EC2实例Apache2.4无法运行PHP7.1文件的问题求助
Hey there, let's get this sorted out—your issue boils down to using Debian/Ubuntu-style package names on Amazon Linux (which is RHEL/CentOS-based) and missing the proper Apache-PHP integration module. Here's how to fix it step by step:
1. Understand the Package Name Mismatch
The command sudo yum install libapache2-mod-php7.1 fails because that's a package naming convention for Debian/Ubuntu systems. On Amazon Linux (and RHEL/CentOS), Apache is packaged as httpd, and the PHP module for Apache uses a totally different naming structure.
2. Install the Correct PHP 7.1 & Apache Module
Amazon Linux 2 includes PHP 7.1 in its extras repository. Run these commands to set it up properly:
- First, enable the PHP 7.1 repository:
sudo amazon-linux-extras enable php7.1 - Clear outdated metadata to avoid installation conflicts:
sudo yum clean metadata - Install PHP 7.1, common extensions, and the Apache PHP module:
sudo yum install php71 php71-common php71-cli mod_php71
3. Restart Apache to Load the PHP Module
Once the packages are installed, restart the Apache (httpd) service to activate the PHP module:
sudo systemctl restart httpd
4. Verify the Fix
Create a test PHP file to confirm everything is working:
- Generate a
phpinfo.phpfile in Apache's default document root:echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/phpinfo.php - Visit
http://your-ec2-public-ip/phpinfo.phpin your browser. You should see the detailed PHP info page (not raw source code) if the setup was successful.
5. Double-Check Apache Configuration (If Needed)
In most cases, installing mod_php71 automatically adds the rules needed to process .php files. If you still see source code, verify that /etc/httpd/conf.d/php.conf includes these lines:
AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps
If they're missing, add them manually, then restart httpd again.
内容的提问来源于stack exchange,提问作者Volatil3

