本地搭建的LAMP环境,使用的域名是在阿里云购买的。本地LAMP环境里搭建了一个私有云NextCloud与一个博客WordPress,通过DDNS实现外网可以访问。
部署Https(SSL)主要是为了访问私有云的时候安全一些吧,有时无聊的时候就这么折腾一下,看看能不能成功。
一、申请SSL证书
首先进入阿里云域名控制台,为本地环境使用的域名申请一个SSL证书。这里申请的是单域名免费证书,申请过程很简单,在之前的博客《阿里云虚拟主机部署https(SSL)记录》已经做好了记录。
申请的免费证书很快就可以审核通过,大概就10分钟左右审核通过。然后进入SSL证书→下载
如下图
下载的时候,注意选择合适自己的环境配置。
下载下来的是一个zip压缩包,解压出来里面有4个文件分别是:
1534736464756.key
1534736464756.pem
chain.pem
public.pem
阿里云的证书安装说明:
1. 证书文件1534736464756.pem,包含两段内容,请不要删除任何一段内容。
2. 如果是证书系统创建的CSR,还包含:证书私钥文件1534736464756.key、证书公钥文件public.pem、证书链文件chain.pem。
( 1 ) 在Apache的安装目录下创建cert目录,并且将下载的全部文件拷贝到cert目录中。如果申请证书时是自己创建的CSR文件,请将对应的私钥文件放到cert目录下并且命名为1534736464756.key;
( 2 ) 打开 apache 安装目录下 conf 目录中的 httpd.conf 文件,找到以下内容并去掉“#”:
#LoadModule ssl_module modules/mod_ssl.so (如果找不到请确认是否编译过 openssl 插件) #Include conf/extra/httpd-ssl.conf
( 3 ) 打开 apache 安装目录下 conf/extra/httpd-ssl.conf 文件 (也可能是conf.d/ssl.conf,与操作系统及安装方式有关), 在配置文件中查找以下配置语句:
# 添加 SSL 协议支持协议,去掉不安全的协议 SSLProtocol all -SSLv2 -SSLv3 # 修改加密套件如下 SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM SSLHonorCipherOrder on # 证书公钥配置 SSLCertificateFile cert/public.pem # 证书私钥配置 SSLCertificateKeyFile cert/1534736464756.key # 证书链配置,如果该属性开头有 '#'字符,请删除掉 SSLCertificateChainFile cert/chain.pem
( 4 ) 重启 Apache。
( 5 ) 通过 https 方式访问您的站点,测试站点证书的安装配置,如遇到证书不信任问题,请查看帮助视频。
二、安装SSL证书
按照阿里云的安装证书说明,根本不适合我的本地LAMP环境。Apache安装目录下conf目录中的httpd.conf文件里,根本找不到mod_ssl.so与httpd-ssl.conf
#LoadModule ssl_module modules/mod_ssl.so (如果找不到请确认是否编译过 openssl 插件)
但我的环境已安装有openssl
[root@localhost ~]# openssl version OpenSSL 1.0.2k-fips 26 Jan 2017 [root@localhost ~]# yum -y update openssl 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.cn99.com * epel: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: centos.ustc.edu.cn * webtatic: sp.repo.webtatic.com No packages marked for update
千万不能生搬硬套这些教程,我在这里折腾了很久。但是不搬教程,又怎么知道怎么搞?
我的LAMP环境是
centOS7.3
Apache2.4
MySQL5.7
PHP7.1
搜索了很多教程资料,发现我的LAMP环境需要安装一个mod_ssl,下面开始进入正题。
1、上传证书至服务器
在Apache的安装目录下创建cert目录
即是:etc/httpd/cert/
然后将解压出来的证书的4个文件
1534736464756.key
1534736464756.pem
chain.pem
public.pem
上传到此目录:etc/httpd/cert/
2、安装mod_ssl
[root@localhost ~]# yum -y install mod_ssl 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.cn99.com * epel: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: centos.ustc.edu.cn * webtatic: sp.repo.webtatic.com 正在解决依赖关系 --> 正在检查事务 ---> 软件包 mod_ssl.x86_64.1.2.4.6-80.el7.centos.1 将被 安装 --> 解决依赖关系完成 依赖关系解决 ================================================================================ Package 架构 版本 源 大小 ================================================================================ 正在安装: mod_ssl x86_64 1:2.4.6-80.el7.centos.1 updates 111 k 事务概要 ================================================================================ 安装 1 软件包 总下载量:111 k 安装大小:224 k Downloading packages: mod_ssl-2.4.6-80.el7.centos.1.x86_64.rpm | 111 kB 00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction 正在安装 : 1:mod_ssl-2.4.6-80.el7.centos.1.x86_64 1/1 验证中 : 1:mod_ssl-2.4.6-80.el7.centos.1.x86_64 1/1 已安装: mod_ssl.x86_64 1:2.4.6-80.el7.centos.1 完毕!
安装好mod_ssl之后,修改ssl.conf
/etc/httpd/conf.d/ssl.conf
首先备份ssl.conf这个配置文件。这里特别注意!不能在/etc/httpd/conf.d/这个目录直接备份文件!
/etc/httpd/conf.d/我当初就是直接在这个目录里备份为ssl (复件).conf
结果Apache无法正常启动,报错如下
[root@localhost ~]# systemctl stop httpd.service [root@localhost ~]# systemctl start httpd.service Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.
看不懂错在哪里,反正又停在这里解决不了。打算放弃的时候,把目录/etc/httpd/conf.d/备份的ssl (复件).conf删掉。然后启动Apache,居然可以正常启动了。谢天谢地啊……
3、修改ssl.conf
文件所在目录:/etc/httpd/conf.d/ssl.conf
# General setup for the virtual host, inherited from global configuration DocumentRoot "/var/www/html" //*把前面的#符号去掉*// ServerName www.sg*****.top:443 //*这里填上自己的域名*// # //*下面照搬阿里的证书参数修改*// # SSL Cipher Suite: # List the ciphers that the client is permitted to negotiate. # See the mod_ssl documentation for a complete list. SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM SSLHonorCipherOrder on # Server Certificate: # Point SSLCertificateFile at a PEM encoded certificate. If # the certificate is encrypted, then you will be prompted for a # pass phrase. Note that a kill -HUP will prompt again. A new # certificate can be generated using the genkey(1) command. SSLCertificateFile /etc/httpd/cert/public.pem # Server Private Key: # If the key is not combined with the certificate, use this # directive to point at the key file. Keep in mind that if # you've both a RSA and a DSA private key you can configure # both in parallel (to also allow the use of DSA ciphers, etc.) SSLCertificateKeyFile /etc/httpd/cert/1534736464756.key # Server Certificate Chain: # Point SSLCertificateChainFile at a file containing the # concatenation of PEM encoded CA certificates which form the # certificate chain for the server certificate. Alternatively # the referenced file can be the same as SSLCertificateFile # when the CA certificates are directly appended to the server # certificate for convinience. SSLCertificateChainFile /etc/httpd/cert/chain.pem
修改好之后保存,然后重启Apache即可生效。
至此本地LAMP环境部署Https(SSL)完成,接下来就需要修改本地服务器里面的站点的https的配置了。
4、完成网站配置
NextCloud直接通过https访问即可,首次访问会提示:通过不被信任的域名访问。解决方法:《NextCloud通过不被信任的域名访问》
WordPress请参考:《阿里云虚拟主机部署https(SSL)记录》这篇博文配置WordPress修改即可。
需要注意的是,本地搭建的MySQL5.7数据库修改的步骤。因为我本地LAMP环境没有安装任何管理数据库的程序与插件,所以直接在centOS终端登录数据库修改。
使用root账户登录数据库
[root@localhost ~]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 736 Server version: 5.7.22 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
显示所有数据库
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | nextcloud | | performance_schema | | sys | | wordpress | +--------------------+ 6 rows in set (0.05 sec)
选择数据库
mysql> use wordpress Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed
显示数据库表
mysql> show tables; +---------------------------+ | Tables_in_wordpress | +---------------------------+ | wp_bp_activity | | wp_bp_activity_meta | | wp_bp_friends | | wp_bp_groups | | wp_bp_groups_groupmeta | | wp_bp_groups_members | | wp_bp_messages_messages | | wp_bp_messages_meta | | wp_bp_messages_notices | | wp_bp_messages_recipients | | wp_bp_notifications | | wp_bp_notifications_meta | | wp_bp_xprofile_data | | wp_bp_xprofile_fields | | wp_bp_xprofile_groups | | wp_bp_xprofile_meta | | wp_commentmeta | | wp_comments | | wp_links | | wp_options | | wp_postmeta | | wp_posts | | wp_signups | | wp_term_relationships | | wp_term_taxonomy | | wp_termmeta | | wp_terms | | wp_usermeta | | wp_users | +---------------------------+ 29 rows in set (0.00 sec)
执行需要修改数据库的命令
mysql> update wp_posts set post_content = REPLACE(post_content,'http://sg*****.top:****','https://www.sg*****.top:****'); Query OK, 40 rows affected (0.08 sec) Rows matched: 180 Changed: 40 Warnings: 0
本地LAMP环境的WordPress部署Https(SSL)所有配置基本完成任务!