[英]AWS EC2 Apache User Cannot run php exec
I am using the following PHP script to send an email from my server.我正在使用以下 PHP 脚本从我的服务器发送 email。 I need to send an email to the admin when a new record is created in the DB.
在数据库中创建新记录时,我需要将 email 发送给管理员。 So within the same php script that updates the DB, I want to trigger the other script that sends the email.
因此,在更新数据库的同一个 php 脚本中,我想触发另一个发送 email 的脚本。
Problem is no matter what I do, apache will not execute the email script when requested from the web/api.问题是无论我做什么,当从 web/api 请求时,apache 都不会执行 email 脚本。
However, when I run php sendemail.php
from the command line it works.但是,当我从命令行运行
php sendemail.php
时,它可以工作。 Also when I run php updatedb.php
which includes the exec('php sendemail.php')
also works from the command line (these are all executed with root "ec2-user").此外,当我运行
php updatedb.php
时,其中包括exec('php sendemail.php')
也可以从命令行工作(这些都使用 root “ec2-user”执行)。
Things I tried:我尝试过的事情:
exec
with shell_exec
, and include
, no luck.shell_exec
替换exec
,并且include
,没有运气。 Here is 'sendemail.php':这是'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";
}
a short version of the updatedb.php file, omitting all transactions:更新后的 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();
?>
in the updatedb.php
file, if I change the first line to echo exec('whoami')
and hit it from the web it works.在更新的
updatedb.php
文件中,如果我将第一行更改为echo exec('whoami')
并从 web 中点击它,它就可以工作。 Which is what I am looking for exactly except that I want to work for php sendemail.php
这正是我正在寻找的,除了我想为
php sendemail.php
工作
Environment: AWS EC2 Amazon Linux 2 AMI.环境:AWS EC2 Amazon Linux 2 AMI。 PHP 7.2.34
PHP 7.2.34
I hope it is clear.我希望很清楚。 I am beginner with linux.
我是 linux 的初学者。 Please help.
请帮忙。 Thanks in advance to all.
在此先感谢大家。
Thank you so much @Riz your tip about sudo -u apache php sendemail.php
saved my day!非常感谢@Riz,您关于
sudo -u apache php sendemail.php
的提示拯救了我的一天! I was able to debug on the command line and it turns out that my mistake was with the line require 'vendor/autoload.php';
我能够在命令行上进行调试,结果发现我的错误在于行
require 'vendor/autoload.php';
in my original sendemail.php
script I was requiring the file from a directory that did not belong to the apache
group.在我原来的
sendemail.php
脚本中,我需要来自不属于apache
组的目录中的文件。 So, once I moved the vendor folder to the same folder as the sendemail.php
script file everything worked great!因此,一旦我将供应商文件夹移动到与
sendemail.php
脚本文件相同的文件夹,一切都很好!
Lesson learned: Make sure all required/included files belong to the apache
group.经验教训:确保所有必需/包含的文件都属于
apache
组。 There was no need to grant apache any execution permissions on any file.无需授予 apache 对任何文件的任何执行权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.