ssl对于用户和web服务器之间的安全通信很有用。证书在公共线路上传输时对数据进行加密,这样它就不会受到黑客的攻击。自签名证书是免费使用的,但不在生产环境中使用,例如使用信用卡、paypal信息的机密数据。本篇文章将介绍关于在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 incorporatedinto 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 blankfor some fields there will be a default value,if you enter '.', the field will be left blank.-----country name (2 letter code) [xx]: instate or province name (full name) []: delhilocality name (eg, city) [default city]: delhiorganization name (eg, company) [default company ltd]: tecadminorganizational unit name (eg, section) []: blogcommon name (eg, your name or your server's hostname) []: www.example.comemail address []: admin@example.com
上面的命令将在当前目录中创建一个ssl密钥文件example.com.key和一个证书文件example.com.crt。
步骤3:在apache中安装自签名证书
现在拥有了自签名ssl证书和密钥文件。接下来编辑apache ssl配置文件并按照以下指令进行编辑/更新。
apache虚拟主机配置:
<virtualhost _default_:443> serveradmin admin@example.com 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
步骤5:使用https测试网站
最后,使用https在你的web浏览器中打开你的站点。它需要打开端口443才能使用https访问站点。
https://www.example.com
当我们使用自签名证书时,你将在浏览器中收到一条警告消息,忽略此消息就可以了。
以上就是如何在apache中创建和安装自签名证书的详细内容。