-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapache-instantiate.sh
More file actions
executable file
·169 lines (141 loc) · 4.04 KB
/
apache-instantiate.sh
File metadata and controls
executable file
·169 lines (141 loc) · 4.04 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
# #!/usr/bin/env bash
#
# Author: Francesco Ciannavei
# Author Email: francesco@ciannavei.dev
# Author Website: https://www.ciannavei.link
# Author GitHub: https://github.com/Franky5831
#
# This script creates a new Apache project directory and sets permissions
# 1. It creates the directory for the project inside the /var/www directory.
# 2. It configures apache to serve the project on the next available port.
# 3. It restarts apache to apply the changes.
#
# Usage:
# sh ./apache-instantiate.sh <project-name>
#
# Example:
# sh ./apache-instantiate.sh my-project.com
# sh ./apache-instantiate.sh github.com
set -eu
# --- helpers ---------------------------------------------------------------
show_help() {
cat <<'EOF'
apache-instanciate.sh — manage multiple Apache instances on one host
USAGE:
apache-instanciate.sh [project-name]
EXAMPLES:
sudo ./apache-instanciate.sh example-site
sudo ./apache-instanciate.sh example-site.com
sudo ./apache-instanciate.sh test.com
Notes:
- You probably need to run this script as sudo
EOF
}
require_root() {
if [ "$(id -u)" -ne 0 ]; then
echo "🔴 Please run as root (e.g., sudo sh $0 <project-name>)" >&2
exit 1
fi
}
find_free_port() {
start="${1:-8080}"
port="$start"
while :; do
in_use=0
if command -v ss >/dev/null 2>&1; then
# match ":PORT" at end or before whitespace
if ss -tuln | awk -v p=":$port" '$5 ~ p"($|[[:space:]])" {found=1} END{exit !(found)}'
then in_use=1; fi
elif command -v netstat >/dev/null 2>&1; then
if netstat -tuln | awk -v p=":$port" '$0 ~ p"($|[[:space:]])" {found=1} END{exit !(found)}'
then in_use=1; fi
fi
declared=$(grep -R "^[[:space:]]*Listen[[:space:]]*$port\\b" /etc/apache2 2>/dev/null | wc -l | tr -d ' ')
if [ "$in_use" -eq 0 ] && [ "$declared" -eq 0 ]; then
echo "$port"
return 0
fi
port=$((port + 1))
done
}
configtest() {
if command -v apache2ctl >/dev/null 2>&1; then
apache2ctl -t
else
apachectl -t
fi
}
reload_apache() {
if command -v systemctl >/dev/null 2>&1; then
systemctl reload apache2
else
service apache2 reload
fi
}
# --- main ------------------------------------------------------------------
# Print help and exit early if -h/--help is anywhere in the args.
for _arg in "$@"; do
case "$_arg" in
-h|--help)
show_help
exit 0
;;
esac
done
require_root
project="${1:-}"
if [ -z "$project" ]; then
echo "Usage: sudo sh $0 <project-name>" >&2
exit 1
fi
webroot="/var/www/$project"
sites_avail="/etc/apache2/sites-available"
conf="$sites_avail/$project.conf"
if [ -e "$conf" ]; then
echo "🔴 Vhost already exists: $conf" >&2
exit 1
fi
if [ -e "$webroot" ]; then
echo "🔴 Directory already exists: $webroot" >&2
exit 1
fi
mkdir -p "$webroot"
chmod 755 "$webroot"
printf '<h1>%s</h1>\n' "$project" > "$webroot/index.html"
echo "🟡 Project directory created: $webroot"
port="$(find_free_port 8080)"
# Write a minimal vhost that also declares its own Listen
cat > "$conf" <<EOF
# Auto-generated vhost for $project
Listen $port
<VirtualHost *:$port>
ServerName $project.local
DocumentRoot $webroot
<Directory $webroot>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog \${APACHE_LOG_DIR}/$project-error.log
CustomLog \${APACHE_LOG_DIR}/$project-access.log combined
</VirtualHost>
EOF
echo "🟡 Vhost created: $conf (port $port)"
# Enable site, test config, then reload
a2ensite "$project.conf" >/dev/null
if configtest >/dev/null 2>&1; then
reload_apache
else
echo "🔴 Apache config test failed. Rolling back..." >&2
a2dissite "$project.conf" >/dev/null 2>&1 || true
rm -f "$conf"
exit 1
fi
# Determine an IP to print
ipaddr=""
if command -v ip >/dev/null 2>&1; then
ipaddr=$(ip -4 route get 1.1.1.1 2>/dev/null | awk '{for(i=1;i<=NF;i++) if($i=="src"){print $(i+1); exit}}')
fi
[ -z "$ipaddr" ] && ipaddr=$(hostname -I 2>/dev/null | awk '{print $1}')
[ -z "$ipaddr" ] && ipaddr="127.0.0.1"
echo "🟢 Project running on: http://$ipaddr:$port"