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


当前回答

我刚刚尝试了这一点-我需要在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和他的有用文章提供——尽管现在已经过时了。)

其他回答

我想在@CodeWarrior的非常好的回答中添加一些东西,这在Chrome上完美地工作,但对于Firefox需要额外的步骤。

由于Firefox不支持默认情况下Windows提供的CA证书,您需要继续:config,向下滚动到security.enterprise_root。Enabled并将其更改为true。

现在,您的证书在Firefox上也应该是有效的。

当然,这仅用于开发目的,因为ssl信任是一个关键的安全问题,只有在知道其影响时才更改此设置。

为此我使用ngrok (https://ngrok.com/)。 Ngrok是一个命令行工具,用于为本地主机创建隧道。它同时创建http和https连接。 下载完成后,需要执行以下命令:

ngrok http 80

(在版本2中,语法是:ngrok http 80。在版本2中,任何端口都可以通过隧道。)

几秒钟后,它会给出两个url:

http://a_hexadecimal_number.ngrok.com
https://a_hexadecimal_number.ngrok.com

现在,两个url都指向localhost。

这很简单,

只需运行以下命令

sudo a2enmod ssl

sudo service apache2 restart

sudo a2ensite default-ssl.conf

就这样,你完成了。

如果你想强制使用SSL(总是使用https),编辑文件:

sudo nano /etc/apache2/sites-available/000-default.conf

加上这一行

<VirtualHost *:80>
        . . .

        Redirect "/" "https://your_domain_or_IP/"

        . . .
</VirtualHost>

然后重新启动

sudo service apache2 restart

我刚刚尝试了这一点-我需要在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 + Apache 2.4,例如:

uncomment ssl_module in your httpd.conf file. LoadModule ssl_module modules/mod_ssl.so listen 443 port just like 80 port in your httpd.conf file. Listen 80 Listen 443 uncomment Include Virtual hosts in your httpd.conf file. # Virtual hosts Include conf/extra/httpd-vhosts.conf add VirtualHost in your conf/extra/httpd-vhosts.conf <VirtualHost _default_:443> DocumentRoot "D:/www" #your site directory path ServerName localhost #ServerAlias localhost.com localhost2.com SSLEngine on SSLCertificateFile "${SRVROOT}/conf/ssl/server.crt" SSLCertificateKeyFile "${SRVROOT}/conf/ssl/server.key" <Directory "D:/www"> Options -Indexes +FollowSymLinks +ExecCGI AllowOverride All Require all granted </Directory> </VirtualHost>

只有端口号443和SSL......行与正常的HTTP配置不同。

保存配置文件并重新启动apache服务。然后你可以访问https://localhost/

浏览器会在第一时间警告你这是不安全的,选择继续。