centos6-7下apache使用let's encrypt免费证书

211 阅读1分钟

0、准备工作(已经安装可以不需要再安装)

yum install git

yum install zlib-devel

yum install bzip2-devel

yum install openssl-devel

yum install ncurses-devel

yum install sqlite-devel

cd/usr/local/srcwget www.python.org/ftp/python/…

tar -zxvf Python-2.7.12.tar.xz

#编译python

cd Python-2.7.12/

./configure --prefix=/usr/local/python2.7

make && make install

ln -s /usr/local/python2.7/bin/python2.7 /usr/local/bin/python

#解决系统 Python 软链接指向 Python2.7 版本后,yum是不兼容 Python 2.7的,所需要指定 yum 的Python版本

vi /usr/bin/yum 将头部的

#!/usr/bin/python 改成 #!/usr/bin/python2.6.6 2、Apache安装(自行度娘) 3、生成密钥文件

git clone github.com/letsencrypt…

#进入letsencrypt目录(/usr/local/src/letsencrypt)cdletsencrypt

#生成证书

./letsencrypt-auto certonly --standalone --email abc@163.com -d ******

在/etc/letsencrypt/live/******(自己的域名) 下面看到privkey.pem和fullchain.pem

4、配置到Apache服务器

证书生成完毕后,可以在 /etc/letsencrypt/live/ 目录下看到对应域名的文件夹找到证书。 这时候我们的第一生成证书已经完成了,接下来就是配置我们的web服务器,启用HTTPS。 Apache需要安装ssl插件,然后在/etc/httpd/conf.d/ssl.conf 底部里面进行配置,如下例。

<VirtualHost 0.0.0.0:443>
        DocumentRoot "/var/www/html"
        ServerName www.example.com
        SSLEngine on
        SSLCertificateFile /etc/letsencrypt/live/www.example.com/cert.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
        SSLCertificateChainFile /etc/letsencrypt/live/www.example.com/chain.pem
</VirtualHost>

5、证书有效期为90天,需要延期

./letsencrypt-auto certonly --renew-by-default --email abc@163.com -d ******

一条命令更新所有服务端的证书

./letsencrypt-auto renew

6、查看证书有效期的命令

openssl x509 -noout -dates -in /etc/letsencrypt/live/demo.baidu.com/cert.pem

7、设置crontab自动更新证书

00 05 01 * * /usr/local/src/letsencrypt/letsencrypt-auto certonly --renew-by-default --email 74****@.com -d demo.baidu.com

8、HTTP 80 强制转 HTTPS

网站根目录下,新建 .htaccess

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]