Skip to content

Commit b12f5a4

Browse files
author
Jens Tirsvad Nielsen
committed
Bug fix
1 parent d58d7f9 commit b12f5a4

File tree

5 files changed

+127
-73
lines changed

5 files changed

+127
-73
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ Need a server with debian linux compatibel distibution and root access.
77
I am using a Linode VP server account. Get one here from 5$ a month https://www.linode.com/?r=a60fb437acdf27a556ec0474b32283e9661f2561
88

99
### First step prepare
10-
Default server setup:
10+
11+
Some common settings
1112

1213
locale-gen && apt -y install curl # Debian
1314

15+
Default server setup:
16+
1417
curl -L https://github.com/TirsvadCMS-Bashscripts/LinuxServerSetup/tarball/master | tar xz -C /root/ --strip-components 2
15-
./LinuxServerSetup/scripts/setup.sh
18+
cd LinuxServerSetup && ./setup
1619

1720
Manuel server setup:
1821

@@ -22,7 +25,7 @@ Manuel server setup:
2225
change settings.sh file as needed. If not, you will get a default server.
2326

2427
nano conf/settings.sh
25-
./scripts/setup.sh
28+
./setup.sh
2629

2730
Example of adding settings file to script
2831

src/LinuxServerSetup/main.sh

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
#!/bin/bash
22
set -euo pipefail
33
IFS=$'\t'
4-
NONINTERACTIVE="yes"
5-
export DEBIAN_FRONTEND="noninteractive"
64

75
# Setting some path
86
declare -r DIR="$( cd "$( dirname "$0" )" && pwd )"
97
declare -r DIR_TOOLS="$( cd "$DIR/tools" && pwd )"
108
declare -r DIR_CONF="$( cd "$DIR/conf" && pwd )"
11-
declare -r FILE_LOG="$( cd "$DIR/log" && pwd )/install.log"
9+
declare -r FILE_LOG="$( cd "$DIR/log" && pwd )/main.log"
1210

1311
# Put all output to logfile
1412
exec 3>&1 1>>${FILE_LOG} 2>&1
1513

16-
[ ! -f "$DIR_CONF/settings.sh" ] && cp "$DIR_CONF/settings.sh.default" "$DIR_CONF/settings.sh"
17-
1814
. $DIR_CONF/applicationVersions.sh
1915
. $DIR_CONF/settings.sh
2016
. $DIR_TOOLS/precheck.sh
2117
. $DIR_TOOLS/functions.sh
2218

