Apache2.x 部署SSL站点
如何在没有SSL警告的情况下通过HTTPS访问本地开发环境?
作为开发人员,您可能需要处理多个项目,客户端和Web应用程序。Web应用程序开发的先决条件之一是在开发阶段在浏览器上本地测试网站。很有可能您正在开发的应用程序将在生产环境中使用SSL / TLS证书进行保护。
同意?
如果你必须测试某些功能,利用第三方API,这需要来源为https://?
您可以说自签名证书,并且没有任何问题。但您是否尝试过访问自签名证书的网站?您仍会在Chrome和其他浏览器上收到证书警告。
自签名证书
你看到不安全徽章了吗?
不好,对吧?
在开发环境中获得有效SSL证书的最佳方法是管理您自己的CA,并使用mkcert进行管理。易于实施,使您可以在以下本地开发Web地址上获得有效证书。
example.com
* .example.com的
example.test
本地主机
127.0.0.1
:: 1
您可以mkcert在macOS,Windows,CentOS,Ubuntu和其他基于UNIX的操作系统上实现。以下示例来自Ubuntu。
首先,让我们安装certutil必须管理证书数据库的网络安全服务工具。
apt-get updateapt-get install libnss3-tools
您可能还需要确保服务器上安装了brew。如果不使用以下命令安装。
apt-get install linuxbrew-wrapper
最后,安装mkcert使用brew。
brew install mkcert
注意:要使用brew安装,您不应该是root。它安装在/home/$USER/.linuxbrew/bin/mkcert
其中$ USER是您用于安装的用户名
现在,是时候在系统信任库中安装本地CA.
root@geekflare:~/mkcert# /home/chandan/.linuxbrew/bin/mkcert -installUsing the local CA at "/root/.local/share/mkcert" ✨The local CA is now installed in the system trust store! ⚡️root@geekflare:~/mkcert#
然后,为开发环境生成证书。假设您将在example.com上拥有您的网站,您可以使用以下命令获取证书和密钥文件。
root@geekflare:~/mkcert# /home/chandan/.linuxbrew/bin/mkcert example.comUsing the local CA at "/root/.local/share/mkcert" ✨Created a new certificate valid for the following names 📜
- "example.com"The certificate is at "./example.com.pem" and the key at "./example.com-key.pem" ✅root@geekflare:~/mkcert#
大!现在,我有一个有效的证书及其密钥文件,可以在我的Nginx,Apache或其他Web服务器上使用。
我们来看一个Apache HTTP服务器的例子。如果尚未启用,请启用SSL模块和配置。
root@geekflare:/etc/apache2# a2enmod sslConsidering dependency setenvif for ssl:Module setenvif already enabledConsidering dependency mime for ssl:Module mime already enabledConsidering dependency socache_shmcb for ssl:Enabling module socache_shmcb.Enabling module ssl.See /usr/share/doc/apache2/README.Debian.gz on how to configure SSL and create self-signed certificates.To activate the new configuration, you need to run:
systemctl restart apache2root@geekflare:/etc/apache2#
如建议的那样,重新启动Apache。
此时,如果你netstat,你会发现Apache已经启动了安全端口443。
root@geekflare:/etc/apache2# netstat -anlp |grep 443tcp6 0 0 :::443 :::* LISTEN 11616/apache2root@geekflare:/etc/apache2#
但是,我们还没有完成。它从默认(虚拟)证书开始,我们需要替换它。
使用文件进行修改,并将以下内容替换为生成密钥和证书文件的路径。default-ssl.confvi
SSLCertificateFile /root/mkcert/example.com.pemSSLCertificateKeyFile /root/mkcert/example.com-key.pem
在重新启动Apache之前,您还必须操作hostsexample.com的文件,以便它解析为您的localhost而不是Internet。完成后,重新启动Apache HTTP服务器并访问example.com - 您将看到正在提供可信证书。