q789321 发表于 2018-11-25 10:42:54

Apache2.0实现https

  首先安装SSL,再编译安装APACHE,再配置证书即可
  1. 下载安装apache和openssl
  网址:http://www.apache.org,http://www.openssl.org
  #tar zxvf httpd-2.0.54.tar.gz
  #tar zxvf openssl-0.9.7g.tar.gz
  编译安装openssl,这个软件主要是用来生成证书:
  #cd openssl-0.9.7g
  #./config
  #make
  #make test
  #make install
  把 openssl放进内核目录下,使其在任何目录下都能运行。
  #cd /usr/local/bin
  #ln -s /usr/local/ssl/bin/openssl openssl
  编译安装apache
  #cd /opt/httpd-2.0.54
  #./configure --prefix="/opt/apache2" --enable-so --enable-ssl --with-ssl="/usr/local/ssl/bin"
  #make
  #make install
  2. 安装完毕,生成证书:
  在/opt/apache2/conf下建立一个ssl.key目录
  #cd ../apache2/
  #cd conf/
  #mkdir ssl.key
  然后在该目录下生成证书:
  #cd ssl.key/
  生成服务器私钥:
  #openssl genrsa -des3 -out server.key 1024
  Generating RSA private key, 1024 bit long modulus
  .......................++++++
  .................................................++++++
  e is 65537 (0x10001)
  Enter pass phrase for server.key: //密码
  Verifying - Enter pass phrase for server.key: //确认和上面密码相同
  生成服务器证书请求,并按要求填些相关证书信息:
  #openssl req -new -key server.key -out server.csr
  Enter pass phrase for server.key: //上面的密码
  You are about to be asked to enter information that will be incorporated
  into your certificate request.
  What you are about to enter is what is called a Distinguished Name or a DN.
  There are quite a few fields but you can leave some blank
  For some fields there will be a default value,
  If you enter '.', the field will be left blank.
  -----
  Country Name (2 letter code) :CN
  State or Province Name (full name) :BJ
  Locality Name (eg, city) []:BJ
  Organization Name (eg, company) :163
  Organizational Unit Name (eg, section) []:TD
  Common Name (eg, YOUR name) []:a.test.com//行使 SSL 加密的网站地址。请注意这里并不是单指您的域名,而是直接使用SSL的网站名称 例如:pay.abc.com是一个网站;www.abc.com 是另外一个网站;pay.abc.com 又是另外一个网站。
  Email Address []:lala@corp.netease.com
  Please enter the following 'extra' attributes
  to be sent with your certificate request
  A challenge password []:**********
  An optional company name []:126
  签证:
  # openssl x509 -req -days 700 -in server.csr -signkey server.key -out server.crt
  Signature ok
  subject=/C=AU/ST=Some-State/L=tyl/O=tz/OU=tz/CN=tyl/emailAddress=tangyl@ruyi.com
  Getting Private key
  Enter pass phrase for server.key: //输入创建key时的密码
  3. 修改httpd.conf文件
  Apache 一般有2个版本下载,一个是带SSL模块的,一个是不带SSL的,请首先检查自己的SSL是包含了SSL模块的,否则就没法做了。 Apache的参数配置都在httpd.conf文件中,SSL配置也是如此,此外还可以通过include httpd-ssl.conf来包含一个专门配置SSL的配置文件,如果激活了httpd-include ssl.conf,则可以打开httpd-ssl.conf来配置相应参数。
  一般,我们直接在httpd.conf文件中直接配置SSL参数:
  #加载模块mod_ssl.so,此模块是启用SSL功能必须的。
  LoadModule ssl_module modules/mod_ssl.so //静态编进apache内核的则不需要此段
  #监听443端口
  Listen 443
  #建立一个SSL的虚拟站点,避免SSL配置影响原来HTTP的站点配置
  NameVirtualHost *:443
  NameVirtualHost *:80
  
  ServerName shydserver226_15.easou.com
  DocumentRoot /var/www/html
  ErrorLog /app/soft/apache-search/logs/error.log
  CustomLog /app/soft/apache-search/logs/access.log combined
  
  
  ServerName www.search.com
  DocumentRoot /var/www/html
  ErrorLog /app/soft/apache-search/logs/error.log
  CustomLog /app/soft/apache-search/logs/access.log combined
  SSLEngine on
  SSLCertificateFile /app/soft/apache-search/conf/mycert/server.crt
  SSLCertificateKeyFile /app/soft/apache-search/conf/mycert/server.key
  
  #说明主站是使用HTTP通信的,只有上面虚拟站点有SSL
  SSLEngine off
  配置参数说明如下(完整的SSL配置参数见这里):
  * Listen 443:
  SSL协议监听的端口,同下面 Virtualhost 中的端口需要匹配,SSL协议缺省使用443端口,也有使用8443的情况。
  * SSLEngine on:
  SSL功能打开,如果在Virtualhost 出现这句,则仅作用于虚拟机站点配置范围,这个虚拟机站点全部使用SSL通信,如果出现在Virtualhost外,则作用于全局,整个服务器都使用 SSL(HTTPS)通信,不能采用HTTP通信,所以通常都在Virtualhost中加这句。
  * SSLCertificateFile:证书文件,server.crt
  * SSLCertificateKeyFile:私钥文件,server.key
  * SSLCertificateChainFile:证书链文件,GeoTrust证书除了EV以外,都是单根证书,都不需要配置这个参数,如果是EV
  证书,则需要加入EV的证书链。
  * Errorlog,TranksferLog:日志文件
  * SSLEngine off:整个站点,除了虚拟站点外,关闭SSL,仍然支持HTTP通信
  4. 重新启动Apache
  如果是在Linux下,输入:
  apachectl stop
  apachectl startssl
  Server a.test.com:443 (RSA)
  Enter pass phrase://输入创建证书和密钥时的密码,正确输入后ssl就连同apache一起启动
  在浏览器中输入:https://220.181.75.119
  要求下载证书,OK~https建立就完成了
