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


这实际上很简单,假设您手边有一个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和他的有用文章提供——尽管现在已经过时了。)


为此我使用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。


这是最简单的方法

首先复制这些服务器。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>

这应该是工作Ubuntu,薄荷类似于Apache2

这是一个很好的指南,所以遵循这个

https://www.digitalocean.com/community/tutorials/how-to-create-a-ssl-certificate-on-apache-for-ubuntu-14-04

然后像这样留下ssl。conf

<VirtualHost _default_:443>
        ServerAdmin your@email.com
        ServerName localhost
        ServerAlias www.localhost.com

        DocumentRoot /var/www


    SSLEngine on
    SSLCertificateFile /etc/apache2/ssl/apache.crt
    SSLCertificateKeyFile /etc/apache2/ssl/apache.key

你可以得到它。

希望这对linuxer有帮助


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/

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


另一个简单的方法是在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/


这很简单,

只需运行以下命令

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

这个HowTo for CentOS很简单,只花了大约5分钟:https://wiki.centos.org/HowTos/Https

我不会在这里详细介绍每一步,但主要步骤如下:

1)。安装apache的openssl模块(如果尚未安装)

2)。生成自签名证书

—此时,您应该能够成功访问https://localhost

3)。如果需要,可以设置一个虚拟主机


对于那些使用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

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


为了保护发送到web服务器和从web服务器发送的信息的安全,启用客户端和服务器之间的通信加密是一个好主意。这通常称为SSL。

因此,让我们在Apache2上使用自签名证书设置HTTPS。我将列出你应该遵循的步骤:

在你的机器上安装apache2 web server。对于linux机器,打开终端并输入

Sudo apt-get安装apache2

安装成功后,可以通过命令检查apache2服务的状态

Sudo服务apache2状态

它应该输出

导航到浏览器并输入

http://localhost:80

验证您获得了apache2的默认页面,如下所示。

为了加密web连接,我们需要CA(证书颁发机构)的证书,或者我们可以使用自签名证书。让我们使用以下命令创建一个自签名证书。

Openssl req -x509 -newkey rsa:2048 -keyout mykey。输入mycert。Pem -days 365 -nodes

请按如下所示填写相关信息。

mykey。Key和mycert。Pem应在当前工作目录中创建。

如果我们把证书和密钥移到一个公共的地方,apache2 web服务器就很容易找到它们了。让我们执行下面的命令

Sudo cp mycert。pem /etc/ssl/certs

Sudo cp mykey。关键/etc/ssl/private

让我们在服务器上启用SSL模式

Sudo a2enmod SSL

它应该像这样输出

让我们将apache2配置为使用上面生成的自签名证书和密钥。

sudo vi /放开

请找到这两行,并用您的证书和密钥路径替换它们。

最初的

最后

启用站点

cd /etc/apache2/sites-available / Sudo a2ensite default-ssl.conf

重新启动apache2服务

Sudo服务apache2重启

在HTTPS上验证apache2 web服务器。再次打开浏览器并输入

https://localhost:443

它应该输出如下内容,并警告您即将查看的页面不安全,因为我们已经使用自签名证书配置了服务器。

恭喜您已经将apache2配置为HTTPS端点,现在单击高级—>添加例外—>确认安全例外,您将再次看到默认页面。


这适用于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文件。请参阅本页上的其他答案,以找到这两个文件。

就是这样!


博士tl;

ssh -R youruniquesubdomain:80:localhost:3000 serveo.net

您的本地环境可以从https://youruniquesubdomain.serveo.net访问

Serveo是最好的

没有注册。 没有安装。 HTTPS。 全球范围内访问。 您可以指定自定义修复子域。 你可以自己托管它,这样你就可以使用自己的域名,即使服务宕机,你也可以为未来做准备。

当我发现这项服务时,我简直不敢相信。它提供了一切,而且是最容易使用的。如果有这样一个简单无痛的工具可以解决所有问题……


2021年更新

我发布这个答案,因为我自己也在努力解决这个问题,Chrome更新了他们的安全性,要求主题替代名称,很多帖子都没有,因为当他们作为答案发布时,这是不需要的。我假设WAMP已经安装了。

步骤1

