Selecting default PHP version

amphora

Verified User
Joined
Feb 21, 2013
Messages
46
I'm offering 4 PHP versions on my server:

php1_release=7.0
php2_release=7.1
php3_release=7.2
php4_release=7.3

The way I have it now is that all news sites I add on the server will have PHP 7.0 as default. I would like to be 7.3 the default. The only way to do that is by setting php1_release to 7.3 but that means all sites that use 7.0 now will upgrade to 7.3, I don't want that.
Ideally custombuild would support an option like php_default=4 so all newly added domains will use the latest php version without changing all existing domains.
Is there a workaround available to do this?
 

bdacus01

Verified User
Joined
Jul 22, 2017
Messages
2,693
Location
Murfreesboro
Might need more info.

Are you using CloudLinux or native OS? What OS are you using? Which selector are you referring to?

Have you actually tested and determined it wont work?

Don't the users select the PHP they want in PHP selector? So if they have 7.0 selected you are saying CB would change their selection to 7.3? I hope not.
 

amphora

Verified User
Joined
Feb 21, 2013
Messages
46
I'm using DA on CentOS 7. I'm not referring to any existing selector because it's not there. So maybe it's a feature request and should have posted it somewhere else?
I want to be able to set a default PHP version for new domains I add. Now it picks php1_release as default. I want it to be php4_release for example. Or when PHP 7.4 is out (and I replace php1_release=7.0 with php1_release=7.4) I want php1_release to be the default again.
 
Last edited:

bdacus01

Verified User
Joined
Jul 22, 2017
Messages
2,693
Location
Murfreesboro
the php selector is already in the system

Screen Shot 2019-07-15 at 10.56.45.png


The user can choose whatever they need.

Screen Shot 2019-07-15 at 10.58.19.png


Might need to go in feature section.
 

amphora

Verified User
Joined
Feb 21, 2013
Messages
46
I want a system wide default selector. I don't want to manually need to change it everytime I add a new user
 

bdacus01

Verified User
Joined
Jul 22, 2017
Messages
2,693
Location
Murfreesboro
I see what you are saying.

After some random testing of my own. It seems to be based on Position. Not good.

What the user selects needs to be maintained as long as it available no matter the position in the list.

example if we have this set

php1_release=7.0
php2_release=7.1
php3_release=7.2
php4_release=7.3

then the users have

user a 7.3
user b 7.2
user c 7.1
user d 7.0

then we go into options conf
and set

php1_release=7.3
php2_release=7.2
php3_release=7.1
php4_release=7.0

then the users have

user a 7.0
user b 7.1
user c 7.2
user d 7.3

not good..

Am I close...now?
 

amphora

Verified User
Joined
Feb 21, 2013
Messages
46
Exactly. Changing the phpN_release order every time a new version is released is far from ideal. But I do want new customers/users/domains to use the latest version when adding them. And with the current implementation that is only possible by changing php1_release value but that affects all current domains/users so unless I wanna change all users manually (or make a script which can be risky) I'm not going to do that.
Easiest way to avoid this, I think, is adding an option like php_default=4. And in DirectAdmin itself it would be nice that the PHP version selector is sorted by version number (latest on top) and not position.

So if I have something like this:

php1_release=7.1
php2_release=7.3
php3_release=7.0
php4_release=7.2
php_default=2

The select box would show

7.3 (default)
7.2
7.1
7.0

