-
Notifications
You must be signed in to change notification settings - Fork 9
/
script.sh
326 lines (254 loc) · 9.54 KB
/
script.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
#!/bin/bash
#Version: 0.0.1
#Script: Shell Script for Server Setup for Nginx
#Description: This is for configuring nginx, php and permission management.
# Some commands may not be executed successfully due to permission issue.
# This file is built for root user not others. To modify for others users , simply add 'sudo' as prefix
# of every command
# Issues and Security: This file is built for only single use. You are also free to use.
# You are fully responsible for if any damage happened due to use of this bash script. So, use it
# at your own risk
function run_script() {
if cd /var/www; then
echo "=========================================================="
echo "Welcome to Shell for Server Setup Nginx"
echo "Created By: Ariful Islam"
echo "----Version: 0.0.2---"
echo "--https://github.com/arif98741----"
echo "============================================================"
#variables for using in bash script
#this is sensitive information. Please keep your head cool before changing this variables value
project='something.com' #your project name. you should change it according based on domain name
clone_dir="/var/www/html/$project" #where to clone . no need to change
clone_url='[email protected]:arif98741/something.git' #your git repository url example: [email protected]:user/repo.git
ssh_email='[email protected]' #this should be your github account email
server='nginx'
php_version="8.1" # refers to php 8.1; In same way you can install 7.2, 7.3, 7.4, 8.0, 8.1, 8.2
permission_755=755
echo
echo "=========================================================="
echo "Step1: SSH key generate"
echo "============================================================"
echo
echo "Generate SSH Key. After Generating please copy and paste it to "
echo
echo 'https://github.com/settings/keys'
# shellcheck disable=SC2162
read -p "Do you want to generate ssh-key, yes/no: " decision
if [ "$decision" = 'yes' ]; then
echo "Generating SSH-Key for User $USER"
echo
ssh-keygen -t rsa -b 4096 -C "$ssh_email"
echo "Successfully Generated Key for user $USER"
echo
fi
echo
ssh-keygen -y
cat ~/.ssh/id_rsa.pub
echo
echo "Cloning $project from $clone_dir at $clone_dir"
git clone $clone_url $clone_dir
#if else condition for running further step
# shellcheck disable=SC2162
read -p "Have you copied and paste ssh key to github, yes/no: " decision
if [ "$decision" = 'yes' ]; then
echo "Excellent Boss!. "
echo
echo 'Running further script like nginx and so on ....'
echo
echo "=========================================================="
echo "Step2: PHP Installation"
echo "============================================================"
# shellcheck disable=SC2162
read -p "Do you want to install php, yes/no: " decision
if [ "$decision" = 'yes' ]; then
echo "Enter your desired PHP version : "
#EOF
echo
echo "1: PHP 8.2
2: PHP 8.1
3: PHP 8.0
4: PHP 7.4
5: PHP 7.3
6: PHP 7.2
"
#EOF
# shellcheck disable=SC2162
read -p "select 1 to 6: " selector
#echo "You have selected "$selector
#EOF
case $selector in
1)
# shellcheck disable=SC2162
read -p " " phpversion
echo -n "You have selected PHP 8.2"
;;
2)
# shellcheck disable=SC2162
read -p " " phpversion
echo -n "You have selected PHP 8.1"
;;
3)
# shellcheck disable=SC2162
read -p " " phpversion
echo -n "You have selected PHP 8.0"
;;
4)
# shellcheck disable=SC2162
read -p " " phpversion
echo -n "You have selected PHP 7.4"
;;
5)
# shellcheck disable=SC2162
read -p " " phpversion
echo -n "You have selected PHP 7.3"
;;
6)
# shellcheck disable=SC2162
read -p " " phpversion
echo -n "You have selected PHP 7.2"
;;
*)
phpversion=$php_version
echo -n "You have not selected any PHP versions. Now installing default PHP version declared at the top of script ".$phpversion
;;
esac
echo
echo "Installing php $phpversion"
echo
# shellcheck disable=SC2091
$(php_installation $phpversion)
echo "Successfully Installed PHP $phpversion"
echo
fi
echo "=========================================================="
echo "Step3: Composer Installation"
echo "============================================================"
# shellcheck disable=SC2162
read -p "Do you want to install composer, yes/no: " decision
if [ "$decision" = 'yes' ]; then
echo "Installing composer"
echo
# shellcheck disable=SC2091
$(composer_installation)
echo
fi
echo "=========================================================="
echo "Step4: Nginx Installation"
echo "============================================================"
# shellcheck disable=SC2162
read -p "Do you want to install nginx, yes/no: " decision
if [ "$decision" = 'yes' ]; then
echo "Installing Nginx"
echo
echo
sudo apt update
sudo apt install nginx
echo
echo "Nginx Successfully Installed"
echo
nginx -v
echo
echo "Showing Ufw Permissions"
ufw app list
echo
echo "Allowing Ufw Permissions"
echo
ufw allow 'Nginx HTTP'
echo
echo "Nginx HTTP Allowed Successfully. Listed Below"
ufw status
echo
echo "==================================Showing Server Status================================"
#systemctl status nginx
echo
echo "Booting.."
systemctl restart nginx
echo
echo "Nginx Booted..."
echo
fi
echo "Adding Permission directory"
# shellcheck disable=SC2086
chown -R $USER:"$USER" $clone_dir
echo
echo "Now You are setting step of Creating Server Block"
echo
echo "Permission for $clone_dir"
# shellcheck disable=SC2086
chown -R $USER:"$USER" "/var/www/html"
chmod -R $permission_755 $clone_dir
echo
echo "Permission given to $clone_url"
echo
echo "=========================================================="
echo "Step5: Manage Nginx Server Block"
echo "============================================================"
echo
# shellcheck disable=SC2162
read -p "Do you want to manage nginx server block, yes/no " decision
if [ "$decision" = 'yes' ]; then
echo \
echo 'Creating server block'
echo
# shellcheck disable=SC2162
read -p "Do you want to copy pre-built default file? yes/no: " copydecision
if [ "$copydecision" = 'yes' ]; then #this will handle whether new file will be created or not
#nano /var/www/etc
cp /etc/nginx/sites-available/default /etc/nginx/sites-available/$project.".conf"
echo "New server block added as virtualhost for project: $project"
echo
nano /etc/nginx/sites-available/$project.".conf"
echo "Activating domain: "
echo
# shellcheck disable=SC2226
ln -s /etc/nginx/sites-available/$project.".conf" /etc/nginx/sites-enabled/
echo "Activated domain: $project"
echo
else
echo "You are editing the default nginx config"
nano /etc/nginx/sites-available/default
fi
#nginx status
nginx -t
# shellcheck disable=SC2162
read -p "Have you done editing? yes/no: " done_editing
if [ "$done_editing" = 'yes' ]; then
echo "Congratulations you have done editing configuration file"
fi
nginx -t
systemctl restart nginx
else
echo 'Server Block Management ignored!'
fi
echo 'Script Execution Successfully Completed'
fi
else
echo "Could not run script on this directory. Run this script in /var/www"
fi
}
# install php
function php_installation() { #accepts one param version. don't change here. Change from php_version at the top of this script
sudo apt install software-properties-common apt-transport-https -y
sudo add-apt-repository ppa:ondrej/php -y
apt-get install php"$1" -y
# shellcheck disable=SC2086
apt-get install php$1-fpm php$1-cli php$1-mysql php$1-curl php$1-json php$1-mbstring php$1-zip -y
systemctl start php"$1"-fpm
# shellcheck disable=SC2086
systemctl enable php$1-fpm
}
# install composer
function composer_installation() { #accepts one param version. don't change here. Change from composer_Version at the top of this script
curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
# shellcheck disable=SC2034
# shellcheck disable=SC2006
HASH=$(curl -sS https://composer.github.io/installer.sig)
php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer is verified'; } else { echo 'Installer is corrupted'; unlink('composer-setup.php'); } echo PHP_EOL;"
sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
echo
echo "Successfully installed composer!. "
echo
composer --version
}
run_script