如何使一个bash脚本通过nginx的/ PHP执行

问题描述:

我的目标是做一个方法来自动生成谁希望自己的网站添加到我的CDN服务人员的SSL证书(https://hostcloak.com如何使一个bash脚本通过nginx的/ PHP执行

这里是脚本通过SSH的作品,而不是当我试图通过PHP

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

这里来执行它是bash脚本:

当通过ssh控制台直接使用
#!/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 = [email protected] 
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”的作品,但是当我尝试它通过PHP的exec函数,它说:“找不到命令”为“nginx的-s重装”

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

快速回答:在exec功能与bash替换sh或修改你的脚本与SH工作

说明:you can find here, in this * thread

+0

试过了,但是我得到了这个:Array([0] =>/usr/local/bin/autossl:第6行:/etc/nginx/sites/yoyyo.com.conf:Permission denied [1] =>/usr/local/bin/autossl:第14行:nginx:command not找到[2] =>请求以root权限重新运行/ usr/local/bin/certbot-auto ... [3] => [4] =>我们相信您已经收到本地系统[5] =>管理员,通常归结为以下三件事:[6] => [7] =>#1)尊重他人的隐私。 [8] =>#2)在键入之前考虑一下。 [9] =>#3)拥有巨大的权力将会带来巨大的责任。 [0] => [11] => sudo:没有tty目前... – John

+0

嗯,第一个错误可能是因为php/nginx有其他权限,您的用户,第二个因为nginx命令不在php/nginx路径bash –

+0

如何将它添加到php/nginx bash的路径中? – John

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

你可以禁用个人的密码要求,但我不会建议给www-data sudo权限。让你的网站向数据库添加请求或者每隔几分钟将其作为一个cron作业进行轮询,然后让这个帐户做su stuff

+0

是的这是一个更好的解决方案,一个简单的文件队列会做。 – Scriptonomy

+0

是的,我在visudo中为ngixin添加了nopassword,但它仍然要求输入密码。我已经用php exec发出'whoami',它给了nginx,仍然要求输入密码。 :( – John