And when 7.4 is release I'll just replace php3_release=7.0 with php3_release=7.4 and set php_default=3
This way only users who are still using 7.0 (who probably shouldn't anymore) get affected and automatically upgraded.
 
Last edited:

mmgenius

Verified User
Joined
Jan 20, 2004
Messages
140
Is this php_default allready implemented? Now that PHP 7.4 is ready, it would be usefull.
 

tanto.prihartanto

New member
Joined
Dec 21, 2019
Messages
3
PHP7.4 is a good choice, but remember.. many external featured have not supported php7.4 yet. For the example, ioncube loader.
 

kristian

Verified User
Joined
Nov 4, 2005
Messages
195
Location
Norway
Until this feature request is added, what method is best to work with when adding new PHP versions, and swapping the default one? You risk people being downgraded when the selector is based on position, e.g. in the following scenario:

Before:
Bash:
php1_release=7.2
php2_release=7.3
php3_release=7.4

Let's say myuser has selected 7.4, so position 3.

I now want to have 7.4 the default on the server, but still have the option of providing 7.2 for people not ready to upgrade.

After:
Bash:
php1_release=7.4
php2_release=7.3
php3_release=7.2

This will effectively downgrade myuser from 7.4 to 7.2, because position 3 is still the selected position.

Is there a script I can run before/after that will try and maintain the versions from the selector, rather than the positions?
 

hugor

New member
Joined
Dec 16, 2019
Messages
3
Hi,

I encountered this same problem (again), and made a solution for this.

My settings were like this:
Code:
php1_release=7.2
php2_release=7.3
php3_release=7.4

Desired state was:
  • Swap php1 and php3, so that php7.4 becomes the default
  • Don't mass-update sites currently running 7.2 (version 1) to php 7.4 (version 3)
I made a simple bash script to update users' config, to swap it's php version selector. If you run this, and modify the options.conf values, effectively the users' php version stays the same, but the php1_release (and therefore the default) will change.

An example will make this more clear I think:
Code:
# swap versions in users' config (custom script, see below)
./da_php_swap_version.sh 1 3

# Update DA options and rebuild php
cd /usr/local/directadmin/custombuild/
./build set php1_release 7.4
./build set php3_release 7.2
./build php n

# probably not necessary
./build rewrite_confs

The used script has the following content:
Bash:
#!/bin/bash

# Swap version index numbers in config. This might be helpful when changing the
# order of your php-fpm index numbers in the DirectAdmin configuration.
# In case version "1" has been requested, change configs with no version
# set, as "1" is the default.

set -euo pipefail
# helper functions
die() { echo "$*" >&2; exit 1;}
warn() { echo "$*" >&2; }

usage="Usage: $0 <version 1-4> <version 1-4>"

[ `id -nu` != "root" ] && die "Please execute as root"

[ "$#" -ne 2 ] && die $usage
! [[ "$1" =~ ^[1-4]$ ]] && die $usage
! [[ "$2" =~ ^[1-4]$ ]] && die $usage

version_swap1=$1
version_swap2=$2

swap_version() {
    local conf=$1
    local version_current

    [ -f "$conf" ] || die "File does not exist: $conf"

    # Get current version if set in config
    set +e
    version_current=$(grep -soP "php1_select=\K[0-9]" $conf) 
    set -e
    [ $? -eq 2 ] && die "grep encountered a fatal error"

    if [ -z $version_current ]; then
        # current version not found in config. assume "1"
        version_current=1
    fi

    # Is this the version we're looking for?
    if [ $version_current -eq $version_swap1 ]; then
        # replace or add line
        sed -i '/^php1_select=/{h;s/=.*/='$version_swap2'/};${x;/^$/{s//php1_select='$version_swap2'/;H};x}' "$conf"
        echo "$conf: changed"
    elif [ $version_current -eq $version_swap2 ]; then
        # replace or add line
        sed -i '/^php1_select=/{h;s/=.*/='$version_swap1'/};${x;/^$/{s//php1_select='$version_swap1'/;H};x}' "$conf"
        echo "$conf: changed"
    fi
}

for f in /usr/local/directadmin/data/users/*/domains/*.conf; do
    swap_version $f
done

echo "To update the running configuration, please run: /usr/local/directadmin/custombuild/build rewrite_confs"

Not needed for this, but if you would like to update users with a certain php version to a different version (replace version X with Y), you can use this script:
Bash:
#!/bin/bash

# Search for specific version (1-4) and replace with specified version. If
# replacement of version "1" has been requested, change configs with no version
# set, as "1" is the default.

set -euo pipefail
# helper functions
die() { echo "$*" >&2; exit 1;}
warn() { echo "$*" >&2; }

# Default values
confused=false
version_search=0
version_replace=0

[ `id -nu` !=  "root" ] && die "Please execute as root"

while getopts s:r:v c; do
    case "$c" in
        s) version_search="$OPTARG";;
        r) version_replace="$OPTARG";;
        *) confused=true;;
    esac
done

shift `expr $OPTIND - 1`

$confused || [ "$version_search" -eq 0 -o "$version_replace" -eq 0 ] && die "Usage: -s <version to search for> -r <version to replace with>"
! [[ "$version_search" =~ ^[1-4]$ ]] && die "Search version should be between 1-4"
! [[ "$version_replace" =~ ^[1-4]$ ]] && die "Replacement version should be between 1-4"

replace_version() {
    local conf=$1
    local version_current

    [ -f "$conf" ] || die "File does not exist: $conf"

    # Get current version if set in config
    set +e
    version_current=$(grep -soP "php1_select=\K[0-9]" $conf) 
    set -e
    [ $? -eq 2 ] && die "grep encountered a fatal error"

    if [ -z $version_current ]; then
        # current version not found in config. assume "1"
        #echo "$conf has no version set, assume 1"
        version_current=1
    fi

    # Is this the version we're looking for?
    if [ $version_current -eq $version_search ]; then
        # replace or add line
        sed -i '/^php1_select=/{h;s/=.*/='$version_replace'/};${x;/^$/{s//php1_select='$version_replace'/;H};x}' "$conf"
        echo "$conf: changed"
    fi
}

for f in /usr/local/directadmin/data/users/*/domains/*.conf; do
    replace_version $f
done

echo "To update the running configuration, please run: /usr/local/directadmin/custombuild/build rewrite_confs"
 

kristian

Verified User
Joined
Nov 4, 2005
Messages
195
Location
Norway
I ended up writing a script for it myself. After reading through yours, I realized I missed a case when the config existed, but the php1_select did not, so thanks for that. ;-)

I've put my script up at https://gitlab.com/nordhost/da-scripts/-/blob/master/php-version-selections in case anyone might find it useful.

In short, what it does is that it lets you save the actual PHP version each domain has selected. After you've updated the options.conf, the script can then restore those versions by updating the php1_select (or fall back to the default) accordingly. Then when you do a ./build rewrite_confs, versions should stay the same.
 

kristian

Verified User
Joined
Nov 4, 2005
Messages
195
Location
Norway
The script itself doesn't really know anything about versions, it uses the values specified in options.conf, so it should work for anything.
 
Top