[英]AWS EC2 Apache User Cannot run php exec
我正在使用以下 PHP 脚本从我的服务器发送 email。 在数据库中创建新记录时,我需要将 email 发送给管理员。 因此,在更新数据库的同一个 php 脚本中,我想触发另一个发送 email 的脚本。
问题是无论我做什么,当从 web/api 请求时,apache 都不会执行 email 脚本。
但是,当我从命令行运行php sendemail.php
时,它可以工作。 此外,当我运行php updatedb.php
时,其中包括exec('php sendemail.php')
也可以从命令行工作(这些都使用 root “ec2-user”执行)。
我尝试过的事情:
shell_exec
替换exec
,并且include
,没有运气。这是'sendemail.php':
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
use Aws\Ses\SesClient;
use Aws\Exception\AwsException;
$SesClient = new SesClient([
'profile' => 'default',
'version' => '2010-12-01',
'region' => 'us-west-2'
]);
$sender_email = 'sender@example.com';
$recipient_emails = ['recipient1@example.com','recipient2@example.com'];
$configuration_set = 'ConfigSet';
$subject = 'Amazon SES test (AWS SDK for PHP)';
$plaintext_body = 'This email was sent with Amazon SES using the AWS SDK for PHP.' ;
$html_body = '<h1>AWS Amazon Simple Email Service Test Email</h1>'.
'<p>This email was sent with <a href="https://aws.amazon.com/ses/">'.
'Amazon SES</a> using the <a href="https://aws.amazon.com/sdk-for-php/">'.
'AWS SDK for PHP</a>.</p>';
$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,
],
],
'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";
}
更新后的 b.php 文件的简短版本,省略了所有事务:
<?php
exec('php send_email.php', $sendEmail);
require_once 'response.php';
$response = new response();
$response->setHttpStatusCode(201);
$response->setSuccess(true);
$response->addMessage('DB Record Inserted successfully ::: ');
$response->setData($sendEmail);
$response->send();
?>
在更新的updatedb.php
文件中,如果我将第一行更改为echo exec('whoami')
并从 web 中点击它,它就可以工作。 这正是我正在寻找的,除了我想为php sendemail.php
工作
环境:AWS EC2 Amazon Linux 2 AMI。 PHP 7.2.34
我希望很清楚。 我是 linux 的初学者。 请帮忙。 在此先感谢大家。
非常感谢@Riz,您关于sudo -u apache php sendemail.php
的提示拯救了我的一天! 我能够在命令行上进行调试,结果发现我的错误在于行require 'vendor/autoload.php';
在我原来的sendemail.php
脚本中,我需要来自不属于apache
组的目录中的文件。 因此,一旦我将供应商文件夹移动到与sendemail.php
脚本文件相同的文件夹,一切都很好!
经验教训:确保所有必需/包含的文件都属于apache
组。 无需授予 apache 对任何文件的任何执行权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.