gpt4 book ai didi

bash - 在新的 Ubuntu 16.04 服务器上使用 SSL 编写新的 nginx 实例的正确方法是什么?

转载 作者:行者123 更新时间:2023-11-29 08:50:40 25 4
gpt4 key购买 nike

到目前为止,我已经有了这个,但我遗漏了一些东西,比如编写 cron 作业的脚本。不想以 root 身份执行此操作。所以我假设可以做更多的工作来同时设置第一个用户。该脚本需要是幂等的(如果之前使用相同的参数运行它,则可以一遍又一遍地运行而不会冒更改任何内容的风险)。

单域certnginx.sh:

#!/bin/bash
if [ -z "$3" ]; then
echo use is "singledomaincertnginx.sh <server-ssh-address> <ssl-admin-email> <ssl-domain>"
echo example: "singledomaincertnginx.sh user@mydomain.com admin@mydomain.com some-sub-domain.mydomain.com"
exit
fi
ssh $1 "cat > ~/wks" << 'EOF'
#!/bin/bash
echo email: $1
echo domain: $2
sudo add-apt-repository -y ppa:certbot/certbot
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install -y software-properties-common
sudo apt-get install -y python-certbot-nginx
sudo apt-get install -y nginx
sudo sed -i "s/server_name .*;/server_name $2;/" /etc/nginx/sites-available/default
sudo systemctl restart nginx.service
if [[ -e /etc/letsencrypt/live/$2/fullchain.pem ]]; then
sudo certbot -n --nginx --agree-tos -m "$1" -d "$2"
fi
if [[ ! sudo crontab -l | grep certbot ]]; then
# todo: add cron job to renew: 15 3 * * * /usr/bin/certbot renew --quiet
EOF
ssh $1 "chmod +x ~/wks"
ssh -t $1 "bash -x -e ~/wks $2 $3"

最佳答案

I have this so far but I'm missing a couple of things like getting the cron job scripted.

这是完成(并更正)您开始的工作的一种方法:

if ! sudo crontab -l | grep certbot; then
echo "15 3 * * * /usr/bin/certbot renew --quiet" | sudo tee -a /var/spool/cron/crontabs/root >/dev/null
fi

这是我更喜欢的另一种方式,因为它不需要知道 crontabs 的路径:

if ! sudo crontab -l | grep certbot; then
sudo crontab -l | { cat; echo "15 3 * * * /usr/bin/certbot renew --quiet"; } | sudo crontab -
fi

我发现缺少的是证书文件 /etc/letsencrypt/live/$domain/fullchain.pem 是如何创建的。您是否通过其他方式提供,或者你需要这部分的帮助吗?

Don't want to do this as root.

大部分步骤涉及运行apt-get,为此,您已经需要 root。也许您的意思是您不想使用 root 进行续订。一些服务作为专用用户而不是 root 运行,但通过 documentation of certbot 查看我还没有见过这样的东西。所以用 root 进行更新似乎是一种常见的做法,所以将更新命令添加到 root 的 crontab 对我来说似乎没问题。

我会改进脚本中的一些内容以使其更健壮:

  • $1$2等散落在各处的位置参数很容易忘记,从而导致错误。我会给他们适当的名字。

  • 命令行参数验证 if [ -z "$3"] 较弱,我会使其更严格,如 if [ $# != 3 ].

  • 远程脚本生成后,用bash -e调用,有利于安全。但是,如果脚本在没有 -e 的情况下被其他东西调用,则保护措施将不存在。最好使用 set -e 将这种保护措施构建到脚本本身。我会更进一步,使用更严格的 set -euo pipefail。我也会把它放在外部脚本中。

  • 远程脚本中的大多数命令都需要 sudo。一方面,写起来很乏味。另一方面,如果一个命令最终花费了很长时间,以至于 sudo session 过期,您可能不得不再次输入 root 密码,这会很烦人,尤其是当您出去玩时咖啡时间(休闲时光。最好通过添加对执行用户的 uid 的检查来要求始终以 root 身份运行。

  • 由于您使用 bash -x ~/wks ... 而不是 ~/wks 运行远程脚本,因此无需使其可执行使用 chmod,以便可以删除该步骤。

把上面的(然后是一些)放在一起,我会这样写:

#!/bin/bash

set -euo pipefail

if [ $# != 3 ]; then
echo "Usage: $0 <server-ssh-address> <ssl-admin-email> <ssl-domain>"
echo "Example: singledomaincertnginx.sh user@mydomain.com admin@mydomain.com some-sub-domain.mydomain.com"
exit 1
fi

remote=$1
email=$2
domain=$3

remote_script_path=./wks

ssh $remote "cat > $remote_script_path" << 'EOF'
#!/bin/bash

set -euo pipefail

if [[ "$(id -u)" != 0 ]]; then
echo "This script must be run as root. (sudo $0)"
exit 1
fi

email=$1
domain=$2

echo email: $email
echo domain: $domain

add-apt-repository -y ppa:certbot/certbot
apt-get update
apt-get upgrade -y
apt-get install -y software-properties-common
apt-get install -y python-certbot-nginx
apt-get install -y nginx
sed -i "s/server_name .*;/server_name $domain;/" /etc/nginx/sites-available/default
systemctl restart nginx.service
#service nginx restart

if [[ -e /etc/letsencrypt/live/$domain/fullchain.pem ]]; then
certbot -n --nginx --agree-tos -m $email -d $domain
fi

if ! crontab -l | grep -q certbot; then
crontab -l | {
cat
echo
echo "15 3 * * * /usr/bin/certbot renew --quiet"
echo
} | crontab -
fi
EOF

ssh -t $remote "sudo bash -x $remote_script_path $email $domain"

关于bash - 在新的 Ubuntu 16.04 服务器上使用 SSL 编写新的 nginx 实例的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46804396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com