2017-10-18 3 views
1

은 (https://hostcloak.com)이 어떻게 bash는 스크립트의 nginx/PHP를 통해 실행할 수 있도록 내 목표는 자동으로 내 CDN 서비스에 자신의 사이트를 추가 할 사람들을위한 SSL 인증서에서 생성하는 방법을 확인하는 것입니다

다음

는 스크립트입니다 내가하려고하지만 경우에, 직접 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는"작동합니다 SSH를 통해 작동하지만 내가 PHP 여기

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

를 통해 그것을 실행하려고 할 때하면 bash는 스크립트입니다 PHP의 간부 인 기능을 통해, 그것은 "의 nginx -s 다시로드"를 "명령을 찾을 수 없습니다"라는

그래서 내 질문은 : 나는

답변

0

(내 웹 사이트에 의해 자동화되어야한다) PHP를 통해이를 어떻게 빠른 대답 : exec 기능에 bash으로 sh를 교체하거나 쉬와 함께 작동하도록 스크립트를 수정

설명 : you can find here, in this stackoverflow thread

+0

시도해 보았지만 이것을 얻었습니다 : Array ([0]/usr/local/bin/autossl : 행 6 : /etc/nginx/sites/yoyyo.com.conf : 권한 거부 [1] =>/usr/local/bin/autossl : 줄 14 : nginx : 명령 안함 root 권한으로/usr/local/bin/certbot-auto를 다시 실행하라는 요청을 받았습니다. [3] => [4] => 우리는 당신이 로컬 시스템 [5] => 관리자. 일반적으로 다음 세 가지로 귀결됩니다 : [6] => [7] => # 1) 다른 사람들의 프라이버시를 존중하십시오. [8] => # 2) 입력하기 전에 생각하십시오. [9] => # 3) 위대한 힘으로 위대한 책임을지게됩니다. [10] => [11] => sudo : 아무런 tty가 없습니다. – John

+0

php/nginx는 php/nginx 경로에 nginx 명령이 없으므로 사용자가 두 번째로 다른 권한을 가지고 있기 때문에 첫 번째 오류 일 가능성이 큽니다. bash –

+0

php/nginx bash 경로에 어떻게 추가합니까? – John

1

당신이 여기 무엇을하려고 생각해. sudo 명령을 실행하려면 www- 데이터 (또는 웹 서버가 실행중인 사용자 계정)에 문의하십시오. 아마 su 권한조차 가지고 있지 않을 수도 있습니다. 그랬다 할지라도, sudo를 처음 사용하려고하면 어떻게됩니까? 암호를 입력해야합니다 ...

암호 요구 사항을 개별적으로 해제 할 수 있지만 www- 데이터 sudo 권한을 부여하지 않는 것이 좋습니다. 귀하의 웹 사이트에 데이터베이스 또는 무언가에 대한 요청을 추가하고 su priveleges가있는 사용자로부터 몇 분마다 cron 작업으로 설문 조사를 실시하고 그 계정에 su 자료를 요청하십시오.

+0

네, 이것은 더 나은 해결책입니다, 간단합니다 파일 대기열이 수행합니다. – Scriptonomy

+0

예, visudo에서 ngixin에 nopassword를 추가했으나 여전히 비밀번호를 묻습니다. 나는 'whoami'를 PHP exec와 함께 발행했으며 nginx에게 여전히 패스워드를 묻습니다. :( – John