奇忠诚 发表于 2018-11-20 13:16:55

Windows系统中如何配置Apache的SSL加密访问

系统事件日志提示:ssl里的server.crt文件不存在或者为空。该怎么办?
  http://www.server110.com/apache/201404/9462.html
  到Apache的网站上下载包含有SSL服务的安装包。
  如果你已经安装过了Apache,在安装含有SSL服务的Apache服务器之前需要卸载已经安装过的。到控制面板里面直接卸载就可以了。 注意:Apache在卸载的时候不会删除httpd.conf这个配置文件和htdocs文件夹下面的内容。如果你确实不需 要,你需要手工删除。
  在Windows的环境下面安装含有SSL的Apache和不含有 Apache的方法和过程一致。
  这里关键说安装以后的服务配置。
  httpd.conf文件,如果需要Apache服务器支持SSL,这里需要载入几个模块。
  去掉下面两行之前的#符号
  LoadModule ssl_module modules/mod_ssl.so
  Include conf/extra/httpd-ssl.conf
  LoadModule ssl_module modules/mod_ssl.so的作用是载入SSL模块

  Include conf/extra/httpd-ssl.conf 的作用是告诉Apache的SSL模块到那里去找配置文件

  重新启动服务器,测试一下看有没有什么问题。
  显然在重新启动服务器的时候报了一个错误,说服务器无法正常启动。这时候我们可以看看日志文件了。
   Parent: Received restart signal -- Restarting the server.
   Child 740: Exit event signaled. Child process is ending.
  Syntax error on line 99 of D:/Dkits/Apache2.2/conf/extra/httpd-ssl.conf:
  SSLCertificateFile: file 'D:/Dkits/Apache2.2/conf/server.crt' does not exist or is empty
   (OS 995)The I/O operation has been aborted because of either a thread exit or an application request.: winnt_accept: Asynchronous AcceptEx failed.

   Child 740:>   Child 740: All worker threads have exited.
   Child 740: Child process is exiting
  上面说在D:/Dkits/Apache2.2/conf/server.crt路径下找不到server.crt这个文件。
  那么,这个文件在那里呢?
  这个文件需要我们用openssl这个程序来生成的。生成这个 server.crt文件需要2个文件,一个是openssl.exe 另一个是openssl.cnf。
  一般来说openssl.exe 在你Apache的安装路径下的bin文件夹里面,openssl.cnf在Apache安装路径下的conf文件里面。找不到?如果找不到就用 windows的查找搜索一下了。这个时候你需要将openssl.exe和openssl.cnf放在同一个文件夹里面。
  一般的需要将找到的openssl.cnf文件拷贝到bin文件里面。
  用openssl生成KEY 生成自签名文件
  首先生成csr和pem文件。
  命令是:openssl req -config openssl.cnf -new -out server.csr -keyout server.pem
  运行结果如下:

  D:\Dkits\Apache2.2\bin>openssl req -config openssl.cnf -new -out server.csr -keyout server.pem
  Loading 'screen' into random state - done
  Generating a 1024 bit RSA private key
  .........++++++
  .........++++++
  writing new private key to 'server.pem'
  Enter PEM pass phrase:
  Verifying - Enter PEM pass phrase:
  -----
  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) :US
  State or Province Name (full name) :New Hampshire
  Locality Name (eg, city) []:Manchester
  Organization Name (eg, company) :CUIST
  Organizational Unit Name (eg, section) []:IT Section
  Common Name (eg, YOUR name) []:localhost
  Email Address []:huyuchengus@gmail.com
  Please enter the following 'extra' attributes
  to be sent with your certificate request
  A challenge password []:
  An optional company name []:
  D:\Dkits\Apache2.2\bin>
  其中有几个地方需要注意
  Enter PEM pass phrase:
  Verifying - Enter PEM pass phrase:
  用于输入和重复输入密码的,密码长度不能小于4位
  Common Name (eg, YOUR name) []:localhost
  用于输入客户端能够访问SSL的地址:这里SSL服务器是在本地运行的,因此你可以输入localhost如果你的SSL地址运行网站主机上,那么这里需 要输入例如www.cuist.com了。
  An optional company name []:留空,如果输入将会报错。

  其他的提示输入部分,可以输入也可以留空,最重要的就是Common Name (eg, YOUR name) []:这个变量,关系到你的SSL服务能不能够运行。
  为Apache创建没有密码保护的key 下面需要为Apache创建一个没有密码保护的key了。
  命令是openssl rsa -in server.pem -out server.key
  运行结果如下

  在这里你需要输入密码,这个密码是你在上一步输入过的。如果没有错误,你将会看到writing RSA key这句话后面没有任何内容和错误提示。
  创建X.509证书 现在需要为Apache创建X.509证书了。
  命令是:openssl x509 -in server.csr -out server.crt -req -signkey server.key -days 365
  结果如下:

  如果没有错误,将会看到签名已经成功,后面有签名的详细信息和内容。
  现在你再看看opensel.exe所在的文件夹下面是不是多了4个文件了,分别 是:server.pem,server.key,server.csr,server.crt

  其实我们并不需要所有的这4个文件,我一向喜欢偷懒,就把这4个文件直接拷贝到conf文件夹下面保存。
  重新启动错误 如果重新启动没有错误,但是还是不能访问。
  这时候你需要检查日志文件,有一种可能是443端口的冲突,如果你的机器已经安装了IIS,那需要将IIS的web服务,FTP,SMTP服务全部关掉再 重新启动。
  如果既没有日志输出,也不能访问,那是因为配置文件没有被正确的载入,这时候你需要先在httpd.conf文件里面注释掉这句话Include conf/extra/httpd-ssl.conf,然后重新启动Apache的服务。等服务成功启动以后,再打开这句话,再重新启动服务。这时候你就 能看到日志的输出了。否则不管你重新启动多少次,Apache还是只会载入老的httpd-ssl.conf文件,不会重新读取httpd- ssl.conf的。
  如果在日志里面有警告
   RSA server certificate CommonName (CN) `localhost' does NOT match server name!?

   Child 3664:>   RSA server certificate CommonName (CN) `localhost' does NOT match server name!?
  那是因为你输入的SSL服务为localhost但是你Apache在安装的时候服务却不是localhost。这时候你需要改几个地方。
  httpd-ssl.conf文件中的

  # ServerAdmin: Your address, where problems with the server should be
  # e-mailed.This address appears on some server-generated pages, such
  # as error documents.e.g. admin@your-domain.com
  #
  ServerAdmin admin@rochester.cuist.local
  将ServerAdmin 变量中admin@rochester.cuist.local的 改成admin@localhost
  保存上述两个配置文件后重新启动Apache服务器,如果没有日志输出,则需要先在httpd.conf文件里面注释掉这句话Include conf/extra/httpd-ssl.conf,然后重新启动Apache的服务。等服务成功启动以后,再打开这句话,再重新启动服务。
  在输出的日志里面应该不会包含有任何的警告信息了。
   Parent: Received restart signal -- Restarting the server.
   Child 3908: Exit event signaled. Child process is ending.
   Apache/2.2.9 (Win32) mod_ssl/2.2.9 OpenSSL/0.9.8h PHP/5.2.6 configured -- resuming normal operations
   Server built: Jun 13 2008 04:04:59
   Parent: Created child process 2976

   Child 3908:>   Child 2976: Child process is running
   Child 2976: Acquired the start mutex.
   Child 2976: Starting 64 worker threads.
   Child 2976: Starting thread to listen on port 443.
   Child 2976: Starting thread to listen on port 80.
   Child 3908: All worker threads have exited.
   Child 3908: Child process is exiting

  #   General setup for the virtual host
  DocumentRoot "D:/Dkits/Apache2.2/htdocs"
  ServerName CUIST_PC02.rochester.cuist.local:443
  ServerAdmin admin@rochester.cuist.local
  ErrorLog "D:/Dkits/Apache2.2/logs/error.log"
  TransferLog "D:/Dkits/Apache2.2/logs/access.log"
  将ServerName CUIST_PC02.rochester.cuist.local:443变量中的 CUIST_PC02.rochester.cuist.local:443改成localhost:443
  将ServerAdmin 变量中admin@rochester.cuist.local的 改成admin@localhost
  httpd.conf文件中的

  HTTP服务启动在80端口,SSL服务启动在443端口。
  验证 在HTTP能正常访问的地址下面加上S变成https。
  如果http://localhost/phpMyAdmin/index.php变化成https://localhost/phpMyAdmin /index.php
  这时候应该能够看到一个证书警告的信息

  同意这个证书,看看是不是能够看到web页面了呀,如果能,则说明SSL已经被正确的配置到Apache服务里面来了。可以轻松一下了喔。。
  转载请注明原文地址:http://www.server110.com

页: [1]
查看完整版本: Windows系统中如何配置Apache的SSL加密访问