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.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`

生成类型为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>

重启你的Apache
`service apache2 restart`

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

```

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

### 遇到的问题

```
然后就一直提示我`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`所以就没管,就直接注释掉了。

应该就是这些问题了。

猜你喜欢

转载自www.linuxidc.com/Linux/2017-02/140801.htm