Linux apache自建证书搭建https

时间:2019-08-28
本文章向大家介绍Linux apache自建证书搭建https,主要包括Linux apache自建证书搭建https使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

前言

         搭建https有两种方式,分为单向认证和双向认证。单向认证就是传输的数据加密过了,但是不会校验客户端的来源,也就只有客户端验证服务端证书。
 
单向认证

1、安装mod_ssl

yum -y install mod_ssl

2、HTTP 服务器上配置mod_ssl

2.1、进入http服务器配置文件所在目录

cd /etc/pki/tls/certs/

2.2、建立服务器密钥

[root@fee6202a726e certs]# make server.key           #建立服务器密钥
umask 77 ; \
/usr/bin/openssl genrsa -aes128 2048 > server.key
Generating RSA private key, 2048 bit long modulus
...............................+++
........+++
e is 65537 (0x10001) 
Enter pass phrase:                        #设置一个口令
Verifying - Enter pass phrase:             #确定口令
[root@fee6202a726e certs]#

[root@fee6202a726e certs]# openssl rsa -in server.key -out server.key    #从密钥中删除密码(以避免系统启动后被询问口令)

2.3、建立服务器公钥

[root@fee6202a726e certs]# make server.csr              #生成服务器公钥
umask 77 ; \
/usr/bin/openssl req -utf8 -new -key server.key -out server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN                       #填入国家代码 例如CN
State or Province Name (full name) []:Shanghai              #省 例如 Shanghai
Locality Name (eg, city) [Default City]:Shanghai            #市 例如 Shanghai
Organization Name (eg, company) [Default Company Ltd]:NOC   #组织名  例如NO  (任意)
Organizational Unit Name (eg, section) []:Newegg            #组织单位名 例如 New (任意)
Common Name (eg, your name or your server's hostname) []:NOC  #通用名 例如 NO (任意)
Email Address []:noc@newegg.com                               # 电子邮箱

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:                             # 不填,直接回车
An optional company name []:                         # 不填,直接回车
[root@fee6202a726e certs]# 

2.3、建立服务器证书

openssl x509 -in server.csr -out server.pem -req -signkey server.key -days 365

Signature ok subject=/C=CN/ST=Shanghai/L=Shanghai/O=NOC/OU=Newegg/CN=NOC/emailAddress=noc@newegg.com Getting Private key

chmod 400 server.* # 修改权限为400

2.4、设置SSL

vim /etc/httpd/conf.d/ssl.conf
# General setup for the virtual host, inherited from global configuration DocumentRoot "/var/www/html" # 去掉此参数的注释‘#’

systemctl restart httpd # 重启httpd 服务,

验证:  通过https 打开网页查看是否成功

代理(强制http请求跳转到https)

<Directory "/var/www/html">     # 此类容最后增加下面三行
RewriteEngine on # 开启重定向擎设置为on,就是让url重写生效
RewriteCond %{SERVER_PORT} !^443$ # 设置规则,端口重定向成 443 端口
RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R] # ^(.*)?$是一个正则表达式,意思是对所有请求都重定向到https://....


其它代码解释

RewriteCond %{REQUEST_FILENAME} !-f          #如果文件存在,就直接访问文件,不进行下面的RewriteRule.

RewriteCond %{REQUEST_FILENAME} !-d          # 如果目录存在就直接访问目录不进行RewriteRule

RewriteCond $1 !^(index\.php|images|robots\.txt)    #配置url重写规则,!^(index\.php|images|robots\.txt) 这个正则表达式指明了哪些文件不需要重                                                         写,而是直接访问;

 

zabbix 修改https 并实现http跳转到https

vim /etc/httpd/conf/httpd.conf    # 添加如下内容

<Directory "/usr/share/"> # 这里的路径是 zabbix 的路径 /usr/share/zabbix Options Indexes FollowSymLinks Require all granted RewriteEngine on RewriteCond %{SERVER_PORT} !^443$ RewriteRule (.*) https://%{SERVER_NAME}/$1 [R] #RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R] </Directory>

原文地址:https://www.cnblogs.com/surplus/p/11423258.html