Set/change PHP version command [Ubuntu/Debian]

Command to change PHP version in system and Apache2 with multiple PHP versions installed (from https://launchpad.net/~ondrej/+archive/ubuntu/php )

#!/bin/sh

if [ -z "$1" ]
then
        echo 'PHP version required'
else
        if test -f "/usr/bin/php"$1; then
                sudo a2dismod php7.0 > /dev/null
                sudo a2dismod php7.1 > /dev/null
                sudo a2dismod php7.2 > /dev/null
                sudo a2dismod php7.3 > /dev/null
                sudo a2dismod php7.4 > /dev/null
                sudo a2enmod php$1 > /dev/null
                systemctl restart apache2

                sudo ln -fs /usr/bin/php$1 /etc/alternatives/php
                sudo ln -fs /usr/bin/php-config$1 /etc/alternatives/php-config
                sudo ln -fs /usr/bin/phpdbg$1 /etc/alternatives/phpdbg
                sudo ln -fs /usr/bin/phpize$1 /etc/alternatives/phpize
                sudo ln -fs /usr/bin/phar$1 /etc/alternatives/phar
                sudo ln -fs /usr/bin/phar.phar$1 /etc/alternatives/phar.phar
                echo 'Changed PHP version to: '$1
        else
                echo 'Wrong PHP version: '$1
        fi
fi

Reset TFS database without removing accounts, players and guilds

SQL queries to reset database to ‘server start’ without removing accounts, players and guilds. All players are set to level 30 with valid hp, mana and cap for their vocations.

UPDATE `players`
SET
`health` = 185 + 22 * 5,
`healthmax` = 185 + 22 * 5,
`mana` =  35 + 22 * 30,
`manamax` = 35 + 22 * 30,
`cap` = 470 + 22 * 10,
`level` = 30,
`experience` = 368300,
`maglevel` = 30,
`soul` = 100,
`manaspent` = 0,
`lookaddons` = 0,
`posx` = 0,
`posy` = 0,
`posz` = 0, 
`conditions` = '',
`skull` = 0,
`skulltime` = 0,
`blessings` = 0,
`offlinetraining_time` = 43200,
`offlinetraining_skill` = -1,
`stamina` = 2520,
`skill_fist` = 10,
`skill_club` = 10,
`skill_sword` = 10,
`skill_axe` = 10,
`skill_dist` = 10,
`skill_shielding` = 10,
`skill_fishing` = 10,
`skill_fist_tries` = 0,
`skill_club_tries` = 0,
`skill_sword_tries` = 0,
`skill_axe_tries` = 0,
`skill_dist_tries` = 0,
`skill_shielding_tries` = 0,
`skill_fishing_tries` = 0
WHERE `vocation` =  1 OR `vocation`= 5  OR `vocation`= 2 OR `vocation`= 6;

UPDATE `players`
SET
`health` = 185 + 22 * 10,
`healthmax` = 185 + 22 * 10,
`mana` =  35 + 22 * 15,
`manamax` = 35 + 22 * 15,
`cap` = 470 + 22 * 20,
`level` = 30,
`experience` = 368300,
`maglevel` = 5,
`soul` = 100,
`manaspent` = 0,
`lookaddons` = 0,
`posx` = 0,
`posy` = 0,
`posz` = 0, 
`conditions` = '',
`skull` = 0,
`skulltime` = 0,
`blessings` = 0,
`offlinetraining_time` = 43200,
`offlinetraining_skill` = -1,
`stamina` = 2520,
`skill_fist` = 10,
`skill_club` = 10,
`skill_sword` = 10,
`skill_axe` = 10,
`skill_dist` = 30,
`skill_shielding` = 10,
`skill_fishing` = 10,
`skill_fist_tries` = 0,
`skill_club_tries` = 0,
`skill_sword_tries` = 0,
`skill_axe_tries` = 0,
`skill_dist_tries` = 0,
`skill_shielding_tries` = 0,
`skill_fishing_tries` = 0
WHERE `vocation` =  3 OR `vocation`= 7;

UPDATE `players`
SET
`health` = 185 + 22 * 15,
`healthmax` = 185 + 22 * 15,
`mana` =  35 + 22 * 5,
`manamax` = 35 + 22 * 5,
`cap` = 470 + 22 * 25,
`level` = 30,
`experience` = 368300,
`maglevel` = 2,
`soul` = 100,
`manaspent` = 0,
`lookaddons` = 0,
`posx` = 0,
`posy` = 0,
`posz` = 0, 
`conditions` = '',
`skull` = 0,
`skulltime` = 0,
`blessings` = 0,
`offlinetraining_time` = 43200,
`offlinetraining_skill` = -1,
`stamina` = 2520,
`skill_fist` = 10,
`skill_club` = 30,
`skill_sword` = 30,
`skill_axe` = 30,
`skill_dist` = 10,
`skill_shielding` = 10,
`skill_fishing` = 10,
`skill_fist_tries` = 0,
`skill_club_tries` = 0,
`skill_sword_tries` = 0,
`skill_axe_tries` = 0,
`skill_dist_tries` = 0,
`skill_shielding_tries` = 0,
`skill_fishing_tries` = 0
WHERE `vocation` =  4 OR `vocation`= 8;

DELETE FROM `account_ban_history`;
DELETE FROM `account_bans`;
DELETE FROM `house_lists`;
DELETE FROM `houses`;
DELETE FROM `market_history`;
DELETE FROM `market_offers`;
DELETE FROM `player_deaths`;
DELETE FROM `player_depotitems`;
DELETE FROM `player_inboxitems`;
DELETE FROM `player_items`;
DELETE FROM `player_spells`;
DELETE FROM `player_storage`;
DELETE FROM `players_online`;
DELETE FROM `tile_store`;

[TFS 1.x+] Generate big depot

LUA code to generate backpack full of backpacks:

function fillContainer(container, level, innerContainerCount)
    if level > 0 then
        for i = 1, innerContainerCount do
            local innerContainer = Game.createItem(2000)
            container:addItemEx(innerContainer)
            fillContainer(innerContainer, level - 1, innerContainerCount)
        end
    else
        for i = 1, 20 do
            local item = Game.createItem(i % 2 == 0 and 2376 or 2400)
            container:addItemEx(item)
        end
    end
end

function generateBigContainer(player, innerContainerCount, deepness)
    local item = player:getTile():addItem(2000)
    fillContainer(item, deepness, innerContainerCount)
    return item
end

Code to call it:

!lua generateBigContainer(player, 20, 2)

Requires !lua debug:

https://otland.net/threads/debug-lua-scripts-with-talkaction.112027/

https://otland.net/threads/tfs-1-x-luajit-debug-lua-scripts-with-talkaction.260910/

https://otland.net/threads/tfs-1-3-lua-5-2-debug-lua-scripts-with-talkaction.266086/

Fast configure multiple PHP versions php.ini

Script to configure PHP variables for development environment.

<?php

$phpDir = '/etc/php';

$backupPath = __DIR__ . DIRECTORY_SEPARATOR . 'config_backup.' . date('Y-m-d_H-i-s') . '.zip';
echo 'Backup config file: ' . $backupPath . PHP_EOL;
system('zip -r ' . $backupPath . ' ' . $phpDir . ' > /dev/null');
chmod($backupPath, 0777);

$values = [
	'upload_max_filesize' => '256M',
	'post_max_size' => '256M',
	'max_file_uploads' => 100,
	'memory_limit' => '1024M',
	'max_execution_time' => 15,
	'error_reporting' => 'E_ALL & ~E_DEPRECATED & ~E_STRICT',
	'display_errors' => 'Off',
	'log_errors' => 'On',
	'xdebug.remote_enable' => '1',
];
setPhpConfigValues($values, $phpDir, ['5.6', '7.0', '7.1', '7.2', '7.3'], ['apache2', 'fpm']);

$values = [
	'memory_limit' => -1,
	'max_execution_time' => 0,
	'error_reporting' => 'E_ALL & ~E_DEPRECATED & ~E_STRICT',
	'display_errors' => 'On',
	'log_errors' => 'Off',
];
setPhpConfigValues($values, $phpDir, ['5.6', '7.0', '7.1', '7.2', '7.3'], ['cli']);

system('systemctl restart apache2');
foreach(['5.6', '7.0', '7.1', '7.2', '7.3'] as $version) {
	system('systemctl restart php' . $version . '-fpm');
}

function setPhpConfigValues($values, $phpDir, $phpVersions, $phpTypes)
{
	foreach($phpVersions as $phpVersion) {
		foreach($phpTypes as $phpType) {
			$filePath = $phpDir . DIRECTORY_SEPARATOR . $phpVersion . DIRECTORY_SEPARATOR . $phpType . DIRECTORY_SEPARATOR . 'php.ini';
			if (file_exists($filePath)) {
				if (is_readable($filePath)) {
					if (is_writable($filePath)) {
						$lines = file($filePath);
						echo 'FILE: ' . $filePath . PHP_EOL;
						foreach($values as $searchKey => $newValue) {
							foreach($lines as $line => $text) {
								if (substr_count($text, '=') > 0) {
									list($lineKey, $lineValue) = explode('=', $text, 2);

									if (strpos($lineKey, $searchKey) !== false) {
										if (trim($lineValue) != $newValue) {
											echo 'Change key "' . $searchKey . '" value from "' . trim($lineValue) . '" to "' . $newValue . '"' . PHP_EOL;
											$lines[$line] = $searchKey . ' = ' . $newValue . PHP_EOL;
										}
										continue 2;
									}
								}
							}

							echo 'Add key "' . $searchKey . '" with value"' . $newValue . '"' . PHP_EOL;
							$lines[] = $searchKey . ' = ' . $newValue . PHP_EOL;
						}
						file_put_contents($filePath, implode('', $lines));
						echo 'SAVE FILE: ' . $filePath . PHP_EOL;
					} else {
						echo 'Config file not writable: ' . $filePath . PHP_EOL;
					}
				} else {
					echo 'Config file not readable: ' . $filePath . PHP_EOL;
				}
			} else {
				echo 'Config file not found: ' . $filePath . PHP_EOL;
			}
		}
	}
}

Linux Mint 19 – PHP/MariaDB dev env v1

apt install htop screen screenie mc nano tcpdump vim mariadb-server mariadb-client sshfs filezilla dbeaver-ce postgresql wireshark

Hasło do konta 'root' MariaDB ustawić na 'root'.

mysql
UPDATE mysql.user SET plugin = '', host = '%';
flush privileges;
SET PASSWORD FOR 'root'@'%' = PASSWORD('root');
flush privileges;

add-apt-repository ppa:ondrej/php
add-apt-repository ppa:ondrej/apache2
apt update

apt install apache2

apt install php5.6 php5.6-fpm php5.6-bcmath php5.6-bz2 php5.6-calendar php5.6-cli php5.6-common php5.6-ctype php5.6-curl php5.6-dba php5.6-dev php5.6-dom php5.6-exif php5.6-fileinfo php5.6-gd php5.6-gettext php5.6-gmp php5.6-iconv php5.6-imagick php5.6-intl php5.6-json php5.6-ldap php5.6-mailparse php5.6-mbstring php5.6-mongodb php5.6-mysql php5.6-mysqli php5.6-mysqlnd php5.6-oauth php5.6-odbc php5.6-opcache php5.6-pdo php5.6-pdo-dblib php5.6-pdo-mysql php5.6-pdo-odbc php5.6-pdo-pgsql php5.6-pdo-sqlite php5.6-pgsql php5.6-phar php5.6-phpdbg php5.6-posix php5.6-radius php5.6-readline php5.6-redis php5.6-simplexml php5.6-smbclient php5.6-soap php5.6-sockets php5.6-solr php5.6-sqlite3 php5.6-ssh2 php5.6-sybase php5.6-tokenizer php5.6-uploadprogress php5.6-uuid php5.6-xdebug php5.6-xml php5.6-xmlreader php5.6-xmlrpc php5.6-xmlwriter php5.6-yaml php5.6-zip

apt install php7.0 php7.0-fpm php7.0-bcmath php7.0-bz2 php7.0-calendar php7.0-cli php7.0-common php7.0-ctype php7.0-curl php7.0-dba php7.0-dev php7.0-dom php7.0-exif php7.0-fileinfo php7.0-gd php7.0-gettext php7.0-gmp php7.0-http php7.0-iconv php7.0-imagick php7.0-intl php7.0-json php7.0-ldap php7.0-mailparse php7.0-mbstring php7.0-mongodb php7.0-mysql php7.0-mysqli php7.0-mysqlnd php7.0-oauth php7.0-odbc php7.0-opcache php7.0-pdo php7.0-pdo-dblib php7.0-pdo-mysql php7.0-pdo-odbc php7.0-pdo-pgsql php7.0-pdo-sqlite php7.0-pgsql php7.0-phar php7.0-phpdbg php7.0-posix php7.0-radius php7.0-readline php7.0-redis php7.0-simplexml php7.0-smbclient php7.0-soap php7.0-sockets php7.0-solr php7.0-sqlite3 php7.0-ssh2 php7.0-sybase php7.0-tokenizer php7.0-uploadprogress php7.0-uuid php7.0-xdebug php7.0-xml php7.0-xmlreader php7.0-xmlrpc php7.0-xmlwriter php7.0-yaml php7.0-zip

apt install php7.1 php7.1-fpm php7.1-bcmath php7.1-bz2 php7.1-calendar php7.1-cli php7.1-common php7.1-ctype php7.1-curl php7.1-dba php7.1-dev php7.1-dom php7.1-exif php7.1-fileinfo php7.1-gd php7.1-gettext php7.1-gmp php7.1-http php7.1-iconv php7.1-imagick php7.1-intl php7.1-json php7.1-ldap php7.1-mailparse php7.1-mbstring php7.1-mongodb php7.1-mysql php7.1-mysqli php7.1-mysqlnd php7.1-oauth php7.1-odbc php7.1-opcache php7.1-pdo php7.1-pdo-dblib php7.1-pdo-mysql php7.1-pdo-odbc php7.1-pdo-pgsql php7.1-pdo-sqlite php7.1-pgsql php7.1-phar php7.1-phpdbg php7.1-posix php7.1-radius php7.1-readline php7.1-redis php7.1-simplexml php7.1-smbclient php7.1-soap php7.1-sockets php7.1-solr php7.1-sqlite3 php7.1-ssh2 php7.1-sybase php7.1-tokenizer php7.1-uploadprogress php7.1-uuid php7.1-xdebug php7.1-xml php7.1-xmlreader php7.1-xmlrpc php7.1-xmlwriter php7.1-yaml php7.1-zip

apt install php7.2 php7.2-fpm php7.2-bcmath php7.2-bz2 php7.2-calendar php7.2-cli php7.2-common php7.2-ctype php7.2-curl php7.2-dba php7.2-dev php7.2-dom php7.2-exif php7.2-fileinfo php7.2-gd php7.2-gettext php7.2-gmp php7.2-http php7.2-iconv php7.2-imagick php7.2-intl php7.2-json php7.2-ldap php7.2-mailparse php7.2-mbstring php7.2-mongodb php7.2-mysql php7.2-mysqli php7.2-mysqlnd php7.2-oauth php7.2-odbc php7.2-opcache php7.2-pdo php7.2-pdo-dblib php7.2-pdo-mysql php7.2-pdo-odbc php7.2-pdo-pgsql php7.2-pdo-sqlite php7.2-pgsql php7.2-phar php7.2-phpdbg php7.2-posix php7.2-radius php7.2-readline php7.2-redis php7.2-simplexml php7.2-smbclient php7.2-soap php7.2-sockets php7.2-solr php7.2-sqlite3 php7.2-ssh2 php7.2-sybase php7.2-tokenizer php7.2-uploadprogress php7.2-uuid php7.2-xdebug php7.2-xml php7.2-xmlreader php7.2-xmlrpc php7.2-xmlwriter php7.2-yaml php7.2-zip

apt install php7.3 php7.3-fpm php7.3-bcmath php7.3-bz2 php7.3-calendar php7.3-cli php7.3-common php7.3-ctype php7.3-curl php7.3-dba php7.3-dev php7.3-dom php7.3-exif php7.3-fileinfo php7.3-gd php7.3-gettext php7.3-gmp php7.3-http php7.3-iconv php7.3-imagick php7.3-intl php7.3-json php7.3-ldap php7.3-mailparse php7.3-mbstring php7.3-mongodb php7.3-mysql php7.3-mysqli php7.3-mysqlnd php7.3-oauth php7.3-odbc php7.3-opcache php7.3-pdo php7.3-pdo-dblib php7.3-pdo-mysql php7.3-pdo-odbc php7.3-pdo-pgsql php7.3-pdo-sqlite php7.3-pgsql php7.3-phar php7.3-phpdbg php7.3-posix php7.3-radius php7.3-readline php7.3-redis php7.3-simplexml php7.3-smbclient php7.3-soap php7.3-sockets php7.3-solr php7.3-sqlite3 php7.3-ssh2 php7.3-sybase php7.3-tokenizer php7.3-uploadprogress php7.3-uuid php7.3-xdebug php7.3-xml php7.3-xmlreader php7.3-xmlrpc php7.3-xmlwriter php7.3-yaml php7.3-zip


wyłączyć 'raphf' w PHP 5.6 (crashuje PHPa, a jest zaciągane razem z którąś z paczek)
rm /etc/php/5.6/cli/conf.d/20-raphf.ini /etc/php/5.6/fpm/conf.d/20-raphf.ini /etc/php/5.6/phpdbg/conf.d/20-raphf.ini
wyłączyć 'propro' i 'http', mają zależności do 'raphf' w PHP 5.6



a2enmod proxy
a2enmod proxy_fcgi

zmienić konfig PHPa:

/etc/php/5.6/fpm/php.ini
/etc/php/7.0/fpm/php.ini
/etc/php/7.1/fpm/php.ini
/etc/php/7.2/fpm/php.ini
/etc/php/7.3/fpm/php.ini

upload_max_filesize = 128M
post_max_size = 256M
memory_limit = 1024M
max_execution_time = 0

systemctl restart php5.6-fpm php7.0-fpm php7.1-fpm php7.2-fpm php7.3-fpm

a2enconf php5.6-fpm php7.0-fpm php7.1-fpm php7.2-fpm php7.3-fpm

// konfig domen domyslny wywalic, dodac konfig dla domen php**.sbg.best

<VirtualHost *:80>
        ServerName php56.sbg.best 

        ServerAdmin webmaster@php56.sbg.best
        DocumentRoot /home/jskalski/p/

        <FilesMatch \.php$>
                SetHandler "proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost/"
        </FilesMatch>

        ErrorLog ${APACHE_LOG_DIR}/error_56.log
        CustomLog ${APACHE_LOG_DIR}/access_56.log combined
</VirtualHost>



systemctl restart apache2



Generate big depot

LUA code to generate backpack full of backpacks:

function fill(container, level)
	for i = 1, 20 do
		local innerContainer = doAddContainerItem(container, 2000)
		if level > 0 then
			fill(innerContainer, level - 1)
		end
	end
end
container = doCreateItem(2000, getThingPos(cid)) fill(container, 2)

One-liner for !lua debug talkaction:

function fill(c, l) for i = 1, 20 do ic = doAddContainerItem(c, 2000) if l > 0 then fill(ic, l -1) end end end c = doCreateItem(2000, getThingPos(cid)) fill(c, 2)

Then put it in parcel and send to depot.

Keep 2 folders synchronized

Code to copy all changes from one folder to another. I use it to copy changes from my local folder to development server mounted by SSHFS.

In example files are filtered. Only *.cpp and *.h files are copied.

while inotifywait -e modify,create,delete "/home/jskalski/p/prv/otserv/"; do
    rsync -zvur --include="*.cpp" --include="*.h" --exclude="*" "/home/jskalski/p/prv/otserv/" "/mnt/devserver/home/js/otserv/"
done