这是本文档旧的修订版!
服务器安全设置
主要运用 ubuntu 自带的防火墙工具 UFW 进行设定。1) 2) 3) 也可以利用 iptables 白名单的方式进行保护。4)
UFW 使用比较简单,因此首先考虑使用这种方式。
安装 sudo apt install ufw
检查状态 sudo ufw status verbose
列举可用的配置文件 sudo ufw app list
查看 Nginx 使用了哪些端口 sudo ufw app info 'Nginx Full'
允许ssh sudo ufw allow ssh
(很重要,否则连不上服务器了)
启动UFW服务 sudo ufw enable
停用服务 sudo ufw disable
重置设定 sudo ufw reset
列举现有的规则 sudo ufw status numbered
删除3号规则 sudo ufw delete 3
限制端口22访问 sudo ufw limit 22
防止黑客对其进行攻击
sudo ufw allow http sudo ufw allow 80/tcp sudo ufw allow 'Nginx HTTP' sudo ufw allow https sudo ufw allow 443/tcp sudo ufw allow 'Nginx HTTPS' sudo ufw allow 7100:7200/tcp # 允许特点端口 sudo ufw allow 7100:7200/udp sudo ufw allow from 64.63.62.61 # 允许白名单 sudo ufw allow from 64.63.62.61 to any port 22 # 只允许64.63.62.61的主机访问端口22 sudo ufw allow from 192.168.0.100/200 to any port 873 sudo ufw allow from 192.168.0.0/24 to any port 22 # 允许整个子网访问 sudo ufw deny from 23.24.25.0/24 # 拒绝黑名单 sudo ufw default deny incoming # 拒绝所有访问,除非指定 sudo ufw default allow outgoing # 允许数据出去
- 启动/关闭
service iptables start
(stop) - 删除
apt-get purge netfilter-persistent
&& reboot - 强制删除
rm -rf /etc/iptables && reboot
SSL 证书
利用Cerbot5)获取证书。按照官网的指令(官方文档) (中文文档),
最后为两个域名生成证书,将这两份证书重新复制到别的主机,应该也可以用。
显示当前证书:certbot certificates
证书的有效期为三个,需要用下面命令定期更新:sudo certbot renew --dry-run
或者添加定时任务 0 0 1 * * /usr/bin/certbot renew --quiet
(每月1号更新一下)
接着到Nginx的conf配置文件中更改证书目录就可以了。
如果需要迁移的第二台服务器,直接压缩certbot的文件夹就好了。 tar zpcvf backup.tar.gz /etc/letsencrypt/
为主域名和下面的子域名申请通配证书 6)
certbot certonly -d *.simperator.com --manual --preferred-challenges dns # -d 为那些主机申请证书,如果是通配符,输入 *simperator.com # –preferred-challenges dns,使用 DNS 方式校验域名所有权
* 可以使用certonly或run命令来创建一个新证书,即使已经存在具有该域名的证书,也没有关系,那么certbot会更新该证书,否则会创建一个新证书,一句话存在就更新,不存在就创建
按照指示添加TXT记录,成功后存储在 /etc/letsencrypt/archive/simperator.com
校验证书:
openssl x509 -in /etc/letsencrypt/archive/simperator.com/cert1.pem -noout -text
为域名更换证书 change Domain
更新邮件地址
download
certbot register --update-registration --email <email> certbot update_account --email [email protected]
打开 /etc/letsencrypt/renewal 文件夹下查看 *.conf 文件就能知道目前用的是哪个 account 。(当前我申请的两个证书都是同一个aacount)
为每一个子域名配置证书,如 xx.simperator.com,有多少个子网站就重复多少次。Certbot会找到Nginx并正确的配置。
certbot --nginx -d fun.simperator.com
端口分配的设置参考Nginx Redirect HTTP To HTTPS / How To Configure Nginx with SSL as a Reverse Proxy for Jenkins
吊销证书
严格来说不需要吊销一个证书,除非自己已经对这个域名失去控制。只要删除证书就好,三个月后会自动过期。并且revoke
命令也不会删除,下次renew的时候还是会被更新。
certbot delete --cert-name MyDomain
sslforfree
另外一种申请证书的方式 免费ssl证书
SSL 更新
更新证书遇到错误:
sudo certbot renew --dry-run - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Processing /etc/letsencrypt/renewal/simperator.com.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Failed to renew certificate simperator.com with error: The manual plugin is not working; there may be problems with your existing configuration. The error was: PluginError('An authentication script must be provided with --manual-auth-hook when using the manual plugin non-interactively.') - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - All renewals failed. The following certificates could not be renewed: /etc/letsencrypt/live/simperator.com/fullchain.pem (failure) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1 renew failure(s), 0 parse failure(s) Ask for help or search for solutions at https://community.letsencrypt.org. See the logfile /var/log/letsencrypt/letsencrypt.log or re-run Certbot with -v for more details.
尝试新的命令
download
certbot certonly --manual -d *.simperator.com
更新证书后要重启服务器 sudo systemctl restart nginx
将 /etc/letsencrypt/archive/simperator.com/*.pem
文件拷贝到第二台计算机上,ln -s
命令更新软链接,就可以在新的计算机上使用此证书。
為了防止過多的 TXT 紀錄,以後要刪除舊的紀錄。ionos 1小時, pi server 2小時。
buypass TLS 证书可以6个月有效
certbot certonly --standalone --agree-tos -d www.simperator.com -d simperator.com --server https://api.buypass.com/acme/directory
ACME 证书
apt install -y socat curl https://get.acme.sh | sh ~/.acme.sh/ // 安装到自己目录下
注册证书
~/.acme.sh/acme.sh --register-account -m [email protected] // 开通 80 和 443 端口,否则无法监听 // 如果一直不通就加上参数 --server letsencrypt ~/.acme.sh/acme.sh --issue -d simperator.top --standalone
安装完成后会自动更新证书,查看crontab 会发现这条命令
root@serverpi:~# crontab -l
40 0 * * * “/root/.acme.sh”/acme.sh –cron –home “/root/.acme.sh” > /dev/null
拷贝证书到指定地点
~/.acme.sh/acme.sh --installcert -d simperator.top --key-file /root/ssl-keys/private.key --fullchain-file /root/ssl-keys/cert.crt
没有通配证书,simperator.top 和 ionos.simperator.top 要分别申请。
为 nginx 配置证书 以域名 ionos.simperator.top 为例
1. 申请证书 acme.sh --issue -d ionos.simperator.top --nginx
2. 创建文件夹 mkdir -p /etc/nginx/ssl/ionos.simperator.top
3. 拷贝证书
~/.acme.sh/acme.sh --install-cert -d ionos.simperator.top \ --key-file /etc/nginx/ssl/ionos.simperator.top/key.pem \ --fullchain-file /etc/nginx/ssl/ionos.simperator.top/cert.pem \ --reloadcmd "service nginx force-reload"
4 配置 conf 文件
ssl_certificate "/etc/nginx/ssl/ionos.simperator.top/cert.pem"; ssl_certificate_key "/etc/nginx/ssl/ionos.simperator.top/key.pem"; ssl_session_cache shared:SSL:1m; ssl_session_timeout 10m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf;
查看证书 acme.sh list 有效日期
cloudflare 现在已经不支持通过API设置.cf, .ga, .gq, .ml, 或.tk域名的DNS记录,在acme.sh设置TXT记录时会出错。只能单个证书申请。
cloudflare
托管在 cloudflare 上的域名参考 用 acme.sh 脚本申请 SSL 证书并自动更新 以及 使用 acme.sh 自动签发和更新证书
定时任务更新证书,并重新加载 nginx
0 3 15 */2 * acme.sh --installcert -d simperator.com --fullchain-file /root/ssl-keys/fullchain.pem --key-file /root/ssl-keys/private.key --ecc && /etc/init.d/nginx reload
自动更新 acme.sh --upgrade --auto-upgrade