介绍
服务器名称指示(SNI)是TLS协议的一项功能,可在一个IP地址上设置多个SSL证书。
Lighttpd 必须已经安装和运行. 阅读这些文章以获取它。
设置
两个域将用于本教程. 两者都将使用自签名的SSL证书。
** 域名 1:** example.com ** 通用名称:** *.example.com
** 域名 2:** digitalocean.com ** 通用名称:** www.digitalocean.com
SSL 设置需要一个默认证书,就像默认虚拟主机一样。
创建私钥
在 `/etc/lighttpd 中创建一个目录,以便放置密钥和证书。
1mkdir /etc/lighttpd/certs
创建一个私钥,然后输入一个passphrase。第二个命令是删除passphrase。
1cd /etc/lighttpd/certs
2openssl genrsa -des3 -out example.com.key 2048
3openssl rsa -in example.com.key -out example.com.key
对于第二个域也做同样的事情。
1openssl genrsa -des3 -out digitalocean.com.key 2048
2openssl rsa -in digitalocean.com.key -out digitalocean.com.key
如果没有删除该短语,Lighttpd 会在每次启动或重新启动时提示它。
创建签名申请证书
为两个域名创建 CSR。
1cd /etc/lighttpd/certs
2openssl req -new -key digitalocean.com.key -out digitalocean.com.csr
3openssl req -new -key example.com.key -out example.com.csr
填写详细信息并输入一个点 .
如果您希望留空一个字段。
1-----
2Country Name (2 letter code) [AU]:US
3State or Province Name (full name) [Some-State]:.
4Locality Name (eg, city) []:NYC
5Organization Name (eg, company) [Internet Widgits Pty Ltd]:DigitalOcean Inc
6Organizational Unit Name (eg, section) []:.
7Common Name (e.g. server FQDN or YOUR name) []:www.digitalocean.com
8Email Address []:[email protected]
9
10Please enter the following 'extra' attributes
11to be sent with your certificate request
12A challenge password []:
13An optional company name []:
填写example.com
的共同名称
字段时,请确保输入*.example.com
。
1-----
2Country Name (2 letter code) [AU]:IN
3State or Province Name (full name) [Some-State]:.
4Locality Name (eg, city) []:Chennai
5Organization Name (eg, company) [Internet Widgits Pty Ltd]:.
6Organizational Unit Name (eg, section) []:.
7Common Name (e.g. server FQDN or YOUR name) []:*.example.com
8Email Address []:[email protected]
9
10Please enter the following 'extra' attributes
11to be sent with your certificate request
12A challenge password []:
13An optional company name []:
我们已经为这个CSR输入了不同的国家和城市,所以很容易区分哪个证书呈现。
签署您的SSL证书
在这里,我们将为两种证书设置不同的有效期长度(‘-days’ 参数),以便我们可以轻松区分它们。
1cd /etc/lighttpd/certs
2openssl x509 -req -days 100 -in example.com.csr -signkey example.com.key -out example.com.crt
3openssl x509 -req -days 200 -in digitalocean.com.csr -signkey digitalocean.com.key -out digitalocean.com.crt
从证书及其私钥中创建一个.pem 文件。
1cat example.com.{key,crt}> example.com.pem
2cat digitalocean.com.{key,crt}> digitalocean.com.pem
通过仅允许 root 用户阅读这些文件来保护此目录中的文件。
1chmod -R 400 /etc/lighttpd/certs/
Lighttpd 示威者在降至 www-data 之前从根特权开始,所以这不应该是一个问题。
配置 Lighttpd for SSL
编辑 /etc/lighttpd/lighttpd.conf
文件,并在末尾添加以下行。
1$SERVER["socket"] == "0.0.0.0:443" {
2 ssl.engine = "enable"
3 ssl.pemfile = "/etc/lighttpd/certs/example.com.pem"
4 $HTTP["host"] =~ "(^|www\.)digitalocean.com" {
5 ssl.pemfile = "/etc/lighttpd/certs/digitalocean.com.pem"
6 }
7}
由于 example.com
的 ceritifcate 是默认的,我们不会将其放置在主机状态中。
重新启动 Lighttpd Daemon。
1service lighttpd force-reload
如果您在证书路径或创建.pem 文件时没有犯错误,则该服务应成功重新启动。
HTTPS 测试
在不编辑 **hosts ** 文件的情况下测试这个简单的方法是使用弯曲
。
此命令有一个--resolve
参数,该参数将域与 IP 地址进行地图。
1curl -k -o /dev/null --resolve "www.digitalocean.com:443:1.1.1.1" -s -v https://www.digitalocean.com
这将给出一个无语的输出. -o
重定向所有 HTML 输出,因为我们不需要它.这是我们感兴趣的片段。
1* SSL connection using AES256-SHA
2* Server certificate:
3* subject: C=US; L=NYC; O=DigitalOcean Inc; CN=www.digitalocean.com; [email protected]
4* start date: 2014-03-26 18:39:25 GMT
5* expire date: 2014-10-12 18:39:25 GMT
6* common name: www.digitalocean.com (matched)
7* issuer: C=US; L=NYC; O=DigitalOcean Inc; CN=www.digitalocean.com; [email protected]
8* SSL certificate verify result: self signed certificate (18), continuing anyway.
查看下一个域名。
1curl -k -o /dev/null --resolve "www.example.com:443:1.1.1.1" -s -v https://www.example.com
出产的细节:
1* SSL connection using AES256-SHA
2* Server certificate:
3* subject: C=IN; L=Chennai; CN=*.example.com; [email protected]
4* start date: 2014-03-26 18:39:20 GMT
5* expire date: 2014-07-04 18:39:20 GMT
6* common name: *.example.com (matched)
7* issuer: C=IN; L=Chennai; CN=*.example.com; [email protected]
8* SSL certificate verify result: self signed certificate (18), continuing anyway.
请注意 主题: 和 到期日期: 字段中的两个证书之间的差异。
试着直接访问IP地址。
1user@droplet~$ curl -k -o /dev/null -s -v https://1.1.1.1
2
3* SSL connection using AES256-SHA
4* Server certificate:
5* subject: C=IN; L=Chennai; CN=*.example.com; [email protected]
6* start date: 2014-03-26 18:39:20 GMT
7* expire date: 2014-07-04 18:39:20 GMT
8* common name: *.example.com (does not match '128.199.206.19')
9* issuer: C=IN; L=Chennai; CN=*.example.com; [email protected]
10* SSL certificate verify result: self signed certificate (18), continuing anyway.
这应该返回 example.com的证书。