为了推动更安全的HTTPS加密协议普及全网,谷歌Chrome从2017年开始逐步对HTTP网站标记"不安全"警告,并提示"你与此网站之间建立的连接不安全"。在Chrome 68版本中,对所有HTTP网站标记"不安全",帮助用户了解与网站之间的连接何时不安全,同时激励网站所有者提高其网站的安全性。 此外,某些版本的chrome还结合一组互补字符串使用,让用户更容易理解安全标识的含义。对于有效HTTPS:显示"secure" 和"https",配合绿色安全锁对于HTTP:显示"http"和"site not secure",配合黑色圆圈对于无效HTTPS:显示"not secure"and "site not secure",配合红色三角警告 对于一些个人博客来说,虽然不存在注册、交易等可能泄露个人隐私信息的情况,但是也建议为网站申请和部署SSL证书(免费的即可),毕竟即使是不安全提示也会吓退一部分用户。 本文记录一下如何在阿里云服务器上部署SSL证书,希望对您有所帮助。 首先访问阿里云SSL证书-免费证书(证书申请请参考:如何申请阿里云SSL证书?),根据你的web服务器类型,下载对应的证书。我选择的是Apache服务器,这里需要根据实际情况进行选择。 证书由3个文件组成,分别为:证书文件:以xxx_public.crt为后缀或文件类型。 证书链文件:以xxx_chain.crt为后缀或文件类型。 密钥文件:以xxx.key为后缀或文件类型。 证书下载完成之后,就可以开始部署SSL证书了。开放端口443 由于HTTPS使用443端口,因此首先需要配置一下阿里云安全组的访问规则,放开443端口。 配置入口:https://ecs.console.aliyun.com/#/server/i-j6cagiqhn4rzss2r6jjj/group/group?regionId=cn-hongkong 最简单的方法就是复制一下80端口的规则,然后将端口号改为443即可。 安装mod_ssl 对于CentOS 7请使用yum命令安装mod_ssl模块,如果系统为CentOS 8/RockyLinux 8/AlmaLinux8等,可以使用dnf命令。# 安装mod_ssl模块 yum -y install mod_ssl # 为了使能mod_ssl,需要重新启动Apache systemctl restart httpd # 使用httpd或者apachectl检查是否使能了mod_ssl,如果输出ssl_module(shared),那么配置成功。 httpd -M | grep ssl apachectl -M | grep ssl ssl_module (shared) 输入httpd -S 可以看到Apache当前的配置信息(当前仅显示虚拟主机设置) 其中 *:443 localhost (/etc/httpd/conf.d/ssl.conf:40) 表示当前已经在localhost监听443端口。# httpd -S VirtualHost configuration: *:80 is a NameVirtualHost default server domain1.com (/etc/httpd/conf.d/domain1.com.conf:1) port 80 namevhost domain1.com (/etc/httpd/conf.d/domain1.com.conf:1) alias www.domain1.com port 80 namevhost domain2.com (/etc/httpd/conf.d/domain1.com.conf:17) alias www.domain2.com *:443 localhost (/etc/httpd/conf.d/ssl.conf:40) ServerRoot: "/etc/httpd" Main DocumentRoot: "/var/www/html" Main ErrorLog: "/etc/httpd/logs/error_log" Mutex lua-ivm-shm: using_defaults Mutex ssl-stapling: using_defaults Mutex proxy: using_defaults Mutex authn-socache: using_defaults Mutex ssl-cache: using_defaults Mutex default: dir="/etc/httpd/run/" mechanism=default Mutex cache-socache: using_defaults Mutex authdigest-opaque: using_defaults Mutex watchdog-callback: using_defaults Mutex proxy-balancer-shm: using_defaults Mutex rewrite-map: using_defaults Mutex ssl-stapling-refresh: using_defaults Mutex authdigest-client: using_defaults PidFile: "/etc/httpd/run/httpd.pid" Define: DUMP_VHOSTS Define: DUMP_RUN_CFG User: name="apache" id=48 Group: name="apache" id=48 mod_ssl配置文件位于下面两个文件中:/etc/httpd/conf.d/ssl.conf /etc/httpd/conf.modules.d/00-ssl.conf 其中 00-ssl.conf 的内容只有一行,表示加载mod_ssl模块:LoadModule ssl_module modules/mod_ssl.so ssl.conf 则是SSL配置文件拷贝证书 将证书拷贝到服务器上的Apache安装目录中,对于YUM安装的Apache来说,安装目录位于:/etc/http。我们创建一个cert目录(目录名称随意)专门用来存放证书。 根据[https://serverfault.com/questions/216477/what-should-be-the-permissions-of-apache-ssl-directory-certificate-and-key]:cert目录和证书文件的属主应该为root(root)。cert目录的权限应该设置为700,证书文件的权限应该设置为600。 之所以需要写权限是为了定期更新证书。如果手动更新的话,目录权限可以设置为500,证书权限可以设置为400。# chown -R root:root /etc/httpd/cert # mkdir /etc/httpd/cert # chmod 700 /etc/httpd/cert/配置SSL证书 在 /etc/httpd/conf.modules.d/00-ssl.conf 文件(也可以在/etc/httpd/conf.d/ssl.conf文件或者创建一个新的.conf文件)中添加以下内容: 注意:如果证书包含多个域名,复制...,并将ServerName修改为其它域名。LoadModule ssl_module modules/mod_ssl.soServerName www.haitaolab.com DocumentRoot /var/www/html SSLEngine on SSLProtocol all -SSLv2 -SSLv3 SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM SSLHonorCipherOrder on SSLCertificateFile /etc/httpd/cert/7772727_www.haitaolab.com_public.crt SSLCertificateKeyFile /etc/httpd/cert/7772727_www.haitaolab.com.key SSLCertificateChainFile /etc/httpd/cert/7772727_www.haitaolab.com_chain.crt ServerName haitaolab.com DocumentRoot /var/www/html SSLEngine on SSLProtocol all -SSLv2 -SSLv3 SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM SSLHonorCipherOrder on SSLCertificateFile /etc/httpd/cert/7772727_www.haitaolab.com_public.crt SSLCertificateKeyFile /etc/httpd/cert/7772727_www.haitaolab.com.key SSLCertificateChainFile /etc/httpd/cert/7772727_www.haitaolab.com_chain.crt 此时如果访问https://haitaolab.com的话,应该可以正常访问,并且带有一把小锁。 在[https://www.digitalocean.com/community/questions/apache-non-www-to-www-https-secure-and-let-s-encrypt]中还介绍了一种方法,下一步就是将http链接的访问永久重定向到https链接。执行301永久重定向 重定向有两种方法:服务器虚拟主机配置文件.htaccess文件 (1)服务器虚拟主机配置文件 老外的大多数教程都使用了下面这种方法,即在主配置文件添加下面的配置sudo vi /etc/httpd/conf/httpd.conf ServerAdmin admin@domain.co.uk ServerName www.domain.co.uk ServerAlias domain.co.uk DocumentRoot /var/www/html/domain Options FollowSymLinks AllowOverride All Require all granted # Redirect non-www to www RewriteEngine On RewriteCond %{HTTP_HOST} !^www. [NC] RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined Include /etc/letsencrypt/options-ssl-apache.conf SSLCertificateFile /etc/letsencrypt/live/domain.co.uk/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/domain.co.uk/privkey.pem ServerName www.example.com Redirect "/" "https://www.example.com/" 不过,我觉得单独创建一个配置文件更好一些,例如在 httpd/conf.d 目录中创建一个 haitaolab.conf 配置文件,内容为:ServerName www.haitaolab.com Redirect permanent "/" "https://www.haitaolab.com/" 注意:permanent 的意思是301永久重定向。 (2).htaccess 也可以通过在.htaccess文件中添加规则进行重定向,这种方法需要 使能/enable 读取.htaccess文件,Apache默认是禁止的。 这是因为考虑到安全性,强烈建议将根目录的AllowOverride属性设置为 Override=None。# # AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # Options FileInfo AuthConfig Limit # AllowOverride None 如果允许.htaccess文件,那么可以这样配置重定向RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://example.com/$1 [L,R=301] 也有这种写法RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L] Apache配置文件检测# 检查配置文件是否合法 apachectl configtest # 重新加载配置文件 systemctl reload httpd 更多介绍请参考:https://www.cyberciti.biz/faq/apache-2-reload-httpd-config-file-unix-linux-command/SSL证书检测 https://www.ssllabs.com/ssltest/ https://www.thesslstore.com/ssltools/ssl-checker.php#results 参考https://help.aliyun.com/document_detail/98727.htmlhttps://linuxconfig.org/how-to-install-mod-ssl-on-redhat-8https://www.ssldragon.com/blog/how-to-install-an-ssl-certificate-on-apache/https://www.thesslstore.com/ssltools/ssl-checker.phphttps://hrefgo.com/blog/redirect-http-to-https#%E5%A6%82%E4%BD%95%E5%9C%A8_Apache_%E4%B8%AD%E8%BF%9B%E8%A1%8C_HTTP_%E9%87%8D%E5%AE%9A%E5%90%91https://hbayraktar.medium.com/how-to-install-ssl-certificate-on-apache-for-centos-7-38c25b84d8b1https://developers.google.com/search/docs/advanced/crawling/301-redirects?hl=zh-cn#permanent-server-side-redirectshttps://www.namecheap.com/support/knowledgebase/article.aspx/10313/33/ssl-certificate-installation-on-httpd-centos/https://www.cnblogs.com/kevingrace/p/9565123.htmlhttps://www.digitalocean.com/community/tutorials/how-to-create-an-ssl-certificate-on-apache-for-centos-7