Files
zyt/identity-admin/.test-runtime/production-x/isolate_fpm_remote.py
T
2026-11-11 14:22:02 +08:00

95 lines
3.4 KiB
Python

import os,shutil,subprocess,time,json
ROOT='/www/wwwroot/x.zhenyangtang.com.cn'
BACKUP=ROOT+'/backups/fpm-isolation-20261104'
CONFIG='/etc/identity-group/php-fpm.conf'
UNIT='/etc/systemd/system/x-identity-php82.service'
assert not os.path.exists(BACKUP) and not os.path.exists(CONFIG) and not os.path.exists(UNIT)
os.makedirs(BACKUP,mode=0o700)
os.makedirs(os.path.dirname(CONFIG),mode=0o700)
config='''[global]
pid = /run/x-identity-php82.pid
error_log = /www/wwwlogs/x-identity-php82.log
daemonize = no
[identity_group]
user = www
group = www
listen = /tmp/x-identity-group-82.sock
listen.owner = www
listen.group = www
listen.mode = 0660
pm = ondemand
pm.max_children = 8
pm.process_idle_timeout = 20s
pm.max_requests = 300
request_terminate_timeout = 60
catch_workers_output = yes
clear_env = yes
security.limit_extensions = .php
php_admin_value[open_basedir] = @ROOT@/:/tmp/
php_admin_flag[display_errors] = off
php_admin_flag[log_errors] = on
php_admin_value[error_log] = @ROOT@/shared/runtime/group-php-errors.log
[identity_source]
user = www
group = www
listen = /tmp/x-identity-source-82.sock
listen.owner = www
listen.group = www
listen.mode = 0660
pm = ondemand
pm.max_children = 8
pm.process_idle_timeout = 20s
pm.max_requests = 300
request_terminate_timeout = 60
catch_workers_output = yes
clear_env = yes
security.limit_extensions = .php
php_admin_value[open_basedir] = @ROOT@/:/www/wwwroot/zyt/:/tmp/
php_admin_flag[display_errors] = off
php_admin_flag[log_errors] = on
php_admin_value[error_log] = @ROOT@/shared/runtime/source-php-errors.log
'''.replace('@ROOT@',ROOT)
unit='''[Unit]
Description=Group Identity dedicated PHP 8.2 FPM
After=network.target
[Service]
Type=simple
ExecStart=/www/server/php/82/sbin/php-fpm -F -y /etc/identity-group/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
KillSignal=SIGQUIT
Restart=on-failure
RestartSec=3
[Install]
WantedBy=multi-user.target
'''
for p,s in [(CONFIG,config),(UNIT,unit)]:
with open(p,'w') as f:f.write(s)
os.chmod(p,0o600 if p==CONFIG else 0o644)
subprocess.check_call(['/www/server/php/82/sbin/php-fpm','-t','-y',CONFIG])
subprocess.check_call(['systemctl','daemon-reload'])
subprocess.check_call(['systemctl','enable','--now','x-identity-php82.service'])
for attempt in range(50):
if all(os.path.exists(p) for p in ['/tmp/x-identity-group-82.sock','/tmp/x-identity-source-82.sock']):break
time.sleep(0.1)
else:raise RuntimeError('Dedicated FPM sockets did not appear')
for name,sock in [('x.zhenyangtang.com.cn.conf','x-identity-group-82.sock'),('x-identity-source-internal.conf','x-identity-source-82.sock')]:
p='/www/server/panel/vhost/nginx/'+name
shutil.copy2(p,BACKUP+'/'+name)
s=open(p).read()
assert s.count('fastcgi_param PHP_ADMIN_VALUE ')==1
s='\n'.join(l for l in s.split('\n') if 'fastcgi_param PHP_ADMIN_VALUE ' not in l)
assert s.count('fastcgi_pass unix:/tmp/php-cgi-82.sock;')==1
s=s.replace('fastcgi_pass unix:/tmp/php-cgi-82.sock;','fastcgi_pass unix:/tmp/'+sock+';')
with open(p+'.x-staged','w') as f:f.write(s)
os.replace(p+'.x-staged',p)
subprocess.check_call(['/www/server/nginx/sbin/nginx','-t'])
subprocess.check_call(['/www/server/nginx/sbin/nginx','-s','reload'])
# Clear only the existing shared PHP 8.2 workers affected by the prior per-request override.
# The original pool configuration and all of its virtual hosts remain intact.
subprocess.check_call(['/etc/init.d/php-fpm-82','reload'])
print(json.dumps({'dedicated_service':'x-identity-php82.service','backup':BACKUP,'shared_pool_reloaded':True}))