-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetphp.sh
More file actions
277 lines (228 loc) · 13.8 KB
/
Copy pathgetphp.sh
File metadata and controls
277 lines (228 loc) · 13.8 KB
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
#!/usr/bin/env bash
# Install Homebrew requirements for Linux
if [[ "${OSTYPE}" == "linux-gnu"* ]]; then
if ! command -v gcc &> /dev/null; then sudo apt install -y build-essential; fi
if ! command -v ps &> /dev/null; then sudo apt install -y procps; fi
if ! command -v file &> /dev/null; then sudo apt install -y file; fi
if ! command -v git &> /dev/null; then sudo apt install -y git; fi
fi
# ========================================
# --- ENVIRONMENT CONFIGURATION ----------
# ========================================
REMOTE_URL='https://raw.githubusercontent.com/getphporg/getphp/HEAD/getphp.sh'
BASE_DIR="${HOME}/getphp"
DOC_ROOT="${BASE_DIR}/www"
LOGS_DIR="${BASE_DIR}/logs"
ESC='\033'
RED="${ESC}[31m"
GREEN="${ESC}[32m"
CYAN="${ESC}[36m"
BOLD="${ESC}[1m"
UNDERLINE="${ESC}[4m"
RESET="${ESC}[0m"
HOMEBREW=0
APACHE=0
MYSQL=0
PHP=0
PHPMYADMIN=0
STACK=0
printf "\n"
printf "┌────────────────────────────────────┐\n"
printf "│ _ ____ _ _ ____ │\n"
printf "│ __ _ ___| |_| _ \| | | | _ \ │\n"
printf "│ / _\` |/ _ \ __| |_) | |_| | |_) | │\n"
printf "│ | (_| | __/ |_| __/| _ | __/ │\n"
printf "│ \__, |\___|\__|_| |_| |_|_| │\n"
printf "│ |___/${CYAN} www.getPHP.org${RESET} │\n"
printf "└────────────────────────────────────┘\n"
printf "\n"
brew --version &> /dev/null && HOMEBREW=1 && BREW_PREFIX=$(brew --prefix) || BREW_PREFIX='/notfound'
printf "Your PHP Stack:\n"
printf "~~~~~~~~~~~~~~~\n"
printf "Apache ${CYAN}------->${RESET} "
brew list --versions httpd &> /dev/null && brew list --versions httpd | awk '{print $2}' && APACHE=1 || printf "${RED}not installed${RESET}\n"
printf "MySQL ${CYAN}-------->${RESET} "
brew list --versions mysql &> /dev/null && brew list --versions mysql | awk '{print $2}' && MYSQL=1 || printf "${RED}not installed${RESET}\n"
printf "PHP ${CYAN}---------->${RESET} "
brew list --versions php &> /dev/null && brew list --versions php | awk '{print $2}' && PHP=1 || printf "${RED}not installed${RESET}\n"
printf "phpMyAdmin ${CYAN}--->${RESET} "
brew list --versions phpmyadmin &> /dev/null && brew list --versions phpmyadmin | awk '{print $2}' && PHPMYADMIN=1 || printf "${RED}not installed${RESET}\n"
if [[ $HOMEBREW == 1 && $APACHE == 1 && $MYSQL == 1 && $PHP == 1 && $PHPMYADMIN == 1 ]]; then
STACK=1
fi
printf "\n"
printf "Service Status:\n"
printf "~~~~~~~~~~~~~~~\n"
printf "Apache ${CYAN}------->${RESET} " && [[ $APACHE == 1 ]] && brew services | grep httpd | awk '{print $2}' || printf "${RED}not available${RESET}\n"
printf "MySQL ${CYAN}-------->${RESET} " && [[ $MYSQL == 1 ]] && brew services | grep mysql | awk '{print $2}' || printf "${RED}not available${RESET}\n"
printf "PHP ${CYAN}---------->${RESET} " && [[ $PHP == 1 ]] && brew services | grep php | awk '{print $2}' || printf "${RED}not available${RESET}\n"
printf "\n"
if [[ $STACK == 1 ]]; then
DOC_ROOT_REAL=$(grep -F "DocumentRoot \"" "$BREW_PREFIX/etc/httpd/httpd.conf" | awk '{print substr($2, 2, length($2)-2);}')
printf "${CYAN}Where to put website files?${RESET} ${DOC_ROOT_REAL}\n"
printf "${CYAN}How to test your PHP setup?${RESET} http://localhost/phpinfo.php\n"
printf "${CYAN}Where to access phpMyAdmin?${RESET} http://localhost/phpmyadmin\n"
printf "${CYAN}How to log into phpMyAdmin?${RESET} Username: root | Password: [blank]\n"
printf "\n"
fi
printf "Stack Commands:\n"
printf "~~~~~~~~~~~~~~~\n"
printf "${CYAN}${UNDERLINE}I${RESET}${CYAN}nstall${RESET} Install/repair the PHP stack.\n"
printf "${CYAN}${UNDERLINE}U${RESET}${CYAN}pdate${RESET} Update packages to latest versions.\n"
printf "${CYAN}${UNDERLINE}R${RESET}${CYAN}estart${RESET} Restart all PHP stack services.\n"
printf "${CYAN}${UNDERLINE}S${RESET}${CYAN}top${RESET} Stop all PHP stack services.\n"
printf "${CYAN}${UNDERLINE}D${RESET}${CYAN}elete${RESET} Show delete information.\n"
printf "${CYAN}${UNDERLINE}Q${RESET}${CYAN}uit${RESET} Quit this application.\n"
printf "\n"
printf "${BOLD}==> Enter command:${RESET} " && read command
printf "\n"
case "${command}" in
[iI]|[iI]nstall)
# ========================================
# --- CREATE DOCUMENT ROOT DIRECTORY -----
# ========================================
if [ ! -d "${DOC_ROOT}" ]; then
mkdir -p "${DOC_ROOT}"
printf "[${GREEN} OK ${RESET}] Created directory: ${DOC_ROOT}\n"
else
printf "[${GREEN} OK ${RESET}] Directory already exists: ${DOC_ROOT}\n"
fi
# ========================================
# --- CREATE LOGS DIRECTORY --------------
# ========================================
if [ ! -d "${LOGS_DIR}" ]; then
mkdir -p "${LOGS_DIR}"
printf "[${GREEN} OK ${RESET}] Created directory: ${LOGS_DIR}\n"
else
printf "[${GREEN} OK ${RESET}] Directory already exists: ${LOGS_DIR}\n"
fi
# ========================================
# --- INSTALL HOMEBREW -------------------
# ========================================
[[ $HOMEBREW == 0 ]] && /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" && BREW_PREFIX=$(brew --prefix) && HOMEBREW=1
# Add Homebrew to PATH in Linux
if [[ "${OSTYPE}" == "linux-gnu"* ]]; then
if ! command -v brew &> /dev/null; then
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> ~/.bashrc
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
printf "[${GREEN} OK ${RESET}] Added Homebrew to PATH.\n\n"
else
printf "[${GREEN} OK ${RESET}] Homebrew is already in PATH.\n\n"
fi
fi
# ========================================
# --- INSTALL PHP STACK ------------------
# ========================================
[[ $APACHE == 0 ]] && brew install httpd && APACHE=1
[[ $MYSQL == 0 ]] && brew install mysql && MYSQL=1
[[ $PHP == 0 ]] && brew install php && PHP=1
[[ $PHPMYADMIN == 0 ]] && brew install phpmyadmin && PHPMYADMIN=1
# ========================================
# --- HTTPD.CONF -------------------------
# ========================================
# Linux-specific settings
if [[ "${OSTYPE}" == "linux-gnu"* ]]; then
# Allow httpd to listen on (privileged) port 80 without being root
sudo setcap 'cap_net_bind_service=+ep' $(readlink -f $(which httpd))
sed -i.bak "s/User _www/User www-data/" "${BREW_PREFIX}/etc/httpd/httpd.conf"
printf "[${GREEN} OK ${RESET}] Changed user to www-data in the httpd.conf file.\n"
sed -i.bak "s/Group _www/Group www-data/" "${BREW_PREFIX}/etc/httpd/httpd.conf"
printf "[${GREEN} OK ${RESET}] Changed group to www-data in the httpd.conf file.\n"
fi
# Enable port 80
sed -i.bak "s/Listen 8080/Listen 80/" "${BREW_PREFIX}/etc/httpd/httpd.conf"
printf "[${GREEN} OK ${RESET}] Enabled port 80 in the httpd.conf file.\n"
# Update DocumentRoot and Directory targets
sed -i.bak "s@${BREW_PREFIX}/var/www@$DOC_ROOT@g" "${BREW_PREFIX}/etc/httpd/httpd.conf"
printf "[${GREEN} OK ${RESET}] Updated DocumentRoot and Directory in the httpd.conf file.\n"
# Update log files destinations
sed -i.bak "s@${BREW_PREFIX}/var/log/httpd/error_log@${LOGS_DIR}/error.log@g" "${BREW_PREFIX}/etc/httpd/httpd.conf"
sed -i.bak "s@${BREW_PREFIX}/var/log/httpd/access_log@${LOGS_DIR}/access.log@g" "${BREW_PREFIX}/etc/httpd/httpd.conf"
printf "[${GREEN} OK ${RESET}] Updated log files destinations in the httpd.conf file.\n"
# Enable rewrite_module
sed -i.bak "s@#LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so@LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so@g" "${BREW_PREFIX}/etc/httpd/httpd.conf"
sed -i.bak "s/AllowOverride None/AllowOverride All/g" "${BREW_PREFIX}/etc/httpd/httpd.conf"
printf "[${GREEN} OK ${RESET}] Enabled rewrite_module in the httpd.conf file.\n"
# Update DirectoryIndex
sed -i.bak "s/DirectoryIndex index.html/DirectoryIndex index.php index.html/" "${BREW_PREFIX}/etc/httpd/httpd.conf"
printf "[${GREEN} OK ${RESET}] Updated DirectoryIndex in the httpd.conf file.\n"
# Enable php_module and create alias for phpMyAdmin
grep -F "LoadModule php_module ${BREW_PREFIX}/opt/php/lib/httpd/modules/libphp.so" "${BREW_PREFIX}/etc/httpd/httpd.conf" &> /dev/null || printf "\nLoadModule php_module ${BREW_PREFIX}/opt/php/lib/httpd/modules/libphp.so\n" >> ${BREW_PREFIX}/etc/httpd/httpd.conf
ENABLE_PHP=$(cat <<EOF
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
EOF
)
grep -F "<FilesMatch \.php$>" "${BREW_PREFIX}/etc/httpd/httpd.conf" &> /dev/null || printf "\n${ENABLE_PHP}\n" >> ${BREW_PREFIX}/etc/httpd/httpd.conf
printf "[${GREEN} OK ${RESET}] Enabled php_module in the httpd.conf file.\n"
ENABLE_PHPMYADMIN=$(cat <<EOF
Alias /phpmyadmin ${BREW_PREFIX}/share/phpmyadmin
<Directory ${BREW_PREFIX}/share/phpmyadmin/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require local
</Directory>
EOF
)
grep -F "Alias /phpmyadmin ${BREW_PREFIX}/share/phpmyadmin" "${BREW_PREFIX}/etc/httpd/httpd.conf" &> /dev/null || printf "\n${ENABLE_PHPMYADMIN}\n" >> ${BREW_PREFIX}/etc/httpd/httpd.conf
printf "[${GREEN} OK ${RESET}] Created alias for phpMyAdmin in the httpd.conf file.\n"
# ========================================
# --- CONFIG.INC.PHP (PMA) ---------------
# ========================================
sed -i.bak "s/\$cfg\['blowfish_secret'\] = '';/\$cfg\['blowfish_secret'\] = '12345678901234567890123456789012';/" "${BREW_PREFIX}/etc/phpmyadmin.config.inc.php"
printf "[${GREEN} OK ${RESET}] Filled in blowfish secret in the config.inc.php file for phpMyAdmin.\n"
sed -i.bak "s/\$cfg\['Servers'\]\[\$i\]\['AllowNoPassword'\] = false;/\$cfg\['Servers'\]\[\$i\]\['AllowNoPassword'\] = true;/" "${BREW_PREFIX}/etc/phpmyadmin.config.inc.php"
printf "[${GREEN} OK ${RESET}] Enabled passwordless login in the config.inc.php file for phpMyAdmin.\n"
# ========================================
# --- CREATE PHPINFO.PHP FILE ------------
# ========================================
printf "<?php phpinfo(); ?>\n" > "${DOC_ROOT}/phpinfo.php"
printf "[${GREEN} OK ${RESET}] Created file: ${DOC_ROOT}/phpinfo.php\n"
# ========================================
# --- START PHP STACK SERVICES -----------
# ========================================
brew services start httpd
brew services start mysql
brew services start php
# ========================================
# --- INSTALLATION RESULT ----------------
# ========================================
[[ $HOMEBREW == 1 && $APACHE == 1 && $MYSQL == 1 && $PHP == 1 && $PHPMYADMIN == 1 ]] && printf "[${GREEN} OK ${RESET}] INSTALLATION COMPLETE!\n" && /bin/bash -c "$(curl -fsSL "${REMOTE_URL}")" || printf "[${RED} Error ${RESET}] INSTALLATION FAILED!\n\n"
;;
[uU]|[uU]pdate)
[[ $HOMEBREW == 1 ]] && brew update && brew upgrade && brew autoremove && brew cleanup || printf "[${GREEN} OK ${RESET}] Nothing to update.\n"
/bin/bash -c "$(curl -fsSL "${REMOTE_URL}")"
;;
[rR]|[rR]estart)
[[ $HOMEBREW == 1 ]] && brew services restart httpd 2>/dev/null && brew services restart mysql 2>/dev/null && brew services restart php 2>/dev/null || printf "[${GREEN} OK ${RESET}] Nothing to restart.\n"
/bin/bash -c "$(curl -fsSL "${REMOTE_URL}")"
;;
[sS]|[sS]top)
[[ $HOMEBREW == 1 ]] && brew services stop httpd && brew services stop mysql && brew services stop php || printf "[${GREEN} OK ${RESET}] Nothing to stop.\n"
/bin/bash -c "$(curl -fsSL "${REMOTE_URL}")"
;;
[dD]|[dD]elete)
printf "${RED}THIS WILL BE DELETED:${RESET}\n"
printf "${RED}- Apache, MySQL, PHP, and phpMyAdmin.${RESET}\n"
printf "${RED}- Services, config files, and logs.${RESET}\n\n"
printf "${GREEN}THIS WILL NOT BE DELETED:${RESET}\n"
printf "${GREEN}- Your website files.${RESET}\n"
printf "${GREEN}- Your MySQL databases.${RESET}\n\n"
printf "Are you sure you want to proceed?\n\n"
printf "${BOLD}==> Enter Yes/No${RESET}: " && read confirm_delete
printf "\n"
if [[ "${confirm_delete}" == "Yes" || "${confirm_delete}" == "yes" || "${confirm_delete}" == "Y" || "${confirm_delete}" == "y" || "${confirm_delete}" == "YES" ]]; then
[[ $HOMEBREW == 1 ]] && brew services stop httpd 2>/dev/null && brew services stop mysql 2>/dev/null && brew services stop php 2>/dev/null && brew uninstall httpd 2>/dev/null && brew uninstall mysql 2>/dev/null && brew uninstall php 2>/dev/null && brew uninstall phpmyadmin 2>/dev/null && brew autoremove && brew cleanup && rm -rf ${BREW_PREFIX}/etc/httpd/httpd.conf && rm -rf ${BREW_PREFIX}/etc/phpmyadmin.config.inc.php && rm -rf ${LOGS_DIR} && printf "[${GREEN} OK ${RESET}] Deleted remaining config files and logs.\n"
printf "[${GREEN} OK ${RESET}] DELETION COMPLETE!\n\n"
else
printf "[${GREEN} OK ${RESET}] Nothing was deleted.\n" && /bin/bash -c "$(curl -fsSL "${REMOTE_URL}")"
fi
;;
[qQ]|[qQ]uit)
printf "[${GREEN} OK ${RESET}] Goodbye!\n\n"
;;
*)
printf "[${RED} Error ${RESET}] Command not recognized.\n" && /bin/bash -c "$(curl -fsSL "${REMOTE_URL}")"
;;
esac