📜 ⬆️ ⬇️

How I persuaded BILL and ISPmanager Lite 5 to change the RAM at the rate of virtual hosting

Until recently, I created websites and plugins on WordPress, renting virtual hosting from providers. For myself, a long time ago I singled out the ISP panel for its convenience and practicality. It so happened that I worked all the time on Windows, therefore, Linux for me is a dark forest with wild animals. Sites “grew” over time and became more demanding, at least to disk space and sometimes to RAM.

A couple of months ago, for some reasons, I decided to rent a virtual server on Linux and install ISP and BILL on my own to create and manage services.

After reading a few hours with the documentation and the SSH console, I launched my first server on CentOS. During the week I found out: why gmail.ru and mail.ru do not want to receive emails from my hosting, how to set restrictions on disk space, control php settings for each virtual hosting and that BILL, having in its arsenal the ability to purchase additional parameters, including item “RAM” cannot actually install it.
')
image

And the matter is not in BILL itself, but in ISP Lite - as I received technical support from ISP System:

The settings of the “RAM” add-on in billing affects the RAM allocated to the user in ISPmanager. But you use ISPmanager Lite, where there are no restrictions on RAM. Restriction on RAM is only in ISPmanager Business.

Pechalka. Pokhchav hour or two in Google, nothing useful and free did not find. In turn, there is only the possibility of "handles" to change the parameter:

1. Changing the php.ini file in the user folder:

image

2. Open the possibility for the client in ISPmanager to independently change the PHP parameters, which essentially changes the same php.ini file and also at the discretion of the client:

image

These methods "killed" all the automation of purchase and maintenance of hosting. In general, so "not ice." Since I now have my own server, then there is an opportunity to “climb” into the program, intercept the event and rewrite the cherished memory_limit item to the required value in the files.

Alpha solution

First of all, there was a simple plan: to find the installed data for each user in the Bill database and rewrite them in php.ini files with a simple PHP script. And PHP itself run through CRON. But remembering the situations when there was not enough 128MB for the site, I understood the following: these cases happened by chance and required almost an instant solution. Making CRON run this script every 5 minutes is too illiterate solution of the problem with an idle job 99% of the time.

Beta solution

Began to look for a way to hang the script on the event of the change of additional services shared hosting. The torment was just beginning, since the documentation on the ISPsystem site is not updated at all, which is recognized by the administrators and moderators themselves in their own forum. Googling, I found a way to create an event handler for PHP - which is just magical! Well, in any case, for me, since 90% of my working time is spent precisely writing scripts in this language. The goal was set for the script to be executed immediately after the additional services were changed by the client in the BILL panel and the restriction value was recorded in the php.ini file for the current virtual hosting. All the work was "at random, trial and error." As a result, a sufficiently working way was found to change the values ​​in the INI file at the time of updating the additional virtual hosting services in BILLmanager. In the documentation, this event is called “vhost.edit”, which occurs at the moment of opening the hosting settings and immediately after clicking on the “OK” button.

The script worked on the event and coped with the task, but what I didn’t figure out was the php-bin folders and the php.ini file attached to them. Namely: at first, these folders were located and worked fine along the path / var / www / {user} / data / php-bin / , but some time after my “bullying” on the ISP and the server as a whole, they changed their location to / var / www / php-bin / {user} / ... According to Google and the aforementioned non-documentation from ISP, there is an option DisableSecurePhpBin for this, but it did not affect in any way. As a result, I added to my plugin a search for the php.ini file in both places, as well as a search for a file named /var/www/{user}/data/php-bin/.php.ini (with a dot at the beginning).

What should be done

I will not write for a long time, I’ll just give you a ready-made instruction on how to use my mini-plugin for BILLmanager 5:

1. You need to create 2 files with root rights and the following content:

1.1. /usr/local/mgr5/etc/xml/billmgr_mod_hiweb_vhostram.xml

<?xml version="1.0" encoding="UTF-8"?> <mgrdata> <handler name="hiweb_vhostram.php" type="cgi"> <event name="vhost.edit" after="yes"/> </handler> </mgrdata> 

