/
lampwithdb.sh
187 lines (162 loc) · 4.63 KB
/
lampwithdb.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
#!/bin/bash
TEXTDOMAIN=lemp
function install_packages()
{
apt-get update && apt-get install -y --allow-unauthenticated ${@}
echo 'Package installation completed'
}
## Usage
print_help(){
cat <<EOH
Usage: $0 [-h] [-n]
-h | This help menu
-n | not installing MySQL
EOH
exit 1
}
nginx=/var/www/html
### nginx restart command
restartnginx='systemctl reload nginx'
### set default nginx server block
nginxdefault='/etc/nginx/sites-available/default'
###check for root
if [ "$(whoami)" != 'root' ]; then
echo $"You don't have the permission to run $0 as non-root user. Sudo maybe?"
exit 1;
fi
###Install all necessary things!
function mysqlinstall(){
apt install nginx curl mariadb-server redis-server -y
}
function nomysql(){
apt install nginx curl redis-server -y
}
function ipaddr(){
###Get IP/domain server
echo -n "Enter your Server IP or domain and press [ENTER]: "
read IP
}
### edit
function editnginx(){
if ! echo "server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name $IP;
location / {
try_files \$uri \$uri/ =404;
}
location ~ \.php\$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}" >| $nginxdefault
then
echo -e $"There is an ERROR create $nginxdefault file"
exit;
else
echo -e $"New $nginxdefault Created."
fi
### make sure nginx can start with new config
nginx -t
### restart nginx
$restartnginx
}
function secureinstall(){
echo -e "\n\nValidate password sometimes messed up your password make sure to choose which suit you well...."
secure_mysql='mysql_secure_installation'
$secure_mysql
}
function DBconfig(){
# If /root/.my.cnf exists then it won't ask for root password
if [ -f /root/.my.cnf ]; then
echo "Please enter the NAME of the new database! (example: database1)"
read dbname
echo "Please enter the database CHARACTER SET! (example: latin1, utf8, ...)"
read charset
echo "Creating new database..."
mysql -e "CREATE DATABASE ${dbname} /*\!40100 DEFAULT CHARACTER SET ${charset} */;"
echo "Database successfully created!"
echo "Showing existing databases..."
mysql -e "show databases;"
echo ""
echo "Please enter the NAME of the new database user! (example: user1)"
read username
echo "Please enter the PASSWORD for the new database user!"
read userpass
echo "Creating new user..."
mysql -e "CREATE USER ${username}@localhost IDENTIFIED BY '${userpass}';"
echo "User successfully created!"
echo ""
echo "Granting ALL privileges on ${dbname} to ${username}!"
mysql -e "GRANT ALL PRIVILEGES ON ${dbname}.* TO '${username}'@'localhost';"
mysql -e "FLUSH PRIVILEGES;"
echo "You're good now :)"
# If /root/.my.cnf doesn't exist then it'll ask for root password
else
echo "Please enter root user MySQL password!"
read rootpasswd
echo "Please enter the NAME of the new database! (example: database1)"
read dbname
echo "Please enter the database CHARACTER SET! (example: latin1, utf8, ...)"
read charset
echo "Creating new database..."
mysql -uroot -p${rootpasswd} -e "CREATE DATABASE ${dbname} /*\!40100 DEFAULT CHARACTER SET ${charset} */;"
echo "Database successfully created!"
echo "Showing existing databases..."
mysql -uroot -p${rootpasswd} -e "show databases;"
echo ""
echo "Please enter the NAME of the new database user! (example: user1)"
read username
echo "Please enter the PASSWORD for the new database user!"
read userpass
echo "Creating new user..."
mysql -uroot -p${rootpasswd} -e "CREATE USER ${username}@localhost IDENTIFIED BY '${userpass}';"
echo "User successfully created!"
echo ""
echo "Granting ALL privileges on ${dbname} to ${username}!"
mysql -uroot -p${rootpasswd} -e "GRANT ALL PRIVILEGES ON ${dbname}.* TO '${username}'@'localhost';"
mysql -uroot -p${rootpasswd} -e "FLUSH PRIVILEGES;"
echo "You're good now :)"
fi
}
function redissetup(){
echo "Enter your redis password: "
read redispass
sed -i 's|^supervised no|supervised systemd|g' /etc/redis/redis.conf
sed -i "s|^# requirepass foobared|requirepass $redispass|g" /etc/redis/redis.conf
systemctl restart redis
}
function trywrite(){
if ! echo $domain > $nginx/index.html
then
echo $"ERROR: Unable to write in $nginx/. Please check permissions."
exit;
else
echo $"Added index.html into $nginx/"
fi
}
if [ "$1" = '-h' ]; then
print_help
fi
if [ "$1" = '-n' ]; then
install_packages
ipaddr
nomysql
editnginx
redissetup
trywrite
else
install_packages
ipaddr
mysqlinstall
secureinstall
editnginx
DBconfig
redissetup
trywrite
fi