Repository navigation
Expand file tree
/
Copy pathssl-install.sh
More file actions
executable file
·233 lines (195 loc) · 7.71 KB
/
Copy pathssl-install.sh
File metadata and controls
executable file
·233 lines (195 loc) · 7.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env bash
# Copyright:: Copyright (c) 2016 Been Kyung-yoon (http://www.php79.com/)
# License:: The MIT License (MIT)
STACK_ROOT=$( cd "$( dirname "$0" )" && pwd )
source "${STACK_ROOT}/includes/function.inc.sh"
cd ${STACK_ROOT}
function show_usage
{
welcome_short
title "Let's Encrypt SSL 인증서 발급 순서는 다음과 같습니다."
outputComment " 1. nginx 사이트 설정에 Let's Encrypt 인증용 디렉토리 접근 추가"
echo
outputComment " 2. certbot-auto 로 SSL 인증서 발급 테스트후, 실제 인증서 발급"
outputComment " 주의) 인증서를 발급 받은 도메인의 IP가 현재 서버로 지정되어 있어야 인증서 발급이 가능합니다."
echo
outputComment " 3. nginx 사이트 설정 마지막에 SSL 설정 추가"
echo
echo
echo "Example:"
outputComment " ${0} --user=php79 --domain=php79.net"
echo
outputComment " ${0} --user=phpmyadmin --domain=phpmyadmin.php79.net"
echo
outputComment " ${0} --user=wordpress --domain=wordpress.php79.net"
echo
echo
echo "Usage:"
echo -n " "
outputInfo "--user"
echo " 아이디(/etc/nginx/conf.d/아이디.conf). ./user-add.sh 로 추가한 아이디를 입력하세요."
echo
echo -n " "
outputInfo "--domain"
echo " 도메인. SSL 인증서 발급시 사용할 도메인으로 www. 없이 입력하세요."
echo " Tip) 발급시 www. 를 자동 추가하여 php79.net, www.php79.net 형태의 2개 도메인을 지원합니다."
echo
echo -n " "
outputInfo "--skip-nginx"
echo " (선택) nginx 설정을 생략하고, certbot-auto 로 SSL 인증서 발급만 진행합니다."
echo " Tip) 이미 nginx 설정이 완료된 상태이거나, 직접 수동 설정하실 경우에 사용하세요."
echo
}
function input_abort
{
outputError "${1}"
echo
exit 1
}
# Options
if [ -z ${1} ]; then
show_usage
exit
else
INPUT_SKIP_NGINX=0
for i in "${@}"
do
case $i in
--user=*)
shift
INPUT_USER="${i#*=}"
;;
--domain=*)
shift
INPUT_DOMAIN="${i#*=}"
;;
--skip-nginx)
shift
INPUT_SKIP_NGINX=1
;;
-h | --help )
show_usage
exit
;;
esac
done
fi
# 입력값 검사
if [ -z ${INPUT_USER} ]; then
input_abort "user 항목을 입력하세요."
fi
if [ -z ${INPUT_DOMAIN} ]; then
input_abort "domain 항목을 입력하세요."
fi
outputComment "## Let's Encrypt SSL 인증서 발급을 시작합니다.\n\n"
NGINX_USER_CONF="/etc/nginx/conf.d/${INPUT_USER}.conf"
if [ ${INPUT_SKIP_NGINX} != "1" ]; then
outputInfo " - 수정할 Nginx config : ${NGINX_USER_CONF}\n\n"
if [ ! -f ${NGINX_USER_CONF} ]; then
input_abort "${NGINX_USER_CONF} 설정 파일이 존재하지 않습니다."
fi
fi
# 추가전 nginx 설정 체크
if [ ${INPUT_SKIP_NGINX} != "1" ]; then
outputComment "# nginx 설정 변경전, nginx 설정을 테스트합니다."
echo
/usr/sbin/nginx -t
echo
if [ "${?}" != "0" ]; then
abort "nginx 설정에 문제가 있습니다."
fi
fi
# nginx 설정 백업 (문자열 치환이 들어가므로 유실 가능성 고려해 백업)
NGINX_BACKUP_DIR="/etc/nginx/conf.d/backups"
if [ ! -d ${NGINX_BACKUP_DIR} ]; then
mkdir ${NGINX_BACKUP_DIR}
fi
outputComment "# nginx 설정 변경전, 하위 backups 디렉토리로 설정을 백업합니다.\n"
cp -av ${NGINX_USER_CONF} "${NGINX_BACKUP_DIR}/${INPUT_USER}_$(date +%Y%m%d"_"%H%M%S)"
echo
outputComment "## [1/3] nginx - Let's Encrypt 인증용 디렉토리 접근 추가\n\n"
if [ ${INPUT_SKIP_NGINX} != "1" ]; then
WELL_KNOWN_CONF="/etc/letsencrypt/php79/well-known.conf"
grep ${WELL_KNOWN_CONF} ${NGINX_USER_CONF}
if [ "${?}" != "1" ]; then
outputComment "# nginx 에 다음 설정이 이미 추가되었으므로 생략합니다.\n include ${WELL_KNOWN_CONF}\n\n"
else
outputComment "# nginx 설정의 첫번째 'root /home/...' 이전 라인에 다음 설정을 추가합니다.\n include ${WELL_KNOWN_CONF}\n\n"
sed -i "0,/[^[ \t]*root[ \t]*/ s/root/include \/etc\/letsencrypt\/php79\/well-known\.conf;\n root/" ${NGINX_USER_CONF}
outputComment "# nginx 설정 변경후, nginx 설정을 다시 테스트합니다.\n"
/usr/sbin/nginx -t
if [ "${?}" != "0" ]; then
abort "nginx 설정에 문제가 있어 중단합니다. 에러를 확인하여 수정된 내용을 복구해주세요."
fi
# nginx 재시작
outputComment "# nginx 를 재시작합니다.\n"
echo
if [ $OS = "centos7" ]; then
systemctl reload nginx
else
service nginx reload
fi
fi
else
outputComment "# nginx 설정을 생략합니다. (--skip-nginx)\n"
fi
echo
outputComment "## [2/3] certbot-auto 로 SSL 인증서 발급 테스트후, 실제 인증서 발급\n\n"
if [ -z ${LETSENCRYPT_EMAIL} ]; then
abort "stack.conf 에서 'LETSENCRYPT_EMAIL=이메일' 설정에 관리용 이메일 주소를 입력해주세요."
fi
LETSENCRYPT_PEM="/etc/letsencrypt/live/${INPUT_DOMAIN}/fullchain.pem"
if [ ! -f ${LETSENCRYPT_PEM} ]; then
# 주의) 발급 테스트 과정을 생략하지 마세요. 실제 발급시 에러가 일정 횟수 이상 발생하면, 일정 기간 인증 시도가 차단됩니다.
outputComment "# 인증서 발급 테스트를 시작합니다. (--dry-run)\n"
cmd "certbot-auto certonly --webroot -w /var/www/letsencrypt/ -d ${INPUT_DOMAIN} -d www.${INPUT_DOMAIN} -m ${LETSENCRYPT_EMAIL} --agree-tos -n --dry-run"
outputComment "# 실제 인증서 발급을 시작합니다.\n"
cmd "certbot-auto certonly --webroot -w /var/www/letsencrypt/ -d ${INPUT_DOMAIN} -d www.${INPUT_DOMAIN} -m ${LETSENCRYPT_EMAIL} --agree-tos -n"
else
outputComment "# 인증서가 존재하므로 발급을 생략합니다.\n"
ls -l ${LETSENCRYPT_PEM}
fi
echo
outputComment "## [3/3] nginx 사이트 단위 설정 마지막에 SSL 설정 추가\n\n"
if [ ${INPUT_SKIP_NGINX} != "1" ]; then
if [ ! -f ${LETSENCRYPT_PEM} ]; then
abort "발급된 인증서가 없어서 nginx 설정을 중단합니다. -> ${LETSENCRYPT_PEM}"
fi
grep -P 'listen(.)*ssl' ${NGINX_USER_CONF}
if [ "${?}" != "1" ]; then
outputComment "# nginx 에 'listen ... ssl' 설정이 이미 추가되었으므로 생략합니다.\n"
else
outputComment "# nginx 설정의 마지막에 다음 설정을 추가합니다.\n\n"
sed -e "s/_INPUT_DOMAIN_/${INPUT_DOMAIN}/g" "${STACK_ROOT}/letsencrypt/template-server.conf"
echo
# 마지막 } 문자를 #} 로 주석 처리
tac ${NGINX_USER_CONF} | sed -e "0,/\}/s/\}/\n/" | tac > /tmp/php79-ssl-install-nginx
cat /tmp/php79-ssl-install-nginx > ${NGINX_USER_CONF}
rm -f /tmp/php79-ssl-install-nginx
# 마지막에 SSL 설정 추가
sed -e "s/_INPUT_DOMAIN_/${INPUT_DOMAIN}/g" "${STACK_ROOT}/letsencrypt/template-server.conf" >> ${NGINX_USER_CONF}
echo "}" >> ${NGINX_USER_CONF}
outputComment "# nginx 설정 변경후, nginx 설정을 다시 테스트합니다.\n"
/usr/sbin/nginx -t
if [ "${?}" != "0" ]; then
abort "nginx 설정에 문제가 있어 중단합니다. 에러를 확인하여 수정된 내용을 복구해주세요."
fi
# nginx 재시작
outputComment "# nginx 를 재시작합니다.\n"
echo
if [ $OS = "centos7" ]; then
systemctl reload nginx
else
service nginx reload
fi
fi
else
outputComment "# nginx 설정을 생략합니다. (--skip-nginx)\n"
fi
echo
outputComment "### Let's Encrypt SSL 인증서 발급이 완료되었습니다. ###\n\n"
if [ ${INPUT_SKIP_NGINX} != "1" ]; then
outputInfo " - 수정된 Nginx config : ${NGINX_USER_CONF}\n\n"
fi
outputInfo " - SSL 인증서 확인 : curl -Iv https://${INPUT_DOMAIN}\n\n"
sleep 1; curl -Iv "https://${INPUT_DOMAIN}"