IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> Apache 配置 https -> 正文阅读

[网络协议]Apache 配置 https

Apache 配置 https

Apache 版本: 2.4.10
Linux 版本 : Debian

安装Apache

控制台命令:sudo apt-get install apache2
安装好了Apache2会自动启动,但是自动启动的不包含https仅仅是http

默认的配置路径

Apache配置文件路径: cd /etc/apache2/
Apache默认日志路径: cd /var/log/apache2

配置https

####首先
进入Apache的配置文件目录
cd /etc/apache2/

查看目录结构
tree

具体的目录结构如下

apache2.conf
conf-available
conf-enabled
envvars
magic
mods-available
ports.conf
sites-available
sites-enabled

其中 ** apache2.conf** 是整个Apache的主配置文件,
部分代码

# Include module configuration:
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

# Include list of ports to listen on
Include ports.conf


# Sets the default security model of the Apache2 HTTPD server. It does
# not allow access to the root filesystem outside of /usr/share and /var/www.
# The former is used by web applications packaged in Debian,
# the latter may be used for local directories served by the web server. If
# your system is serving content from a sub-directory in /srv you must allow
# access here, or in any related virtual host.
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>

<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>

<Directory /var/www/>
Options Indexes FollowSymLinks
nclude module configuration:
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

# Include list of ports to listen on
Include ports.conf

从代码可以看出配置文件主要就是引入了 ports.confmods-enabled/*.confmods-enabled/*.loadconf-enabled/*.confsites-enabled/*.conf文件,从文件名称也能看出来,除了ports.conf,其他的文件夹名称中包含-enabled都代表着在Apache中启用的配置,而-available的都为提供的模块但是并不一定已经在用。而且-enabled文件夹中的文件都是-available文件中的一个软链接。

我们需要启用https,也就是需要使用ssl协议,所以我们需要找到在mods-available文件夹中的ssl.confssl.load,然后把这两个文件的软链接到mods-enabled中,这代表着在Apache中启用ssl模块
/etc/apache2/目录下:

ln -s ./mods-available/ssl.conf ./mods-enabled/ssl.conf
ln -s ./mods-available/ssl.load ./mods-enabled/ssl.load

然后在mods-enabled目录下就能看到ssl.confssl.load这两个文件了。

新建一个目录用来存放自己的证书文件
mkdir ssl && cd ssl

开始证书制作:

生成2048位的加密私钥
openssl genrsa -out server.key 2048

生成证书签名请求(CSR)
openssl req -new -key server.key -out server.csr
在这一步当中会要求输入一些信息,比如国家,城市,这些都不重要,重要的是** Common Name** 这个需要输入你想要把证书用在什么域名是 比如我的就是 www.notrue.cn 然后这个后面的配置有关系。好像也可以写通配符,但是我没尝试过有兴趣的可以去试试。

生成类型为X509的自签名证书。有效期设置3650天,即有效期为10年
openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt

####修改vhost

这个在sites-enabled文件夹的000-default.conf文件当中
vim 000-default.conf

代码:

<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request\'s Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>

可以看到这里面只是配置了一个普通的80端口的虚拟主机,也就是http请求,我们需要做的就是配置一个https的虚拟主机
在文件末尾添加

<VirtualHost *:443>
ServerName www.notrue.cn
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/2_www.notrue.cn.crt
SSLCertificateKeyFile /etc/apache2/ssl/3_www.notrue.cn.key
</VirtualHost>

其中的ServerName填入你刚才制作证书的时候的Common Name,然后保存

重启你的Apache
service apache2 restart

查看状态
service apache2 status
如果不出意外的话应该就是显示运行状态为** active (running) ** 。然后就可以访问了。
域名前面需要加https://(PS:会有一个×,这是因为没有CA认证)

加入CA认证

一般的话国内各大云服务商都有免费的CA证书。
阿里云腾讯云,都会有提供免费 的CA证书,然后你可以申请,记住域名修改了的话在Apache中000-default.conf文件中的VirtualHost中的ServerName也需要做相应的修改,然后你就可以云服务器商给你的文件上传到服务器上面去,并且在Apache配置中给添加上去,比如我的就是

<VirtualHost *:443>
ServerName www.notrue.cn
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/2_www.notrue.cn.crt
SSLCertificateKeyFile /etc/apache2/ssl/3_www.notrue.cn.key
SSLCertificateChainFile /etc/apache2/ssl/1_root_bundle.crt
</VirtualHost>

然后现在在浏览器访问的时候地址栏的https那儿就不会有一个×了。

遇到的问题

遇到了很多问题,最主要的还是自己CA制作证书,因为不懂,所以在000-default.conf文件中写成了

<VirtualHost *:443>
ServerName localhost
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/2_www.notrue.cn.crt
SSLCertificateKeyFile /etc/apache2/ssl/3_www.notrue.cn.key
</VirtualHost>

然后就一直提示我ServerName和公钥当中的ID不对,后面也是看了其他的人才知道。而且网上其他的人都没有用这个版本,或者说没有这个版本的教程,所以自己去看apache2.conf才知道整个Apache的文件结构,然后再根据自己的常识去改。

还有,在ssl.conf中注释掉

#SSLSessionCache dbm:${APACHE_RUN_DIR}/ssl_scache
#SSLSessionCache shmcb:${APACHE_RUN_DIR}/ssl_scache(512000)
#SSLSessionCacheTimeout 300

这三行,(前面加** # **表示注释),因为不注释的话会报错,报错一个模块没有引入。因为我还并不是太需要这个Cache所以就没管,就直接注释掉了.

应该就是这些问题了。。。

参考链接

Apache 使用ssl模块配置HTTPS
apache 配置https 支持ssl
自动安装证书

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2022-10-31 12:34:57  更:2022-10-31 12:38:39 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 14:52:59-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码