Apache 配置 https

7 篇文章 0 订阅

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
自动安装证书

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值