荣耀之链论坛

 找回密码
 立即注册
搜索
查看: 12|回复: 1

用letsencrypt给apache申请证书

[复制链接]

1462

主题

2726

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11547
发表于 2026-6-14 23:02 | 显示全部楼层 |阅读模式
  1. ---------------------------------
  2. 用letsencrypt给apache申请证书

  3. 我用如下命令启动的apache服务
  4. docker run -it \
  5. -d \
  6. --name httpd_3 \
  7. -v /root/docker/lamp/httpd/apache2/:/usr/local/apache2/ \
  8. -e TZ=Asia/Shanghai \
  9. --network=docker_bridge_192_168_21 --ip=192.168.21.3 \
  10. -p 80:80 \
  11. -p 443:443 \
  12. --restart unless-stopped \
  13. httpd:latest

  14. 然后网站的配置文件如下:
  15. <VirtualHost *:80>
  16.     ServerAdmin webmaster@域名.com
  17.     DocumentRoot "/usr/local/apache2/htdocs/域名.com/www"
  18.     ServerName www.域名.com
  19.     ServerAlias www.域名.com
  20.     ErrorLog "logs/www.域名.com-error_log"
  21.     CustomLog "logs/www.域名.com-access_log" common
  22. </VirtualHost>

  23. 启动这容器
  24. 现在80端口能正常访问

  25. ---------------------------------

  26. 然后申请证书,就这一条命令就行,以后更新证书可以直接重新运行一次这个就行

  27. docker run -it --rm \
  28.   -v /root/docker/lamp/httpd/apache2/htdocs/域名.com/zhengshu:/etc/letsencrypt \
  29.   -v /root/docker/lamp/httpd/apache2/htdocs/域名.com/www:/var/www/html \
  30.   certbot/certbot certonly --webroot \
  31.   -w /var/www/html \
  32.   -d www.域名.com \
  33.   -m webmaster@域名.com

  34. 这个命令里面如果需要为其他域名申请证书,就只需要改域名就好

  35. 说明:
  36. 存放证书的目录:/root/docker/lamp/httpd/apache2/htdocs/域名.com/zhengshu
  37. 供验证使用的目录:/root/docker/lamp/httpd/apache2/htdocs/域名.com/www  这个就是网站的根目录,我估计验证过程可能就是certbot生成一个文件,然后证书颁发机构访问网站的这个文件,能访问说明是对的

  38. ---------------------------------

  39. 运行结果如下:
  40. Saving debug log to /var/log/letsencrypt/letsencrypt.log
  41. Requesting a certificate for www.域名.com

  42. Successfully received certificate.
  43. Certificate is saved at: /etc/letsencrypt/live/www.域名.com/fullchain.pem
  44. Key is saved at:         /etc/letsencrypt/live/www.域名.com/privkey.pem
  45. This certificate expires on 2026-09-12.
  46. These files will be updated when the certificate renews.

  47. NEXT STEPS:
  48. - The certificate will need to be renewed before it expires. Certbot can automatically renew the certificate in the background, but you may need to take steps to enable that functionality. See https://certbot.org/renewal-setup for instructions.

  49. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  50. If you like Certbot, please consider supporting our work by:
  51. * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
  52. * Donating to EFF:                    https://eff.org/donate-le
  53. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  54. ---------------------------------
  55. 结果说明:
  56. live/www.域名.com/fullchain.pem
  57. live/www.域名.com/privkey.pem
  58. live/www.域名.com/chain.pem

  59. `privkey.pem`  : the private key for your certificate.
  60. `fullchain.pem`: the certificate file used in most server software.
  61. `chain.pem`    : used for OCSP stapling in Nginx >=1.3.7.

  62. 然后我看文件目录的结构,发现这2个文件其实是一个链接,真正的文件在archive/www.域名.com/这个目录下,但是估计以后重新生成这个目录的文件名字会变,那么就使用live目录下的吧

  63. 那么我修改一下我的httpd配置文件吧

  64. 下面是教程写的
  65. SSLCertificateFile /usr/local/apache2/conf/server.crt
  66. SSLCertificateKeyFile /usr/local/apache2/conf/server.key
  67. SSLCertificateChainFile /usr/local/apache2/conf/ca.crt
  68. 将 /data/certbot/conf/live/您的域名/fullchain.pem 映射为 server.crt
  69. 将 /data/certbot/conf/live/您的域名/privkey.pem 映射为 server.key
  70. 将 /data/certbot/conf/live/您的域名/chain.pem 映射为 ca.crt
  71. 这个教程真麻烦,还改名字,直接用现成的不就好了


  72. 下面是我最终用的
  73. SSLCertificateFile "/usr/local/apache2/htdocs/域名.com/zhengshu/live/www.域名.com/fullchain.pem"
  74. SSLCertificateKeyFile "/usr/local/apache2/htdocs/域名.com/zhengshu/live/www.域名.com/privkey.pem"
  75. SSLCertificateChainFile "/usr/local/apache2/htdocs/域名.com/zhengshu/live/www.域名.com/chain.pem"

  76. 改好了配置文件,重启httpd容器
  77. docker restart httpd_3

  78. 最后用https访问,就能访问了
