关于ubuntu服务器+apache2 组合配置网站免费ssl : letsencrypt 在网上找了很多资料,东拼西凑最终解决了问题,这里讲过程记录下来,方便后续查阅,也方便又需要的同仁。
在介绍具体的配置之前我们先简单了解下ubuntu系统(不同的linux系统可能存在细微差别)下Apache 配置目录和文件。
/etc/apache2 是存放所有apache相关配置文件的地方,一个最核心的文件就是 apache2.conf, 简单看下该文件的核心配置:
# Include module configuration:
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
# Include list of ports to listen on
Include ports.conf
# access here, or in any related virtual host.
Options FollowSymLinks
AllowOverride None
Require all denied
AllowOverride None
Require all granted
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
AccessFileName .htaccess
# Include generic snippets of statements
IncludeOptional conf-enabled/*.conf
# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf
这是我从我的ubuntu服务器上拷贝出来的,这个conf文件相对于其他linux系统的httpd.conf要简短得多,因为它把一些模块声明文件和配置文件分散到了其它目录了,比如它包含了mods-enabled目录下所有的.load文件和.conf配置文件。所以如果我们要修改具体的配置和开启/禁用某些模块加载可能就需要先找到对应的文件才行,这也是很多新手比较头疼的地方。
/etc/apache2/ports.conf 端口监听配置
/etc/apache2/mods-available/ssl.load ssl加载模块
/var/www/html/.htaccess 网站全局跳转配置
如果想了解letsencrypt 可以看他们的网站:https://letsencrypt.org/ 免费ssl,目前了解到证书有效期是90天,90天后需要重新跑这个流程,当然续期有些操作可以省掉。
1. 首先第一步安装 letsencrypt :
sudo apt-get install letsencrypt
2. 生成证书:
letsencrypt certonly --renew-by-default --email [email protected] -d yumysoft.com -d www. yumysoft.com
certonly 表示只颁发证书
--email : 你的邮箱地址
-d : 指定希望应用https的网站地址,可以同时制定多个
执行命令后会出如下提示:
点击ok,正常情况下会提示:
Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/domain.com/fullchain.pem...
说明证书生成成功,默认会保存在/etc/letsencrypt/live/domain.com 目录下.
3. 使用证书:
找到ssl配置文件配置VirtualHost,这个在ubuntu 系统下有点特殊,网上找到的资料基本说了ssl.conf, httpd-ssl.conf 这几个文件都没有找到,通过摸索,发现apache端口定义都放在 /etc/apache2/ports.conf文件中:
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf
Listen 80
Listen 443
而且这个文件头部明显说明了配置VirtualHost的地方在/etc/apache2/sites-enabled/000-default.conf这个文件去配置, 所有找到这个文件,我吗看到了80端口的监听配置,在后面追加如下443端口配置:
DocumentRoot "/var/www/html"
ServerName www.yumysoft.com
ServerAlias yumysoft.com
SSLEngine on
SSLProtocol TLSv1 TLSv1.1 TLSv1.2
SSLCertificateFile /etc/letsencrypt/live/yumysoft.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/yumysoft.com/privkey.pem
目前实测,配置这些参数已经可以实现ssl安全访问网站,更多更全参数参考如下:
DocumentRoot "C:/phpstudy/WWW"
ServerName www.youdomain.cn
ServerAlias youdomain.cn
SSLEngine on
SSLProtocol TLSv1 TLSv1.1 TLSv1.2
SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
SSLCertificateFile "C:/phpstudy/Apache/conf/2_www.youdomain.cn.crt"
SSLCertificateKeyFile "C:/phpstudy/Apache/conf/3_www.youdomain.cn.key"
SSLCertificateChainFile "C:/phpstudy/Apache/conf/1_root_bundle.crt"
Options FollowSymLinks ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
从其他地方拷贝过来的,具体效果可自行研究验证.
重启apache
配置完后,重启apache:
service apache2 restart
done, perfect!!!
错误解决:
在执行apache重启命令的时候可能碰到如下错误提醒:
Invalid command 'SSLEngine', perhaps misspelled or defined by a module not included in the server configuration
导致这个问题是因为没有启用ssl模块,执行如下命令重启apache可解决问题:
a2enmod ssl
service apache2 restart
关于http自动跳转https
我们可能还希望用户在输入http浏览时自动重定向到https安全服务, 首先启动重定向:
sudo a2enmod rewrite
或者
sudo ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load
有以下几种做法:
- 修改网站跟目录 .htaccess文件,实现自动跳转, 一般wordpress网站会有一个默认配置
RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.* https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
- 修改 /etc/apache2/apache2.conf 文件:
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R]
- 打开 /etc/apache2/sites-available/000-default.conf
在 <\VirtualHost *:80><\VirtualHost> 标签内随便一个地方加入以下三行
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}$1 [L,R]
more...
到最后发现有的文章指出可以通过certbot工具在生成证书的时候制定生成ssl.conf文件,如下:
certbot-auto certonly --apache --apache-le-vhost-ext/etc/httpd/conf.d/ssl.conf -d mj.AA.com.cn -d game.aa.com.cn
效果如何,待君验证
参考资料:### 征服 Apache + SSL
ubuntu16.04服务器apache的ssl证书配置
https://docs.woocommerce.com/document/ssl-and-https/