Skip to content

Instantly share code, notes, and snippets.

@DonSYS91
Created July 10, 2023 16:48
Show Gist options
  • Save DonSYS91/c4d964683e47ea1deffee4750fa84a24 to your computer and use it in GitHub Desktop.
Save DonSYS91/c4d964683e47ea1deffee4750fa84a24 to your computer and use it in GitHub Desktop.

Revisions

  1. DonSYS91 created this gist Jul 10, 2023.
    40 changes: 40 additions & 0 deletions optimize_php.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    #!/bin/bash

    # Check if PHP version is passed as an argument
    if [ -z "$1" ]; then
    echo "Please pass PHP version as an argument."
    echo "Usage: ./script_name.sh php_version"
    exit 1
    fi

    # PHP version from the command line argument
    PHP_VERSION=$1

    # Path to your php.ini file
    PHP_INI_PATH="/usr/local/lsws/lsphp${PHP_VERSION//.}/etc/php/${PHP_VERSION}/litespeed/php.ini"

    # Get total memory in megabytes
    TOTAL_MEM=$(free -m | awk '/^Mem:/{print $2}')

    # Calculate memory for post_max_size and upload_max_filesize (30% of total memory)
    UPLOAD_AND_POST_SIZE=$(( TOTAL_MEM * 30 / 100 ))

    # Calculate memory for memory_limit (40% of total memory)
    MEMORY_LIMIT=$(( TOTAL_MEM * 40 / 100 ))

    # Values for max_execution_time, max_input_time and max_input_vars
    MAX_EXECUTION_TIME=500
    MAX_INPUT_TIME=300
    MAX_INPUT_VARS=5000

    # Update values in php.ini file
    sed -i "s/post_max_size = .*/post_max_size = ${UPLOAD_AND_POST_SIZE}M/" $PHP_INI_PATH
    sed -i "s/upload_max_filesize = .*/upload_max_filesize = ${UPLOAD_AND_POST_SIZE}M/" $PHP_INI_PATH
    sed -i "s/memory_limit = .*/memory_limit = ${MEMORY_LIMIT}M/" $PHP_INI_PATH
    sed -i "s/max_execution_time = .*/max_execution_time = ${MAX_EXECUTION_TIME}/" $PHP_INI_PATH
    sed -i "s/max_input_time = .*/max_input_time = ${MAX_INPUT_TIME}/" $PHP_INI_PATH
    sed -i "s/;max_input_vars = .*/max_input_vars = ${MAX_INPUT_VARS}/" $PHP_INI_PATH

    # Restart lsws and kill all lsphp processes
    systemctl restart lsws
    killall -9 lsphp