This repository has been archived by the owner on Jun 29, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
ghost-nginx-naxsi.sh
714 lines (592 loc) · 21.5 KB
/
ghost-nginx-naxsi.sh
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
#!/bin/bash
#
# Use this automated bash script to install Ghost blog on Ubuntu, Debian or CentOS,
# with Nginx (as a reverse proxy) and Naxsi web application firewall.
#
# DO NOT RUN THIS SCRIPT ON YOUR PC OR MAC!
#
# Copyright (C) 2015-2018 Lin Song <[email protected]>
# Based on the work of Herman Stevens (Copyright 2013)
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see http://www.gnu.org/licenses/.
max_blogs=10
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
echoerr() { echo "Error: $1" >&2; }
ghost_blog_install() {
# Check operating system
os_type="$(lsb_release -si 2>/dev/null)"
os_vers="$(lsb_release -sr 2>/dev/null)"
if [ -z "$os_type" ]; then
[ -f /etc/os-release ] && os_type="$(. /etc/os-release && printf '%s' "$ID")"
[ -f /etc/os-release ] && os_vers="$(. /etc/os-release && printf '%s' "$VERSION_ID")"
[ -f /etc/lsb-release ] && os_type="$(. /etc/lsb-release && printf '%s' "$DISTRIB_ID")"
[ -f /etc/lsb-release ] && os_vers="$(. /etc/lsb-release && printf '%s' "$DISTRIB_RELEASE")"
[ "$os_type" = "debian" ] && os_type=Debian
[ "$os_type" = "ubuntu" ] && os_type=Ubuntu
fi
if [ "$os_type" = "Ubuntu" ]; then
if [ "$os_vers" != "16.04" ] && [ "$os_vers" != "14.04" ]; then
echoerr "This script only supports Ubuntu 16.04 and 14.04."
exit 1
fi
elif [ "$os_type" = "Debian" ]; then
os_vers="$(sed 's/\..*//' /etc/debian_version 2>/dev/null)"
if [ "$os_vers" != "8" ] && [ "$os_vers" != "9" ]; then
echoerr "This script only supports Debian 9 and 8."
exit 1
fi
else
if [ ! -f /etc/redhat-release ]; then
echoerr "This script only supports Ubuntu, Debian and CentOS."
exit 1
elif ! grep -qs -e "release 6" -e "release 7" /etc/redhat-release; then
echoerr "This script only supports CentOS 7 and 6."
exit 1
fi
os_type="CentOS"
fi
# Check for root permission
if [ "$(id -u)" != 0 ]; then
echoerr "Script must be run as root. Try 'sudo bash $0'"
exit 1
fi
# Check if the server has enough RAM
phymem="$(free | awk '/^Mem:/{print $2}')"
[ -z "$phymem" ] && phymem=0
if [ "$phymem" -lt 500000 ]; then
echoerr "A minimum of 512 MB RAM is required for Ghost blog install."
exit 1
fi
# Check for valid blog domain name (FQDN)
if [ "$1" = "" ] || [ "$1" = "BLOG_FULL_DOMAIN_NAME" ]; then
script_name=$(basename "$0")
echo "Usage: bash $script_name BLOG_FULL_DOMAIN_NAME (Replace with actual value)"
exit 1
fi
FQDN_REGEX='^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$'
if ! printf %s "$1" | grep -Eq "$FQDN_REGEX"; then
echoerr "Invalid parameter. You must enter a fully qualified domain name (FQDN)."
exit 1
fi
if id -u "ghost$max_blogs" >/dev/null 2>&1; then
echoerr "Maximum number of Ghost blogs ($max_blogs) reached."
exit 1
fi
ghost_num=1
ghost_user=ghost
ghost_port=2368
if id -u ghost >/dev/null 2>&1; then
echo 'It looks like this server already has Ghost blog installed! '
if [ -d "/var/www/$1" ]; then
echo
echo "To install additional blogs, you must use a new full domain name."
exit 1
fi
for count in $(seq 2 $max_blogs); do
if ! id -u "ghost$count" >/dev/null 2>&1; then
ghost_num="$count"
ghost_user="ghost$count"
ghost_port=$((ghost_port+count))
ghost_port=$((ghost_port-1))
break
fi
done
echo
read -r -p "Install another Ghost blog on this server? [y/N] " response
case $response in
[yY][eE][sS]|[yY])
echo
;;
*)
echo "Aborting."
exit 1
;;
esac
phymem_req=250
phymem_req1=$((phymem_req*ghost_num))
phymem_req2=$((phymem_req*ghost_num*1000))
if [ "$phymem" -lt "$phymem_req2" ]; then
echo "This server might not have enough RAM to install another Ghost blog."
echo "It is estimated that at least $phymem_req1 MB total RAM is required."
echo
echo 'WARNING! If you continue, the install could fail and your blog will NOT work!'
echo
read -r -p "Do you REALLY want to continue (at your own risk)? [y/N] " response
case $response in
[yY][eE][sS]|[yY])
echo
;;
*)
echo "Aborting."
exit 1
;;
esac
fi
fi
clear
cat <<EOF
Welcome! This script will install Ghost blog version 0.11.14
on your server, with Nginx (as a reverse proxy) and Naxsi WAF.
The full domain name for your new blog is:
>> $1 <<
Please double check. This MUST be correct for it to work!
IMPORTANT: DO NOT RUN THIS SCRIPT ON YOUR PC OR MAC!
It should only be used on a virtual private server (VPS) or dedicated server,
with *freshly installed* Ubuntu 16.04/14.04, Debian 9/8 or CentOS 7/6.
EOF
read -r -p "Confirm and proceed with the install? [y/N] " response
case $response in
[yY][eE][sS]|[yY])
echo
echo "Please be patient. Setup is continuing..."
echo
;;
*)
echo "Aborting."
exit 1
;;
esac
BLOG_FQDN=$1
# Create and change to working dir
mkdir -p /opt/src
cd /opt/src || exit 1
if [ "$os_type" = "CentOS" ]; then
# Add the EPEL repository
yum -y install epel-release || { echoerr "Cannot add EPEL repo."; exit 1; }
# We need some more software
yum --enablerepo=epel -y install unzip fail2ban gcc gcc-c++ make openssl-devel \
wget curl sudo libxml2-devel curl-devel httpd-devel pcre-devel \
|| { echoerr "'yum install' failed."; exit 1; }
# Create basic Fail2Ban rules
if [ ! -f /etc/fail2ban/jail.local ] ; then
cat > /etc/fail2ban/jail.local <<'EOF'
[ssh-iptables]
enabled = true
filter = sshd
action = iptables[name=SSH, port=ssh, protocol=tcp]
logpath = /var/log/secure
EOF
fi
else
# Update package index
export DEBIAN_FRONTEND=noninteractive
apt-get -yq update || { echoerr "'apt-get update' failed."; exit 1; }
# We need some more software
apt-get -yq install unzip fail2ban \
build-essential apache2-dev libxml2-dev wget curl sudo \
libcurl4-openssl-dev libpcre3-dev libssl-dev zlib1g-dev \
|| { echoerr "'apt-get install' failed."; exit 1; }
fi
# Insert required IPTables rules
if ! iptables -C INPUT -p tcp --dport 80 -j ACCEPT 2>/dev/null; then
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
fi
if ! iptables -C INPUT -p tcp --dport 443 -j ACCEPT 2>/dev/null; then
iptables -I INPUT -p tcp --dport 443 -j ACCEPT
fi
# Start Fail2ban
service fail2ban stop >/dev/null 2>&1
service fail2ban start
# Insert IPTables rules on boot
if ! grep -qs "ghost blog setup script" /etc/rc.local; then
if [ -f /etc/rc.local ]; then
if [ "$os_type" != "CentOS" ]; then
sed --follow-symlinks -i -e '/^exit 0/d' /etc/rc.local
fi
else
echo '#!/bin/sh' > /etc/rc.local
fi
cat >> /etc/rc.local <<'EOF'
# Added by ghost blog setup script
(sleep 15
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
iptables -I INPUT -p tcp --dport 443 -j ACCEPT
service fail2ban restart)&
EOF
if [ "$os_type" != "CentOS" ]; then
echo "exit 0" >> /etc/rc.local
fi
chmod +x /etc/rc.local
fi
# Next, we need to install Node.js.
# Ref: https://github.com/nodesource/distributions
if [ "$ghost_num" = "1" ] || [ ! -f /usr/bin/node ]; then
if [ "$os_type" = "CentOS" ]; then
curl -sL https://rpm.nodesource.com/setup_6.x | bash -
sed -i '/gpgkey/a exclude=nodejs' /etc/yum.repos.d/epel.repo
yum -y --disablerepo=epel install nodejs || { echoerr "Failed to install 'nodejs'."; exit 1; }
else
curl -sL https://deb.nodesource.com/setup_6.x | bash -
apt-get -yq install nodejs || { echoerr "Failed to install 'nodejs'."; exit 1; }
fi
fi
# To keep your Ghost blog running, install "forever".
npm install forever -g
# Create a user to run Ghost:
mkdir -p /var/www
useradd -d "/var/www/$BLOG_FQDN" -m -s /bin/false "$ghost_user"
# Stop running Ghost blog processes, if any.
su - "$ghost_user" -s /bin/bash -c "forever stopall"
# Create temporary swap file to prevent out of memory errors during install
# Do not create if OpenVZ VPS
swap_tmp="/tmp/swapfile_temp.tmp"
if [ ! -f /proc/user_beancounters ]; then
echo
echo "Creating temporary swap file, please wait ..."
echo
dd if=/dev/zero of="$swap_tmp" bs=1M count=512 2>/dev/null || /bin/rm -f "$swap_tmp"
chmod 600 "$swap_tmp" && mkswap "$swap_tmp" &>/dev/null && swapon "$swap_tmp"
fi
# Switch to Ghost blog user. We use a "here document" to run multiple commands as this user.
cd "/var/www/$BLOG_FQDN" || exit 1
sudo -u "$ghost_user" BLOG_FQDN="$BLOG_FQDN" ghost_num="$ghost_num" ghost_port="$ghost_port" HOME="/var/www/$BLOG_FQDN" /bin/bash <<'SU_END'
# Get the Ghost blog source, unzip and install.
ghost_url="https://github.com/TryGhost/Ghost/releases/download/0.11.14/Ghost-0.11.14.zip"
if ! wget -t 3 -T 30 -nv -O ghost-latest.zip "$ghost_url"; then
echo "Error: Cannot download Ghost blog source." >&2
exit 1
fi
unzip -o -qq ghost-latest.zip && /bin/rm -f ghost-latest.zip
# Fix for https://github.com/TryGhost/Ghost/issues/10244
sed -i -e '1322s/5\.1\.2/5.1.3/' -e '1324s/5\.1\.2/5.1.3/' npm-shrinkwrap.json
npm install --production
# Generate config file and make sure that Ghost uses your actual domain name
/bin/cp -f config.js config.js.old 2>/dev/null
sed "s/my-ghost-blog.com/$BLOG_FQDN/" <config.example.js >config.js
sed -i "s/port: '2368'/port: '$ghost_port'/" config.js
# We need to make certain that Ghost will start automatically after a reboot
cat > starter.sh <<'EOF'
#!/bin/sh
pgrep -u ghost -f "/usr/bin/node" >/dev/null
if [ $? -ne 0 ]; then
export PATH=/usr/local/bin:$PATH
export NODE_ENV=production
NODE_ENV=production forever start --sourceDir /var/www/YOUR.DOMAIN.NAME index.js >> /var/log/nodelog.txt 2>&1
else
echo "Already running!"
fi
EOF
# Replace placeholder with your actual domain name:
sed -i "s/YOUR.DOMAIN.NAME/$BLOG_FQDN/" starter.sh
if [ "$ghost_num" != "1" ]; then
sed -i "/^pgrep/s/ghost/ghost$ghost_num/" starter.sh
sed -i "s/nodelog\.txt/nodelog$ghost_num.txt/" starter.sh
fi
# Make the script executable with:
chmod +x starter.sh
# We use crontab to start this script after a reboot:
crontab -r 2>/dev/null
crontab -l 2>/dev/null | { cat; echo "@reboot /var/www/$BLOG_FQDN/starter.sh"; } | crontab -
SU_END
# Remove temporary swap file
[ -f "$swap_tmp" ] && swapoff "$swap_tmp" && /bin/rm -f "$swap_tmp"
# Check if Ghost blog download was successful
[ ! -f "/var/www/$BLOG_FQDN/index.js" ] && exit 1
# Create the logfile:
if [ "$ghost_num" = "1" ]; then
touch /var/log/nodelog.txt
chown ghost.ghost /var/log/nodelog.txt
else
touch "/var/log/nodelog$ghost_num.txt"
chown "ghost$ghost_num.ghost$ghost_num" "/var/log/nodelog$ghost_num.txt"
fi
if [ "$ghost_num" = "1" ] || [ ! -f /opt/nginx/sbin/nginx ]; then
# Download and extract Naxsi:
cd /opt/src || exit 1
wget -t 3 -T 30 -qO- https://github.com/nbs-system/naxsi/archive/0.55.3.tar.gz | tar xz
[ ! -d naxsi-0.55.3 ] && { echoerr "Cannot download Naxsi source."; exit 1; }
# Ref: https://github.com/nbs-system/naxsi/commit/c7e8cff427eaf6b6ece1b0b1c8ddaba11bb21d45
sed -i -e 's/127\.0\.0\.1:443/127.0.0.1:9200/' -e '/use_ssl/s/"True"/false/' naxsi-0.55.3/nxapi/nxapi.json
# Next we create a user for nginx:
if [ "$os_type" != "CentOS" ]; then
adduser --system --no-create-home --disabled-login --disabled-password --group nginx
else
useradd --system --no-create-home -s /bin/false -U nginx
fi
# Download and compile Nginx:
cd /opt/src || exit 1
wget -t 3 -T 30 -qO- https://nginx.org/download/nginx-1.14.2.tar.gz | tar xz
[ ! -d nginx-1.14.2 ] && { echoerr "Cannot download Nginx source."; exit 1; }
cd nginx-1.14.2 || exit 1
./configure --add-module=../naxsi-0.55.3/naxsi_src/ \
--prefix=/opt/nginx --user=nginx --group=nginx \
--with-http_ssl_module --with-http_v2_module --with-http_realip_module
make -s && make -s install
# Add Naxsi core rules
mkdir -p /etc/nginx
/bin/cp -f /opt/src/naxsi-0.55.3/naxsi_config/naxsi_core.rules /etc/nginx/
# Add Naxsi whitelist rules needed for Ghost blog.
# Ref: https://github.com/nbs-system/naxsi/wiki/whitelists
cat > /etc/nginx/mysite.rules <<'EOF'
#LearningMode; #Enables learning mode
SecRulesEnabled;
#SecRulesDisabled;
DeniedUrl "/RequestDenied";
## check rules
CheckRule "$SQL >= 8" BLOCK;
CheckRule "$RFI >= 8" BLOCK;
CheckRule "$TRAVERSAL >= 4" BLOCK;
CheckRule "$EVADE >= 4" BLOCK;
CheckRule "$XSS >= 8" BLOCK;
BasicRule wl:1000 "mz:$URL_X:^/ghost/api/v[0-9]+\.[0-9]+/users/([0-9]+/)?$|BODY|NAME";
BasicRule wl:1000 "mz:$URL_X:^/ghost/api/v[0-9]+\.[0-9]+/posts/([0-9]+/)?$|BODY|NAME";
BasicRule wl:1000 "mz:$URL_X:^/ghost/api/v[0-9]+\.[0-9]+/tags/([0-9]+/)?$|BODY|NAME";
BasicRule wl:1000,1001,1007,1008,1009,1010,1011,1013,1015 "mz:$URL_X:^/ghost/api/v[0-9]+\.[0-9]+/posts/([0-9]+/)?$|BODY";
BasicRule wl:1016,1100,1101,1205,1302,1303,1310,1311,1314 "mz:$URL_X:^/ghost/api/v[0-9]+\.[0-9]+/posts/([0-9]+/)?$|BODY";
BasicRule wl:1000,1001,1007,1008,1009,1010,1011,1013,1015 "mz:$URL_X:^/ghost/api/v[0-9]+\.[0-9]+/posts/([0-9]+/)?$|$BODY_VAR_X:^markdown$";
BasicRule wl:1016,1100,1101,1205,1302,1303,1310,1311,1314 "mz:$URL_X:^/ghost/api/v[0-9]+\.[0-9]+/posts/([0-9]+/)?$|$BODY_VAR_X:^markdown$";
BasicRule wl:1015 "mz:$URL_X:^/ghost/api/v[0-9]+\.[0-9]+/posts/$|ARGS";
BasicRule wl:1015 "mz:$URL_X:^/ghost/api/v[0-9]+\.[0-9]+/posts/$|$ARGS_VAR_X:^type$";
BasicRule wl:1015 "mz:$URL_X:^/ghost/api/v[0-9]+\.[0-9]+/settings/$|ARGS";
BasicRule wl:1015 "mz:$URL_X:^/ghost/api/v[0-9]+\.[0-9]+/settings/$|$ARGS_VAR_X:^type$";
BasicRule wl:1310,1311 "mz:$URL_X:^/ghost/api/v[0-9]+\.[0-9]+/users/password/$|BODY|NAME";
BasicRule wl:1310,1311 "mz:$URL_X:^/ghost/api/v[0-9]+\.[0-9]+/authentication/setup/$|BODY|NAME";
BasicRule wl:1310,1311 "mz:$URL_X:^/ghost/api/v[0-9]+\.[0-9]+/authentication/passwordreset/$|BODY|NAME";
BasicRule wl:1001,1015,1205,1302,1303,1310,1311 "mz:$URL_X:^/ghost/api/v[0-9]+\.[0-9]+/settings/$|BODY";
BasicRule wl:1001,1015,1205,1302,1303,1310,1311 "mz:$URL_X:^/ghost/api/v[0-9]+\.[0-9]+/settings/$|$BODY_VAR_X:^value$";
BasicRule wl:16 "mz:$URL_X:^/ghost/api/v[0-9]+\.[0-9]+/mail/test/$|BODY";
BasicRule wl:2 "mz:$URL_X:^/ghost/api/v[0-9]+\.[0-9]+/uploads/$|BODY";
BasicRule wl:15,1101 "mz:$URL_X:^/ghost/api/v[0-9]+\.[0-9]+/settings/$|BODY";
BasicRule wl:1010,1011 "mz:$URL_X:^/ghost/api/v[0-9]+\.[0-9]+/slugs/post/\(Untitled\)/$|URL";
BasicRule wl:0 "mz:$URL_X:^/ghost/api/v[0-9]+\.[0-9]+/users/password/$|BODY";
EOF
# Set up NXAPI (Naxsi log parser, whitelist & report generator)
# Ref: https://github.com/nbs-system/naxsi/tree/master/nxapi
cd /opt/src/naxsi-0.55.3/nxapi/ && python setup.py install
# Create the following files to make Nginx autorun:
if [ -d /etc/init ] && [ "$os_type" != "CentOS" ]; then
cat > /etc/init/nginx.conf <<'EOF'
# nginx
description "nginx http daemon"
author "Philipp Klose <me@[thisdomain].de>"
start on (filesystem and net-device-up IFACE!=lo)
stop on runlevel [!2345]
env DAEMON=/opt/nginx/sbin/nginx
env PID=/opt/nginx/logs/nginx.pid
expect fork
respawn
respawn limit 10 5
#oom never
pre-start script
$DAEMON -t
if [ $? -ne 0 ]
then exit $?
fi
end script
exec $DAEMON
EOF
fi
if grep -qs "release 6" /etc/redhat-release; then
# Ref: https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/
cat > /etc/init.d/nginx <<'EOF'
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: NGINX is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /opt/nginx/conf/nginx.conf
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/opt/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
EOF
chmod +x /etc/init.d/nginx
chkconfig nginx on
fi
if [ -d /lib/systemd/system ]; then
cat > /lib/systemd/system/nginx.service <<'EOF'
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/opt/nginx/logs/nginx.pid
ExecStartPre=/opt/nginx/sbin/nginx -t
ExecStart=/opt/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload 2>/dev/null
systemctl enable nginx.service 2>/dev/null
fi
fi
# Create the public folder which will hold robots.txt, etc.
mkdir -p "/var/www/$BLOG_FQDN/public"
# Download example Nginx configuration file
cd /opt/nginx/conf || exit 1
/bin/cp -f nginx.conf nginx.conf.old
if [ "$ghost_num" = "1" ]; then
example_conf1=https://github.com/hwdsl2/setup-ghost-blog/raw/master/conf/nginx-naxsi.conf
if ! wget -t 3 -T 30 -nv -O nginx.conf "$example_conf1"; then
echoerr "Cannot download example nginx.conf."
exit 1
fi
fi
example_conf2=https://github.com/hwdsl2/setup-ghost-blog/raw/master/conf/nginx-naxsi-include.conf
if ! wget -t 3 -T 30 -nv -O nginx-include.conf "$example_conf2"; then
echoerr "Cannot download example nginx-include.conf."
exit 1
fi
# Modify example configuration for use
if [ "$ghost_num" = "1" ]; then
/bin/cp -f nginx-include.conf nginx-blog1.conf
sed -i "s/YOUR.DOMAIN.NAME/$BLOG_FQDN/g" nginx-blog1.conf
else
/bin/cp -f nginx-include.conf "nginx-blog$ghost_num.conf"
sed -i -e "/127\.0\.0\.1:2368/s/2368/$ghost_port/" \
-e "s/ghost_upstream/ghost_upstream$ghost_num/" \
-e "s/YOUR.DOMAIN.NAME/$BLOG_FQDN/g" "nginx-blog$ghost_num.conf"
sed -i "/include nginx-blog1\.conf/a\ include nginx-blog$ghost_num.conf;" nginx.conf
fi
# Check the validity of the nginx.conf file:
echo; /opt/nginx/sbin/nginx -t; echo
# The output should look like:
# nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
# nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
# Make sure Nginx can access the blog files
chmod 755 "/var/www/$BLOG_FQDN"
# Finally, start Ghost blog and Nginx:
su - "$ghost_user" -s /bin/bash -c "./starter.sh"
service nginx stop 2>/dev/null
service nginx start
# Retrieve server IP for display below
PUBLIC_IP=$(wget -t 3 -T 15 -qO- http://ipv4.icanhazip.com)
cat <<EOF
==================================================================================
Setup is complete. Your new Ghost blog is now ready for use!
Ghost blog is installed in: /var/www/$BLOG_FQDN
Naxsi and Nginx config files: /etc/nginx and /opt/nginx/conf
Nginx web server logs: /opt/nginx/logs
[Next Steps]
You must set up DNS (A Record) to point $BLOG_FQDN to this server $PUBLIC_IP
Browse to http://$BLOG_FQDN/ghost (alternatively, set up SSH port forwarding
and browse to http://localhost:$ghost_port/ghost) to complete the initial configuration
of your blog. Choose a very secure password.
To restart this Ghost blog:
su - $ghost_user -s /bin/bash -c 'forever stopall; ./starter.sh'
To restart Nginx web server:
service nginx restart
(Optional) Check out my blog article for more configuration steps:
https://blog.ls20.com/install-ghost-0-4-with-nginx-and-naxsi-on-ubuntu/
Ghost docs: https://docs.ghost.org/v0.11/docs
Real-time chat: https://ghost.org/slack
Naxsi docs: https://github.com/nbs-system/naxsi/wiki
==================================================================================
EOF
}
## Defer setup until we have the complete script
ghost_blog_install "$@"
exit 0