Linux apache自建证书搭建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>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库