-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate-server.sh
executable file
·288 lines (250 loc) · 8.73 KB
/
create-server.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
#!/bin/bash
# File: create_server.sh
# Before sourcing this file:
# 1. If you have elected to change the Access Point's IP address
# then you should change the '10.10.10.10' IP address in the
# "export AP_IP="10.10.10.10" line early in the script.
# to the address you choose. Corresponding changes should have
# been or will still need to be made in dnsmasq.conf and
# interfaces (and possibly elsewhere as well.)
# 2. Near the end, you'll see comments pertaining to an entry
# in the `/etc/fstab` file; specifically `LABEL=Static`. You
# may want to change the `LABEL` to something other than
# "Static" to suit your own purposes.
echo "Begin create_server.sh script: $(date)"
echo "Assign AP_IP - Access Point IP address..."
if [ -z "$AP_IP" ]
then
export AP_IP="10.10.10.10"
echo "...defaults to $AP_IP"
else
echo "...set to $AP_IP (by config file)"
fi
if [ -a /etc/hosts.original ]
then
echo "/etc/hosts.original exists so we assume additions"
echo "have already been made to /etc/hosts."
else
echo "Saving a copy of the original /etc/hosts file..."
if sudo cp /etc/hosts /etc/hosts.original
then
echo "...successfull copy to /etc/hosts.original."
else
echo "... cp /etc/hosts => /etc/hosts.original failed!"
fi
fi
echo "Assigning URLs..."
echo "...for pathagar..."
if [ -z "$LIBRARY_URL" ]
then
export LIBRARY_URL='library.lan'
echo "...using default: $LIBRARY_URL"
else
echo "...from config: $LIBRARY_URL"
fi
if [ -z "$RACHEL_URL" ]
then
export RACHEL_URL="rachel.lan"
echo "...using default: $RACHEL_URL"
else
echo "...from config: $RACHEL_URL"
fi
echo "Append a (IP > URLs) line to /etc/hosts..."
if sudo sh -c "echo $AP_IP $LIBRARY_URL $RACHEL_URL >> /etc/hosts"
# the following are two alternative ways of doing the same thing.
# the first has been tested, the second has not.
# sudo -E sh -c 'echo "$AP_IP $LIBRARY_URL $RACHEL_URL" >> /etc/hosts'
# echo "$AP_IP $LIBRARY_URL $RACHEL_URL"|sudo tee -a /etc/hosts >/dev/null
# See footnote by Aaron at end of file.
then
echo "... success appending line to /etc/hosts."
else
echo "... appending line to /etc/hosts FAILED! TERMINATING!"
fi
# The entry
# $AP_IP $LIBRARY_URL $RACHEL_URL
# in /etc/hosts will direct WiFi dhcp clients to server.
# The ultimate goal is to have
# $LIBRARY_URL directed to pathagar book server
# and $RACHEL_URL directed to static content server.
echo "Prepare a mount point for the Static Content..."
if [ -z "$MOUNT_POINT" ]
then
export MOUNT_POINT="/mnt/Static"
echo "...defaults to /mnt/Static..."
else
echo "...set to $MOUNT_POINT (by config file)"
fi
if [ -d "$MOUNT_POINT" ]
then
echo "...Warning: directory $MOUNT_POINT already exists!"
else
echo "Creating a $MOUNT_POINT directory..."
if sudo mkdir $MOUNT_POINT
then
echo "... success."
echo "Assign ownership..."
if [ -z "$MAIN_USER" ]
then
export MAIN_USER="${USER}"
echo "...defaults to $USER..."
else
echo "...set to $MAIN_USER (by config file)"
fi
echo "Change its ownership to user $MAIN_USER..."
if sudo chown "${MAIN_USER}:${MAIN_USER}" "${MOUNT_POINT}"
then
echo "...ownership successfully changed to '$MAIN_USER'"
else
echo "...change of ownership FAILED! TERMINATING!"
fi
else
echo "... creation of /mnt/Static directory FAILED! TERMINATING!"
fi
fi
echo "Prepeare a directory for static content..."
echo "Assign DIR4STATIC variable..."
if [ -z "$DIR4STATIC" ]
then
export DIR4STATIC="/var/www/static"
echo "...defaults to '/var/www/static'..."
else
echo "...set to '$DIR4STATIC' (by config file)"
fi
if [ -d $DIR4STATIC ]
then
echo "Warning: directory '$DIR4STATIC' already exists!"
else
# The following directory is created to host content
# for the static content server.
echo "Creating $DIR4STATIC directory..."
if sudo mkdir "$DIR4STATIC"
then
echo "... $DIR4STATIC created."
echo "Changing its ownership to user '$MAIN_USER'..."
if sudo chown "${MAIN_USER}:${MAIN_USER}" "$DIR4STATIC"
then
echo "... ownership successfully changed."
else
echo "... failure of ownership change!"
fi
else
echo "...failure of directory creation! Teminating!"
fi
fi
# If get an error about resolving host name, check that the correct
# host name appears in /etc/hosts:
# 127.0.0.1 localhost localhost.localdomain <hostname>
# 127.0.1.1 <hostname>.
# and that /etc/hostname contains the correct <hostname>
echo "Setting up the Apache Server..."
if [ -f /etc/apache2/sites-available/static.conf ]
then
echo "Warning: /etc/apache2/sites-available/static.conf exists!"
else
echo " 1. Copy static.conf to sites-available..."
if sudo cp static.conf /etc/apache2/sites-available/static.conf
then
echo " ... successful copy."
else
echo " ... copy failed!"
fi
fi
echo "Copy an index.html file; get a sample site running..."
# thus providing an opportunity to test that all is well before
# copying over the static content:
if [ -f ${DIR4STATIC}/index.html ]
then
echo "Warning: ${DIR4STATIC}/index.html exists!"
else
echo " 1. Copy to ${DIR4STATIC}/index.html..."
if cp html-index-file ${DIR4STATIC}/index.html
then
echo " ...copy successful"
else
echo " ...copy command failed!"
fi
fi
echo "Dissable Apache's default site..."
if sudo a2dissite 000-default
then
echo "...default site disabled."
else
echo "...Failed to disable default site!!"
fi
echo " |vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv|"
echo " | Expect a warning: |"
echo " | To activate the new configuration, you need to run: |"
echo " | systemctl reload apache2. |"
echo " | |"
echo " | The upcoming reboot will accomplish the same thing. |"
echo " |^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|"
echo "Enable the static site..."
if sudo a2ensite static
then
echo "...static site enabled"
else
echo "...Failed to enable static site!"
fi
echo "Reload apache..."
if sudo service apache2 reload
then
echo "...successful reload."
else
echo "...reload FAILED!"
fi
# Not sure why but may get the following error:
# Warning: Unit file of apache2.service changed on disk, 'systemctl
# daemon-reload' recommended.
# A reboot will likely solve the problem.
# At this point, expect that the test site will be visible at
# rachel.lan. Will still need to replace test site index.html
# with the full Static Content.
# If the Static Content is provided on an ext4 formatted
# USB device with LABEL=Static, the following will cause
# it to be automatically mounted:
echo "Customizing /etc/fstab..."
if [ -a /etc/fstab.original ]
then
echo "...Warning: /etc/fstab.original already exists!"
else
echo " 1. Save a copy of the original /etc/fstab file..."
if sudo cp /etc/fstab /etc/fstab.original
then
echo " .../etc/fstab.original saved."
echo " 2. Add a 'LABEL=Static ... line to /etc/fstab..."
# shellcheck disable=SC2016
if sudo sh -c 'echo "LABEL=Static $MOUNT_POINT ext4 nofail 0 0" >> /etc/fstab'
then
echo " ... successfully added the line."
else
echo " ... Appending the line FAILED!"
fi
else
echo " ...failure to save /etc/fstab.original!"
fi
# echo "LABEL=Static $MOUNT_POINT ext4 nofail 0 0"|
# sudo tee -a /etc/fstab >/dev/null
fi
echo "End create-server.sh script: $(date)"
sudo shutdown -r now
## Foot Note:
# As Michael explains, it really comes down to the quoting you're
# using and which shell the command is being evaluated in.
# sudo sh -c "echo $AP_IP library library.lan rachel rachel.lan >> /etc/hosts"
# I would recommend this one as the "correct" solution. IMHO it's
# the simplest one and avoids `sudo -E`. The `$AP_IP` is evaluated
# in the current shell. You don't need to export the variable
# because the sub-shells don't need to read the variable and never
# see it. They already have the value within the command. `sh` sees
# the full command with the shell redirection. With sudo, `sh` has
# the proper permissions to follow the redirection and write to
# `/etc/hosts`.
# In general I would say `sudo -E` is a poor pattern and should be
# avoided. You can imagine this being problematic:
# PATH=/tmp/evil/bin:$PATH sudo -E echo foo
# This is why even with `sudo -E`, sudo still blocks certain
# environment variables (like PATH) based on the security policy.
# --
# Aaron D Borden <[email protected]>
# Human and Hacker