19+
log_headline `basename "$0"`
20+
2321
while true
2422
do {
2523
ping -c1 www.google.com > /dev/null && break
@@ -28,8 +26,6 @@ do {
2826
}
2927
done
3028

31-
log "$( date +%T ) script was started"
32-
3329
[ ! $( which sudo ) ] && install_package sudo
3430

3531
# Setting default values
@@ -73,7 +69,7 @@ infoscreen "Setting" "public ipv6"
7369
# }
7470
# }
7571

76-
[ ! "${SSHD_PERMITROOTLOGIN:-}" == "yes" ] && {
72+
[[ ! ${SSHD_PERMITROOTLOGIN:-} == "yes" ]] && {
7773
[ ! -z "${USER_ID:-}" ] && [ ! -z "${USER_PASSWORD:-}" ] || { echo "User credential not set in config file"; exit 1; }
7874
[ ! "${SSHD_PASSWORDAUTH:-}" == "yes" ] && {
7975
[ ! "$SSHD_PASSWORDAUTH" == "yes" ] && [ -z "${USER_SSHKEY:-}" ] && [ ! -f $DIR_CONF/.ssh/keys ] && { echo -e "Global varible USER_SSHKEY not set in config file and there is no sshkey file.\nBut required as no password is acceptet for login"; exit 1; }
@@ -88,6 +84,16 @@ infoscreen "Setting" "public ipv6"
8884
} || infoscreenfailed
8985
}
9086

87+
###################################################################################
88+
# Check configuration
89+
###################################################################################
90+
[[ ( ${NGINX_INSTALL:-} == "on" && ${NGINX_COMPILE:-} == "on" ) ]] && {
91+
infoscreenfailed "Your settings.sh file have configuration error\nNGINX_INSTALL and NGINX_COMPILE can't both be set to 'on'"
92+
CHECK_ERROR=1
93+
}
94+
95+
[ ${CHECK_ERROR:-} ] && exit 1
96+
9197
###################################################################################
9298
# Servername
9399
###################################################################################
@@ -103,9 +109,9 @@ infoscreen "Setting" "public ipv6"
103109
###################################################################################
104110
# Creating a priviliged user
105111
###################################################################################
106-
[ ! -z ${USER_ID:-} ] && {
112+
[ ${USER_ID:-} ] && {
107113
infoscreen "Adding" "priviliged user ${USER_ID}"
108-
[ ! $( id -u "${USER_ID}" ) ] && useradd -create-home -s "$USER_SHELL" $( lower "$USER_ID" -p "$USER_PASSWORD" )
114+
[ ! $( id -u "${USER_ID}" ) ] && useradd --create-home -s "$USER_SHELL" $( lower "$USER_ID" -p "$USER_PASSWORD" )
109115
case $OS in
110116
"Debian GNU/Linux")
111117
adduser "$USER_ID" sudo
@@ -117,7 +123,7 @@ infoscreen "Setting" "public ipv6"
117123
usermod -aG wheel "$USER_ID"
118124
;;
119125
esac
120-
USER_HOME=`system_get_user_home "$USER_ID"`
126+
USER_HOME=$(system_get_user_home $USER_ID)
121127
[ ! -d "$USER_HOME/.ssh" ] && sudo -u "$USER_ID" mkdir "$USER_HOME/.ssh"
122128
[ -f $DIR_CONF/.ssh/keys ] && {
123129
cp -f $DIR_CONF/.ssh/keys $USER_HOME/.ssh/authorized_keys
@@ -131,7 +137,7 @@ infoscreen "Setting" "public ipv6"
131137
}
132138
infoscreendone
133139

134-
[ ! "$SSHD_PASSWORDAUTH" == "yes" ] && [ ! -f "$USER_HOME/.ssh/authorized_keys" ] && {
140+
[[ ! $SSHD_PASSWORDAUTH == "yes" ]] && [ ! -f "$USER_HOME/.ssh/authorized_keys" ] && {
135141
dialog --title "copy client " \
136142
--colors \
137143
--msgbox \
@@ -176,26 +182,27 @@ infoscreendone
176182
###################################################################################
177183
# Nginx
178184
###################################################################################
179-
[[ ! ("${NGINX_INSTALL:-}" == "on" && "${NGINX_COMPILE:-}" == "on" ) ]] && {
185+
[[ ${NGINX_COMPILE:-} == "on" ]] && {
180186

181-
[ ! $(id -u www-data) ] && useradd www-data --user-group -s /sbin/nologin
187+
$DIR_TOOLS/nginx.sh compile --nginx-ver $NGINX_VER
188+
install_package stunnel4
189+
[ -f $DIR_CONF/stunnel4/stunnel.conf ] && cp -f $DIR_CONF/stunnel4/stunnel.conf /etc/stunnel/
182190

183-
[ "${NGINX_INSTALL:-}" == "on" ] && {
184-
infoscreen "installing" "nginx"
185-
$DIR_TOOLS/nginx.sh install
186-
infoscreendone
187-
}
191+
[[ ( -f /srv/www/default/html/live.html && ! -z "${PUBLIC_IPV4:-}" ) ]] && sed -i "s@HOSTNAME_OR_IP@$PUBLIC_IPV4@g" /srv/www/default/html/live.html
192+
iptables -A INPUT -p tcp --dport 1935 -m state --state NEW,ESTABLISHED -j ACCEPT # rtmp for live broadcasting
193+
cat $DIR_CONF/nginx/rtmp.conf >> /etc/nginx/nginx.conf
194+
}
188195

189-
[ "${NGINX_COMPILE:-}" == "on" ] && {
190-
infoscreen "building" "nginx $NGINX_VER"
191-
$DIR_TOOLS/nginx.sh compile --nginx-ver $NGINX_VER
192-
infoscreendone
193-
install_package stunnel4
194-
[ -f $DIR_CONF/stunnel4/stunnel.conf ] && cp -f $DIR_CONF/stunnel4/stunnel.conf /etc/stunnel/
195-
}
196+
[[ ${NGINX_INSTALL:-} == "on" ]] && {
197+
$DIR_TOOLS/nginx.sh install
198+
[ -f /srv/www/default/html/live.html ] && rm /srv/www/default/html/live.html
199+
}
200+
201+
[[ ${NGINX_COMPILE:-} == "on" || ${NGINX_INSTALL:-} == "on" ]] && {
202+
[ ! $(id -u www-data) ] && useradd www-data --user-group -s /sbin/nologin
196203

197204
[ -f $DIR_CONF/nginx/nginx.conf ] && cp $DIR_CONF/nginx/nginx.conf /etc/nginx/
198-
[ -d $DIR_CONF/nginx/sites-available ] && cp $DIR_CONF/nginx/sites-available/* /etc/nginx/sites-available/
205+
[ -d $DIR_CONF/nginx/sites-available ] && cp -r $DIR_CONF/nginx/sites-available /etc/nginx/
199206

200207
find /etc/nginx/sites-available -type f -print0 | while IFS= read -r -d $'\0' file; do ln -s $file /etc/nginx/sites-enabled/ ; done
201208

@@ -214,33 +221,25 @@ infoscreendone
214221
ip6tables -A INPUT -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
215222
ip6tables -A INPUT -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
216223

217-
[ "${NGINX_COMPILE:-}" == "on" ] && {
218-
[[ ( -f /srv/www/default/html/live.html && ! -z "${PUBLIC_IPV4:-}" ) ]] && sed -i "s@HOSTNAME_OR_IP@$PUBLIC_IPV4@g" /srv/www/default/html/live.html
219-
iptables -A INPUT -p tcp --dport 1935 -m state --state NEW,ESTABLISHED -j ACCEPT # rtmp for live broadcasting
220-
cat $DIR_CONF/nginx/rtmp.conf >> /etc/nginx/nginx.conf
221-
} || {
222-
[ -f /srv/www/default/html/live.html ] && rm /srv/www/default/html/live.html
223-
}
224-
225224
systemctl daemon-reload
226225
systemctl restart nginx
227226
systemctl enable nginx
228227

229-
} || echo "Your settings.sh file have configuration error\nNGINX_INSTALL and NGINX_COMPILE can't both be set to 'on'"
228+
}
230229

231230
###################################################################################
232231
# LetsEncrypt
233232
###################################################################################
234-
[ "${LETSENCRYPT_INSTALL:-}" == "on" ] && {
233+
[[ ${LETSENCRYPT_INSTALL:-} == "on" ]] && {
235234
regex="^[a-z0-9!#\$%&'*+/=?^_\`{|}~-]+(\.[a-z0-9!#$%&'*+/=?^_\`{|}~-]+)*@([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+[a-z0-9]([a-z0-9-]*[a-z0-9])?\$"
236-
[[ ! ${LETSENCRYPT_EMAIL}=~$regex ]] && {
235+
[[ ! ${LETSENCRYPT_EMAIL} =~ $regex ]] && {
237236
log "LETSENCRYPT_EMAIL value is not a valid email adress"
238237
} || {
239-
[ "${NGINX_INSTALL:-}" == "on" ] && install_package python-certbot-nginx
240-
[ ! -Z "${NGINX_SITES_HOSTNAMES:-}"] && {
238+
[[ ${NGINX_INSTALL:-} == "on" ]] && install_package python-certbot-nginx
239+
[ ! -Z ${NGINX_SITES_HOSTNAMES:-} ] && {
241240
for HOSTNAME in "${NGINX_SITES_HOSTNAMES[@]}"
242241
do
243-
$DIR_TOOLS/nginx.sh add --domain $HOSTNAME --email $LETSENCRYPT_EMAIL
242+
# $DIR_TOOLS/nginx.sh add --domain $HOSTNAME --email $LETSENCRYPT_EMAIL
244243
log "ssl certificate for $HOSTNAME"
245244
done
246245
}
@@ -250,14 +249,14 @@ infoscreendone
250249
###################################################################################
251250
# Database
252251
###################################################################################
253-
[ "${POSTGRESQL_INSTALL:-}" == "yes" ] && {
252+
[[ ${POSTGRESQL_INSTALL:-} == "yes" ]] && {
254253
install_package progresql postgresql-contrib
255254
}
256255

257256
###################################################################################
258257
# Bash stuff
259258
###################################################################################
260-
[ "${BASH_STUFF:-}" == "on" ] && {
259+
[[ ${BASH_STUFF:-} == "on" ]] && {
261260
infoscreen "Setting" "bash stuff for root - $OS version $OS_VER"
262261
case $OS in
263262
'Debian GNU/Linux'|'Ubuntu')
@@ -269,13 +268,15 @@ infoscreendone
269268
infoscreendone
270269
}
271270

272-
[ ! "${NONINTERACTIVE:-}" == "yes" ] && {
273-
count_down 9
274-
}
275-
276271
###################################################################################
277272
# Extra scripts
278273
###################################################################################
279-
[ -f $DIR_CONF/autorun.sh ] && bash $DIR_CONF/autorun.sh
274+
[ -f $DIR_CONF/autorun.sh ] && . $DIR_CONF/autorun.sh
275+
276+
log_headline 'script finish at'
277+
278+
[[ ! ${NONINTERACTIVE:-} == "yes" ]] && {
279+
count_down 9
280+
}
280281

281282
reboot

src/LinuxServerSetup/setup.sh

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ declare -r DIR_TOOLS="$( cd "$DIR/tools" && pwd )"
1111
declare -r DIR_CONF="$( cd "$DIR/conf" && pwd )"
1212

1313
[ ! -d "$DIR/log" ] && mkdir "$DIR/log"
14-
declare -r FILE_LOG="$( cd "$DIR/log" && pwd )/install.log"
14+
declare -r FILE_LOG="$( cd "$DIR/log" && pwd )/setup.log"
1515

1616
# Put all output to logfile
1717
exec 3>&1 1>>${FILE_LOG} 2>&1
1818

1919
. $DIR_TOOLS/precheck.sh
2020
. $DIR_TOOLS/functions.sh
2121

22+
log_headline `basename "$0"`
23+
2224
usage(){
2325
echo "-u | --url <url> Url link to configuration" 1>&3
2426
echo "-s | --strip-components <number> Strip tarball" 1>&3
@@ -37,13 +39,17 @@ while [ $# -gt 0 ]; do
3739
-i | --interactive )
3840
NONINTERACTIVE="no"
3941
;;
42+
--curl-user-password )
43+
shift
44+
CURL_USER_PASSWORD=$1
45+
;;
4046
--curl-user )
41-
CURL_USER=$1
4247
shift
48+
CURL_USER=$1
4349
;;
4450
--curl-password )
45-
CURL_PASSWORD=$1
4651
shift
52+
CURL_PASSWORD=$1
4753
;;
4854
-u | --url )
4955
shift
@@ -60,16 +66,25 @@ while [ $# -gt 0 ]; do
6066
shift
6167
done
6268

69+
[ NONINTERACTIVE == "yes" ] && export DEBIAN_FRONTEND="noninteractive"
70+
6371
install_package ntpdate
6472
ntpdate -s time.nist.gov
6573

6674
cd /root/LinuxServerSetup/
6775
regex='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
6876
[[ ${URL_SETTINGS:-} =~ $regex ]] && {
6977
[ ! ${STRIP_COMPONENTS:-} ] && STRIP_COMPONENTS=0
70-
log 'Downloading the settings file fram $URL_SETTINGS'
71-
[ $CURL_USER && $CURL_PASSWORD ] && curl -L --user $CURL_USER:$CURL_PASSWORD $URL_SETTINGS | tar xz -C /root/LinuxServerSetup --strip-components $STRIP_COMPONENTS ||
72-
curl -L $URL_SETTINGS | tar xz -C /root/LinuxServerSetup --strip-components $STRIP_COMPONENTS
78+
log "Downloading the settings file fram $URL_SETTINGS"
79+
[ ${CURL_USER_PASSWORD:-} ] && {
80+
CURL_USER=${CURL_USER_PASSWORD%:*}
81+
CURL_PASSWORD=${CURL_USER_PASSWORD#*:}
82+
}
83+
[[ ${CURL_USER:-} && ${CURL_PASSWORD:-} ]] && {
84+
curl -L --user $CURL_USER:$CURL_PASSWORD $URL_SETTINGS | tar -xz -C /root/LinuxServerSetup --strip-components $STRIP_COMPONENTS
85+
} || {
86+
curl -L $URL_SETTINGS | tar xz -C /root/LinuxServerSetup --strip-components $STRIP_COMPONENTS
87+
}
7388
} || {
7489
[ ! -f $DIR_CONF/settings.sh ] && {
7590
log 'Downloading the default settings file from https://github.com/TirsvadCMS-Bashscripts/LinuxServerSetupDefaultConfig/tarball/master'

src/LinuxServerSetup/tools/functions.sh

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ BROWN='\033[0;33m'
1010
BLUE='\033[0;34m'
1111
YELLOW='\033[1;33m'
1212
WHITE='\033[0;37m'
13+
FILLING=" : ............................................................................."
1314

1415
infoscreen() {
15-
printf "%-.80s" $(printf "${BROWN}$1 ${BLUE}${2-:} : .................................................................................${NC}") 1>&3
16-
log $1 ${2-:}
16+
printf "%-.80b" "${BROWN}$1 ${BLUE}${2:-}${FILLING}" 1>&3
17+
log "$1" ${2:-}
1718
}
1819

1920
infoscreendone() {
@@ -29,19 +30,29 @@ infoscreenfailed() {
2930
printf " ${RED}FAILED${NC}\n" 1>&3
3031
log_error "FAILED"
3132
} || {
32-
printf " ${RED}${1-:}${NC}\n" 1>&3
33+
printf " ${RED}${1:-}${NC}\n" 1>&3
3334
log_error ${1}
3435
}
3536
}
3637

37-
_scriptname=$0
38-
_scriptname=$( basename $_scriptname )
39-
_scriptname=${_scriptname%.*}
40-
FILE_LOGGER="$( cd "$DIR/log" && pwd )/$_scriptname.log"
38+
[ ! ${FILE_LOGGER_NAME:-} ] && FILE_LOGGER_NAME=install
39+
FILE_LOGGER="$( cd "$DIR/log" && pwd )/$FILE_LOGGER_NAME.log"
4140
touch $FILE_LOGGER
4241

4342
log(){
44-
[ ${2+x} ] && printf "%-.80s" $(printf "${BROWN}$1 ${BLUE}${2} : .................................................................................${NC}") >> $FILE_LOGGER || printf "${YELLOW}${1}${NC}\n" >> $FILE_LOGGER
43+
[ ${2:-} ] && {
44+
printf "%-.80b" "${BROWN}$1 ${BLUE}${2:-}${FILLING}" >> $FILE_LOGGER
45+
} || {
46+
printf "${NC}${1}${NC}\n" >> $FILE_LOGGER
47+
}
48+
}
49+
50+
log_headline(){
51+
var=$(date +"%T %d-%m-%Y")
52+
printf "${BLUE}**${NC}\n" >> $FILE_LOGGER
53+
printf "${BLUE}* ${1:-} ${var}${NC}\n" >> $FILE_LOGGER
54+
printf "${BLUE}**${NC}\n" >> $FILE_LOGGER
55+
unset var
4556
}
4657

4758
log_succes(){
@@ -111,12 +122,24 @@ count_down() {
111122
install_package() {
112123
case $PACKAGE_HANDLER in
113124
"apt" )
114-
infoscreen "Installing" "$@"
115-
[[ $# -gt 0 ]] && apt-get -qq install "$@"
116-
[[ $? -eq 0 ]] && infoscreendone || infoscreenfailed
125+
[[ $# -gt 0 ]] && {
126+
for var in $@
127+
do
128+
infoscreen "installing" "$var"
129+
DEBIAN_FRONTEND=noninteractive apt-get -qq install $var
130+
[[ $? -eq 0 ]] && infoscreendone || infoscreenfailed
131+
done
132+
}
117133
;;
118134
"dnf" )
119-
[[ $# -gt 0 ]] && dnf -y -q install "$@"
135+
[[ $# -gt 0 ]] && {
136+
for var in $@
137+
do
138+
infoscreen "installing" "$var"
139+
dnf -y -q install $var
140+
[[ $? -eq 0 ]] && infoscreendone || infoscreenfailed
141+
done
142+
}
120143
;;
121144
* )
122145
exit 1;

0 commit comments

Comments
 (0)