如何在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.key -out example.crt

  输出

  Generating a 2048 bit RSA private key

  ....................................+++

  ...................................+++

  writing new private key to 'example.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]: TecAdmin

  Organizational Unit Name (eg, section) []: blog

  Common Name (eg, your name or your server's hostname) []: example

  Email Address []: admin@example

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

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

  现在拥有了自签名SSL证书和密钥文件。接下来编辑Apache SSL配置文件并按照以下指令进行编辑/更新。

  Apache虚拟主机配置:

  ServerAdmin admin@example

  ServerName example

  ServerAlias example

  DocumentRoot /var/www/html

  SSLEngine on

  SSLCertificateFile /etc/pki/tls/certs/example.crt

  SSLCertificateKeyFile /etc/pki/tls/certs/example.key

  步骤4:重启Apache

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

  $ sudo systemctl restart apache2 # Debian based systems

  $ sudo systemctl restart httpd # Redhat based systems

  步骤5:使用https测试网站

  最后,使用https在你的Web浏览器中打开你的站点。它需要打开端口443才能使用HTTPS访问站点。

  example

  当我们使用自签名证书时,你将在浏览器中收到一条警告消息,忽略此消息就可以了。

posted @ 2022-02-07 20:36  ebuybay  阅读(447)  评论(0编辑  收藏  举报