176 lines
5.3 KiB
Python
176 lines
5.3 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
|
|
|
|
#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('--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('--password', help='The password of the new user', required=True)
|
|
parser.add_argument('--path', help='The path of the wordpress install', required=True)
|
|
parser.add_argument('--email', help='The email of the new user', required=True)
|
|
parser.add_argument('--domain', help='The domain of the new wordpress', required=True)
|
|
parser.add_argument('--title', help='The title of the new wordpress', required=True)
|
|
|
|
parser.add_argument('--user_mysql', help='The password of the root mysql', required=True)
|
|
parser.add_argument('--password_mysql', help='The password of the root mysql', required=True)
|
|
|
|
parser.add_argument('--db_mysql', help='Database for new wp site', required=True)
|
|
|
|
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='The version of php used', required=True)
|
|
|
|
args = parser.parse_args()
|
|
|
|
wp_command='/usr/local/bin/wp'
|
|
|
|
php_command=''
|
|
|
|
linux_distro=distro.id()
|
|
|
|
if linux_distro=='arch':
|
|
|
|
php_command='/usr/bin/php-legacy'
|
|
|
|
if linux_distro=='fedora' or linux_distro=='rocky' or linux_distro=='almalinux':
|
|
|
|
php_command='/usr/bin/php'+args.php_version.replace('.', '')
|
|
|
|
if linux_distro=='debian' or linux_distro=='ubuntu':
|
|
|
|
php_command='/usr/bin/php'+args.php_version
|
|
|
|
|
|
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':
|
|
apache_group='apache'
|
|
|
|
if linux_distro=='arch':
|
|
apache_group='http'
|
|
|
|
|
|
home_user=args.home_user
|
|
|
|
stat_group=os.stat(home_user)
|
|
uid=stat_group.st_uid
|
|
|
|
user=pwd.getpwuid(uid)[0]
|
|
|
|
if 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 wordpress 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 wordpress in this folder\n")
|
|
sys.exit(1)
|
|
|
|
os.chdir(home_user)
|
|
else:
|
|
home_user=args.home_user+'/'
|
|
|
|
os.chdir(home_user)
|
|
|
|
if os.path.isfile(home_user+'/index.php'):
|
|
|
|
print("WARNING, Deleting old installations of wordpress in document root")
|
|
|
|
if subprocess.call("sudo su %s -s /bin/bash -c 'rm -f -r %s/*'" % (user, home_user), shell=True) > 0:
|
|
print("Error, ")
|
|
sys.exit(1)
|
|
|
|
# Php-fpm for user
|
|
|
|
print('Installing Wordpress...')
|
|
|
|
if subprocess.call("sudo su %s -s /bin/bash -c '%s %s core download'" % (user, php_command, wp_command), shell=True) > 0:
|
|
print("Error: %s %s core download\n" % (php_command, wp_command))
|
|
sys.exit(1)
|
|
|
|
mysql_user=args.user_mysql
|
|
mysql_password=args.password_mysql
|
|
|
|
host_db=args.server_mysql
|
|
|
|
if args.port_mysql!=3306:
|
|
host_db=host_db+':'+str(args.port_mysql)
|
|
|
|
if subprocess.call("sudo su %s -s /bin/bash -c '%s %s config create --dbname=%s --dbuser=%s --dbpass=\"%s\" --dbhost=%s'" % (user, php_command, wp_command, args.db_mysql, mysql_user, mysql_password, host_db), shell=True) > 0:
|
|
print('Error')
|
|
sys.exit(1)
|
|
|
|
print('Created basic config...')
|
|
|
|
# Add cronjob for wordpress
|
|
|
|
if subprocess.call("sudo su %s -s /bin/bash -c '%s %s core install --url=%s --title=\"%s\" --admin_user=%s --admin_password=\"%s\" --admin_email=%s'" % (user, php_command, wp_command, args.domain+args.path, args.title, args.user, args.password, args.email), shell=True) > 0:
|
|
print('Error: cannot install wordpress')
|
|
sys.exit(1)
|
|
else:
|
|
print('Installed Wordpress successfully...')
|
|
|
|
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])
|
|
|
|
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/proxyphp'.format(name_file, apache_cmd), shell=True) > 0:
|
|
print('Error')
|
|
sys.exit(1)
|
|
|
|
print('Restarting apache...')
|
|
|
|
if subprocess.call("sudo systemctl restart "+apache_cmd, shell=True) > 0:
|
|
print("Error: cannot update and restart apache")
|
|
sys.exit(1)
|
|
|
|
|
|
print('Apache restarted!')
|