复制代码
回复

使用道具 举报

1462

主题

2726

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11547
 楼主| 发表于 2026-6-14 23:21 | 显示全部楼层
  1. ---------------------------------
  2. 在无痕模式下,域名直接访问的时候会提示没有https
  3. 那么还是同时也给域名申请证书吧
  4. 运行下面的命令

  5. docker run -it --rm \
  6.   -v /root/docker/lamp/httpd/apache2/htdocs/域名.com/zhengshu:/etc/letsencrypt \
  7.   -v /root/docker/lamp/httpd/apache2/htdocs/域名.com/www:/var/www/html \
  8.   certbot/certbot certonly --webroot \
  9.   -w /var/www/html \
  10.   -d 域名.com -d www.域名.com \
  11.   -m webmaster@域名.com

  12. ---------------------------------

  13. 运行以后结果如下:

  14. Saving debug log to /var/log/letsencrypt/letsencrypt.log

  15. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  16. You have an existing certificate that contains a portion of the domains you
  17. requested (ref: /etc/letsencrypt/renewal/www.域名.com.conf)

  18. It contains these names: www.域名.com

  19. You requested these names for the new certificate: 域名.com,
  20. www.域名.com.

  21. Do you want to expand and replace this existing certificate with the new
  22. certificate?
  23. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  24. (E)xpand/(C)ancel: E
  25. Renewing an existing certificate for 域名.com and www.域名.com

  26. Successfully received certificate.
  27. Certificate is saved at: /etc/letsencrypt/live/www.域名.com/fullchain.pem
  28. Key is saved at:         /etc/letsencrypt/live/www.域名.com/privkey.pem
  29. This certificate expires on 2026-09-12.
  30. These files will be updated when the certificate renews.

  31. NEXT STEPS:
  32. - The certificate will need to be renewed before it expires. Certbot can automatically renew the certificate in the background, but you may need to take steps to enable that functionality. See https://certbot.org/renewal-setup for instructions.

  33. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  34. If you like Certbot, please consider supporting our work by:
  35. * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
  36. * Donating to EFF:                    https://eff.org/donate-le
  37. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


  38. ---------------------------------

  39. 然后顺便给域名也同时配置一个https


  40. <VirtualHost _default_:443>

  41. DocumentRoot "/usr/local/apache2/htdocs/域名.com/www"
  42. ServerName 域名.com:443
  43. ServerAdmin webmaster@域名.com
  44. ErrorLog /proc/self/fd/2
  45. TransferLog /proc/self/fd/1

  46. SSLEngine on

  47. SSLCertificateFile "/usr/local/apache2/htdocs/域名.com/zhengshu/live/www.域名.com/fullchain.pem"

  48. SSLCertificateKeyFile "/usr/local/apache2/htdocs/域名.com/zhengshu/live/www.域名.com/privkey.pem"

  49. SSLCertificateChainFile "/usr/local/apache2/htdocs/域名.com/zhengshu/live/www.域名.com/chain.pem"

  50. <FilesMatch "\.(cgi|shtml|phtml|php)$">
  51.     SSLOptions +StdEnvVars
  52. </FilesMatch>
  53. <Directory "/usr/local/apache2/cgi-bin">
  54.     SSLOptions +StdEnvVars
  55. </Directory>

  56. BrowserMatch "MSIE [2-5]" \
  57.          nokeepalive ssl-unclean-shutdown \
  58.          downgrade-1.0 force-response-1.0

  59. CustomLog /proc/self/fd/1 \
  60.           "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x "%r" %b"

  61. </VirtualHost>                                 

  62. 删掉多余的注释以后就这么多,我是直接复制默认的配置的,这样配置没啥问题
  63. ---------------------------------

  64. 最后在无痕模式下,直接域名访问,也能正常https,查看证书是域名

  65. 然后用www访问也能https,这里查看证书也是不带www的域名,那么未来每次弄-d参数还是都带上吧
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

荣耀之链

GMT+8, 2026-6-19 04:37 , Processed in 0.091950 second(s), 20 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表