我被要求在Apache上的localhost上使用自签名证书设置HTTPS,但我实际上如何做到这一点?我完全不知道。


当前回答

对于那些使用macOS的人来说,这是一个很好的指南https://getgrav.org/blog/macos-sierra-apache-multiple-php-versions来设置您的本地web开发环境。在第三部分https://getgrav.org/blog/macos-sierra-apache-ssl Andy Miller解释了如何使用自签名证书设置apache:

这是关键命令:

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server.key -out server.crt

但有几个步骤你需要遵循,所以检查,祝你好运!,)

其他回答

这是最简单的方法

首先复制这些服务器。CRT &服务器。关键文件(见附件)到apache/conf/ssl目录

然后打开httpd.conf文件并添加以下行

Listen 80
Listen 443

NameVirtualHost *:80
NameVirtualHost *:443

<VirtualHost *:443>
    DocumentRoot "d:/wamp/www"  #your wamp www root dir
    ServerName localhost
    SSLEngine on
    SSLCertificateFile "d:/wamp/bin/apache/Apache2.4.4/conf/ssl/server.crt"
    SSLCertificateKeyFile "d:/wamp/bin/apache/Apache2.4.4/conf/ssl/server.key"
</VirtualHost>

这实际上很简单,假设您手边有一个openssl安装。(您在哪个站台?)

假设你使用的是linux/solaris/mac os/x, Van的Apache SSL/TLS mini-HOWTO有一个很好的演练,我在这里就不再赘述了。

但是,执行摘要是您必须创建一个自签名证书。由于您运行apache for localhost大概是为了开发(即不是公共web服务器),您将知道您可以信任自签名证书,并且可以忽略浏览器抛出的警告。

我刚刚尝试了这一点-我需要在Windows上的本地主机Apache上测试一些开发代码。这比想象中要困难得多。但下面这些步骤在经历了多次拉扯之后还是有效的……

我发现我的Apache安装自带openssl.exe,这很有帮助。如果你没有副本,你需要下载。我的拷贝在Apache2\bin文件夹中,这就是我在下面引用它的方式。

步骤:

Ensure you have write permissions to your Apache conf folder Open a command prompt in Apache2\conf folder Type ..\bin\openssl req -config openssl.cnf -new -out blarg.csr -keyout blarg.pem You can leave all questions blank except: PEM Passphrase: a temporary password such as "password" Common Name: the hostname of your server When that completes, type ..\bin\openssl rsa -in blarg.pem -out blarg.key Generate your self-signed certificate by typing: ..\bin\openssl x509 -in blarg.csr -out blarg.cert -req -signkey blarg.key -days 365 Open Apache's conf\httpd.conf file and ensure SSL module is enabled - there should be no hash at the start of this line: LoadModule ssl_module modules/mod_ssl.so Some Apache installations place the SSL config in a separate file. If so, ensure that the SSL conf file is being included. In my case I had to uncomment this line: Include conf/extra/httpd-ssl.conf In the SSL config httpd-ssl.conf I had to update the following lines: Update SSLSessionCache "shmcb:C:\Program Files (x86)\Zend\Apache2/logs/ssl_scache(512000)" to SSLSessionCache "shmcb:C:/Progra\~2/Zend/Apache2/logs/ssl_scache(512000)" (The brackets in the path confuse the module, so we need to escape them) DocumentRoot - set this to the folder for your web files ServerName - the server's hostname SSLCertificateFile "conf/blarg.cert" SSLCertificateKeyFile "conf/blarg.key" Restart Apache. Try loading https://localhost/ in your browser.

希望你能做到这一步。请随时更新这篇文章与任何其他有用的信息。

(截图由Neil Obremski和他的有用文章提供——尽管现在已经过时了。)

这适用于Windows 10和Apache24:

1 -将此添加到C:/Apache24/conf/httpd.conf的底部

Listen 443
<VirtualHost *:443>
    DocumentRoot "C:/Apache24/htdocs"
    ServerName localhost
    SSLEngine on
    SSLCertificateFile "C:/Apache24/conf/ssl/server.crt"
    SSLCertificateKeyFile "C:/Apache24/conf/ssl/server.key"
</VirtualHost>

2—添加服务器。CRT和服务器。打开“C:/Apache24/conf/ssl”文件夹下的key文件。请参阅本页上的其他答案,以找到这两个文件。

就是这样!

另一个简单的方法是在Ubuntu中使用Python Server。

Generate server.xml with the following command in terminal: openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes Note: Assuming you have openssl installed. Save below code in a file named simple-https-server.py in any directory you want to run the server. import BaseHTTPServer, SimpleHTTPServer import ssl httpd = BaseHTTPServer.HTTPServer(('localhost', 4443), SimpleHTTPServer.SimpleHTTPRequestHandler) httpd.socket = ssl.wrap_socket (httpd.socket, certfile='./server.pem', server_side=True) httpd.serve_forever() Run the server from terminal: python simple-https-server.py Visit the page at: https://localhost:4443

额外的笔记:

You can change the port in simple-https-server.py file in line httpd = BaseHTTPServer.HTTPServer(('localhost', 4443), SimpleHTTPServer.SimpleHTTPRequestHandler) You can change localhost to your IP in the same line above: httpd = BaseHTTPServer.HTTPServer(('10.7.1.3', 4443), SimpleHTTPServer.SimpleHTTPRequestHandler) and access the page on any device your network connected. This is very handy in cases like "you have to test HTML5 GeoLocation API in a mobile, and Chrome restricts the API in secure connections only".

要点:https://gist.github.com/dergachev/7028596

http://www.piware.de/2011/01/creating-an-https-server-in-python/