0%

Apache配置http跳转https

Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由于其多平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩展,将Perl/Python等解释器编译到服务器中。

安装ssl模块

1
yum install -y mod_ssl

可以看到会生成/etc/httpd/conf.modules.d/00-ssl.conf文件,文件内容为:

LoadModule ssl_module modules/mod_ssl.so

上传SSL证书文件

文件包含私钥、公钥,例如

  • 2022-zhaohongye.com.pem
  • 2022-zhaohongye.com.key

修改配置文件

/etc/httpd/conf.d/projectapi.conf

1
2
3
4
5
<VirtualHost *:80>
ServerName projectapi.zhaohongye.com
RewriteEngine on
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</VirtualHost>

/etc/httpd/conf.d/projectapi-https.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<VirtualHost *:80>
ServerName projectapi.zhaohongye.com
RewriteEngine on
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</VirtualHost>
[root@pm-prd-php-0-69 conf.d]# pwd
/etc/httpd/conf.d
[root@pm-prd-php-0-69 conf.d]# cat projectapi-https.conf
<VirtualHost *:443>
ServerName projectapi.zhaohongye.com
DocumentRoot /data/app/izu-project-api
CustomLog logs/projectapi.log combined
DirectoryIndex index.php
SSLEngine on
SSLCertificateFile /data/servers/nginx/ssl/2022-zhaohongye.com.pem
SSLCertificateKeyFile /data/servers/nginx/ssl/2022-zhaohongye.com.key
</VirtualHost>
<Directory "/data/app/izu-project-api">
Require all granted
</Directory>

验证效果

1
2
3
4
5
6
7
8
9
~ curl projectapi.zhaohongye.com

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://projectapi.zhaohongye.com/">here</a>.</p>
</body></html>

参考博客地址:https://blog.csdn.net/ranrancc_/article/details/98985023