-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapache.sh
More file actions
348 lines (293 loc) · 10.7 KB
/
Copy pathapache.sh
File metadata and controls
348 lines (293 loc) · 10.7 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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/bin/bash
# Exit script on any error
set -e
# Function to display messages
function echo_msg() {
echo ">>> $1"
}
# Function to display error messages in red
function echo_error() {
echo -e "\033[31m>>> ERROR: $1\033[0m"
}
# Validate the domain format
function validate_domain() {
local domain="$1"
if [[ ! "$domain" =~ ^[a-zA-Z0-9.-]+$ ]]; then
echo_error "Invalid domain name. Please enter a valid domain (e.g., dhruvjoshi.dev)."
return 1
fi
return 0
}
# Check if the domain points to the server's IP
function check_dns() {
local domain="$1"
local server_ip=$(hostname -I | awk '{print $1}')
local dns_ip=$(dig +short "$domain" A | head -n 1)
if [[ "$dns_ip" != "$server_ip" ]]; then
echo_error "The domain '$domain' does not point to this server's IP ($server_ip)."
echo "Please update the DNS A record for '$domain' to point to this server's IP."
read -p "Do you want to continue with the installation? (y/n, default: y): " CONTINUE_INSTALL
CONTINUE_INSTALL=${CONTINUE_INSTALL:-y}
if [[ ! "$CONTINUE_INSTALL" =~ ^[yY]$ ]]; then
echo_msg "Exiting installation."
exit 1
fi
fi
}
# Function to add domain to /etc/hosts
function add_to_hosts() {
local domain="$1"
local ip=$(hostname -I | awk '{print $1}')
echo_msg "You are about to add $domain to /etc/hosts with IP $ip."
echo_msg "Adding this entry can improve local resolution for testing purposes."
# Prompt the user for confirmation
read -p "Do you want to add this entry to /etc/hosts? (y/n, default: n): " ADD_TO_HOSTS
ADD_TO_HOSTS=${ADD_TO_HOSTS:-n} # Default to 'n' if no input
# Check if the entry exists
if [[ "$ADD_TO_HOSTS" =~ ^[yY]$ ]]; then
if ! grep -q "$ip $domain" /etc/hosts; then
echo "$ip $domain" | sudo tee -a /etc/hosts > /dev/null
echo_msg "Added $domain to /etc/hosts with IP $ip."
else
echo_msg "$domain with IP $ip is already in /etc/hosts."
fi
else
echo_msg "Skipping addition of $domain to /etc/hosts."
fi
}
# Install Git if not already installed
function install_git() {
if ! command -v git &> /dev/null; then
echo_msg "Installing Git..."
sudo $PACKAGE_MANAGER install git -y
else
echo_msg "Git is already installed."
fi
}
# Get the package manager
function detect_package_manager() {
if command -v apt &> /dev/null; then
PACKAGE_MANAGER="apt"
elif command -v yum &> /dev/null; then
PACKAGE_MANAGER="yum"
elif command -v dnf &> /dev/null; then
PACKAGE_MANAGER="dnf"
else
echo_error "No supported package manager found (apt, yum, dnf). Exiting."
exit 1
fi
}
# Function to install Apache
function install_apache() {
echo_msg "Installing Apache..."
if [[ "$PACKAGE_MANAGER" == "apt" ]]; then
sudo apt install apache2 -y
sudo systemctl start apache2
sudo systemctl enable apache2
sudo ufw allow 'Apache Full'
else
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
fi
}
# Function to check and stop Nginx if running
function check_and_stop_nginx() {
if ! command -v nginx &> /dev/null; then
echo_error "Nginx is not installed on this server."
exit 1
fi
if systemctl is-active --quiet nginx; then
echo_msg "Nginx is currently running."
read -p "Do you want to stop Nginx to free up port 80? (y/n, default: y): " STOP_NGINX
STOP_NGINX=${STOP_NGINX:-y}
if [[ "$STOP_NGINX" =~ ^[yY]$ ]]; then
echo_msg "Stopping Nginx service..."
sudo systemctl stop nginx
echo_msg "Nginx service stopped."
else
echo_error "Nginx must be stopped to run another service on port 80."
exit 1
fi
else
echo_msg "Nginx is not running."
fi
}
# Function to install PHP and its extensions
function install_php() {
echo_msg "Installing PHP and extensions..."
if [[ "$PACKAGE_MANAGER" == "apt" ]]; then
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update -y
sudo apt install -y php libapache2-mod-php php-mysql php-fpm \
php-curl php-gd php-mbstring php-xml php-zip php-bcmath php-json
sudo a2enmod php8.3
sudo a2enconf php8.3-fpm
else
sudo yum install php php-mysqlnd php-fpm php-curl php-gd php-mbstring php-xml php-zip php-bcmath -y
fi
}
# Install MySQL server
function install_mysql() {
echo_msg "Installing MySQL server..."
sudo $PACKAGE_MANAGER install mysql-server -y
echo_msg "Please run 'mysql_secure_installation' manually to secure your MySQL installation."
}
# Enable Apache rewrite module
function enable_rewrite() {
echo_msg "Enabling Apache rewrite module..."
if [[ "$PACKAGE_MANAGER" == "apt" ]]; then
sudo a2enmod rewrite
fi
sudo systemctl restart apache2 || sudo systemctl restart httpd
}
# Create directories for virtual hosts
function create_virtual_host() {
if [ ! -d "/var/www/$DOMAIN" ]; then
echo_msg "Creating directory /var/www/$DOMAIN..."
sudo mkdir -p /var/www/$DOMAIN
else
echo_msg "Directory /var/www/$DOMAIN already exists."
fi
}
# Create virtual host configuration files
function create_virtual_host_config() {
echo_msg "Creating virtual host configuration files..."
cat <<EOF | sudo tee /etc/apache2/sites-available/$DOMAIN.conf
<VirtualHost *:80>
ServerName $DOMAIN
ServerAlias *.$DOMAIN
DocumentRoot /var/www/$DOMAIN/Public/
<Directory /var/www/$DOMAIN/Public>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
EOF
echo_msg "Enabling virtual host configurations..."
if [[ "$PACKAGE_MANAGER" == "apt" ]]; then
sudo a2ensite $DOMAIN.conf
else
echo_msg "Ensure to manually include your virtual host configuration in your Apache config."
fi
}
# Fetch available Git branches
function fetch_branches() {
echo_msg "Fetching branches from the repository..."
branches=$(git ls-remote --heads https://github.com/DevDhruvJoshi/Precocious.git | awk '{print $2}' | sed 's|refs/heads/||')
echo_msg "Available branches:"
echo "$branches" | nl
}
# Clone the selected branch from the Git repository
function clone_repository() {
local selected_branch="$1"
local target_dir="/var/www/$DOMAIN"
if [ -d "$target_dir" ]; then
echo_msg "Directory $target_dir already exists."
read -p "Do you want to delete it and continue? (y/n, default is y): " choice
choice=${choice:-y} # Default to 'y' if no input is given
if [[ "$choice" == "y" || "$choice" == "Y" ]]; then
echo_msg "Deleting the existing directory..."
sudo rm -rf "$target_dir"
else
echo_msg "Updating the existing repository..."
cd "$target_dir" || exit
git checkout "$selected_branch"
git pull origin "$selected_branch"
return
fi
fi
echo_msg "Cloning the Git repository into $target_dir from branch '$selected_branch'..."
git clone --branch "$selected_branch" https://github.com/DevDhruvJoshi/Precocious.git "$target_dir"
}
# Set ownership for web directories
function set_ownership() {
echo_msg "Setting ownership for the web directories..."
sudo chown -R www-data:www-data /var/www/$DOMAIN || sudo chown -R apache:apache /var/www/$DOMAIN
}
# Install Composer
function install_composer() {
read -p "Do you want to install Composer? (y/n, default: y): " INSTALL_COMPOSER
INSTALL_COMPOSER=${INSTALL_COMPOSER:-y}
if [[ "$INSTALL_COMPOSER" =~ ^[yY]$ ]]; then
if command -v composer &> /dev/null; then
echo_msg "Composer is already installed."
else
echo_msg "Installing Composer..."
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
expected_hash="$(curl -s https://composer.github.io/installer.sha384sum | awk '{print $1}')"
actual_hash="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
if [ "$expected_hash" != "$actual_hash" ]; then
echo_error "Installer corrupt"
rm composer-setup.php
exit 1
fi
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer
echo_msg "Composer has been installed successfully."
fi
fi
}
# Main script execution starts here
# Prompt for domain name
while true; do
read -p "Enter your domain name (default: dhruvjoshi.dev): " DOMAIN
DOMAIN=${DOMAIN:-dhruvjoshi.dev}
if validate_domain "$DOMAIN"; then
break
fi
done
# Check if the domain points to this server's IP
check_dns "$DOMAIN"
# Add domain to /etc/hosts
add_to_hosts "$DOMAIN"
# Determine package manager
detect_package_manager
# Check if it's a new server
read -p "Is this a new server setup? (y/n, default: y): " NEW_SERVER
NEW_SERVER=${NEW_SERVER:-y}
if [[ "$NEW_SERVER" =~ ^[yY]$ ]]; then
sudo $PACKAGE_MANAGER update -y
install_git
# Check if Apache is running and prompt user to stop it
check_and_stop_nginx
install_apache
install_php
install_mysql
enable_rewrite
else
read -p "Do you want to install Apache? (y/n, default: y): " INSTALL_APACHE
INSTALL_APACHE=${INSTALL_APACHE:-y}
[[ "$INSTALL_APACHE" =~ ^[yY]$ ]] && install_apache
read -p "Do you want to install PHP and its extensions? (y/n, default: y): " INSTALL_PHP
INSTALL_PHP=${INSTALL_PHP:-y}
[[ "$INSTALL_PHP" =~ ^[yY]$ ]] && install_php
read -p "Do you want to install MySQL server? (y/n, default: y): " INSTALL_MYSQL
INSTALL_MYSQL=${INSTALL_MYSQL:-y}
[[ "$INSTALL_MYSQL" =~ ^[yY]$ ]] && install_mysql
enable_rewrite
fi
create_virtual_host
create_virtual_host_config
# Fetch branches and clone the selected one
fetch_branches
branch_count=$(echo "$branches" | wc -l)
if [[ $branch_count -eq 1 ]]; then
selected_branch=$(echo "$branches" | sed -n '1p')
echo_msg "Only one branch available: '$selected_branch'."
else
read -p "Enter the number of the branch you want to clone (default: 1): " branch_number
branch_number=${branch_number:-1}
selected_branch=$(echo "$branches" | sed -n "${branch_number}p")
if [[ -z "$selected_branch" ]]; then
echo_error "Invalid selection. Exiting."
exit 1
fi
fi
clone_repository "$selected_branch"
sudo systemctl restart apache2 || sudo systemctl restart httpd
set_ownership
install_composer
echo_msg "Setup complete! Please remember to run 'mysql_secure_installation' manually."