【问题标题】:Why doesn't the php script execute when I include it为什么我包含它时php脚本不执行
【发布时间】:2020-11-27 10:39:50
【问题描述】:

我会尽量说清楚: 我正在开发一个使用 amazon EC2、Amazon S3、Amazon RDS 和现在的 Amazon SES 的 Web 应用程序。 我正在尝试在注册时实施电子邮件验证。不幸的是,由于某种原因,php 邮件功能在亚马逊 EC2 实例上不起作用,我尝试重新安装 sendmail 和 postfix 并查看 php.ini 文件无济于事。这导致我关注这个:https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-sdk-php.html

这可以正常工作,但前提是您使用 putty 从通过 ssh 访问的 shell 运行它。

Using username "ec2-user".
Authenticating with public key "imported-openssh-key"
Last login: Fri Aug  7 05:32:51 2020 from 116.90.72.92

       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2018.03-release-notes/
[ec2-user@ip-172-31-24-178 ~]$ sudo su
[root@ip-172-31-24-178 ec2-user]# php amazon-ses-sample.php
Email sent! Message ID: 010f0173c771a9d3-b9d5e167-7b19-4b42-a550-dce52a8b83d5-000000
[root@ip-172-31-24-178 ec2-user]#


我所有的 php 脚本都是从我正在使用的 EC2 实例上运行的 apache (httpd) 服务器运行的。脚本运行良好,但我尝试包含 amazon ses 示例(请参阅下面的代码)它出错并且不执行:


//include 'amazon-ses-sample.php'; <-------**HERE: including this doesn't work.**
?>

此脚本来自 AWS,只有在您使用以下命令时才有效:$ php amazon-ses-sample.php


// If necessary, modify the path in the require statement below to refer to the
// location of your Composer autoload.php file.
 require '/var/www/aws/aws-autoloader.php';

use Aws\Ses\SesClient;
use Aws\Exception\AwsException;

// Create an SesClient. Change the value of the region parameter if you're
// using an AWS Region other than US West (Oregon). Change the value of the
// profile parameter if you want to use a profile in your credentials file
// other than the default.
$SesClient = new SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region'  => 'us-east-2'
]);

// Replace sender@example.com with your "From" address.
// This address must be verified with Amazon SES.
$sender_email = 'ngudy049@gmail.com';

// Replace these sample addresses with the addresses of your recipients. If
// your account is still in the sandbox, these addresses must be verified.
$recipient_emails = ['ngudy049@gmail.com','ngudy049@gmail.com'];

// Specify a configuration set. If you do not want to use a configuration
// set, comment the following variable, and the
// 'ConfigurationSetName' => $configuration_set argument below.
//$configuration_set = 'ConfigSet';

$nameString = "username3";

$subject = 'Amazon web app sign up test';
$plaintext_body = "This email was sent with Amazon SES using the AWS SDK for PHP.".
"Username: ".$nameString." Password: ".$passwordString."Please click this link to activate your account:".
"http://www.yourwebsite.com/verify.php?email=".$emailString."&hash=".$hashString; // Our message above including the link

$html_body =  "<p>Thanks for signing up! Your account has been created, you can login".
" with the following credentials after you have activated your account by pressing the url below.</p>".
"<br>".
"Username: ".$usernameString. "<br>".
"Password: ".$passwordString. "<br>".
"<br>".
"http://3.23.88.212/verify.php?email=".$emailString."&hash=".$hashString; // Our message above including the link;


$char_set = 'UTF-8';

try {
    $result = $SesClient->sendEmail([
        'Destination' => [
            'ToAddresses' => $recipient_emails,
        ],
        'ReplyToAddresses' => [$sender_email],
        'Source' => $sender_email,
        'Message' => [
          'Body' => [
              'Html' => [
                  'Charset' => $char_set,
                  'Data' => $html_body,
              ],
              'Text' => [
                  'Charset' => $char_set,
                  'Data' => $plaintext_body,
              ],
          ],
          'Subject' => [
              'Charset' => $char_set,
              'Data' => $subject,
          ],
        ],
        // If you aren't using a configuration set, comment or delete the
        // following line
        //'ConfigurationSetName' => $configuration_set,
    ]);
    $messageId = $result['MessageId'];
    echo("Email sent! Message ID: $messageId"."\n");
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo("The email was not sent. Error message: ".$e->getAwsErrorMessage()."\n");
    echo "\n";
}
?>

我尝试将 amazon-ses-sample.php 的内容添加到我的 php 脚本中,但这不起作用。它仅在您使用时才有效:

[root@ip-172-31-24-178 ec2-user]# php /var/www/html/amazon-ses-sample.php

【问题讨论】:

  • 您查看过您的 php 错误日志以检查没有警告吗?
  • 我该怎么做?
  • 在 CLI 上运行 php --info | grep error_log 应该会公开 error_log 位置。根据您的操作系统,CLI 上的 PHP 与 apache 上运行的 PHP 可能有单独的配置

标签: php amazon-web-services apache amazon-s3 amazon-ec2


【解决方案1】:

根据您的代码判断,我看到以下观察结果。

当您运行 /var/www/html/amazon-ses-sample.php 时它可以工作,但是当您在 CLI 上以 root 身份调用时,否则对于 apache 用户它不起作用。

当您使用include 运行时,这只会发出警告以突出显示无法包含文件(因此请务必检查您的本地 PHP 日志),而 require 如果无法到达文件。

有许多问题可能导致此问题,但以下是可能的问题:

  • 检查相对于脚本的文件路径,如果您只是调用include file.php,它将相对于当前目录添加。
  • 确保文件的本地文件权限允许apache用户/组读取和执行文件。此属性可能因操作系统/安装而异,并且可以设置为自定义值。

【讨论】:

    猜你喜欢
    • 2011-11-15
    • 1970-01-01
    • 2013-09-13
    • 2019-07-20
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    相关资源
    最近更新 更多