下载OpenSSL Light并安装


**STEP 2 (Optional)**

虽然这部分是可选的,但它使以后更容易执行命令。如果跳过此步骤,则必须提供执行该命令的openssl.exe的完整路径。如果你喜欢设置它,那么更新环境变量中的openssl.exe路径。

环境变量—>系统变量—> Path—> Edit—> New—> .单击“确定” c:\Program Files\ OpenSSL-Win64 \ bin


**STEP 3**

在c:/wamp64/bin/apache/apache2.4.27(你的版本号)/conf/目录下创建一个名为“key”的文件夹。

为您的CA MyCompanyCA.cnf创建配置文件(您可以根据需要更改它):

[ req ]
distinguished_name  = req_distinguished_name
x509_extensions     = root_ca

[ req_distinguished_name ]
countryName             = Country Name (2 letter code)
countryName_min         = 2
countryName_max         = 2
stateOrProvinceName     = State or Province Name (full name)
localityName            = Locality Name (eg, city)
0.organizationName      = Organization Name (eg, company)
organizationalUnitName  = Organizational Unit Name (eg, section)
commonName              = Common Name (eg, fully qualified host name)
commonName_max          = 64
emailAddress            = Email Address
emailAddress_max        = 64

[ root_ca ]
basicConstraints            = critical, CA:true

创建扩展配置文件MyCompanyLocalhost。您的web服务器证书的Ext:

subjectAltName = @alt_names
extendedKeyUsage = serverAuth

[alt_names]
DNS.1   = localhost
DNS.2   = mycy.mycompany.com

**STEP 4**

按指定顺序执行以下命令生成密钥和证书:

openssl req -x509 -newkey rsa:2048 -out MyCompanyCA.cer -outform PEM -keyout MyCompanyCA.pvk -days 10000 -verbose -config MyCompanyCA.cnf -nodes -sha256 -subj "/CN=MyCompany CA"
openssl req -newkey rsa:2048 -keyout MyCompanyLocalhost.pvk -out MyCompanyLocalhost.req -subj /CN=localhost -sha256 -nodes
openssl x509 -req -CA MyCompanyCA.cer -CAkey MyCompanyCA.pvk -in MyCompanyLocalhost.req -out MyCompanyLocalhost.cer -days 10000 -extfile MyCompanyLocalhost.ext -sha256 -set_serial 0x1111

因此,你将拥有MyCompanyCA。MyCompanyLocalhost cer。和MyCompanyLocalhost。pvk文件。


**STEP 5**

安装MyCompanyCA。cer下

控制面板->管理用户证书->受信任根 证书颁发机构->证书

安装MyCompanyLocalhost。Cer,双击它。


**STEP 6**

打开c:/wamp64/bin/apache/apache2.4.27(你的版本号)/conf/httpd.conf并取消注释(删除#)以下3行:

LoadModule ssl_module modules/mod_ssl.so
Include conf/extra/httpd-ssl.conf
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so

**STEP 7**

打开“c:/wamp64/bin/apache/apache2.4.37/conf/extra/httpd-ssl.conf”,按如下所示修改所有参数。

Directory "c:/wamp64/www"
DocumentRoot "c:/wamp64/www"
ServerName localhost:443
ServerAdmin admin@example.com
ErrorLog "c:/wamp64/bin/apache/apache2.4.27/logs/error.log"
TransferLog "c:/wamp64/bin/apache/apache2.4.27/logs/access.log"
SSLCertificateFile "c:/wamp64/bin/apache/apache2.4.27/conf/key/MyCompanyLocalhost.cer"
SSLCertificateKeyFile "c:/wamp64/bin/apache/apache2.4.27/conf/key/MyCompanyLocalhost.pvk"
SSLSessionCache "shmcb:c:/wamp64/bin/apache/apache2.4.27/logs/ssl_scache(512000)"
CustomLog "c:/wamp64/bin/apache/apache2.4.27/logs/ssl_request.log" \
          "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"

注意:这是比较棘手的部分。如果在编辑此文件时犯了任何小错误,SSL将无法工作。编辑之前先复制一份。


**STEP 8**

重新启动Wamp和Chrome。Localhost现在是安全的:https://localhost


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

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

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

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