码迷,mamicode.com
首页 > Web开发 > 详细

Apache 配置 https

时间:2017-02-16 11:35:01      阅读:471      评论:0      收藏:0      [点我收藏+]

标签:new   blog   ice   and   url   document   this   disco   云服务   

## 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.conf` ,`mods-enabled/*.conf`,`mods-enabled/*.load`,`conf-enabled/*.conf` ,`sites-enabled/*.conf`文件,从文件名称也能看出来,除了`ports.conf`,其他的文件夹名称中包含`-enabled`都代表着在Apache中启用的配置,而`-available`的都为提供的模块但是并不一定已经在用。而且`-enabled`文件夹中的文件都是`-available`文件中的一个软链接。

我们需要启用https,也就是需要使用ssl协议,所以我们需要找到在`mods-available`文件夹中的`ssl.conf`,`ssl.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.conf`和`ssl.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证书。
[阿里云](https://common-buy.aliyun.com/?commodityCode=cas#/buy),[腾讯云](https://www.qcloud.com/product/ssl),都会有提供免费 的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](http://blog.csdn.net/ithomer/article/details/50433363)
[apache 配置https 支持ssl](http://9911505.blog.51cto.com/9901505/1696285)
[自动安装证书](https://certbot.eff.org/#debianjessie-apache)

Apache 配置 https

标签:new   blog   ice   and   url   document   this   disco   云服务   

原文地址:http://www.cnblogs.com/skyfore/p/6404456.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!