You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

EC2实例Apache2.4无法运行PHP7.1文件的问题求助

Fixing PHP 7.1 Source Code Display on Amazon Linux with Apache (httpd)

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.php file in Apache's default document root:
    echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/phpinfo.php
    
  • Visit http://your-ec2-public-ip/phpinfo.php in 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:31:02