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


当前回答

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/

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

其他回答

这很简单,

只需运行以下命令

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

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

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

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

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

2)。生成自签名证书

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

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

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/

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

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

就是这样!