配置Apache SSL并使用mod_jk重定向到Tomcat

5

我想配置我的家用服务器以便在443端口接受SSL连接。

我有www.mydomain.com域名,我刚刚使用mod_jk链接了Apache2和Tomcat,现在我还希望能够接受来自Web的https请求。

这是我的配置:

httpd.conf

<IfModule mod_jk.c>
    JKWorkersFile /etc/apache2/workers.properties
    JkShmFile /var/log/apache2/mod_jk.shm
    JKLogFile /var/log/apache2/mod_jk.log
    JkLogLevel debug
    JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
</IfModule>


<VirtualHost *:80>
    DocumentRoot "/Library/ApacheTomcat/apache-tomcat-6.0.33/webapps/MyTomcatAppName"
    ServerName www.mydomain.com
    ErrorLog "/private/var/log/apache2/www.mydomain.com-error_log"
    CustomLog "/private/var/log/apache2/www.mydomain.com-access_log" common
    JkMountCopy On
    JkMount /* ajp13
</VirtualHost>


<VirtualHost *:80>
    DocumentRoot "/Library/ApacheTomcat/apache-tomcat-6.0.33/webapps/MyTomcatAppName"
    ServerName mydomain.com
    ErrorLog "/private/var/log/apache2/mydomain.com-error_log"
    CustomLog "/private/var/log/apache2/mydomaino.com-access_log" common
    JkMountCopy On
    JkMount /* ajp13
</VirtualHost>

Then this is my Worker.properties file:

worker.list=ajp13

worker.ajp13.type=ajp13
worker.ajp13.host=localhost
worker.ajp13.port=8009

这是我的server.xml文件:
    <Host name="localhost"  appBase="/Library/ApacheTomcat/apache-tomcat-6.0.33/webapps"
        unpackWARs="true" autoDeploy="true"
        xmlValidation="false" xmlNamespaceAware="false">
      <Context path="" docBase="/Library/ApacheTomcat/apache-tomcat-6.0.33/webapps/MyTomcatAppName" />

通过这种配置,我可以正确地在访问http://www.mydomain.com或http://domain.com时浏览我的Tomcat应用程序... 我的问题现在是使用https连接访问同样的网站,如https://www.mydomain.com或https://domain.com。 我在我的Mac Mini服务器(Lion OSX)上安装了GoDaddy证书,因此如果我键入https://www.mydomain.com(或https://domain.com),浏览器会正确地通知我存在“mydomain.com”的证书,但它也会显示:
Forbidden

You don't have permission to access / on this server.
Apache/2.2.20 (Unix) mod_ssl/2.2.20 OpenSSL/0.9.8r DAV/2 mod_jk/1.2.30 Server at mydomain.com Port 443

我确定这是因为我在虚拟主机标签中漏掉了一些内容...那么我该怎么修复它呢?
2个回答

7
我找到了解决方案,所以我的Apache和Tomcat都可以正常工作了... 我将总结解决问题的步骤:
假设您已经正确安装并存储了由GoDaddy签名的mydomain证书在我的Mac服务器的Apple KeyChain中。
  1. Open KeyChain App (with root), expand mydomain certificate label, so you see the private key too.
  2. Save both with p12 extension, then generate .pem file from .p12
  3. Private Key:

    umask 0077
      openssl pkcs12 -in pkfilename.p12 -nocerts -nodes -out filename-key.pem
    umask 0022
    
  4. Certificate:

    openssl pkcs12 -in certfilename.p12 -clcerts -nokeys -out filename-cert.pem
    
  5. Copy filename-key.pem and filename-cert.pem within /etc/apache2/ directory

  6. Considering you have the same httpd.conf configuration showed above, you just need to add 2 more VirtualHost for 443 (https port) connection.
  7. Anyway, add 1 VirtualHost for each ServerName you wish to secure, for instance I just want to secure mydomain.com incoming connection:

    <VirtualHost _default_:443>
        DocumentRoot "/Library/ApacheTomcat/apache-tomcat-6.0.33/webapps/MyServerAppName"
        ServerName mydomain.com
        ErrorLog "/private/var/log/apache2/https_mydomain.com-error_log"
        CustomLog "/private/var/log/apache2/https_mydomain.com-access_log" common
        SSLEngine On
        SSLCertificateFile /etc/apache2/filename-cert.pem
        SSLCertificateKeyFile /etc/apache2/filename-key.pem
        JkMountCopy On
        JkMount /* ajp13
    </VirtualHost>
    
  8. Add Listen 443 in httpd.conf file, just add this line under Listen 80 you find at beginning of it.

您现在可以浏览http://mydomain.com和https://mydomain.com。

如果出现错误,您可以通过/var/log/apache2/中的日志文件进行查看。

特别感谢Bruno user,在创建私钥和证书文件(步骤3和4)时帮助我。

我希望这个指南能够帮助您配置Apache和Tomcat以进行安全的SSL连接。


2

您已经在虚拟主机中为普通HTTP请求配置了mod_jk (VirtualHost *:80)。您还需要在HTTPS虚拟主机中(VirtualHost *:443)配置这些Jk*选项,其中您已经配置了SSL设置。


2
如果你访问 https://www.mydomain.com/ 时收到 HTTP 响应(禁止访问),那么 HTTPS 已经在工作,因此你必须在某个地方配置了 SSL* 选项。尝试设置它们所在的虚拟主机(如果不存在,请使用 VirtualHost *:443 创建一个)并将 Jk* 选项放置在那里。 - Bruno
2
我忘记在VirtualHost中添加证书和私钥,你帮我在另一篇文章中创建了它们,现在我已经总结了步骤在我的回答中...非常感谢你... - piojo
我也遇到了同样的问题,即使进行了这个设置,问题仍未解决。我已经在http://stackoverflow.com/questions/21308928/getting-403-forbidden-error-while-forwarding-request-of-ssl-from-apache-server上发布了我的问题。 - Naved

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接