Compare commits
2 commits
25f8fd6b86
...
d81527ec67
Author | SHA1 | Date | |
---|---|---|---|
![]() |
d81527ec67 | ||
![]() |
6f607f6342 |
1 changed files with 192 additions and 0 deletions
192
security/migrate_sockets.sh
Normal file
192
security/migrate_sockets.sh
Normal file
|
@ -0,0 +1,192 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
#-----------------------------------#
|
||||||
|
# VARIABLES #
|
||||||
|
#-----------------------------------#
|
||||||
|
|
||||||
|
this_script_url="https://git.hhf.technology/hhf/script-management-cloudpanel/raw/branch/main/security/migrate_sockets.sh"
|
||||||
|
this_script_name="PHP-FPM Socket Migration Script by @ccmatrix"
|
||||||
|
formatter_url="https://git.hhf.technology/hhf/TaskFormatter/raw/branch/main/bash_task_formatter/task_formatter.sh"
|
||||||
|
scriptname=$0
|
||||||
|
|
||||||
|
# Initialize success flag
|
||||||
|
success=0
|
||||||
|
|
||||||
|
# Log file
|
||||||
|
LOG_FILE="/var/log/migrate_sockets.log"
|
||||||
|
|
||||||
|
# Database path
|
||||||
|
DB_PATH="/home/clp/htdocs/app/data/db.sq3"
|
||||||
|
|
||||||
|
#-----------------------------------#
|
||||||
|
# FORMATTER #
|
||||||
|
#-----------------------------------#
|
||||||
|
|
||||||
|
# Download and source the formatter with error handling
|
||||||
|
download_formatter() {
|
||||||
|
if [ ! -f "task_formatter.sh" ]; then
|
||||||
|
if ! wget "$formatter_url" --no-check-certificate -O task_formatter.sh > /dev/null 2>&1; then
|
||||||
|
echo "Error: Failed to download task_formatter.sh"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -f "task_formatter.sh" ]; then
|
||||||
|
echo "Error: task_formatter.sh not found after download attempt"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
chmod +x task_formatter.sh
|
||||||
|
source ./task_formatter.sh || { echo "Error: Failed to source task_formatter.sh"; exit 1; }
|
||||||
|
|
||||||
|
if ! declare -f print_header > /dev/null; then
|
||||||
|
echo "Error: print_header function not found after sourcing."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Call the download_formatter function
|
||||||
|
download_formatter
|
||||||
|
|
||||||
|
#-----------------------------------#
|
||||||
|
# FUNCTIONS #
|
||||||
|
#-----------------------------------#
|
||||||
|
|
||||||
|
# Function to log messages
|
||||||
|
log_message() {
|
||||||
|
echo "$(date +"%Y-%m-%d %H:%M:%S") - $1" | tee -a $LOG_FILE
|
||||||
|
}
|
||||||
|
|
||||||
|
# Function to check if a command succeeded
|
||||||
|
check_command() {
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo -e "Failed: $1 $CROSS_MARK"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
echo -e "Success: $1 $CHECK_MARK"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# Function to get available domains
|
||||||
|
get_domains() {
|
||||||
|
local domains=$(sqlite3 $DB_PATH "SELECT id, domain_name, INSTR(vhost_template, 'fastcgi_pass unix') as hasSocket FROM site WHERE type = 'php'")
|
||||||
|
if [ -z "$domains" ]; then
|
||||||
|
echo -e "No PHP sites found $CROSS_MARK"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Available PHP sites:"
|
||||||
|
readarray -t domain_array <<<"$domains"
|
||||||
|
for i in "${!domain_array[@]}"; do
|
||||||
|
IFS='|' read -ra domain_info <<< "${domain_array[i]}"
|
||||||
|
echo "$((i + 1)). ${domain_info[1]} ($([ "${domain_info[2]}" -eq 0 ] && echo 'tcp' || echo 'socket'))"
|
||||||
|
done
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# Function to update pool configuration
|
||||||
|
update_pool_config() {
|
||||||
|
local domain=$1
|
||||||
|
local php_version=$2
|
||||||
|
local pool_file="/etc/php/$php_version/fpm/pool.d/$domain.conf"
|
||||||
|
|
||||||
|
if [ ! -f "$pool_file" ]; then
|
||||||
|
echo -e "Pool configuration not found $CROSS_MARK"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
cp "$pool_file" "$pool_file.bak"
|
||||||
|
local user=$(awk -F' = ' '/^user/ {print $2}' "$pool_file")
|
||||||
|
local group=$(awk -F' = ' '/^group/ {print $2}' "$pool_file")
|
||||||
|
local sock_path="/var/run/php/${domain}.sock"
|
||||||
|
|
||||||
|
sed -i -e "s|listen = 127.0.0.1:.*|listen = $sock_path\nlisten.owner = $user\nlisten.group = $group|" "$pool_file"
|
||||||
|
echo -e "Updated pool configuration $CHECK_MARK"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# Function to update vhost configuration
|
||||||
|
update_vhost_config() {
|
||||||
|
local site_id=$1
|
||||||
|
local domain=$2
|
||||||
|
local sock_path="/var/run/php/${domain}.sock"
|
||||||
|
local escaped_sock_path=$(echo $sock_path | sed 's/\//\\\//g')
|
||||||
|
|
||||||
|
local vhost_template=$(sqlite3 $DB_PATH "SELECT vhost_template FROM site WHERE id = $site_id")
|
||||||
|
local updated_template=$(echo "$vhost_template" | sed "s/fastcgi_pass 127.0.0.1:{{php_fpm_port}};/fastcgi_pass unix:$escaped_sock_path;/")
|
||||||
|
local escaped_template="${updated_template//\'/\'\'}"
|
||||||
|
|
||||||
|
sqlite3 $DB_PATH "UPDATE site SET vhost_template = '$escaped_template' WHERE id = $site_id;"
|
||||||
|
echo -e "Updated vhost configuration $CHECK_MARK"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# Function to restart services
|
||||||
|
restart_services() {
|
||||||
|
local php_version=$1
|
||||||
|
systemctl restart "php$php_version-fpm.service"
|
||||||
|
check_command "PHP-FPM restart"
|
||||||
|
|
||||||
|
systemctl reload nginx
|
||||||
|
check_command "NGINX reload"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# Cleanup function
|
||||||
|
cleanup() {
|
||||||
|
if [ -f "$scriptname" ]; then
|
||||||
|
rm -- "$scriptname"
|
||||||
|
fi
|
||||||
|
if [ -f "task_formatter.sh" ]; then
|
||||||
|
rm task_formatter.sh
|
||||||
|
fi
|
||||||
|
echo -e "Cleanup completed $CHECK_MARK"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
#-----------------------------------#
|
||||||
|
# MAIN LOGIC #
|
||||||
|
#-----------------------------------#
|
||||||
|
|
||||||
|
# Print header
|
||||||
|
print_header "$this_script_name" "$this_script_url"
|
||||||
|
|
||||||
|
# Get and display available domains
|
||||||
|
if ! format_output get_domains "Getting available domains"; then
|
||||||
|
success=1
|
||||||
|
cleanup
|
||||||
|
exit $success
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Get user selection
|
||||||
|
read -p "Please pick a domain by entering its number: " domain_number
|
||||||
|
|
||||||
|
if ! [[ $domain_number =~ ^[0-9]+$ ]] || [ -z "${domain_array[$((domain_number - 1))]}" ]; then
|
||||||
|
log_message "Invalid input or no site selected."
|
||||||
|
success=1
|
||||||
|
cleanup
|
||||||
|
exit $success
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Process selected domain
|
||||||
|
IFS='|' read -ra selected_domain <<< "${domain_array[$((domain_number - 1))]}"
|
||||||
|
site_id=${selected_domain[0]}
|
||||||
|
domain_name=${selected_domain[1]}
|
||||||
|
|
||||||
|
# Get PHP version
|
||||||
|
php_version=$(sqlite3 $DB_PATH "SELECT php_version FROM php_settings WHERE site_id = $site_id")
|
||||||
|
|
||||||
|
# Execute migration steps
|
||||||
|
format_output "update_pool_config $domain_name $php_version" "Updating pool configuration"
|
||||||
|
format_output "update_vhost_config $site_id $domain_name" "Updating vhost configuration"
|
||||||
|
format_output "restart_services $php_version" "Restarting services"
|
||||||
|
format_output cleanup "Performing cleanup"
|
||||||
|
|
||||||
|
# Print final message
|
||||||
|
if [ $success -eq 0 ]; then
|
||||||
|
echo -e "${CHECK_MARK} Migration completed successfully for $domain_name"
|
||||||
|
else
|
||||||
|
echo -e "${CROSS_MARK} Migration failed for $domain_name"
|
||||||
|
fi
|
||||||
|
|
||||||
|
exit $success
|
Loading…
Reference in a new issue