本文将对Nginx/Tengine,Apache,Tomcat的服务器安装证书方式予以描述,本文将以在腾讯服务器服务器上购买的证书为例,所使用的环境均是Centos 7,另证书名称为www.example.com为例
1.Nginx服务器证书安装
1.进入到nginx.conf文件所在的conf文件夹,本服务器操作是:cd /usr/local/openresty/nginx/conf
2.将在腾讯获取的证书文件1_www.example.com_bundle.crt私钥文件2_www.example.com.key文件拷贝到cong所在的文件夹下
3.配置nginx.conf文件
server {
#SSL 访问端口号为 443
listen 443 ssl;
#填写绑定证书的域名
server_name www.example.com;
#证书文件名称
ssl_certificate 1_www.example.com_bundle.crt;
#私钥文件名称
ssl_certificate_key 2_www.example.com.key;
ssl_session_timeout 5m;
#请按照以下协议配置
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location / {
#网站主页路径。此路径仅供参考,具体请您按照实际目录操作。
root /home/nginx-rtmp-module-master/test/www;
index index.html index.htm;
}
}
--------以下配置为将http请求自动跳转至https------
server {
listen 80;
#填写绑定证书的域名
server_name www.example.com;
#把http的域名请求转成https
return 301 https://$host$request_uri;
}
还有在nginx的配置里边若是配置了http自动跳转https则需要把在原来在http上配置的内容剪切到https的部分,注意是剪切,比如定义路径的那些东东。
4.检查nginx配置文件,重启服务
[root@Aliyun ~]# nginx -t
[root@Aliyun ~]# nginx -s reload
2.Apache服务器证书安装
1.新建目录,将证书文件放进去
[root@Aliyun ~]# mkdir /etc/httpd/ssl (如果有就不用新建)
2.在 /etc/httpd/conf 目录下的 httpd.conf 配置文件找到 Include conf.modules.d/*.conf 去掉前边的#号
3.安装ssl模块
[root@Aliyun ~]# yum install mod_ssl
4.在 /etc/httpd/conf.modules.d 目录下的 00-ssl.conf 配置文件找到 LoadModule ssl_module modules/mod_ssl.so 去掉前边的#号
5.修改etc/httpd/conf.d/ssl.conf中的如下内容
<VirtualHost 0.0.0.0:443>
DocumentRoot "/var/www/html"
#填写证书名称
ServerName www.example.com
#启用 SSL 功能
SSLEngine on
#证书文件的路径
SSLCertificateFile /etc/httpd/ssl/2_www.example.com.crt
#私钥文件的路径
SSLCertificateKeyFile /etc/httpd/ssl/3_www.example.com.key
#证书链文件的路径
SSLCertificateChainFile /etc/httpd/ssl/1_root_bundle.crt
</VirtualHost>
6.http请求自动跳转至https
1)编辑 /etc/httpd/conf 目录下的 httpd.conf
查找该配置文件中是否存在LoadModule rewrite_module modules/mod_rewrite.so 若有,去掉其前边的#号注释;若不存在则在/etc/httpd/conf.modules.d/的目录下新建00-rewrite.conf文件,并在里边加入LoadModule rewrite_module modules/mod_rewrite.so
2)在/etc/httpd/conf/httpd.conf中添加以下内容
<Directory "/var/www/html">
# 新增
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)?$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
</Directory>
7.重启服务进行验证
3.Tomcat服务器证书安装
1.将www.example.com.jks密钥库文件从本地目录拷贝至/tomcat9/conf/目录下
2.编辑/tomcat9/conf/server.xml文件,添加以下内容:(定义加密之后访问的端口是8443)
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
#证书保存的路径
keystoreFile="/tomcat/conf/www.example.com.jks"
#密钥库密码
keystorePass="******" #注意密码两侧有双引号
clientAuth="false"/>
其中clientAuth参数:如果设为 true,表示 Tomcat 要求所有的 SSL 客户出示安全证书,对 SSL 客户进行身份验证。
3.启动服务
[root@Aliyun bin]# ./startup.sh
-------------以上已经配置好证书已经可以使用,以下为配置http自动跳转至https(可选)----------------
4.编辑/tomcat9/conf/web.xml文件,在《/welcome-file-list》标签下边换行添加:
<login-config>
<!-- Authorization setting for SSL -->
<auth-method>CLIENT-CERT</auth-method>
<realm-name>Client Cert Users-only Area</realm-name>
</login-config>
<security-constraint>
<!-- Authorization setting for SSL -->
<web-resource-collection>
<web-resource-name>SSL</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
5.编辑/tomcat9/conf下的server.xml文件,修改为8443端口:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
6.执行命令检查配置文件是否存在问题
[root@Aliyun bin]# ./configtest.sh
7.启动
[root@Aliyun bin]# ./startup.sh