#!/bin/sh
# create self-signed server certificate:
read -p "Enter your domain [www.example.com]: " DOMAIN
echo "Create server key..."
# 生成私钥(key文件)
# -genra 生成RSA私钥
# -des3 des3算法
# -out server.key 生成的私钥文件名
# -1024 私钥长度
#openssl genrsa -des3 -out server.pass.key 1024
# 执行后需要输入4位的秘密
openssl genrsa -des3 -out $DOMAIN.key 1024
echo "Create server certificate signing request..."
# 生成CSR(证书签名请求)
# -req 生成证书签名请求
# -new 新生成
# -key 生成私钥文件
# -out 生成的CSR文件
# -subj 生成CSR证书的参数
# subject说明如下
#字段 字段含义 示例
#/C= Country国家 CN
#/ST= State or Province省 BeiJing
#/L= Location or City 城市 BeiJing
#/O= Organization 组织或企业
#/OU= Organization Unit 部门 rjyfzc
#/CN= Common Name 域名或IP www.xxxx.com
SUBJECT="/C=CN/ST=BeiJing/L=Beijing/O=PSBC/OU=rjyfzc/CN=$DOMAIN"
openssl req -new -subj $SUBJECT -key $DOMAIN.key -out $DOMAIN.csr
echo "Remove password..."
# 去除私钥中的密码
mv $DOMAIN.key $DOMAIN.origin.key
openssl rsa -in $DOMAIN.origin.key -out $DOMAIN.key
echo "Sign SSL certificate..."
# 生成自签名SSL证书
# X.509证书包含三个文件:key,csr,crt。
openssl x509 -req -days 3650 -in $DOMAIN.csr -signkey $DOMAIN.key -out $DOMAIN.crt
echo "TODO:"
echo "Copy $DOMAIN.crt to /etc/nginx/ssl/$DOMAIN.crt"
echo "Copy $DOMAIN.key to /etc/nginx/ssl/$DOMAIN.key"
echo "Add configuration in nginx:"
echo "server {"
echo " ..."
echo " listen 443 ssl;"
echo " ssl_certificate /etc/nginx/ssl/$DOMAIN.crt;"
echo " ssl_certificate_key /etc/nginx/ssl/$DOMAIN.key;"
echo "}"