1.2. /usr/local/mgr5/addon/hiweb_vhostram.php

 #!/usr/bin/php <?php $logFile = '/var/www/log/hiweb_vhostram.log'; $logInfo = array(); //////// //       $fileSettings = dirname( dirname( __FILE__ ) ) . '/etc/billmgr.conf.d/db.conf'; if( file_exists( $fileSettings ) ){ $fileSettingsContent = file( $fileSettings, FILE_IGNORE_NEW_LINES ); foreach( $fileSettingsContent as $line ){ $arr = explode( ' ', trim( $line ) ); if( count( $arr ) != 2 ) continue; list( $lineKey, $lineValue ) = $arr; if( $lineKey == 'DBHost' ) $DBHost = $lineValue; if( $lineKey == 'DBUser' ) $DBUser = $lineValue; if( $lineKey == 'DBPassword' ) $DBPassword = $lineValue; if( $lineKey == 'DBName' ) $DBName = $lineValue; } $mysql = new mysqli( $DBHost, $DBUser, $DBPassword, $DBName ); //$R = array($_GET, $_POST, $_SERVER); if( $mysql ){ //    RAM $priceListResult = $mysql->query( 'SELECT id FROM pricelist WHERE itemtype=(SELECT id FROM itemtype WHERE name="RAM" AND statparam != "")' ); $priceList = reset( $priceListResult->fetch_assoc() ); ///   if(!isset($_SERVER['PARAM_addon_'.$priceList])){ $logInfo[] = 'error: no parameter [PARAM_addon_'.$priceList.'] in $_SERVER'; }else{ $additionMemory = $_SERVER['PARAM_addon_'.$priceList]; ///  $userLogin = $_SERVER['PARAM_username']; if( trim( $userLogin ) == '' ){ //do nothing } else{ $logInfo[] = 'select user ['.$userLogin.']'; /// //$userResult = $mysql->query( 'SELECT * FROM itemparam WHERE intname="username" AND item="' . $userId . '"' ); //$userData = $userResult->fetch_assoc(); //$userLogin = $userData['value']; /// ///  $phpIniFiles = array( '/var/www/php-bin/' . $userLogin . '/php.ini', '/var/www/' . $userLogin . '/data/php-bin/.php.ini', '/var/www/' . $userLogin . '/data/php-bin/php.ini' ); //  foreach( $phpIniFiles as $phpIni ){ $B = ''; if( !file_exists( $phpIni ) ){ $logInfo[] = 'error: file not exists!'; }elseif( !is_readable( $phpIni ) ){ $logInfo[] = 'error: file not readable!'; }elseif( !is_file( $phpIni ) ){ $logInfo[] = 'error: this is dir, not file!!!'; }else{ $content = file_get_contents( $phpIni ); preg_match('/^memory_limit = (\d){1,4}[a-zA-Z]?$/im',$content, $match); if(count($match) > 0){ $line = trim(reset($match)); //     $lineExplode = explode( ' = ', $line ); $lineExplode[1] = $additionMemory . 'M'; $newLine = implode( ' = ', $lineExplode ); //         if( trim( $line ) != trim( $newLine ) ){ $logInfo[] = file_put_contents($phpIni, str_replace($line,$newLine,$content)) ? $line.' → '.$newLine : 'error: file_put_content'; }else{ $logInfo[] = 'not change: '.$line.' → '.$newLine; } }else{ $add = 'memory_limit = ' . $additionMemory.'M'; $content .= chr( 13 ) . chr( 10 ) . $add . chr( 13 ) . chr( 10 ); $logInfo[] = file_put_contents($phpIni, $content) ? '+ '.$add : 'error add: file_put_content'; } } } } } } else { $logInfo[] = 'error: mysql not connected!'; } } file_put_contents($logFile, print_r( $logInfo, 1 ) ); echo '<?xml version="1.0" encoding="UTF-8"?><doc>'; echo '</doc>'; ?> 


2. Add an additional option “RAM” to the required tariff - I had it available immediately after installing the ISP, then BILL managers. It remains only to include it in the tariff, indicating a couple of data:

image

image

3. It is also necessary to transfer the WWW-domain → PHP to the “CGI” mode:

image

4. Try to “buy in” RAM for virtual hosting in BILLmanager and click “OK”:

image

5. Just in case, the PHP file for checking the work of changing the RAM limit on the shared hosting. Create it in the root of the desired host, next to the file index.php, then call it to check your domain.ru / file name.php :

 <?php function return_bytes( $val ){ $val = trim( $val ); $last = strtolower( $val[ strlen( $val ) - 1 ] ); switch( $last ){ // The 'G' modifier is available since PHP 5.1.0 case 'g': $val *= 1024; case 'm': $val *= 1024; case 'k': $val *= 1024; } return $val; } /** *        * @param $size - INT  * @return string */ function size_format( $size ){ $size = intval( $size ); if( $size < 1024 ){ return $size . " bytes"; }else if( $size < ( 1024 * 1024 ) ){ $size = round( $size / 1024, 1 ); return $size . " KB"; }else if( $size < ( 1024 * 1024 * 1024 ) ){ $size = round( $size / ( 1024 * 1024 ), 1 ); return $size . " MB"; }else{ $size = round( $size / ( 1024 * 1024 * 1024 ), 1 ); return $size . " GB"; } } $memory = size_format( return_bytes( ini_get( 'memory_limit' ) ) ); echo '<phpmem>'; echo '<val>' . $memory . '</val>'; echo '</phpmem>'; 

Conclusion


Current documentation allows you to save a lot of time! )) This plugin is my first experience of writing such add-ons and not the fact that it will work correctly or work at all, since I only understand Linux complex and Server software. I think, over time, I will finalize it, if it makes sense.

Source: https://habr.com/ru/post/316264/


All Articles