This repository has been archived by the owner on Oct 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathinstall_hackthis_ubuntu.sh
executable file
·197 lines (165 loc) · 5.61 KB
/
install_hackthis_ubuntu.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
#!/bin/bash
cat <<Caption
-------------------------------------------------------------
H a c k T h i s ! !
The Hacker's Playground
https://www.hackthis.co.uk/
-------------------------------------------------------------
Development Environment Installation Script
-------------------------------------------------------------
Caption
function isPackageInstalled {
packagePolicyOutput=`apt-cache policy $1`
echo $packagePolicyOutput | grep -v none | grep Installed > /dev/null
return $?
}
function installMissingPackages {
isPackageInstalled $1
if [ $? = 0 ]; then
echo -e "\t $1 already installed."
else
echo -e "\t Installing package $1:"
sudo apt-get install $1 || {
echo
echo -e "\t \e[0;31mFailed to install $1 package. Aborting.\e[m"
exit 1
}
fi
}
function getMySqlCredentials {
success=1;
until [ $success = 0 ]; do
# Avoid asking twice if already have the answer, just check it
if [ -z "$mysql_user" ]; then
read -p " Please enter mysql user name: " mysql_user
read -sp " Please enter mysql password: " mysql_pass
echo # needed newline since password isn't echoed
fi
#if the password is blank (unsecure, but happens...) don't use -p at all
if [ -z "$mysql_pass" ]; then
pass_clause=""
else
pass_clause="-p${mysql_pass}"
fi
# Connect and quit just to make sure the credentials work
mysql -u $mysql_user $pass_clause -e "quit"
if [ $? = 0 ]; then
success=0;
else
echo -e "\t Couldn't connect to mysql server with these username and password.";
echo -e "\t Trying again... (press ctrl+c to quit)."
mysql_user=""
fi
done;
}
# Install script should only work on Ubuntu
uname -a | grep Ubuntu > /dev/null || {
echo This script is intended to run on Ubuntu machines.
echo For a Windows installation run install_hackthis_windows.sh.
echo
echo Please press Enter to exit.
read
exit 1
}
# Make sure sudo is used to run this script
whoami | grep root > /dev/null || {
echo You need root permissions to run this script.
echo Please use \'sudo !!\' to re-run as root.
exit 1
}
# Make sure the script is run from the project's Git root directory, indentified by the README.md file
ls README.md > /dev/null 2>&1 || {
echo Please run this script from HackThis.co.uk Git\'s root direcotry.
exit 1
}
git_root_dir=`pwd`
# Package installation
required_packages="apache2 php5 libapache2-mod-php5 mysql-server php5-mysql php5-ldap"
echo Checking installed packages
for package in $required_packages; do
installMissingPackages $package
done
# Start web server if not started yet
service apache2 status
if [ $? != 0 ]; then
service apache2 start
fi
# .htaccess and php include_path configuration
echo Configuring .htaccess and php include_path
if [ -e html/.htaccess ]; then
echo -e '\t \e[0;31m**** OVERWRITING ****\e[m The existing .htaccess file was overwritten.'
fi
cp html/example.htaccess html/.htaccess
include_path="${git_root_dir}/files/"
sed -i "s|/path/to/hackthis.co.uk/files/|${include_path}|" html/.htaccess
# Apache setup (name based virtual host)
# Choose virtual host name (for example ht.com)
echo Apache setup
while [ -z "$vdomain" ]; do
echo -ne "\t Please choose a local domain name to use (name based virtual host, e.g. ht.com): "
read vdomain
done
# Prepare directory structure and make a soft link to the html folder
mkdir -p /var/www/vhosts/${vdomain}/log
chmod 755 /var/www/vhosts/${vdomain}/log
ln -s -f -T ${git_root_dir}/html /var/www/vhosts/${vdomain}/htdocs
# Add the site definition to the available sites and enable it
cat > /etc/apache2/sites-available/${vdomain}.conf <<VirtualHostDefinition
<virtualhost *:80>
# Admin email, Server Name (domain name) and any aliases
ServerAdmin webmaster@${vdomain}
ServerName ${vdomain}
ServerAlias ${vdomain}
# Index file and Document Root (where the public files are located)
DirectoryIndex index.php
DocumentRoot /var/www/vhosts/${vdomain}/htdocs
<Directory /var/www/vhosts/${vdomain}/htdocs>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
# Custom log file locations
LogLevel warn
ErrorLog /var/www/vhosts/${vdomain}/log/error.log
CustomLog /var/www/vhosts/${vdomain}/log/access.log combined
</virtualhost>
VirtualHostDefinition
a2ensite $vdomain
service apache2 reload
# Add the virtual domain to /etc/hosts
echo "127.0.0.1 $vdomain" >> /etc/hosts
# config.php
echo Configuring config.php
if [ -e files/config.php ]; then
echo -e '\t \e[0;31m**** OVERWRITING ****\e[m The existing files/config.php file was overwritten.'
fi
getMySqlCredentials
cp files/example.config.php files/config.php
echo -e "\t Updating domain definition in config.php"
sed -i "s/example.org/${vdomain}/" files/config.php
echo -e "\t Updating mysql credentials in config.php"
sed -i "s/'root'/'${mysql_user}'/" files/config.php
sed -i "s/'pass'/'${mysql_pass}'/" files/config.php
# MySql database setup
echo "Initializing HackThis database... (previous data is overwritten)"
getMySqlCredentials
mysql -u $mysql_user $pass_clause < setup.sql
echo -e "\t HackThis database was initialized"
# Setting up directories and permissions
echo Setting up directories and permissions
mkdir -p html/files/css/min{,/light,/dark}
chmod 777 html/files/css/min{,/light,/dark}
mkdir -p html/files/js/min
chmod 777 html/files/js/min
mkdir -p files/uploads/users
chmod 777 files/uploads/users
mkdir -p files/cache{,/twig}
chmod 777 files/cache{,/twig}
mkdir -p files/logs
chmod 777 files/logs
# Enabeling ModRewrite to solve RewriteEngine issue
echo Enabeling Mod_Rewrite
a2enmod rewrite
service apache2 restart
echo Done!