Skip to content

Instantly share code, notes, and snippets.

@Climberdav
Created November 14, 2017 11:00
Show Gist options
  • Save Climberdav/4b4903f5dcc30b9829a00aa56f1f62b9 to your computer and use it in GitHub Desktop.
Save Climberdav/4b4903f5dcc30b9829a00aa56f1f62b9 to your computer and use it in GitHub Desktop.
Plesk Onyx switching php version with backup
#!/usr/bin/ruby
# -*- coding: utf-8 -*-
require 'fileutils'
current = %x[ php -v | awk NR==1'{print $2}' ]
current_short = current[0,3]
versions = Dir.entries("/opt/plesk/php/").select {|f| !File.directory? f}
puts "Actual version is #{current}"
puts "switching php may possible to version #{versions.delete_if{|i|i==current_short}.join(", ")} with symlinks.\n"
puts "Which version switch on?"
prompt = ">"
print prompt
to_version = gets.chomp
until versions.include? to_version do
print prompt
to_version = gets.chomp
end
# Backup original binairies
puts "Checking backup..."
dir = "/opt/plesk/php/#{current_short}"
# Directories
if !File.directory?("#{dir}")
Dir.mkdir("#{dir}")
end
if !File.directory?("#{dir}/bin")
Dir.mkdir("#{dir}/bin")
end
# binairies Files
if !File.file?("#{dir}/bin/php")
puts "Backing up original php"
FileUtils.mv '/usr/bin/php', "#{dir}/bin/"
end
if !File.file?("/opt/plesk/php/#{current_short}/bin/php-cgi")
puts "Backing up original php-cgi"
FileUtils.mv '/usr/bin/php-cgi', "#{dir}/bin/"
end
# deleting /usr/bin/php and /usr/bin/php-cgi
if File.file?("/usr/bin/php")
FileUtils.rm_f "/usr/bin/php"
end
if File.file?("/usr/bin/php-cgi")
FileUtils.rm_f "/usr/bin/php-cgi"
end
puts "done..."
# Creating symlinks to plesk
puts "Creating symlinks ..."
dir = "/opt/plesk/php/#{to_version}/bin"
FileUtils.ln_sf "#{dir}/php", "/usr/bin/php"
FileUtils.ln_sf "#{dir}/php-cgi", "/usr/bin/php-cgi"
puts "done..."
# Check new php version
puts "PHP version is: #{%x[ php -v | awk NR==1'{print $2}' ]}"
puts "and PHP binary path is: #{%x[ which php ]}"
puts "Process completed."
@Climberdav
Copy link
Author

Climberdav commented Nov 14, 2017

This gist permit you to change dynamically your php version in plesk to what contains your /opt/plesk/php install
He creates an hard backup of your original install in the same directory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment