一、自建证书

建立单独目录用来存放证书文件

mkdir /mykey

cd /mykey

1. 使用以下命令生成私钥文件

其中,2048是私钥的长度,可以根据需要进行调整。

openssl genrsa -out ssl.key 2048

2. 生成证书签名请求文件

在执行该命令时,需要填写一些证书信息,如国家、省份、城市、组织、单位、通用名称等。

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

3.最后根据这2个文件(ssl.key ssl.csr)生成自签名证书

其中,-days 3650表示证书的有效期为3650天,可以根据需要进行调整。

openssl x509 -req -days 3650 -in ssl.csr -signkey ssl.key -out ssl.crt

4.验证证书

该命令会输出证书的详细信息,包括证书的颁发者、有效期、公钥等。

openssl x509 -noout -text -in ssl.crt

二、配置nginx

server {

     #SSL 默认访问端口号为 443

     listen 8002 ssl http2; 

     #请填写绑定证书的域名

     server_name localhost; 

     #请填写证书文件的相对路径或绝对路径

     ssl_certificate /opt/mykey/ssl.crt; 

     #请填写私钥文件的相对路径或绝对路径

     ssl_certificate_key /opt/mykey/ssl.key; 

     ssl_session_timeout 5m;

     #请按照以下协议配置

     ssl_protocols TLSv1.2 TLSv1.3; 

     #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。

     ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; 

     ssl_prefer_server_ciphers on;

	root   /usr/share/nginx/html;

	index  index.php index.html index.htm;

	location / {

		root   /usr/share/nginx/html;

		index  index.php index.html index.htm;

	}

	# Pass PHP Scripts To FastCGI Server

	location ~ \.php$ {

		include snippets/fastcgi-php.conf;

		fastcgi_pass unix:/run/php/php8.2-fpm.sock;

		#fastcgi_index index.php;

		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

		include fastcgi_params;

	}

 }