如何为 Office web apps 加上 let’s encrypt 证书?
之前部署过一个 Office web apps ,主要是用于与 seafile 进行结合来进行文件在线预览的。后来考虑到安全问题,就统一部署了 let’s encrypt 的证书。中间走了些弯路,但最终还是搞定了,做个笔记。
1、首先从 let’s encrypt 获取 pfx 证书。在这儿推荐使用 acme.sh 来进行统一集中式通配符证书更新,然后再按需进行分发;
2、打开 IIS ,找到服务器证书,然后按提示导入证书;
3、找到 Office web apps 的两个站点 HTTP80 / HTTP809,然后分别在编辑编写的地方将刚才导入的证书与 https 进行绑定;
4、打开 Powershell,运行:
Set-OfficeWebAppsFarm -InternalUrl "https://owa.mydomain.com" -ExternalUrl "https://owa.mydomain.com" –CertificateName "" -EditingEnabled
然后再试试,大功告成。
Edit(201806061716):
发现一个问题,这样绑定后的证书在服务器重启之后,就会自动解除绑定了。研究了很久也没有什么解决办法。无奈只能手动解决了,方法也很简单:
首先手工绑定好证书之后,在命令行下面查到证书哈希和应用程序 ID:
netsh http show sslcert
然后编一个批处理如下:
netsh http add sslcert ipport=0.0.0.0:443 certhash=0000000000000000000000000000000000000000 appid={00000000-0000-0000-0000-000000000000}
netsh http add sslcert ipport=0.0.0.0:810 certhash=0000000000000000000000000000000000000000 appid={00000000-0000-0000-0000-000000000000}
然后建立一个计划任务,让它在系统启动之后运行。就可以了。
Edit(201911141434):
发现有时候重启之后,也会导致证书失效,写过几个脚本都不怎么管用。找到另外一个 powershell 的脚本,测试了下,没问题。
脚本如下:
# 因为是用于 Windows Server 2008 的,所以需要添加下面这个函数,更高版本的 Windows Server 可以不用。
function Import-PfxCertificate {
param([String]$certPath,[String]$certRootStore = "localmachine",[String]$certStore = "My",$pfxPass = $null)
$pfx = new-object System.Security.Cryptography.X509Certificates.X509Certificate2
if ($pfxPass -eq $null) {$pfxPass = read-host "Password" -assecurestring}
$pfx.import($certPath,$pfxPass,"Exportable,PersistKeySet")
$store = new-object System.Security.Cryptography.X509Certificates.X509Store($certStore,$certRootStore)
$store.open("MaxAllowed")
$store.add($pfx)
$store.close()
}
# 查看当前已绑定的证书
#Get-WebBinding -Port 443 -Name "Default Web Site"
# 解除当前绑定的证书
#Get-WebBinding -Port 443 -Name "Default Web Site" | Remove-WebBinding
# Windows Server 2008 以上的系统,可以直接使用这条命令导入指定证书
#Import-PfxCertificate -FilePath c:\certfile\mydomain.com.pfx -Password (ConvertTo-SecureString -String 'cert_password' -AsPlainText -Force) -CertStoreLocation cert:\LocalMachine\my
$port = 443
$filedir = 'c:\certfile'
$FilePath = 'c:\certfile\mydomain.com.pfx'
$Password = ConvertTo-SecureString -String 'cert_password' -AsPlainText -Force
$website = 'HTTP80'
$webDomain = ''
$secDomain = ''
cd $filedir
rm $FilePath
# 使用 ftp 获取新证书
.\wget ftp://user:user@ftp.mydomain.com/mydomain.com.pfx
#关闭website
Stop-Website -Name $website
#关闭https
Get-WebBinding -Port $port -Name $website | Remove-WebBinding
#删除证书绑定
Remove-Item IIS:\SslBindings\*
#删除导入的证书
Get-ChildItem cert:\LocalMachine\My | Where-Object { $_.Subject -like "CN=*$secDomain*" } |ForEach-Object {
$store = Get-Item $_.PSParentPath
$store.Open('ReadWrite')
$store.Remove($_)
$store.Close()
}
#导入证书
Import-PfxCertificate $filepath "LocalMachine" "My" "mydomain.com"
#启动https
New-WebBinding -Name $website -IPAddress "*" -Port $port -HostHeader $webDomain -Protocol https
#绑定证书
Get-Item -Path "cert:\localmachine\my\*" | Where-Object { $_.Subject -like "CN=*$secDomain*" } | New-Item -Path IIS:\SslBindings\0.0.0.0!$port
#启动website
Start-Website -Name $website