-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapache-manage.sh
More file actions
177 lines (139 loc) · 5.25 KB
/
apache-manage.sh
File metadata and controls
177 lines (139 loc) · 5.25 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
#!/bin/bash
# Check if script is being run as root
if [ "$(id -u)" -ne 0 ]; then
echo "Error: script must be run as root." >&2
exit 1
fi
# Display selection menu
echo "Select an option:"
echo "1. Create a new site"
echo "2. Show enabled sites"
echo "3. Remove a site"
echo "4. Create SSL certificate and add to Apache"
echo "----------------------------------------------"
read -p "Enter the number of the chosen option: " menu_choice
if [ "$menu_choice" -eq 1 ]; then
# Read parameters
read -p "Enter the site name: " site_name
read -p "Enter the site folder path or reverse proxy address: " site_location
read -p "Is this configuration a folder (C) or a reverse proxy (R)? [C/R]: " site_type
# Create log folder
log_dir="/usr/local/apache2/logs/$site_name"
mkdir -p "$log_dir"
# Prepare configuration file
config_file="/usr/local/apache2/conf/extra/$site_name.conf"
if [ "$site_type" = "C" ] || [ "$site_type" = "c" ]; then
# Configuration for a folder
cat >"$config_file" <<EOL
<VirtualHost *:80>
ServerName $site_name
DocumentRoot $site_location
ErrorLog ${log_dir}/error.log
CustomLog ${log_dir}/access.log combined
</VirtualHost>
EOL
elif [ "$site_type" = "R" ] || [ "$site_type" = "r" ]; then
# Configuration for a reverse proxy
cat >"$config_file" <<EOL
<VirtualHost *:80>
ServerName $site_name
ProxyPass / http://$site_location/
ProxyPassReverse / http://$site_location/
ErrorLog ${log_dir}/error.log
CustomLog ${log_dir}/access.log combined
</VirtualHost>
EOL
else
echo "Error: Invalid configuration type. Use 'C' for a folder or 'R' for a reverse proxy." >&2
exit 1
fi
# Enable site
echo "Include conf/extra/$site_name.conf" >> /usr/local/apache2/conf/httpd.conf
apachectl graceful
echo "Site $site_name created and enabled successfully!"
elif [ "$menu_choice" -eq 2 ]; then
echo "Here is the list of enabled sites:"
echo "----------------------------------------------"
grep "ServerName" /usr/local/apache2/conf/extra/*.conf | awk -F: '{print $2}'
elif [ "$menu_choice" -eq 3 ]; then
read -p "Enter the name of the site to remove: " site_name_to_remove
# Disable site
sed -i "/Include conf\/extra\/$site_name_to_remove.conf/d" /usr/local/apache2/conf/httpd.conf
apachectl graceful
# Remove configuration file and log folder
rm -f "/usr/local/apache2/conf/extra/$site_name_to_remove.conf"
rm -rf "/usr/local/apache2/logs/$site_name_to_remove"
echo "Site $site_name_to_remove removed successfully!"
elif [ "$menu_choice" -eq 4 ]; then
# Read parameters
read -p "Enter the domain name: " domain_name
read -p "Enter the site folder path or reverse proxy address: " site_location
read -p "Is this configuration a folder (C) or a reverse proxy (R)? [C/R]: " site_type
# Create log folder
log_dir="/usr/local/apache2/logs/$domain_name"
mkdir -p "$log_dir"
# Prepare configuration file
config_file="/usr/local/apache2/conf/extra/$domain_name.conf"
if [ "$site_type" = "C" ] || [ "$site_type" = "c" ]; then
# Configuration for a folder
cat >"$config_file" <<EOL
<VirtualHost *:80>
ServerName $domain_name
DocumentRoot $site_location
ErrorLog ${log_dir}/error.log
CustomLog ${log_dir}/access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerName $domain_name
DocumentRoot $site_location
ErrorLog ${log_dir}/error.log
CustomLog ${log_dir}/access.log combined
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/$domain_name/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/$domain_name/privkey.pem
</VirtualHost>
EOL
elif [ "$site_type" = "R" ] || [ "$site_type" = "r" ]; then
# Configuration for a reverse proxy
cat >"$config_file" <<EOL
<VirtualHost *:80>
ServerName $domain_name
ProxyPass / http://$site_location/
ProxyPassReverse / http://$site_location/
ErrorLog ${log_dir}/error.log
CustomLog ${log_dir}/access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerName $domain_name
ProxyPass / http://$site_location/
ProxyPassReverse / http://$site_location/
ErrorLog ${log_dir}/error.log
CustomLog ${log_dir}/access.log combined
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/$domain_name/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/$domain_name/privkey.pem
</VirtualHost>
EOL
else
echo "Error: Invalid configuration type. Use 'C' for a folder or 'R' for a reverse proxy." >&2
exit 1
fi
# Enable site
domain_include="Include conf/extra/$domain_name.conf"
if grep -Fxq "$domain_include" /usr/local/apache2/conf/httpd.conf
then
echo "Include for $domain_name already exists in httpd.conf"
else
echo "$domain_include" >> /usr/local/apache2/conf/httpd.conf
echo "Include for $domain_name added to httpd.conf"
fi
apachectl graceful
# Obtain SSL certificate
certbot certonly --webroot -w "$site_location" -d "$domain_name" --agree-tos --email ioszxcvbinz@gmail.com
echo "Site $domain_name created and enabled successfully with SSL!"
else
echo "Error: Invalid option." >&2
exit 1
fi
echo "----------------------------------------------"
echo "Script finished."