Файловый менеджер - Редактировать - /opt/cpmigrate/environments/easyapache3.py
Ðазад
"""EasyApache3 Environment Module""" import glob import json import os import subprocess from datetime import datetime from environments.base import Environment class EasyApache3(Environment): """ Adds support for EasyApache 3 environment. This will be skipped if origin is using EasyApache 4. """ def __init__(self): Environment.__init__(self) self.priority = 99 self.imh_php_packages = [ 'imh-php52', 'imh-php53', 'imh-php54', 'imh-php55', 'imh-php56', 'imh-php70', 'imh-php71', 'imh-php72', ] self.installed_versions = [] self.to_install = [] self.default_version = None self.php_handler = None self.ea3_modules = '/opt/cpmigrate/ea3_modules.json' def check(self, _): if self.xfer.ea_version == 3: self.check_default_php() self.check_installed_php() self.check_apache_modules() if self.default_version > 50 and self.default_version < 70: self.check_php_handler() self.get_packages_toinstall() if len(self.to_install) > 0: self.actions.append( f"+ Install packages: {' '.join(self.to_install)}" ) self.actions.append( "+ Comment out suPHP_ConfigPath from htaccess files." ) self.actions.append( "+ Comment out legacy AddHandler from htaccess files." ) self.actions.append("+ Comment out php_value from htaccess files.") self.actions.append( "+ Change default PHP memory limit from 32M to 512M." ) def run(self, _): if self.xfer.ea_version == 3: self.install_packages() self.set_default_version() self.set_php_handler() self.set_default_memlimit() else: self.info("Skipping this environment, origin is EA4.") def check_default_php(self): """Checks and returns the origin server's default PHP version.""" ret_code, out = self.xfer.origin_command( "/usr/bin/php -v", command_out=subprocess.PIPE, sleep=2, quiet=True ) if ret_code == 0: output = out[0] if 'PHP' in output: version = output[4:7] self.default_version = int(version.replace(".", "")) if self.default_version < 53: self.warning(f"Origin default PHP version is {version}.") else: self.info(f"Origin default PHP version is {version}.") return version self.warning(f"Got unexpected output from PHP binary: {output}") self.error("Failed to obtain server default PHP version.") return None def check_installed_php(self): """Checks the installed PHP versions on the origin.""" self.info("Checking installed PHP versions on origin.") ret_code, out = self.xfer.origin_command( f"/bin/rpm -qa {' '.join(self.imh_php_packages)}", command_out=subprocess.PIPE, sleep=2, ) if ret_code == 0: for line in out: for package in self.imh_php_packages: if package in line: self.installed_versions.append(package) else: self.error("Failed to get RPM status for PHP packages.") def check_apache_modules(self): """Checks the Apache modules used on the origin.""" self.info("Checking Apache modules on origin.") with open(self.ea3_modules, encoding="utf-8") as inp: modules = json.load(inp).get('modules') ret_code, out = self.xfer.origin_command( "/usr/local/apache/bin/httpd -M", command_out=subprocess.PIPE, sleep=2, ) if ret_code == 0: for line in out: for module in modules: if module in line: self.info(f"Detected {module} on origin.") self.to_install.append(modules[module]) else: self.error("Failed to get Apache modules on origin.") def check_php_handler(self): """Checks the PHP handler used by PHP5 on the origin server.""" self.info("Checking default PHP handler on origin.") ret_code, out = self.xfer.origin_command( "/usr/local/cpanel/bin/rebuild_phpconf --current", command_out=subprocess.PIPE, sleep=2, ) if ret_code == 0: for line in out: if "PHP5 SAPI" in line: self.php_handler = line.split(': ')[1] self.php_handler = self.php_handler.rstrip('\n') if self.php_handler == "suphp": self.info("Origin is using suPHP handler.") elif self.php_handler == "cgi": self.info("Origin is using CGI PHP handler.") self.actions.append("+ Set PHP handler to CGI.") elif self.php_handler == "fcgi": self.info("Origin is using FCGI PHP handler.") self.actions.append("+ Install mod_fcgi.") self.to_install.append("ea-apache24-mod_fcgid") self.actions.append("+ Set PHP handler to FCGI.") elif self.php_handler == "dso": self.actions.append( "!! Not installing DSO, unsupported. Will use suPHP." ) self.critical("Origin is using DSO PHP handler.", note=True) self.php_handler = "suphp" else: self.error(f"Unknown PHP handler: [{self.php_handler}]") else: self.error("Failed to retrieve PHP handler.") def get_packages_toinstall(self): """Builds the package list that needs to be installed, based on all of the information we have gathered. """ if len(self.installed_versions) > 0: for package in self.installed_versions: if package.startswith('imh-php'): version = int(package[7:9]) if version >= 54: self.to_install.append(f'ea-php{version}') self.to_install.append(f'ea-php{version}-php-*') self.info(f"Found PHP{version}.") else: self.critical( f"PHP{version} is too old to be installed." ) self.actions.append( f"!! Cannot install PHP{version}, it is not " "supported on EasyApache 4. Sites using this " "may not work." ) version = self.default_version if self.default_version >= 54: if f'ea-php{version}' not in self.to_install: self.to_install.append(f'ea-php{version}') self.to_install.append(f'ea-php{version}-php-*') else: if len(self.installed_versions) == 0: self.critical(f"PHP {version} is too old to be installed.") self.actions.append( f"!! Cannot install PHP {version}, it is not supported on " "EasyApache 4. Sites using this may not work." ) self.actions.append( "+ Server default PHP version will be set to PHP 5.4." ) self.default_version = 54 if 'ea-php54' not in self.to_install: self.to_install.append('ea-php54') self.to_install.append('ea-php54-php-*') def install_packages(self): """Installs the PHP version + all extensions.""" log_path = os.path.join(self.xfer.log_path, 'php_install.log') install_command = [ '/usr/bin/yum', 'install', '-y', '--exclude', '*-recode', '--exclude', '*debuginfo*', '--skip-broken', ] install_command.extend(self.to_install) if len(self.to_install) > 0: self.info(f"Installing PHP packages: {', '.join(self.to_install)}") self.info(f"Log file: {log_path}") with open(log_path, 'w', encoding="utf-8") as out: out.write(f"Running command: {install_command}") ret_code, _ = self.xfer.local_command( install_command, command_out=out ) if ret_code == 0: self.info("Installed PHP packages successfully.") else: self.info("Failed to install PHP packages. Check log.") else: self.info("No PHP packages to install.") def set_default_version(self): """Sets the server default PHP version.""" if self.default_version: self.xfer.whmapi_call( 'php_set_system_default_version', {'version': f'ea-php{self.default_version}'}, ) else: self.warning( "Could not set default PHP version as there was none found." ) def set_php_handler(self): """Sets the PHP handler for the PHP 5 versions that were installed.""" if len(self.installed_versions) > 0: for package in self.installed_versions: if package.startswith('imh-php'): version = int(package[7:9]) if 53 < version < 70: self.xfer.whmapi_call( 'php_set_handler', { 'version': f'ea-php{version}', 'handler': self.php_handler, }, ) self.info( f"Handler for PHP{version} set to " f"{self.php_handler}." ) else: if self.default_version > 53 and self.default_version < 70: self.xfer.whmapi_call( 'php_set_handler', { 'version': f'ea-php{self.default_version}', 'handler': self.php_handler, }, ) self.info( f"Handler for PHP{self.default_version} set to " f"{self.php_handler}." ) def post_transfer(self): if self.xfer.ea_version == 3: self.comment_htaccess() def comment_htaccess(self): """Comments out all suPHP_ConfigPath and legacy AddHandler lines""" self.info( "Commenting out all suPHP_ConfigPath, legacy AddHandler and " "php_value lines in htaccess files." ) date = datetime.strftime(datetime.now(), '%m%d%Y') ret_code, _ = self.xfer.local_command( [ '/bin/find', '/home', '-maxdepth', '6', '-type', 'f', '-name', '.htaccess', '-exec', 'sed', '-E', f'-i.{date}.bak', '-e', r's/^\s*suphp_configpath/# SuPHP_ConfigPath/gi', '-e', r's/^(AddHandler.*httpd\-php.*)/#\1/g', '-e', r's/^\s*php_value/# php_value/g', '{}', ';', ] ) if ret_code == 0: self.info("Successfully commented out.") else: self.warning( "Failed to comment out suPHP_, AddHandler and php_value lines." ) def set_default_memlimit(self): """Changes the global memory limit from 32M to 256M""" files = glob.glob("/opt/cpanel/ea-php**/root/etc/php.ini") command = [ '/bin/sed', '-i', r's/memory_limit = 32M/memory_limit = 512M/g', ] command.extend(files) ret_code, _ = self.xfer.local_command(command) if ret_code == 0: self.info( "Successfully changed default PHP memory limit from 32M " " to 512M." ) else: self.error( "Failed to change default PHP memory limit from 32M to 512M." ) def capture_state(self): state = { 'php_handler': self.php_handler, 'default_version': self.default_version, 'to_install': self.to_install, 'installed_versions': self.installed_versions, } return super().capture_state(state) def load_state(self, loadstate): self.php_handler = loadstate.get('php_handler', None) self.default_version = loadstate.get('default_version', None) self.to_install = loadstate.get('to_install', []) self.installed_versions = loadstate.get('installed_versions', []) super().load_state(loadstate)
| ver. 1.1 | |
.
| PHP 8.3.30 | Ð“ÐµÐ½ÐµÑ€Ð°Ñ†Ð¸Ñ Ñтраницы: 0 |
proxy
|
phpinfo
|
ÐаÑтройка