## 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)