-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_apache_logs_v2.sh
96 lines (75 loc) · 3.04 KB
/
update_apache_logs_v2.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
#!/bin/bash
## Script to update apache config_dev
# Authors: Sibi Jose
# Redux
# Date: 31 July 2024
# Define variables
LOG_DIR="/shared-storage/server_logs"
LOG_USER="deploy"
LOG_GROUP="deploy"
APACHE_CONF_DIR="/etc/apache2/sites-available"
MODIFIED_FILES_LOG="/tmp/modified_apache_conf_files.log"
BACKUP_DIR="/etc/apache2/sites-available/backup_$(date +%Y%m%d%H%M%S)"
DOMAINS_FILE="domains.txt"
# Create the log directory if it doesn't exist
mkdir -p "$LOG_DIR"
# Change ownership of the log directory and its contents
chown -R "$LOG_USER:$LOG_GROUP" "$LOG_DIR"
# Create the backup directory
mkdir -p "$BACKUP_DIR"
# Clear the log file if it exists
> "$MODIFIED_FILES_LOG"
# Check if the domains file exists
if [ ! -f "$DOMAINS_FILE" ]; then
echo "Domains file not found: $DOMAINS_FILE" 1>&2
exit 1
fi
# Read domains from the file
while IFS= read -r domain; do
# Skip comments and empty lines
if [[ "$domain" =~ ^#.* ]] || [[ -z "$domain" ]]; then
continue
fi
echo "Processing domain: $domain"
# Define configuration file names
conf_file="$APACHE_CONF_DIR/000-${domain}.conf"
ssl_conf_file="$APACHE_CONF_DIR/000-${domain}-le-ssl.conf"
# Define the log paths
error_log_path="$LOG_DIR/${domain}.error.log"
access_log_path="$LOG_DIR/${domain}.access.log"
# Check and update the standard configuration file
if [[ -f "$conf_file" ]]; then
echo "Processing standard configuration file: $conf_file"
# Create the log files if they don't exist
touch "$error_log_path" "$access_log_path"
# Change ownership of log files
chown -R "$LOG_USER:$LOG_GROUP" "$LOG_DIR"
# Backup the original configuration file
cp "$conf_file" "$BACKUP_DIR/"
# Apply sed commands to update log paths
sed -i "s|ErrorLog .*|ErrorLog $error_log_path|g" "$conf_file"
sed -i "s|CustomLog .*|CustomLog $access_log_path combined|g" "$conf_file"
# Log the modified file
echo "$conf_file" >> "$MODIFIED_FILES_LOG"
fi
# Check and update the SSL configuration file
if [[ -f "$ssl_conf_file" ]]; then
echo "Processing SSL configuration file: $ssl_conf_file"
# Create the log files if they don't exist
touch "$error_log_path" "$access_log_path"
# Change ownership of log files
# chown "$LOG_USER:$LOG_GROUP" "$error_log_path" "$access_log_path"
# Backup the original configuration file
cp "$ssl_conf_file" "$BACKUP_DIR/"
# Apply sed commands to update log paths
sed -i "s|ErrorLog .*|ErrorLog $error_log_path|g" "$ssl_conf_file"
sed -i "s|CustomLog .*|CustomLog $access_log_path combined|g" "$ssl_conf_file"
# Log the modified file
echo "$ssl_conf_file" >> "$MODIFIED_FILES_LOG"
fi
done < "$DOMAINS_FILE"
# Reload Apache
echo "Reloading Apache..."
#systemctl reload apache2
echo "Webserver configuration complete for all domains. Modified configuration files are logged in $MODIFIED_FILES_LOG."
echo "Backup of original configuration files is stored in $BACKUP_DIR."