From d3d041f5697908808b7e69bf3106de9bef8483c9 Mon Sep 17 00:00:00 2001 From: absurdo Date: Thu, 14 Dec 2023 23:51:40 +0100 Subject: [PATCH] Fixes in proxy virtualhost --- scripts/delete_php.py | 50 +++++++++++++++++++ scripts/files/vhost.tpl | 2 + scripts/install_apache.php | 4 +- scripts/webapps/delete_app_apache.py | 4 +- scripts/webapps/flask/delete_flask.py | 6 ++- scripts/webapps/flask/install_flask_site.py | 11 ++-- .../phpmyadmin/install_phpmyadmin_site.py | 8 +-- scripts/webapps/proxy/install_proxy_site.py | 16 +++--- .../wordpress/install_wordpress_site.py | 4 +- 9 files changed, 84 insertions(+), 21 deletions(-) create mode 100644 scripts/delete_php.py diff --git a/scripts/delete_php.py b/scripts/delete_php.py new file mode 100644 index 0000000..f26e16d --- /dev/null +++ b/scripts/delete_php.py @@ -0,0 +1,50 @@ +#!/usr/bin/python3 -u + +import argparse +import os +#from pastafariutils.unix import add_user, del_user +from pathlib import Path +from subprocess import call, DEVNULL +import json +import time +import shutil +import pwd +import distro +import subprocess +import re +import sys + + +parser=argparse.ArgumentParser(prog='delete_wordpress.py', description='A tool for delete wordpress') + +parser.add_argument('--domain', help='Domain to delete', required=True) + +args=parser.parse_args() + +apache_cmd='apache2' + +apachectl='apache2ctl' + +linux_distro=distro.id() + +if linux_distro!='debian' and linux_distro!='ubuntu': + apache_cmd='httpd' + apachectl='apachectl' + +print('Deleting the php apache configuration of virtualhost..') + +apache_php='/etc/{}/vhosts.d/php/{}-php.conf'.format(apache_cmd, args.domain) + +if os.path.isfile(apache_php): + if subprocess.call("sudo rm %s" % (apache_php), shell=True) > 0: + print('Error: cannot delete php config %s' % apache_php) + sys.exit(1) + +print('Deleted the php apache configuration of virtualhost..') + +if subprocess.call("sudo systemctl restart %s" % (apache_cmd), shell=True) > 0: + print('Error: cannot restart Apache server') + sys.exit(1) + +print('Deleted the apache configuration of virtualhost successfully..') + diff --git a/scripts/files/vhost.tpl b/scripts/files/vhost.tpl index 31d6051..0ebf6e5 100644 --- a/scripts/files/vhost.tpl +++ b/scripts/files/vhost.tpl @@ -13,6 +13,8 @@ Require all granted IncludeOptional vhosts.d/extra/$domain-*.conf + IncludeOptional vhosts.d/extra/proxy/$domain-*.conf + IncludeOptional vhosts.d/extra/proxyphp/$domain-*.conf ErrorLog /var/log/$apache_cmd/$domain-error.log LogLevel error CustomLog /var/log/$apache_cmd/$domain-access.log combined diff --git a/scripts/install_apache.php b/scripts/install_apache.php index f7a9b8b..00cb092 100644 --- a/scripts/install_apache.php +++ b/scripts/install_apache.php @@ -20,11 +20,11 @@ Linux::install_package($linux_package); Linux::json_log('Create directories for Apache manager...', $error=0, $status=0, $progress=0, $no_progress=1); -$create_dirs=['debian' => ["sudo mkdir -p /etc/apache2/vhosts.d/ && sudo mkdir -p /etc/apache2/vhosts.d/php/ && sudo mkdir -p /etc/apache2/vhosts.d/extra/"]]; +$create_dirs=['debian' => ["sudo mkdir -p /etc/apache2/vhosts.d/ && sudo mkdir -p /etc/apache2/vhosts.d/{php,extra/proxyphp,extra/proxy}"]]; $create_dirs['ubuntu']=$create_dirs['debian']; -$create_dirs['fedora']=["sudo mkdir -p /etc/httpd/vhosts.d/{php,extra} "]; +$create_dirs['fedora']=["sudo mkdir -p /etc/httpd/vhosts.d/{php,extra/proxyphp,extra/proxy} "]; $create_dirs['almalinux']=$create_dirs['fedora']; diff --git a/scripts/webapps/delete_app_apache.py b/scripts/webapps/delete_app_apache.py index 14f14e7..17aa8e9 100644 --- a/scripts/webapps/delete_app_apache.py +++ b/scripts/webapps/delete_app_apache.py @@ -48,9 +48,9 @@ if os.path.isfile(apache_php): apache_webapp='/etc/{}/vhosts.d/extra/{}-{}.conf'.format(apache_cmd, args.domain, args.webapp) -apache_sub0_webapp='/etc/{}/vhosts.d/extra/{}-000-{}.conf'.format(apache_cmd, args.domain, args.webapp) +apache_sub0_webapp='/etc/{}/vhosts.d/extra/proxy/{}-{}.conf'.format(apache_cmd, args.domain, args.webapp) -apache_sub1_webapp='/etc/{}/vhosts.d/extra/{}-001-{}.conf'.format(apache_cmd, args.domain, args.webapp) +apache_sub1_webapp='/etc/{}/vhosts.d/extra/proxyphp/{}-{}.conf'.format(apache_cmd, args.domain, args.webapp) if os.path.isfile(apache_webapp): if subprocess.call("sudo rm %s" % (apache_webapp), shell=True) > 0: diff --git a/scripts/webapps/flask/delete_flask.py b/scripts/webapps/flask/delete_flask.py index 83199bd..eff960a 100644 --- a/scripts/webapps/flask/delete_flask.py +++ b/scripts/webapps/flask/delete_flask.py @@ -35,9 +35,11 @@ name_service=os.path.basename(home_user[:-1]).strip() domain=os.path.basename(home_user[:-1].replace(name_service, '')[:-1]).strip() +if home_user.find('/htdocs/', -8)!=-1: + home_user=home_user.replace('htdocs', 'flask') + if name_service=='htdocs': name_service='flask' - #home_user=home_user.replace('flask', 'htdocs') #print(domain) @@ -45,7 +47,7 @@ if subprocess.call('sudo systemctl stop %s-%s.service && sudo rm /etc/systemd/sy print('Error: cannot delete app service in systemd...') sys.exit(1) -if subprocess.call("sudo su %s -s /bin/bash -c 'rm -f -r %s'" % (args.user, args.home_user), shell=True) > 0: +if subprocess.call("sudo su %s -s /bin/bash -c 'rm -f -r %s'" % (args.user, home_user), shell=True) > 0: print('Error: cannot delete %s' % args.home_user) sys.exit(1) diff --git a/scripts/webapps/flask/install_flask_site.py b/scripts/webapps/flask/install_flask_site.py index 41bfd4d..7773bc2 100644 --- a/scripts/webapps/flask/install_flask_site.py +++ b/scripts/webapps/flask/install_flask_site.py @@ -148,12 +148,14 @@ if subprocess.call("sudo su %s -s /bin/bash -c 'git clone %s %sapp && cd %sapp & sys.exit(1) if path=='/': - base_name_file='{}-001-{}.conf'.format(args.domain, name) + base_name_file='{}-{}.conf'.format(args.domain, name) else: - base_name_file='{}-000-{}.conf'.format(args.domain, name) + base_name_file='{}-{}.conf'.format(args.domain, name) name_file='/home/{}/{}'.format(system_user, base_name_file) +apache_path='/proxy' + with open(name_file, 'w') as f: f.write("ProxyPreserveHost On\n") @@ -177,9 +179,12 @@ with open(name_file, 'w') as f: f.write("ProxyPass !\n") f.write("\n") + else: + apache_path='' + print('Updating apache configuration for flask outside of htdocs...') -if subprocess.call('sudo mv {} /etc/{}/vhosts.d/extra && sudo chown root:root /etc/{}/vhosts.d/extra/{}'.format(name_file, apache_cmd, apache_cmd, base_name_file), shell=True) > 0: +if subprocess.call('sudo mv {} /etc/{}/vhosts.d/extra{} && sudo chown root:root /etc/{}/vhosts.d/extra{}/{}'.format(name_file, apache_cmd, apache_path, apache_cmd, apache_path, base_name_file), shell=True) > 0: print('Error') sys.exit(1) diff --git a/scripts/webapps/phpmyadmin/install_phpmyadmin_site.py b/scripts/webapps/phpmyadmin/install_phpmyadmin_site.py index 9715841..a57f788 100644 --- a/scripts/webapps/phpmyadmin/install_phpmyadmin_site.py +++ b/scripts/webapps/phpmyadmin/install_phpmyadmin_site.py @@ -182,16 +182,16 @@ if args.path!='/': \n\ ".format(args.path[:-1], home_user, home_user, args.path[:-1]) - base_name_file='{}-002-{}.conf'.format(args.domain, os.path.basename(args.path[1:-1])) + base_name_file='{}-{}.conf'.format(args.domain, os.path.basename(args.path[1:-1])) - name_file='/home/{}/{}-002-{}.conf'.format(system_user, args.domain, os.path.basename(args.path[1:-1])) + name_file='/home/{}/{}-{}.conf'.format(system_user, args.domain, os.path.basename(args.path[1:-1])) with open(name_file, 'w') as f: f.write(alias_apache) print('Updating apache configuration for wordpress outside of htdocs...\n') - if subprocess.call('sudo mv {} /etc/{}/vhosts.d/extra && sudo chown root:root /etc/{}/vhosts.d/extra/{}'.format(name_file, apache_cmd, apache_cmd, base_name_file), shell=True) > 0: + if subprocess.call('sudo mv {} /etc/{}/vhosts.d/extra/proxyphp && sudo chown root:root /etc/{}/vhosts.d/extra/proxyphp/{}'.format(name_file, apache_cmd, apache_cmd, base_name_file), shell=True) > 0: print('Error') sys.exit(1) @@ -200,7 +200,7 @@ if args.path!='/': print('Restarting apache...') if subprocess.call('sudo '+apachectl+' configtest && sudo systemctl restart '+apache_cmd, shell=True) > 0: - subprocess.call('sudo rm /etc/{}/vhosts.d/extra/{}'.format(apache_cmd, base_name_file), shell=True) + subprocess.call('sudo rm /etc/{}/vhosts.d/extra/proxyphp/{}'.format(apache_cmd, base_name_file), shell=True) print('Error: Error in configtest\n') sys.exit(1) diff --git a/scripts/webapps/proxy/install_proxy_site.py b/scripts/webapps/proxy/install_proxy_site.py index 00cbcfa..11e785d 100644 --- a/scripts/webapps/proxy/install_proxy_site.py +++ b/scripts/webapps/proxy/install_proxy_site.py @@ -69,21 +69,25 @@ if args.path: if path=='': path='/' +apache_path='/proxy' + if path=='/': name='proxy' - base_name_file='{}-001-{}.conf'.format(args.domain, name) + #base_name_file='{}-{}.conf'.format(args.domain, name) - name_file='/home/{}/{}-001-{}.conf'.format(system_user, args.domain, name) + #name_file='/home/{}/{}-{}.conf'.format(system_user, args.domain, name) else: + apache_path='' + name=os.path.basename(home_user[:-1]).strip() - base_name_file='{}-000-{}.conf'.format(args.domain, name) - - name_file='/home/{}/{}-000-{}.conf'.format(system_user, args.domain, name) +base_name_file='{}-{}.conf'.format(args.domain, name) + +name_file='/home/{}/{}-{}.conf'.format(system_user, args.domain, name) with open(name_file, 'w') as f: @@ -126,7 +130,7 @@ with open(name_file, 'w') as f: print('Updating apache configuration for application outside of htdocs...') -if subprocess.call('sudo mv {} /etc/{}/vhosts.d/extra && sudo chown root:root /etc/{}/vhosts.d/extra/{}'.format(name_file, apache_cmd, apache_cmd, base_name_file), shell=True) > 0: +if subprocess.call('sudo mv {} /etc/{}/vhosts.d/extra{} && sudo chown root:root /etc/{}/vhosts.d/extra{}/{}'.format(name_file, apache_cmd, apache_path, apache_cmd, apache_path, base_name_file), shell=True) > 0: print('Error') sys.exit(1) diff --git a/scripts/webapps/wordpress/install_wordpress_site.py b/scripts/webapps/wordpress/install_wordpress_site.py index 1164cbe..df2571b 100644 --- a/scripts/webapps/wordpress/install_wordpress_site.py +++ b/scripts/webapps/wordpress/install_wordpress_site.py @@ -155,14 +155,14 @@ if args.path!='/': \n\ ".format(args.path[:-1], home_user, home_user, args.path[:-1]) - name_file='/home/{}/{}-002-{}.conf'.format(system_user, args.domain, os.path.basename(args.path[1:-1])) + name_file='/home/{}/{}-{}.conf'.format(system_user, args.domain, os.path.basename(args.path[1:-1])) with open(name_file, 'w') as f: f.write(alias_apache) print('Updating apache configuration for wordpress outside of htdocs...') - if subprocess.call('sudo mv {} /etc/{}/vhosts.d/extra'.format(name_file, apache_cmd), shell=True) > 0: + if subprocess.call('sudo mv {} /etc/{}/vhosts.d/extra/proxyphp'.format(name_file, apache_cmd), shell=True) > 0: print('Error') sys.exit(1)