+
109
-

回答

这个跟生成域名的ssl证书是一样的,只不过一个是域名,一个是ip地址

步骤 1:安装 OpenSSL

如果系统没有安装 OpenSSL,可以通过以下命令安装:

CentOS/RHEL:
sudo yum install openssl
Ubuntu/Debian:
sudo apt-get install openssl
步骤 2:生成私钥和证书

创建一个目录来存放证书文件:

mkdir ~/ssl_cert
cd ~/ssl_cert

生成私钥:

openssl genpkey -algorithm RSA -out private.key -aes256

系统会提示你为私钥设置密码。如果需要无密码的私钥,可以去掉 -aes256 参数:

openssl genpkey -algorithm RSA -out private.key

生成证书签名请求(CSR):

openssl req -new -key private.key -out csr.csr

在生成 CSR 时,系统会提示你输入一些信息(如国家、组织等)。对于自签名证书,这些信息可以随意填写,但 Common Name (CN) 必须设置为你的 IP 地址(例如 192.168.1.100)。

生成自签名证书:

openssl x509 -req -days 365 -in csr.csr -signkey private.key -out certificate.crt

这将生成一个有效期为 365 天的自签名证书。这一步的hostname填你的服务器ip800_auto

步骤 3:配置 Web 服务器

将生成的私钥和证书配置到你的 Web 服务器中。

1. Nginx 配置

编辑 Nginx 配置文件(通常位于 /etc/nginx/nginx.conf 或 /etc/nginx/sites-available/default),添加以下内容:

server {
    listen 443 ssl;
    server_name 192.168.1.100;  # 替换为你的 IP 地址

    ssl_certificate /path/to/ssl_cert/certificate.crt;  # 替换为证书路径
    ssl_certificate_key /path/to/ssl_cert/private.key;  # 替换为私钥路径

    location / {
        root /var/www/html;
        index index.html;
    }
}

重启 Nginx:

sudo systemctl restart nginx
2. Apache 配置

编辑 Apache 配置文件(通常位于 /etc/httpd/conf/httpd.conf 或 /etc/apache2/sites-available/default-ssl.conf),添加以下内容:

<VirtualHost *:443>
    ServerName 192.168.1.100  # 替换为你的 IP 地址
    DocumentRoot /var/www/html

    SSLEngine on
    SSLCertificateFile /path/to/ssl_cert/certificate.crt  # 替换为证书路径
    SSLCertificateKeyFile /path/to/ssl_cert/private.key  # 替换为私钥路径
</VirtualHost>

重启 Apache:

sudo systemctl restart apache2
步骤 4:访问 HTTPS

在浏览器中访问 https://<你的IP地址>。由于是自签名证书,浏览器会提示证书不安全,你可以选择忽略警告并继续访问。

步骤 5:解决浏览器警告(可选)

自签名证书会被浏览器标记为不安全。如果你希望避免这种情况,可以将证书添加到系统的受信任根证书中。

1. 导出证书为 .crt 格式:

如果你使用的是 .pem 格式的证书,可以直接使用。否则,可以通过以下命令转换:

   openssl x509 -outform der -in certificate.crt -out certificate.der
2. 将证书添加到系统受信任根证书:Windows:双击 .crt 文件,选择“安装证书”,然后选择“受信任的根证书颁发机构”。Linux:将证书复制到 /usr/local/share/ca-certificates/,然后运行:
sudo update-ca-certificates
macOS:双击 .crt 文件,将其添加到“钥匙串访问”中的“系统”钥匙串,并设置为“始终信任”。总结

通过以上步骤,你可以为 IP 地址创建自签名 SSL 证书,并通过 HTTPS 访问。虽然自签名证书在浏览器中会显示警告,但它适用于内部测试或开发环境。如果需要正式使用,建议使用受信任的证书颁发机构(CA)签发的证书。

网友回复

我知道答案,我要回答