Git Product home page Git Product logo

cloudpanel.helper's People

Contributors

ccmatrix avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

navystack yward

cloudpanel.helper's Issues

Debian 11 inotify as alternative

Because "incron" is not available in Debian 11 anymore this could be an alternative:

apt install inotify-tools

create a file in: /usr/local/bin/clp-install-certificate-watcher

#!/bin/bash

inotifywait -m /etc/nginx/sites-enabled --format %f --exclude '.swp|.swx$' --event create |
    while read line1; do
       /usr/local/bin/clp-install-certificate ${line1}
       systemctl force-reload nginx
    done

create a new systemd service in: /etc/systemd/system/clp-install-certificate-watcher.service

[Unit]
Description="Run the custom Cloudpanel certificate watcher"

[Service]
ExecStart=/usr/local/bin/clp-install-certificate-watcher

[Install]
WantedBy=multi-user.target

and make sure it has the right permissions and reload the daemon and start service:

chmod 777 /usr/local/bin/clp-install-certificate-watcher
systemctl daemon-reload
systemctl enable clp-install-certificate-watcher
systemctl start clp-install-certificate-watcher

Optimization and Enhancement of TCP Socket Migration Script

I have optimized the existing script used for migrating PHP-FPM from TCP sockets to Unix sockets. The new script includes better error handling, improved readability, and streamlined processes using functions for repeated tasks.
Changes:

Centralized error handling.
Use of functions for logging, command success checks, and common tasks.
Enhanced readability and maintainability.

Code:

#!/bin/bash

# Log file
LOG_FILE="/var/log/migrate_sockets.log"

# Function to log messages
log_message() {
  echo "$(date +"%Y-%m-%d %H:%M:%S") - $1" | tee -a $LOG_FILE
}

# Function to check command success
check_success() {
  if [[ $? -ne 0 ]]; then
    log_message "$1"
    exit 1
  fi
}

# Get a list of available domains
domains=$(sqlite3 /home/clp/htdocs/app/data/db.sq3 "SELECT id, domain_name, INSTR(vhost_template, 'fastcgi_pass unix') as hasSocket FROM site WHERE type = 'php'")

# Convert the list of domains into an array
readarray -t domain_array <<<"$domains"

# Check if the domain array is empty
if [ -z "$domains" ]; then
  log_message "No PHP sites found on your server."
  exit 1
fi

# Display available PHP sites
echo "Available PHP sites:"
for i in "${!domain_array[@]}"; do
  IFS='|' read -ra domain_info <<< "${domain_array[i]}"
  domain_name=${domain_info[1]}
  hasSocket=${domain_info[2]}
  echo "$((i + 1)). $domain_name ($([ "$hasSocket" -eq 0 ] && echo 'tcp' || echo 'socket'))"
done

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."
  exit 2
fi

# Get the selected domain id and name
IFS='|' read -ra selected_domain <<< "${domain_array[$((domain_number - 1))]}"
site_id=${selected_domain[0]}
domain_name=${selected_domain[1]}

log_message "----------------------------------------"
log_message "Starting migration of $domain_name"

# Get the PHP version
php_version=$(sqlite3 /home/clp/htdocs/app/data/db.sq3 "SELECT php_version FROM php_settings WHERE site_id = $site_id")
check_success "Failed to get PHP version for $domain_name."

# Load pool configuration content
pool_config_file="/etc/php/$php_version/fpm/pool.d/$domain_name.conf"
if [ ! -f "$pool_config_file" ]; then
  log_message "Error: Pool configuration file not found!"
  exit 5
fi

# Extract user and group values from the pool configuration file
user=$(awk -F' = ' '/^user/ {print $2}' "$pool_config_file")
group=$(awk -F' = ' '/^group/ {print $2}' "$pool_config_file")

sock_path="/var/run/php/${domain_name}.sock"

# Create a backup of the pool configuration file
cp "$pool_config_file" "$pool_config_file.bak"

# Update pool configuration file
tmp_file=$(mktemp)
while IFS= read -r line; do
  case "$line" in
    "listen = 127.0.0.1:"*) 
      echo "listen = $sock_path" >> "$tmp_file"
      echo "listen.owner = $user" >> "$tmp_file"
      echo "listen.group = $group" >> "$tmp_file"
      ;;
    "listen.allowed_clients"*|"listen.backlog"*)
      continue
      ;;
    *)
      echo "$line" >> "$tmp_file"
      ;;
  esac
done < "$pool_config_file"
mv "$tmp_file" "$pool_config_file"

log_message "Updated pool configuration in $pool_config_file"

# Update vhost template in the database
vhost_template=$(sqlite3 /home/clp/htdocs/app/data/db.sq3 "SELECT vhost_template FROM site WHERE id = $site_id")
updated_vhost_template=$(echo "$vhost_template" | sed "s/fastcgi_pass 127.0.0.1:{{php_fpm_port}}/fastcgi_pass unix:$sock_path;/")
escaped_string="${updated_vhost_template//\'/\'\'}"
sqlite3 /home/clp/htdocs/app/data/db.sq3 "UPDATE site SET vhost_template = '$escaped_string' WHERE id = $site_id;"
check_success "Failed to update site vhost in CloudPanel database."

# Update NGINX configuration
nginx_config_file="/etc/nginx/sites-enabled/$domain_name.conf"
if [ -f "$nginx_config_file" ]; then
  cp "$nginx_config_file" "$nginx_config_file.bak"
  sed -i "s/fastcgi_pass 127.0.0.1:[0-9]\+;/fastcgi_pass unix:$sock_path;/" "$nginx_config_file"
  check_success "Failed to update $nginx_config_file"
else
  log_message "Error: NGINX configuration file $nginx_config_file not found"
  exit 8
fi

# Restart services
systemctl restart "php$php_version-fpm.service"
check_success "Failed to restart PHP-FPM service."

systemctl reload nginx
check_success "Failed to reload NGINX service."

log_message "Migration completed for $domain_name"

Steps to Reproduce: (Optional)

Run the current script and note any issues or inefficiencies.
Compare with the optimized script provided.

Expected Improvement:

Reduced complexity
Improved error handling
Enhanced maintainability

Looking forward to your feedback and approval for creating a pull request.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.