注意:可以去掉每次启动时要输入证书私钥的 pass phrase#cp server1.keyserver1.key.secure #/usr/bin/openssl rsa -in aserver1.key.secure-out server1.key  5. 客户端证书认证
  (1)基本客户证书认证
  强制客户对整个站点都必须采用客户证书认证的方式,可以在原来的配置下增加如下参数:
  
  ......
  # 需要客户有一个客户端证书,并由ca.cer这个CA证书签署
  SSLVerifyClient require
  SSLVerifyDepth 1
  SSLCACertificateFile conf/ssl.crt/ca.cer
  
  (2)如果只要求对特定目录采用客户证书认证方式,其他目录都不需要客户端证书,则可以采用如下配置:
  
  ......
  # 需要客户有一个客户端证书,并由ca.cer这个CA证书签署
  SSLVerifyClient none
  SSLCACertificateFile conf/ssl.crt/ca.crt
  
  SSLVerifyClient require
  SSLVerifyDepth 1
  
  
  6. 强制HTTPS访问
  强制HTTPS访问,主要通过将用户访问的HTTP请求自动转换为HTTPS请求,有两种方式:一种是通过编程的方式,详见:"强制用户通过SSL访问网站" ; 另一种方式则是通过Apache强大的Rewrite功能。
  如果要针对整个网站全部执行HTTP自动转为HTTPS的功能,可以在httpd.conf文件中,增加:
  RewriteEngine on
  RewriteCond   %{HTTPS} !=on
  RewriteRule   ^(.*)https://%{SERVER_NAME}$1
  如果,仅要求对\secure目录下的内容强制采用HTTPS访问,可以在httpd.conf文件中加入如下语句:
  
  RewriteEngine on
  RewriteCond   %{HTTPS} !=on
  RewriteRule   ^(.*)https://%{SERVER_NAME}/secure/$1
  

页: [1]
查看完整版本: Apache2.0实现https