#!/usr/bin/python3 -u import sys, os from subprocess import call, DEVNULL import argparse import platform import shutil import pathlib import distro import json #import pymysql.cursors #pymysql.install_as_MySQLdb pyv=platform.python_version_tuple() if pyv[0]!='3': print('Need python 3 for execute this script') sys.exit(1) parser = argparse.ArgumentParser(description='Script for add php to virtualhost.') parser.add_argument('--user', help='The unix user owner of domain', required=True) parser.add_argument('--php_version', help='The php version for add to apache', required=True) parser.add_argument('--domain', help='The domain of the new wordpress', required=True) args = parser.parse_args() print("Updating vhost for use php...") json_return={} apache_cmd='apache2' apachectl='apache2ctl' linux_distro=distro.id() if linux_distro!='debian' and linux_distro!='ubuntu': apache_cmd='httpd' apachectl='apachectl' # # PHP Options socket_php='' php_version=args.php_version print("PHP Version is "+php_version) # PHP-fpm things if linux_distro=='debian' or linux_distro=='ubuntu': socket_php='/run/php/php{}-{}-fpm.sock'.format(php_version, args.user) if not os.path.isfile(socket_php): shutil.copy('/etc/php/{}/fpm/pool.d/www.conf'.format(php_version), '/etc/php/{}/fpm/pool.d/{}.conf'.format(php_version, args.user)) # listen = /run/php/php8.2-fpm.sock # run/php/php8.1-fpm.sock php_edit="sudo sed -i 's/user = www-data/user = {}/g' /etc/php/{}/fpm/pool.d/{}.conf".format(args.user, php_version, args.user) php_edit+=" && sudo sed -i 's/group = www-data/group = {}/g' /etc/php/{}/fpm/pool.d/{}.conf".format(args.user, php_version, args.user) php_edit+=" && sudo sed -i 's/\/run\/php\/php{}-fpm.sock/\/run\/php\/php{}-{}-fpm.sock/g' /etc/php/{}/fpm/pool.d/{}.conf".format(php_version.replace('.', '\.'), php_version, args.user, php_version, args.user) php_edit+=" && sudo sed -i 's/\[www\]/[{}]/g' /etc/php/{}/fpm/pool.d/{}.conf".format(args.user, php_version, args.user) php_edit+=" && sudo sed -i 's/pm = dynamic/pm = ondemand/g' /etc/php/{}/fpm/pool.d/{}.conf".format(php_version, args.user) php_edit+=" && sudo systemctl restart php{}-fpm.service".format(php_version) if call(php_edit, shell=True, stdout=DEVNULL) > 0: json_return['error']=1 json_return['status']=1 json_return['progress']=100 json_return['message']='Error: the config is wrong '+args.domain print(json.dumps(json_return)) exit(1) else: print("Update php-fpm configuration") elif linux_distro=='rocky' or linux_distro=='fedora' or linux_distro=='almalinux' or linux_distro=='centos': php_version=php_version.replace('.', '') socket_php='/var/opt/remi/php'+php_version+'/run/php-fpm/{}-fpm.sock'.format(args.user) if not os.path.isfile(socket_php): shutil.copy('/etc/opt/remi/php'+php_version+'/php-fpm.d/www.conf', '/etc/opt/remi/php'+php_version+'/php-fpm.d/{}.conf'.format(args.user)) #sudo sed -i 's/user = apache/user = hosting/g' /etc/opt/remi/php82/php-fpm.d/www.conf #sudo sed -i 's/group = apache/group = hosting/g' /etc/opt/remi/php82/php-fpm.d/www.conf php_edit="sudo sed -i 's/user = apache/user = {}/g' /etc/opt/remi/php{}/php-fpm.d/{}.conf && sudo sed -i 's/group = apache/group = {}/g' /etc/opt/remi/php{}/php-fpm.d/{}.conf".format(args.user, php_version, args.user, args.user, php_version, args.user) php_edit+=" && sudo sed -i 's/\/var\/opt\/remi\/php{}\/run\/php-fpm\/www\.sock/\/var\/opt\/remi\/php{}\/run\/php-fpm\/{}-fpm.sock/g' /etc/opt/remi/php{}/php-fpm.d/{}.conf".format(php_version, php_version, args.user, php_version, args.user) php_edit+="&& sudo sed -i 's/\[www\]/[{}]/g' /etc/opt/remi/php{}/php-fpm.d/{}.conf".format(args.user, php_version, args.user) php_edit+=" && sudo sed -i 's/pm = dynamic/pm = ondemand/g' /etc/opt/remi/php{}/php-fpm.d/{}.conf".format(php_version, args.user) php_edit+="&& sudo systemctl restart php{}-php-fpm.service".format(php_version) if call(php_edit, shell=True, stdout=DEVNULL) > 0: json_return['error']=1 json_return['status']=1 json_return['progress']=100 json_return['message']='Error: the config is wrong '+vhost print(json.dumps(json_return)) exit(1) elif linux_distro=='arch': # /etc/php-legacy/php-fpm.d # Use php-legacy for arch. # pacman -S php-legacy php-legacy-gd php-legacy-fpm # listen = /run/php-fpm-legacy/php-fpm.sock # Install php socket_php='/run/php-fpm-legacy/{}-fpm.sock'.format(args.user) if not os.path.isfile(socket_php): shutil.copy('/etc/php-legacy/php-fpm.d/www.conf', '/etc/php-legacy/php-fpm.d/{}.conf'.format(args.user)) if call("sudo sed -i 's/user = http/user = {}/g' /etc/php-legacy/php-fpm.d/{}.conf && sudo sed -i 's/group = http/group = {}/g' /etc/php-legacy/php-fpm.d/{}.conf \ && sudo sed -i 's/\/run\/php-fpm-legacy\/php-fpm\.sock/\/run\/php-fpm-legacy\/{}-fpm.sock/g' /etc/php-legacy/php-fpm.d/{}.conf \ && sudo sed -i 's/\[www\]/[{}]/g' /etc/php-legacy/php-fpm.d/{}.conf \ && sudo sed -i 's/pm = dynamic/pm = ondemand/g' /etc/php-legacy/php-fpm.d/{}.conf \ && sudo systemctl restart php-legacy-fpm.service \ ".format(args.user, args.user, args.user, args.user, args.user, args.user, args.user, args.user, args.user), shell=True, stdout=DEVNULL) > 0: json_return['error']=1 json_return['status']=1 json_return['progress']=100 json_return['message']='Error: the config is wrong' print(json.dumps(json_return)) exit(1) pass # Apache things directory_match="# PHP Options \n \ \n \ SetHandler \"proxy:unix:{}|fcgi://localhost/\" \n \ ".format(socket_php) with open('/etc/%s/vhosts.d/php/%s-php.conf' % (apache_cmd, args.domain), 'w') as f: f.write(directory_match) print("Checking Apache configuration...") if call("sudo "+apachectl+" configtest", shell=True, stdout=DEVNULL) > 0: json_return['error']=1 json_return['status']=1 json_return['progress']=100 json_return['message']='Error: the config is wrong '+vhost print(json.dumps(json_return)) exit(1) print("Restarting apache...") if call("sudo systemctl restart "+apache_cmd, shell=True, stdout=DEVNULL) > 0: json_return['error']=1 json_return['status']=1 json_return['progress']=100 json_return['message']='Error: I cannot reload the httpd server' print(json.dumps(json_return)) exit(1)