23

如何为 Apache 服务器创建自签名 SSL 证书以在测试 Web 应用程序时使用?

4

4 回答 4

29

如何为测试目的创建自签名 SSL 证书?

来自http://httpd.apache.org/docs/2.0/ssl/ssl_faq.html#selfcert

  1. 确保 OpenSSL 已安装并在您的 PATH 中。

  2. 运行以下命令,创建 server.key 和 server.crt 文件:

    openssl req -new -x509 -nodes -out server.crt -keyout server.key
    

    这些可以在您的 httpd.conf 文件中按如下方式使用:

    SSLCertificateFile    /path/to/this/server.crt
    SSLCertificateKeyFile /path/to/this/server.key
    
  3. 请务必注意此 server.key 没有任何密码。要将密码短语添加到密钥,您应该运行以下命令,并按要求输入并验证密码短语。

    openssl rsa -des3 -in server.key -out server.key.new
    mv server.key.new server.key
    

    请将 server.key 文件和您输入的密码备份到安全位置。

于 2008-08-20T14:11:40.883 回答
0

存在各种可以生成 SSL 的工具。以 OpenSSL为例。或者,如果您使用的是 Windows,则IIS 6 资源工具包中有一个。

于 2008-08-20T14:09:15.930 回答
0

警告:这对于本地测试以外的目的完全没有用。

将 MYDOMAIN 替换为您的本地域。也适用于本地主机。

在某个文件夹中创建 MYDOMAIN.conf 文件。在其中添加以下内容:

[ req ]
prompt              = no  
default_bits        = 2048  
default_keyfile     = MYDOMAIN.pem  
distinguished_name  = subject  
req_extensions      = req_ext  
x509_extensions     = x509_ext  
string_mask         = utf8only

# The Subject DN can be formed using X501 or RFC 4514 (see RFC 4519 for a description).
#   Its sort of a mashup. For example, RFC 4514 does not provide emailAddress.
[ subject ]
countryName     = KE 
stateOrProvinceName = Nairobi 
localityName            = Nairobi
organizationName         = Localhost


# Use a friendly name here because its presented to the user. The server's DNS
#   names are placed in Subject Alternate Names. Plus, DNS names here is deprecated
#   by both IETF and CA/Browser Forums. If you place a DNS name here, then you 
#   must include the DNS name in the SAN too (otherwise, Chrome and others that
#   strictly follow the CA/Browser Baseline Requirements will fail).
commonName          = Localhost dev cert  
emailAddress            =edwin@gmail.com

# Section x509_ext is used when generating a self-signed certificate. I.e., openssl req -x509 ...
[ x509_ext ]

subjectKeyIdentifier        = hash  
authorityKeyIdentifier  = keyid,issuer

# You only need digitalSignature below. *If* you don't allow
#   RSA Key transport (i.e., you use ephemeral cipher suites), then
#   omit keyEncipherment because that's key transport.
basicConstraints        = CA:FALSE  
keyUsage            = digitalSignature, keyEncipherment  
subjectAltName      = @alternate_names  
nsComment           = "OpenSSL Generated Certificate"

# RFC 5280, Section 4.2.1.12 makes EKU optional
#   CA/Browser Baseline Requirements, Appendix (B)(3)(G) makes me confused
#   In either case, you probably only need serverAuth.
# extendedKeyUsage  = serverAuth, clientAuth

# Section req_ext is used when generating a certificate signing request. I.e., openssl req ...
[ req_ext ]

subjectKeyIdentifier        = hash

basicConstraints        = CA:FALSE  
keyUsage            = digitalSignature, keyEncipherment  
subjectAltName          = @alternate_names  
nsComment           = "OpenSSL Generated Certificate"

# RFC 5280, Section 4.2.1.12 makes EKU optional
#   CA/Browser Baseline Requirements, Appendix (B)(3)(G) makes me confused
#   In either case, you probably only need serverAuth.
# extendedKeyUsage  = serverAuth, clientAuth

[ alternate_names ]

DNS.1       = MYDOMAIN

# Add these if you need them. But usually you don't want them or
#   need them in production. You may need them for development.
# DNS.5       = localhost
# DNS.6       = localhost.localdomain
DNS.7       = 127.0.0.1

# IPv6 localhost
# DNS.8     = ::1

生成证书文件:

$ sudo openssl req -config MYDOMAIN.conf -new -x509 -sha256 -newkey rsa:2048 -nodes -keyout MYDOMAIN.key -days 1024 -out MYDOMAIN.crt
$ sudo openssl pkcs12 -export -out MYDOMAIN.pfx -inkey MYDOMAIN.key -in MYDOMAIN.crt
$ sudo chown -R $USER *

让您的本地机器信任您的证书:

# Install the cert utils
$ sudo apt-get install libnss3-tools

# Trust the certificate for SSL
$ pk12util -d sql:$HOME/.pki/nssdb -i MYDOMAIN.pfx

# Trust self-signed server certificate
$ certutil -d sql:$HOME/.pki/nssdb -A -t "P,," -n 'dev cert' -i MYDOMAIN.crt

编辑/etc/apache2/sites-available/default-ssl.conf并确保这两个指令指向您刚刚创建的文件 .crt 和 .key (如果需要,取消注释):

SSLCertificateFile     /path/to/MYDOMAIN.crt
SSLCertificateKeyFile  /path/to/MYDOMAIN.key

应用配置并重新启动 apache:

# If you are not using the default configuration ( /etc/apache2/sites-available/default-ssl.conf ),
# then replace "default-ssl" for whatever conf file name you've chosen
# ( DO NOT include the .conf bit ).
$ sudo a2ensite default-ssl

$ sudo service apache2 restart

在浏览器上访问https://MYDOMAIN 。Firefox 会警告您证书是自签名的,因此说它是无效的。您将不得不添加一个例外。

来源:

  • 大部分是我从3dw1n_m0535得到的;
  • 如果遇到问题,请阅读 README 文件/usr/share/doc/apache2/README.Debian.gz
于 2019-08-21T22:57:35.133 回答
-3

使用 OpenSSL ( http://www.openssl.org/ )

这是一个教程: http: //novosial.org/openssl/self-signed/

这是一个很好的教程:SSH localhost

于 2008-08-20T14:04:08.033 回答