开发喵星球

nginx shell申请 SSL 证书

nginx shell申请 SSL 证书

#!/bin/bash

# 安装 Nginx
sudo apt-get update
sudo apt-get install -y nginx

# 安装 Certbot
sudo apt-get install -y certbot python3-certbot-nginx

# 创建 Web 目录
sudo mkdir -p /linrui/www

# 获取本机 IP 地址
IP_ADDRESS=(hostname -I | awk '{print1}')

# 输入域名
read -p "请输入要绑定的域名: " DOMAIN_NAME

# 配置 Nginx 虚拟主机
cat << EOF | sudo tee /etc/nginx/sites-available/DOMAIN_NAME
server {
    listen 80;
    server_nameDOMAIN_NAME;
    root /linrui/www;
    index index.html;
}
EOF

# 启用虚拟主机配置
sudo ln -s /etc/nginx/sites-available/DOMAIN_NAME /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default

# 重启 Nginx
sudo systemctl restart nginx

# 申请 SSL 证书
sudo certbot --nginx -dDOMAIN_NAME --non-interactive --agree-tos -m your_email@example.com

# 创建 index.html 文件
echo "Hello, this is the IP address: IP_ADDRESS" | sudo tee /linrui/www/index.html

# 设置自动续期
echo "0 0,12 * * * root certbot renew --quiet" | sudo tee -a /etc/crontab>/dev/null

echo "Nginx 已安装并绑定域名DOMAIN_NAME"
echo "SSL 证书已申请并设置自动续期"
echo "请访问 https://$DOMAIN_NAME 查看结果"
   
分类:金斗云 作者:开发喵 发表于:2024-04-16 01:43:11 阅读量:103
<<   >>


powered by kaifamiao