如何在Apache中创建和安装自签名证书

时间:2019-05-19 01:25:47  来源:igfitidea点击:

SSL用于用户和web服务器之间的安全通信。
证书在公共线路上传输数据时对数据进行加密,这样就不会受到黑客的攻击。
自签名证书可以免费使用,但不能在使用信用卡、支付宝等信息机密数据的生产环境中使用。

本指南将逐步在Linux系统上的Apache服务器中创建和安装自签名证书。

步骤1 -安装mod_ssl包

为了设置SSL证书,请确保在系统上安装了mod_ssl。
如果尚未安装,请使用以下命令进行安装。
另外,安装OpenSSL包来创建证书。

sudo apt-get install openssl          # Debian based systems 
sudo yum install mod_ssl openssl      # Redhat / CentOS systems 
sudo dnf install mod_ssl openssl      # Fedora 22+ systems

步骤2 -创建自签名证书

安装mod_ssl和OpenSSL之后,使用以下命令为域创建一个自签名证书。

sudo mkdir -p /etc/pki/tls/certs
sudo cd /etc/pki/tls/certs

现在创建SSL证书

sudo openssl req -x509 -nodes -newkey rsa:2048 -keyout   **example.com.key**   -out   **example.com.crt** 

输出

Generating a 2048 bit RSA private key
....................................+++
...................................+++
writing new private key to 'example.com.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:   **IN**  
State or Province Name (full name) []:   **Delhi**  
Locality Name (eg, city) [Default City]:   **Delhi**  
Organization Name (eg, company) [Default Company Ltd]:   **theitroad**  
Organizational Unit Name (eg, section) []:   **blog**  
Common Name (eg, your name or your server's hostname) []:   **www.example.com**  
Email Address []:   **Hyman@theitroad** 

以上命令将在当前目录中创建一个ssl密钥文件 example.com.key和一个证书文件 example.com.crt

步骤3 -在Apache中安装自签名证书

现在我们已经有了自签名的SSL证书和密钥文件。
接下来按照以下指令编辑/更新Apache SSL配置文件。
Apache配置虚拟主机

<VirtualHost *:443>
    ServerAdmin [email protected]
    ServerName www.example.com
    ServerAlias example.com

    DocumentRoot /var/www/html

    SSLEngine on
    SSLCertificateFile /etc/pki/tls/certs/example.com.crt
    SSLCertificateKeyFile /etc/pki/tls/certs/example.com.key
</VirtualHost>

步骤4 -重启Apache

如果上面的命令没有显示任何错误,重启Apache服务。

sudo systemctl restart apache2       # Debian based systems   
sudo systemctl restart httpd         # Redhat based systems

第五步-用HTTPS测试网站

最后,在我们最喜欢的web浏览器中使用https打开站点。
它需要打开端口443才能使用https访问站点。

https://www.example.com

由于我们使用的是自签名证书,我们将在浏览器中收到一条警告消息。
我们可以使用以下步骤忽略此消息:

火狐用户:扩展我了解风险>>点击添加异常>>点击确认安全异常。

Chrome用户:点击继续按钮。

IE用户:点击继续到本网站(不推荐)链接。