美文网首页
lighttpd配置https访问

lighttpd配置https访问

作者: IFELSE | 来源:发表于2017-10-18 15:50 被阅读0次

本文介绍了如何在lighttpd上配置https访问,使用Let's Encrypt获取免费SSL证书,以及自动更新证书
by www.kowen.cn

安装openssl

apt-get install openssl*

执行以下命令看到有(ssl)即代表成功

lighttpd -v

使用Let's Encrypt获取免费ssl证书

let's encrypt是比较流行的一个免费ssl证书颁发机构,我们使用简易的脚本来获取,见参考资料1

  1. 建立目录,下载脚本
mkdir letsencrypt
cd letsencrypt
wget https://raw.githubusercontent.com/xdtianyu/scripts/master/lets-encrypt/letsencrypt.conf
wget https://raw.githubusercontent.com/xdtianyu/scripts/master/lets-encrypt/letsencrypt.sh
chmod +x letsencrypt.sh
  1. 修改配置文件letsencrypt.conf,把 DOMAIN_KEY DOMAIN_DIR DOMAINS 三个参数改为你自己的信息,注意把LIGHTTPD=TRUE前的#号删除,否则不能生成pem文件。比如:
ACCOUNT_KEY="letsencrypt-account.key"  
DOMAIN_KEY="kowen.cn.key"  
DOMAIN_DIR="/var/www/blog-public"  
DOMAINS="DNS:kowen.cn,DNS:www.kowen.cn"  
\#ECC=TRUE  
LIGHTTPD=TRUE

注意:需要已经绑定DOMAINS的域名到 DOMAIN_DIR 指定的目录,即通过 http://kowen.cn http://www.kowen.cn 可以访问到 /var/www/blog-public 目录,用于域名的验证.否则会出现错误

  1. 执行命令生成一系列文件
./letsencrypt.sh letsencrypt.conf

里面有两个lighttpd用到的文件:
 lets-encrypt-x3-cross-signed.pem和kowen.pem,其中kowen.pem是lighttpd配置文件中的ssl.pemfile,lets-encrypt-x3-cross-signed.pem是ssl.ca-file

配置lighttpd

通过编辑配置文件 /etc/lighttpd/lighttpd.conf来启用ssl,有几种方式可供选择。配置完成后要重启lighttpd服务生效。

  1. 让服务器仅提供https访问,全局设置中添加ssl配置,更改服务端口为443
ssl.engine                  = "enable"
ssl.pemfile                 = "/root/letsencrypt/kowen.pem"
ssl.ca-file                 = "/root/letsencrypt/lets-encrypt-x3-cross-signed.pem"
server.port                 = 443  #将原来的端口改为443
  1. 不改变原配置,添加443端口访问
$SERVER["socket"] == ":443" {  
    ssl.engine   = "enable"   
    ssl.pemfile  = "/etc/lighttpd/certs/www.example.com.pem" 
    ssl.ca-file  = "/etc/lighttpd/fullchain.pem"
}
  1. 设置443端口为新的虚拟主机
$SERVER["socket"] == ":443" {  
         server.document-root = "/srv/ssl" # use your ssl directory here  
         ssl.engine  = "enable"   
         ssl.pemfile  = >"/etc/lighttpd/certs/www.example.com.pem"    
         ssl.ca-file  = "/etc/lighttpd/fullchain.pem"
 }
  1. 为不同的虚拟主机配置不同的证书文件
$HTTP["host"] == "www.example.org" {  
        ssl.pemfile = "/etc/lighttpd/certs/www.example.org.pem"   
        sl.ca-file  = "/etc/lighttpd/fullchain.pem"  
}  
$HTTP["host"] == "mail.example.org" {    
        ssl.pemfile = "/etc/lighttpd/certs/mail.example.org.pem"   
        sl.ca-file  = "/etc/lighttpd/fullchain.pem"  
}  

自动每月更新证书

Let's Encrypt有有效期,需要续期才能继续使用

编辑定时任务

crontab -e

添加以下任务,根据你的Letsencrypt脚本目录进行相应修改

# m h  dom mon dow   command  
  0 0   1   *   *    /root/letsencrypt/letsencrypt.sh /root/letsencrypt/letsencrypt.conf >> /var/log/lets-encrypt.log>&1  

重定向http服务到https(如果需要)

首先要在配置中添加重定向组件:

server.modules = (  
      ...  
      "mod_redirect",   
      ...  
)

然后配置虚拟主机80端口重定向到https服务:

$SERVER["socket"] == ":80" {  
        $HTTP["host"] =~ "example.org" {  
         url.redirect = ( "^/(.*)" => "https://example.org/$1" )  
          server.name                 = "example.org"   
        }  
}

也可以重定向所有端口到对应https服务:

$SERVER["socket"] == ":80" {
  $HTTP["host"] =~ "(.*)" {
    url.redirect = ( "^/(.*)" => "https://%1/$1" )
  }
}

参考资料

相关文章

网友评论

      本文标题:lighttpd配置https访问

      本文链接:https://www.haomeiwen.com/subject/bwkeuxtx.html