208 lines
6.9 KiB
Python
208 lines
6.9 KiB
Python
#!/usr/bin/python3 -u
|
|
|
|
import sys, os
|
|
import subprocess
|
|
import argparse
|
|
import platform
|
|
import shutil
|
|
import pathlib
|
|
import distro
|
|
import pwd
|
|
import getpass
|
|
from time import sleep
|
|
|
|
#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 create a new wordpress site.')
|
|
|
|
parser.add_argument('--domain', help='The domain where is the site', required=True)
|
|
parser.add_argument('--home_user', help='The name of the new user', required=True)
|
|
parser.add_argument('--user', help='The name of the new user', required=True)
|
|
parser.add_argument('--path', help='The path of the wordpress install')
|
|
|
|
parser.add_argument('--server_mysql', help='Server of MySQL database', required=True)
|
|
|
|
parser.add_argument('--port_mysql', help='The port of the MySQL server', required=True, type=int)
|
|
|
|
parser.add_argument('--php_version', help='PHP version', required=True)
|
|
|
|
args = parser.parse_args()
|
|
|
|
php_version=args.php_version
|
|
|
|
php_command='/usr/bin/php'+str(php_version)
|
|
|
|
linux_distro=distro.id()
|
|
|
|
if linux_distro=='arch':
|
|
|
|
php_command='/usr/bin/php-legacy'
|
|
|
|
system_user=getpass.getuser()
|
|
|
|
apache_cmd='apache2'
|
|
apachectl='apache2ctl'
|
|
|
|
if linux_distro!='debian' and linux_distro!='ubuntu':
|
|
|
|
apache_cmd='httpd'
|
|
apachectl='apachectl'
|
|
|
|
if linux_distro=='rocky' or linux_distro=='fedora' or linux_distro=='almalinux' or linux_distro=='centos':
|
|
apache_group='apache'
|
|
php_command=php_command.replace('.', '')
|
|
|
|
if linux_distro=='arch':
|
|
apache_group='http'
|
|
|
|
|
|
home_user=args.home_user
|
|
|
|
if home_user.find('/', -1)==-1:
|
|
print("Error: you need backslash in home_user option")
|
|
exit(1)
|
|
|
|
home_user_default=home_user.replace('/htdocs/', '')
|
|
|
|
stat_group=os.stat(home_user)
|
|
uid=stat_group.st_uid
|
|
|
|
user=pwd.getpwuid(uid)[0]
|
|
|
|
if args.path and args.path!='/':
|
|
|
|
home_user=args.home_user.replace('/htdocs/', args.path)
|
|
|
|
"""
|
|
if not os.path.isdir(home_user):
|
|
#os.mkdir(home_user)
|
|
if subprocess.call("sudo su %s -s /bin/bash -c 'mkdir -p %s'" % (user, home_user), shell=True) > 0:
|
|
print("Error, cannot create phpmyadmin folder sudo su %s -s /bin/bash -c 'mkdir -p %s'" % (args.user, home_user)+"\n")
|
|
sys.exit(1)
|
|
else:
|
|
print("Error, exists a directory with same name, i cannot install phpmyadmin in this folder")
|
|
sys.exit(1)
|
|
"""
|
|
|
|
#os.chdir(home_user)
|
|
else:
|
|
home_user=args.home_user
|
|
|
|
phpmyadmin_dir=args.home_user.replace('/htdocs/', '/phpmyadmin/')
|
|
|
|
if os.path.isdir(phpmyadmin_dir):
|
|
print('Cleaning directory %s because is not empty, probably by an error in other install' % phpmyadmin_dir)
|
|
|
|
if subprocess.call("sudo su %s -s /bin/bash -c 'rm -f -r %s'" % (user, phpmyadmin_dir), shell=True) > 0:
|
|
print('Error: cannot clean to {}\n'.format(phpmyadmin_dir))
|
|
sys.exit(1)
|
|
|
|
#os.chdir(home_user)
|
|
#print(home_user)
|
|
#sys.exit(0)
|
|
# Php-fpm for user
|
|
|
|
#if not os.path.isfile('/etc/')
|
|
|
|
#shutil.chown('/var/www/sites/'+args.domain, 'hosting', 'hosting')
|
|
|
|
#Dash, the default debian stretch shell, don't support <<<
|
|
|
|
#sudo debconf-set-selections <<< 'mariadb-server mariadb-server/root_password password your_password'
|
|
#sudo debconf-set-selections <<< 'mariadb-server mariadb-server/root_password_again password your_password'
|
|
|
|
print('Installing phpmyadmin...')
|
|
|
|
# composer create-project phpmyadmin/phpmyadmin --repository-url=https://www.phpmyadmin.net/packages.json --no-dev --working-dir={}
|
|
|
|
ret_install=subprocess.call("sudo su %s -s /bin/bash -c '%s /usr/local/bin/composer create-project phpmyadmin/phpmyadmin --repository-url=https://www.phpmyadmin.net/packages.json --no-dev --working-dir=%s'" % (user, php_command, home_user_default), shell=True)
|
|
|
|
if ret_install > 0 and ret_install!=5:
|
|
print('Error: cannot install phpmyadmin with composer in {} with code {}\n'.format(home_user, ret_install))
|
|
sys.exit(1)
|
|
|
|
if ret_install==5:
|
|
print('Updating phpmyadmin using composer...\n')
|
|
|
|
if subprocess.call("sudo su %s -s /bin/bash -c 'cd %s && %s /usr/local/bin/composer update'" % (user,home_user_default+'/phpmyadmin/', php_command), shell=True) > 0:
|
|
print('Error: cannot update phpmyadmin with composer in {}\n'.format(home_user))
|
|
sys.exit(1)
|
|
|
|
if home_user.find('/htdocs/', -8)!=-1:
|
|
|
|
if subprocess.call("sudo su %s -s /bin/bash -c 'cd %s && mv htdocs htdocs_old && mv phpmyadmin htdocs'" % (user, home_user_default), shell=True) > 0:
|
|
print('Error: cannot move phpmyadmin to {}\n'.format(home_user))
|
|
sys.exit(1)
|
|
pass
|
|
else:
|
|
|
|
if args.path!='/phpmyadmin/':
|
|
|
|
if subprocess.call("sudo su %s -s /bin/bash -c 'cd %s && mv phpmyadmin %s'" % (user, home_user_default, home_user), shell=True) > 0:
|
|
print('Error: cannot move phpmyadmin to {}\n'.format(home_user))
|
|
sys.exit(1)
|
|
|
|
pass
|
|
|
|
if args.server_mysql:
|
|
|
|
#echo "\$cfg['Servers'][\$i]['host'] = '$MYSQL_HOST';" >> config.inc.php
|
|
# $cfg['Servers'][$i]['host'] = '%s';
|
|
|
|
config_servers='';
|
|
|
|
if subprocess.call("sudo su {} -s /bin/bash -c 'cp {}/config.sample.inc.php {}/config.inc.php && echo \"\$cfg[\\\"Servers\\\"][\$i][\\\"host\\\"] = \\\"{}:{}\\\";\" >> {}/config.inc.php'".format(user, home_user, home_user, args.server_mysql, args.port_mysql, home_user), shell=True) > 0:
|
|
print('Error: cannot update config of phpmyadmin {}\n'.format(home_user))
|
|
sys.exit(1)
|
|
|
|
"""
|
|
if subprocess.call("sudo mv %s %s & rmdir %s" % (home_user+'/phpmyadmin/*', home_user), shell=True) > 0:
|
|
print('Error: cannot move phpmyadmin to {}'.format(home_user))
|
|
sys.exit(1)
|
|
"""
|
|
|
|
|
|
if args.path!='/':
|
|
|
|
alias_apache=" Alias {} {} \n\
|
|
<Directory {}> \n\
|
|
Options FollowSymLinks MultiViews \n\
|
|
AllowOverride All \n\
|
|
Require all granted \n\
|
|
</Directory> \n\
|
|
<Location {}>\n\
|
|
ProxyPass ! \n\
|
|
</Location> \n\
|
|
".format(args.path[:-1], home_user, home_user, args.path[:-1])
|
|
|
|
base_name_file='{}-{}.conf'.format(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/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)
|
|
|
|
sleep(1)
|
|
|
|
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/proxyphp/{}'.format(apache_cmd, base_name_file), shell=True)
|
|
print('Error: Error in configtest\n')
|
|
sys.exit(1)
|
|
|
|
|
|
print('phpmyadmin installed!')
|