【问题标题】:How to make a bash script execute via nginx/php如何通过 nginx/php 执行 bash 脚本
【发布时间】:2018-03-29 18:50:26
【问题描述】:

我的目标是为想要将其网站添加到我的 CDN 服务 (https://hostcloak.com) 的用户自动生成 SSL 证书

这是通过 SSH 工作的脚本,但当我尝试通过 php 执行时却没有

exec("sudo sh /var/autossl $domain 2>&1", $output);

这是 bash 脚本:

#!/bin/bash

domain=$1

# Set up config file.
cat > /etc/nginx/sites/$domain.conf <<EOF
server {
    listen 80;
    server_name *.$domain;
    root         /var/www/$domain;
}
EOF

nginx -s reload

#########################################

set -o nounset
set -o errexit

mkdir -p /var/www/$domain

# Set up config file.
mkdir -p /etc/letsencrypt
cat > /etc/letsencrypt/cli.ini <<EOF
# Uncomment to use the staging/testing server - avoids rate limiting.
# server = https://acme-staging.api.letsencrypt.org/directory

# Use a 4096 bit RSA key instead of 2048.
rsa-key-size = 4096

# Set email and domains.
email = admin@hostcloak.com
domains = $domain

# Text interface.
text = True
# No prompts.
non-interactive = True
# Suppress the Terms of Service agreement interaction.
agree-tos = True

# Use the webroot authenticator.
authenticator = webroot
webroot-path = /var/www/$domain
EOF

# Obtain cert.
certbot-auto certonly

# Set up daily cron job.
CRON_SCRIPT="/etc/cron.daily/certbot-renew"

cat > "${CRON_SCRIPT}" <<EOF
#!/bin/bash
#
# Renew the Let's Encrypt certificate if it is time. It won't do anything if
# not.
#
# This reads the standard /etc/letsencrypt/cli.ini.
#

# May or may not have HOME set, and this drops stuff into ~/.local.
export HOME="/root"
# PATH is never what you want it it to be in cron.
export PATH="\${PATH}:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

certbot-auto --no-self-upgrade certonly

# If the cert updated, we need to update the services using it. E.g.:
if service --status-all | grep -Fq 'apache2'; then
  service apache2 reload
fi
if service --status-all | grep -Fq 'httpd'; then
  service httpd reload
fi
if service --status-all | grep -Fq 'nginx'; then
  service nginx reload
fi
EOF
chmod a+x "${CRON_SCRIPT}"

#####################################

# Set up config file.
cat > /etc/nginx/sites/$domain.conf <<EOF
        server {

        listen 80;
                server_name *.$domain;
                location / {
                        proxy_set_header x-real-IP \$remote_addr;
                        proxy_set_header x-forwarded-for \$proxy_add_x_forwarded_for;
                        proxy_set_header host \$host;
                        proxy_pass http://google.com;
                        }
                }

        server {
        listen 443;
                server_name *.$domain;
                ssl_certificate /etc/letsencrypt/live/$domain/fullchain.pem;
                ssl_certificate_key /etc/letsencrypt/live/$domain/privkey.pem;
                ssl on;
                ssl_session_cache builtin:1000 shared:SSL:10m;
                ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
                ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
                ssl_prefer_server_ciphers on;
                location / {
                        proxy_set_header x-real_IP \$remote_addr;
                        proxy_set_header x-forwarded-for \$proxy_add_x_forwarded_for;
                        proxy_set_header host \$host;
                        proxy_pass http://google.com;
                }
        }
EOF

nginx -s reload

“autossl”在通过 ssh 控制台直接使用时有效,但是当我通过 php 的 exec 函数尝试它时,它显示“nginx -s reload”的“command not found”

所以我的问题是:我如何通过 PHP 实现这一点(它必须由我的网站自动化)

【问题讨论】:

    标签: php bash ssl ssh


    【解决方案1】:

    想想你想在这里做什么。您要求 www-data(或您的网络服务器运行的任何用户帐户)发出 sudo 命令。它甚至可能没有 su 特权。即使是这样,当您第一次尝试使用 sudo 时会发生什么?您必须输入密码...

    您可以单独禁用密码要求,但我不建议您授予 www-data sudo 权限。让您的网站向数据库或其他东西添加请求,并每隔几分钟从具有 su 权限的用户那里轮询一次 cron 作业,并让该帐户执行 su 操作

    【讨论】:

    • 是的,这是一个更好的解决方案,一个简单的文件队列就可以了。
    • 是的,我已经在 visudo 中为 ngixin 添加了 nopassword,但是它仍然要求输入密码。我已经用 php exec 发布了“whoami”,它给了 nginx,但仍然要求输入密码。 :(
    【解决方案2】:

    快速回答:在 exec 函数中将 sh 替换为 bash 或修改您的脚本以使用 sh

    解释:you can find here, in this stackoverflow thread

    【讨论】:

    • 试过了,但我得到了这个: Array ( [0] => /usr/local/bin/autossl: line 6: /etc/nginx/sites/yoyyo.com.conf: Permission denied [1] => /usr/local/bin/autossl:第 14 行:nginx:找不到命令 [2] => 请求以 root 权限重新运行 /usr/local/bin/certbot-auto... [3] = > [4] => 我们相信您已经收到了本地系统 [5] => 管理员的常规讲座。通常归结为以下三点:[6] => [7] => #1) 尊重隐私别人的。 [8] => #2) 打字前三思。 [9] => #3) 权力越大,责任越大。 [10] => [11] => sudo: no tty present an...
    • 嗯,第一个错误可能是因为 php/nginx 有你的用户的其他权限,第二个是因为 nginx 命令不在 php/nginx bash 的路径中
    • 如何添加到 php/nginx bash 的路径中?
    • 我不知道如何为没有主目录的用户添加,但您可以在脚本中使用提供 nginx 命令的完整路径,如/usr/bin/nginx,或者您可以设置路径系统范围,例如。在 ubuntu ubuntu-docs - 段落系统范围的环境变量,或者您可以使用 export PATH 命令在脚本中设置路径
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-30
    • 2020-08-18
    • 2017-03-21
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多