Added files

This commit is contained in:
absurdo 2023-11-28 00:15:10 +01:00
commit 25aaa0a4bf
31 changed files with 4035 additions and 0 deletions

180
scripts/add_php_vhost.py Normal file
View file

@ -0,0 +1,180 @@
#!/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 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 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 \
<FilesMatch \.php$> \n \
SetHandler \"proxy:unix:{}|fcgi://localhost/\" \n \
</FilesMatch>".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)

274
scripts/check_php.sh Normal file
View file

@ -0,0 +1,274 @@
#!/bin/bash
echo "Checking php..."
# Install last php version supported by wordpress
DISTRO=`python3 -c 'import distro; print(distro.id())'`
PHP_VERSION=$(php -v 2>/dev/null | grep -oE "PHP ([0-9]+\.[0-9]+)" | grep -oE "[0-9]+\.[0-9]+")
CHOOSE_PHP_VERSION="8.2"
if [ -n $1 ]; then
CHOOSE_PHP_VERSION=$1
fi
#echo '{"error": 0, "status": 0, "progress": 100, "no_progress":0, "message": "PHP Version is "}'
# sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 1F3045A5DF7587C3
if [ "$DISTRO" = 'debian' ] || [ "$DISTRO" = 'ubuntu' ]; then
echo "Update PHP if you need..."
if [ ! -f '/etc/apt/sources.list.d/php.list' ]; then
echo '{"error": 0, "status": 0, "progress": 100, "no_progress":0, "message": "Installing Sury repos..."}'
if [ "$DISTRO" = 'debian' ]; then
# sudo wget -O - https://packages.sury.org/php/README.txt | sh -s
sudo apt-get update
sudo apt-get -y install lsb-release ca-certificates curl
sudo curl -sSLo /usr/share/keyrings/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg
sudo ln -s /usr/share/keyrings/deb.sury.org-php.gpg /etc/apt/trusted.gpg.d/deb.sury.org-php.gpg
sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
sudo apt-get update
else
sudo LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
fi
fi
if [ $? -eq 0 ]; then
echo '{"error": 0, "status": 0, "progress": 100, "no_progress":0, "message": "Installed Sury repos sucessfully..."}'
else
echo '{"error": 1, "status": 1, "progress": 100, "no_progress":0, "message": "Error: cannot install Sury repos..."}'
exit
fi
sudo apt-get update
if [ $? -eq 0 ]; then
echo '{"error": 0, "status": 0, "progress": 100, "no_progress":0, "message": "Updated with sury repos sucessfully..."}'
else
echo '{"error": 1, "status": 1, "progress": 100, "no_progress":0, "message": "Error: cannot update apt repos with sury repos..."}'
exit
fi
if [ "$PHP_VERSION" = "$CHOOSE_PHP_VERSION" ]; then
echo "Ok, you have php ${CHOOSE_PHP_VERSION}"
echo "Updating php-fpm pm to ondemand"
sudo sed -i 's/pm = dynamic/pm = ondemand/g' /etc/php/${CHOOSE_PHP_VERSION}/fpm/pool.d/www.conf
else
sudo apt-get install -y php${CHOOSE_PHP_VERSION} php${CHOOSE_PHP_VERSION}-fpm php${CHOOSE_PHP_VERSION}-gd php${CHOOSE_PHP_VERSION}-mysql php${CHOOSE_PHP_VERSION}-curl php${CHOOSE_PHP_VERSION}-mbstring php${CHOOSE_PHP_VERSION}-intl php${CHOOSE_PHP_VERSION}-imagick php${CHOOSE_PHP_VERSION}-xml php${CHOOSE_PHP_VERSION}-zip php${CHOOSE_PHP_VERSION}-redis unzip mariadb-client
if [ $? -eq 0 ]; then
echo "{\"error\": 0, \"status\": 0, \"progress\": 100, \"no_progress\":0, \"message\": \"Installed php ${CHOOSE_PHP_VERSION} sucessfully...\"}"
else
echo "{\"error\": 1, \"status\": 1, \"progress\": 100, \"no_progress\":0, \"message\": \"Error: cannot install php ${CHOOSE_PHP_VERSION}\"}"
exit;
fi
sudo systemctl restart php${CHOOSE_PHP_VERSION}-fpm.service
if [ $? -eq 0 ]; then
echo "{\"error\": 0, \"status\": 0, \"progress\": 100, \"no_progress\":0, \"message\": \"Restarted php ${CHOOSE_PHP_VERSION} sucessfully...\"}"
else
echo "{\"error\": 1, \"status\": 1, \"progress\": 100, \"no_progress\":0, \"message\": \"Cannot restart php ${CHOOSE_PHP_VERSION}...\"}"
exit;
fi
echo "Updating php-fpm pm to ondemand"
sudo sed -i 's/pm = dynamic/pm = ondemand/g' /etc/php/${CHOOSE_PHP_VERSION}/fpm/pool.d/www.conf
fi
if [ -f "/usr/local/bin/composer" ]; then
echo "Updating composer..."
# need sudo for composer autoupdate
sudo /usr/local/bin/composer self-update
else
echo "Installing composer..."
EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
then
>&2 echo 'ERROR: Invalid installer checksum'
rm composer-setup.php
exit 1
fi
php composer-setup.php --quiet
RESULT=$?
rm composer-setup.php
sudo mv composer.phar /usr/local/bin/composer
if [ $RESULT -eq 0 ]; then
echo '{"error": 0, "status": 0, "progress": 100, "no_progress":0, "message": "Composer installed sucessfully..."}'
else
echo '{"error": 1, "status": 1, "progress": 100, "no_progress":0, "message": "Error: cannot install composer!"}'
exit 1
fi
fi
elif [ "$DISTRO" = 'rocky' ] || [ "$DISTRO" = 'fedora' ]; then
echo "Update PHP if you need..."
sudo dnf -y update
FINAL_PHP_VERSION="${CHOOSE_PHP_VERSION/\./}"
#echo $FINAL_PHP_VERSION
if [ "$PHP_VERSION" = "$CHOOSE_PHP_VERSION" ]; then
echo "Ok, you have php ${CHOOSE_PHP_VERSION}"
# Check mariadb installed
sudo dnf -y install mariadb
echo "Updating php-fpm pm to ondemand"
sudo sed -i 's/pm = dynamic/pm = ondemand/g' /etc/opt/remi/php${FINAL_PHP_VERSION}/php-fpm.d/www.conf
else
sudo dnf -y install php${FINAL_PHP_VERSION} php${FINAL_PHP_VERSION}-php-gd php${FINAL_PHP_VERSION}-php-mysqlnd php${FINAL_PHP_VERSION}-php-imap php${FINAL_PHP_VERSION}-php-intl php${FINAL_PHP_VERSION}-php-fpm php${FINAL_PHP_VERSION}-php-process composer unzip mariadb
if [ $? -eq 0 ]; then
echo "{\"error\": 0, \"status\": 0, \"progress\": 100, \"no_progress\":0, \"message\": \"Installed php ${FINAL_PHP_VERSION} sucessfully...\"}"
else
echo "{\"error\": 1, \"status\": 1, \"progress\": 100, \"no_progress\":0, \"message\": \"Error: cannot install php ${FINAL_PHP_VERSION} for wordpress\"}"
exit;
fi
#Installing php8.2-fpm
#/etc/opt/remi/php82/php-fpm.d/www.conf
#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
#pm = dynamic
sudo systemctl restart php${FINAL_PHP_VERSION}-php-fpm.service
if [ $? -eq 0 ]; then
echo "{\"error\": 0, \"status\": 0, \"progress\": 100, \"no_progress\":0, \"message\": \"Restart php ${FINAL_PHP_VERSION} sucessfully...\"}"
else
echo "{\"error\": 1, \"status\": 1, \"progress\": 100, \"no_progress\":0, \"message\": \"Error: cannot restart php ${FINAL_PHP_VERSION}\"}"
exit;
fi
fi
else
sudo pacman -S --needed --noconfirm php-legacy php-legacy-gd php-legacy-fpm php-legacy-sodium composer mariadb
if [ $? -eq 0 ]; then
echo '{"error": 0, "status": 0, "progress": 100, "no_progress":0, "message": "php-legacy installed..."}'
else
echo '{"error": 0, "status": 0, "progress": 100, "no_progress":0, "message": "Cannot install php legacy in arch..."}'
exit;
fi
sed -i 's/pm = dynamic/pm = ondemand/g' /etc/php-legacy/php-fpm.d/www.conf
sudo sed -i 's/^;zend_extension=opcache$/zend_extension=opcache/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=iconv$/extension=iconv/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=intl$/extension=intl/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=mysqli$/extension=mysqli/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=gd$/extension=gd/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=bcmath$/extension=bcmath/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=bz2$/extension=bz2/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=ftp$/extension=ftp/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=gd$/extension=gd/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=gettext$/extension=gettext/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=sockets$/extension=sockets/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=sodium$/extension=sodium/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=pdo_mysql$/extension=pdo_mysql/g' /etc/php-legacy/php.ini
sudo systemctl enable php-legacy-fpm.service && sudo systemctl restart php-legacy-fpm.service
if [ $? -eq 0 ]; then
echo '{"error": 0, "status": 0, "progress": 100, "no_progress":0, "message": "Restarted php legacy sucessfully..."}'
else
echo '{"error": 1, "status": 1, "progress": 100, "no_progress":0, "message": "Error: cannot restart php-legacy"}'
exit;
fi
fi

22
scripts/files/vhost.tpl Normal file
View file

@ -0,0 +1,22 @@
#The most simple virtual host possible.
<VirtualHost $ip:$port>
ServerAdmin $email
ServerName $domain
$ServerAlias
DocumentRoot $rootDir
DirectoryIndex index.html index.htm index.php
# PHP Options
IncludeOptional vhosts.d/php/$domain-php.conf
<Directory $rootDir>
Options $Indexes FollowSymLinks MultiViews
$AllowOverride
Require all granted
</Directory>
IncludeOptional vhosts.d/extra/$domain-*.conf
ErrorLog /var/log/$apache_cmd/$domain-error.log
LogLevel error
CustomLog /var/log/$apache_cmd/$domain-access.log combined
# Space for extra configurations
# Space for extra directories
# SSL Options
</VirtualHost>

View file

@ -0,0 +1,63 @@
<?php
use PhangoApp\LeviathanUtils\Linux;
include('leviathanutils/vendor/autoload.php');
$linux_distro=Linux::get_linux_distro();
$conf_vhosts="IncludeOptional vhosts.d/*.conf";
Linux::json_log('Installing Apache server', $error=0, $status=0, $progress=0, $no_progress=1);
$redhat_package='mod_ssl openssl tar socat policycoreutils-python-utils wget';
$linux_package=['debian' => 'apache2 logrotate socat curl', 'ubuntu' => 'apache2 logrotate socat curl', 'fedora' => $redhat_package, 'almalinux' => $redhat_package, 'rocky' => $redhat_package, 'arch' => 'apache'];
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['ubuntu']=$create_dirs['debian'];
Linux::exec($create_dirs);
if(!file_put_contents('vhosts.conf', $conf_vhosts)) {
Linux::json_log('Error: cannot create vhosts.conf', $error=1, $status=1, $progress=100, $no_progress=0);
exit(1);
}
$create_vhost_file=['debian' => ["sudo mv vhosts.conf /etc/apache2/sites-enabled/ && sudo chown root:root /etc/apache2/sites-enabled/vhosts.conf"]];
$create_vhost_file['ubuntu']=$create_vhost_file['debian'];
Linux::exec($create_vhost_file);
//Modifying apache configuration
Linux::json_log('Modifying Apache configuration...', $error=0, $status=0, $progress=0, $no_progress=1);
$sed_apache=['debian' => ['ServerTokens OS', "ServerTokens Prod", '/etc/apache2/conf-enabled/security.conf']];
$sed_apache['ubuntu']=$sed_apache['debian'];
Linux::sed($sed_apache);
//Activating modules for apache.
$activate_modules=['debian' => ["sudo a2enmod ssl rewrite proxy proxy_fcgi"]];
$activate_modules['ubuntu']=$create_vhost_file['debian'];
Linux::exec($activate_modules);
//Restart apache
$linux_service=['debian' => 'apache2', 'ubuntu' => 'apache2', 'fedora' => 'httpd', 'almalinux' => 'httpd', 'rocky' => 'httpd', 'arch' => 'apache'];
Linux::systemd_service('restart', $linux_service);
Linux::json_log('Apache server installed successfully!', $error=0, $status=0, $progress=100, $no_progress=0);

727
scripts/manage_apache.py Normal file
View file

@ -0,0 +1,727 @@
#!/usr/bin/python3 -u
import sys
import argparse
import os
#from pastafariutils.unix import add_user, del_user, change_password
from pathlib import Path
from subprocess import call, DEVNULL
import json
import time
import shutil
import pwd
import distro
import subprocess
import pwd
php_version={'php74': 'php7.4', 'php80': 'php8.0', 'php81': 'php8.1', 'php82': 'php8.2'}
def manage():
parser=argparse.ArgumentParser(prog='manage_apache.py', description='A tool for admin an apache server')
parser.add_argument('--operation', help='The operation', required=True)
parser.add_argument('--domain', help='The domain for this operation', required=True)
parser.add_argument('--aliases', help='The Domain aliases for the domain')
parser.add_argument('--port', help='The http port where petitions are listened')
parser.add_argument('--ssl_port', help='The https port where petitions are listened')
parser.add_argument('--ip', help='The IP where http petitions are listened')
parser.add_argument('--email', help='If add an domain, you can use an email for apache errors')
parser.add_argument('--user', help='You need an user for the virtualhost')
parser.add_argument('--root_dir', help='If add an domain, you need define where the virtualhost is installed')
parser.add_argument("--indexes", action='store_true')
parser.add_argument("--allow_override", action='store_true')
parser.add_argument("--ssl", help='SSL Type: 0, no SSL')
parser.add_argument("--redirect_ssl", action='store_true')
parser.add_argument("--debug", action='store_true')
parser.add_argument('--type_cgi', help='The CGI type for this virtualhost')
parser.add_argument('--password', help='The password of the unix user')
#parser.add_argument('--ftp_user', help='The CGI type for this virtualhost', required=True)
args=parser.parse_args()
apache_cmd='apache2'
apachectl='apache2ctl'
apache_group='www-data'
linux_distro=distro.id()
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'
port=80
ssl_port=443
ip='*'
yes_ssl=False
debug=False
if args.debug:
debug=True
json_return={'error':0, 'status': 0, 'progress': 0, 'no_progress':0, 'message': ''}
if args.operation=='add' or args.operation=='edit':
if not args.email:
parser.error('You need --email option if you add a new virtual host.')
if not args.root_dir:
parser.error('You need --root_dir option if you add a new virtual host.')
if not args.user:
parser.error('You need --user option if you add a new virtual host.')
if args.port:
port=args.port
if args.ip:
ip=args.ip
if ip.find(':')!=-1:
ip='['+ip+']'
real_root_dir=args.root_dir+'/htdocs'
aliases='ServerAlias www.'+args.domain
if args.aliases:
aliases='ServerAlias '+args.aliases.replace(',', ' ')
indexes=''
if args.indexes:
indexes='Indexes'
allow_override=''
if args.allow_override:
allow_override='AllowOverride all'
type_ssl=0
if args.ssl:
try:
type_ssl=int(args.ssl)
except:
json_return['error']=1
json_return['status']=1
json_return['progress']=100
json_return['message']='Error: bad SSL configuration type'
print(json.dumps(json_return))
exit(1)
#print( os.path.basename(__file__)+'/files/vhost.tpl')
vhost_file=os.path.dirname(os.path.abspath(__file__))+'/files/vhost.tpl'
with open(vhost_file) as f_vhost:
vhost=f_vhost.read()
vhost_ssl=vhost
vhost=vhost.replace('$port', args.port)
vhost=vhost.replace('$ip', args.ip)
vhost=vhost.replace('$email', args.email)
vhost=vhost.replace('$domain', args.domain)
#ServerAlias www.$domain
vhost=vhost.replace('$ServerAlias', aliases)
vhost=vhost.replace('$rootDir', real_root_dir)
vhost=vhost.replace('$Indexes', indexes)
vhost=vhost.replace('$AllowOverride', allow_override)
vhost=vhost.replace('$apache_cmd', apache_cmd)
if type_ssl>0:
if type_ssl==1:
json_return['error']=0
json_return['status']=0
json_return['progress']=0
json_return['message']='Preparing SSL virtualhost'
print(json.dumps(json_return))
#Make letsencrypt
ssl_debug=''
if debug:
ssl_debug='--test-cert'
#certbot certonly --test-cert --webroot -w /var/www/sites/prueba.cuchulu.com/htdocs/ -d prueba.cuchulu.com
final_aliases=''
if args.aliases:
#arr_aliases=[alias.strip() for args.aliases.split(',')]
final_aliases=" ".join(['-d '+alias.strip() for alias in args.aliases.split(',')])
expand=''
if os.path.isfile('/etc/letsencrypt/live/{}/fullchain.pem'.format(args.domain)):
expand='--expand'
certbot_cmd='certbot certonly --noninteractive --agree-tos -m {} {} --webroot -w /home/{}/sites/{}/htdocs/ -d {} {} {}'.format(args.email, ssl_debug, args.user, args.domain, args.domain, final_aliases, expand)
json_return={'error':0, 'status': 0, 'progress': 0, 'no_progress':0, 'message': 'Creating SSL Cert with Letsencrypt...'}
print(json.dumps(json_return))
if call("sudo "+certbot_cmd, shell=True) > 0:
#If error, not stop the virtualhost, simply not creater ssl.
json_return['error']=0
json_return['status']=0
json_return['progress']=100
json_return['message']='Error: the ssl command is wrong '+certbot_cmd
print(json.dumps(json_return))
else:
json_return={'error':0, 'status': 0, 'progress': 100, 'no_progress':0, 'message': 'Created SSL Cert with Letsencrypt...'}
print(json.dumps(json_return))
# /etc/letsencrypt/live/prueba.cuchulu.com/fullchain.pem
# /etc/letsencrypt/live/prueba.cuchulu.com/privkey.pem
vhost_ssl=vhost_ssl.replace('$port', args.ssl_port)
vhost_ssl=vhost_ssl.replace('$ip', args.ip)
vhost_ssl=vhost_ssl.replace('$email', args.email)
vhost_ssl=vhost_ssl.replace('$domain', args.domain)
#ServerAlias www.$domain
vhost_ssl=vhost_ssl.replace('$ServerAlias', aliases)
vhost_ssl=vhost_ssl.replace('$rootDir', real_root_dir)
vhost_ssl=vhost_ssl.replace('$Indexes', indexes)
vhost_ssl=vhost_ssl.replace('$AllowOverride', allow_override)
vhost_ssl=vhost_ssl.replace('$apache_cmd', apache_cmd)
ssl_cert='/etc/letsencrypt/live/{}/fullchain.pem'.format(args.domain)
ssl_key='/etc/letsencrypt/live/{}/privkey.pem'.format(args.domain)
ssl_options=("SSLEngine on",
"SSLCertificateFile %s" % ssl_cert,
"SSLCertificateKeyFile %s" % ssl_key)
vhost_ssl=vhost_ssl.replace('# SSL Options', "\n".join(ssl_options))
if args.redirect_ssl:
print("Adding redirect http to https...")
ssl_options_http=("RewriteEngine On",
"RewriteCond %{HTTPS} !=on",
"RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]")
vhost=vhost.replace('# SSL Options', "\n".join(ssl_options_http))
vhost+="\n\n"+vhost_ssl
pass
if type_ssl==2:
vhost_ssl=vhost_ssl.replace('$port', args.ssl_port)
vhost_ssl=vhost_ssl.replace('$ip', args.ip)
vhost_ssl=vhost_ssl.replace('$email', args.email)
vhost_ssl=vhost_ssl.replace('$domain', args.domain)
#ServerAlias www.$domain
vhost_ssl=vhost_ssl.replace('$ServerAlias', aliases)
vhost_ssl=vhost_ssl.replace('$rootDir', real_root_dir)
vhost_ssl=vhost_ssl.replace('$Indexes', indexes)
vhost_ssl=vhost_ssl.replace('$AllowOverride', allow_override)
vhost_ssl=vhost_ssl.replace('$apache_cmd', apache_cmd)
#Create dir for ssl
if call("sudo mkdir -p /etc/"+apache_cmd+"/ssl/", shell=True) > 0:
print('Error, cannot create SSL Cert directory if not exists...')
exit(1)
ssl_cert='/etc/'+apache_cmd+'/ssl/'+args.domain+'-ssl.crt'
ssl_key='/etc/'+apache_cmd+'/ssl/'+args.domain+'-ssl.key'
ssl_orig_cert=os.path.dirname(os.path.abspath(__file__))+'/files/'+args.domain+'-ssl.crt'
ssl_orig_key=os.path.dirname(os.path.abspath(__file__))+'/files/'+args.domain+'-ssl.key'
if os.path.isfile(ssl_orig_cert):
shutil.copy(ssl_orig_cert, ssl_cert)
shutil.copy(ssl_orig_key, ssl_key)
if not os.path.isfile(ssl_cert):
print('Error, you have not uploaded crt files for ssl vhost...')
exit(1)
ssl_options=("SSLEngine on",
"SSLCertificateFile %s" % ssl_cert,
"SSLCertificateKeyFile %s" % ssl_key)
vhost_ssl=vhost_ssl.replace('# SSL Options', "\n".join(ssl_options))
if args.redirect_ssl:
print("Adding redirect http to https...")
ssl_options_http=("RewriteEngine On",
"RewriteCond %{HTTPS} !=on",
"RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]")
vhost=vhost.replace('# SSL Options', "\n".join(ssl_options_http))
vhost+="\n\n"+vhost_ssl
pass
# Get php version and install
"""
if args.type_cgi!=None:
if 'php' in args.type_cgi:
check_php_version(args.type_cgi, args.user)
#Add php support to virtualhost
# ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/path/to/your/documentroot/$1
# ProxyPassMatch ^/(.*\.php(/.*)?)$ unix:/path/to/socket.sock|fcgi://localhost/path/to/your/documentroot/
# ProxyPassMatch ^/(.*\.php(/.*)?)$ unix:/run/php/php-8.2-hosting.sock|fcgi://localhost/var/www/sites/coesinfo/prueba.cuchulu.com/htdocs/$1
number_version=php_version[args.type_cgi].replace('php', '')
# /run\/php\/php-"+number_version.replace('.', '\.')+"-{}.sock
php_line="# PHP Options\nProxyPassMatch ^/(.*\.php(/.*)?)$ unix:/run/php/php-{}-{}.sock|fcgi://localhost{}/$1".format(number_version, args.user, real_root_dir)
#print(php_line)
vhost=vhost.replace('# PHP Options', php_line)
print('Adding php configuration to virtualhost...')
"""
#time.sleep(1)
# Add user
#err, txt=add_user(args.user)
"""
if err:
json_return['error']=1
json_return['status']=1
json_return['progress']=100
json_return['message']='Error: user exists'
print(json.dumps(json_return))
sys.exit(1)
"""
"""
json_return['progress']=30
json_return['message']='Created user for this site'
print(json.dumps(json_return))
time.sleep(1)
"""
if args.operation=='add':
# Add site directory
"""
yes_chown=False
if not os.path.isdir(args.root_dir):
yes_chown=True
p=Path(real_root_dir)
try:
p.mkdir(mode=0o755, parents=True, exist_ok=False)
#Change owner
#if yes_chown:
#shutil.chown('../'+args.root_dir, args.user, args.user)
if call(("sudo chown -R {}:{} "+args.root_dir).format(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: cannot set the directory permissions'
else:
shutil.chown(real_root_dir, args.user, args.user)
except FileNotFoundError:
json_return['error']=1
json_return['status']=1
json_return['progress']=100
json_return['message']='Error: cannot create the site directory'
print(json.dumps(json_return))
sys.exit(1)
"""
# Add user
if args.user=='root':
json_return['error']=1
json_return['status']=1
json_return['progress']=100
json_return['message']='Error: you cannot use user root for sites'
print(json.dumps(json_return))
sys.exit(1)
#ret_user=add_user(args.user, '', '', user_directory='', shell='/bin/bash')
func_user="sudo useradd -m -s %s %s" % ('/bin/bash', args.user)
ret_user=True
if call(func_user, shell=True, stdout=DEVNULL) > 0:
ret_user=False
if not ret_user:
print("User %s exists, used for the new site" % args.user)
else:
"""
if call("sudo usermod -a -G %s %s" % (apache_group, args.user), shell=True) > 0:
print('Error, cannot add group to user')
exit(1)
"""
# Add ssh key for diverses uses
print("Adding ssh key for user...")
if call('sudo su - %s -s /bin/bash -c \'ssh-keygen -f /home/%s/.ssh/id_rsa.pub -N \\"\\"\'' % (args.user, args.user), shell=True) > 0:
print('Error, cannot add ssh key for user...')
exit(1)
if call("sudo chmod +x %s" % '/home/'+args.user, shell=True) > 0:
print('Error, cannot set permissions for folder %s...' % real_root_dir)
exit(1)
if call("sudo su - %s -s /bin/bash -c 'mkdir -p %s'" % (args.user, real_root_dir), shell=True) > 0:
print('Error, cannot create Virtualhost folder %s...' % real_root_dir)
exit(1)
password_user=''
if args.password:
password_user=args.password
ret_pass=change_password(args.user, password_user)
if not ret_pass[0]:
print('Error, cannot change password for %s' % args.user )
exit(1)
if call("sudo getenforce", shell=True)==0:
# If selinux enabled, set permissions to /home/hosting/sites for sites.
#if call("sudo setsebool -P httpd_enable_homedirs true && sudo chcon -R -t httpd_sys_content_t /var/www/sites && sudo setsebool -P httpd_can_network_connect 1 && sudo semanage fcontext -a -t httpd_sys_rw_content_t \"/var/www/sites(/.*)?\"", shell=True) > 0:
print('Setting selinux permissions for the http folder %s...' % real_root_dir)
if call("sudo setsebool -P httpd_enable_homedirs true && sudo chcon -R -t httpd_sys_content_t %s && sudo setsebool -P httpd_can_network_connect 1 && chcon -R -t httpd_sys_rw_content_t %s" % (real_root_dir, real_root_dir), shell=True) > 0:
print('Error, cannot set selinux permissions...')
exit(1)
# Save virtualhost
virtualhost_base='/etc/'+apache_cmd+'/vhosts.d'
"""
p=Path(virtualhost_base)
try:
p.mkdir(mode=0o755, parents=True, exist_ok=True)
except FileNotFoundError:
json_return['error']=1
json_return['status']=1
json_return['progress']=100
json_return['message']='Error: cannot create the apache vhost directory'
print(json.dumps(json_return))
sys.exit(1)
"""
virtualhost_path=virtualhost_base+'/%s.conf' % (args.domain)
#virtualhost_enabled_path='/etc/'+apache_cmd+'/vhosts.d/%s.conf' % args.domain
with open(virtualhost_path, 'w') as f:
f.write(vhost)
json_return['progress']=60
json_return['message']='Created or edited virtualhost'
print(json.dumps(json_return))
#time.sleep(1)
# Activate virtualhost
#os.symlink(virtualhost_path, virtualhost_enabled_path)
# Check apache configuration, if not delete user and virtualhost.
if call("sudo "+apachectl+" configtest", shell=True) > 0:
json_return['error']=1
json_return['status']=1
json_return['progress']=100
json_return['message']='Error: the config is wrong '+vhost
# Delete user
#os.unlink(virtualhost_enabled_path)
os.remove(virtualhost_path)
#err, txt=del_user(args.user)
print(json.dumps(json_return))
exit(1)
json_return['progress']=75
json_return['message']='Finished virtualhost configuration'
print(json.dumps(json_return))
#time.sleep(1)
# Restart config
if call("sudo systemctl restart "+apache_cmd, shell=True) > 0:
json_return['error']=1
json_return['status']=1
json_return['progress']=100
json_return['message']='Error: I cannot reload the httpd server'
# Delete user
#os.unlink(virtualhost_enabled_path)
os.remove(virtualhost_path)
#err, txt=del_user(args.user)
print(json.dumps(json_return))
exit(1)
json_return['progress']=85
json_return['message']='Apache2 restarted...'
print(json.dumps(json_return))
#time.sleep(1)
# Finish
json_return['progress']=100
json_return['message']='Virtualhost done'
json_return['status']=0
print(json.dumps(json_return))
#time.sleep(1)
elif args.operation=='remove':
# Change uid to user id of the
if args.root_dir=='/':
json_return['error']=1
json_return['status']=1
json_return['progress']=100
json_return['message']='Error: I cannot delete this place...'
#if not args.user:
# parser.error('You need --user option if you want to remove a virtual host.')
virtualhost_path='/etc/'+apache_cmd+'/vhosts.d/%s.conf' % (args.domain)
virtualhost_php_path='/etc/'+apache_cmd+'/vhosts.d/php/%s-php.conf' % (args.domain)
#virtualhost_enabled_path='/etc/'+apache_cmd+'/vhosts.d/%s.conf' % args.domain
# Delete first apache configuration
"""
try:
os.unlink(virtualhost_enabled_path)
except:
json_return['error']=1
json_return['status']=1
json_return['progress']=100
json_return['message']='Error: I cannot remove symbolic link of apache file config'
print(json.dumps(json_return))
exit(1)
"""
try:
os.remove(virtualhost_path)
except:
json_return['error']=1
json_return['status']=1
json_return['progress']=100
json_return['message']='Error: I cannot remove symbolic link of apache file config'
print(json.dumps(json_return))
exit(1)
if os.path.isfile(virtualhost_php_path):
try:
os.remove(virtualhost_php_path)
except:
json_return['error']=1
json_return['status']=1
json_return['progress']=100
json_return['message']='Error: I cannot remove symbolic link of apache file config'
print(json.dumps(json_return))
exit(1)
json_return['progress']=25
json_return['message']='Deleted virtualhost in Apache configuration'
print(json.dumps(json_return))
#time.sleep(1)
# Test apache config
if call("sudo "+apachectl+" configtest", shell=True) > 0:
json_return['error']=1
json_return['status']=1
json_return['progress']=100
json_return['message']='Error: the config is wrong'
exit(1)
json_return['progress']=50
json_return['message']='Apache configuration checked correctly...'
print(json.dumps(json_return))
#time.sleep(1)
# Restart apache
if call("sudo systemctl restart "+apache_cmd, shell=True) > 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)
json_return['progress']=75
json_return['message']='Apache restarted successfully...'
print(json.dumps(json_return))
#time.sleep(1)
# Delete root_dir if exists
# Change effective uid
pw=pwd.getpwnam(args.user)
os.setuid(pw[2])
if args.root_dir:
shutil.rmtree(args.root_dir)
#os.seteuid(0)
# Delete user of virtualhost
#err, txt=del_user(args.user)
"""
if err:
json_return['error']=1
json_return['status']=1
json_return['progress']=100
json_return['message']='Error: I cannot remove the user'
print(json.dumps(json_return))
exit(1)
"""
# Done
json_return['progress']=100
json_return['message']='VHost removed successfully. Done all.'
json_return['status']=0
print(json.dumps(json_return))
#time.sleep(1)
# Function check php version and install if necessary
def check_php_version(version, user):
linux_distro=distro.id()
if linux_distro=='debian' or linux_distro=='ubuntu':
php_packages={'php74': 'php7.4 php7.4-curl php7.4-dom php7.4-gd php7.4-xml php7.4-mbstring php7.4-zip php7.4-fileinfo php7.4-ctype php7.4-simplexml php7.4-xmlreader php7.4-xmlwriter php7.4-mysql php7.4-bz2 php7.4-intl php7.4-ldap php7.4-imap php7.4-bcmath php7.4-gmp php7.4-exif php7.4-opcache php7.4-redis php7.4-memcached php7.4-fpm', 'php80': 'php8.0 php8.0-curl php8.0-dom php8.0-gd php8.0-xml php8.0-mbstring php8.0-zip php8.0-fileinfo php8.0-ctype php8.0-simplexml php8.0-xmlreader php8.0-xmlwriter php8.0-mysql php8.0-bz2 php8.0-intl php8.0-ldap php8.0-imap php8.0-bcmath php8.0-gmp php8.0-exif php8.0-opcache php8.0-redis php8.0-memcached php8.0-fpm', 'php81': 'php8.1 php8.1-curl php8.1-dom php8.1-gd php8.1-xml php8.1-mbstring php8.1-zip php8.1 php8.1-fileinfo php8.1-ctype php8.1-simplexml php8.1-xmlreader php8.1-xmlwriter php8.1-mysql php8.1-bz2 php8.1-intl php8.1-ldap php8.1-imap php8.1-bcmath php8.1-gmp php8.1-exif php8.1-opcache php8.1-redis php8.1-memcached php8.1-fpm', 'php82': 'php8.2 php8.2-curl php8.2-dom php8.2-gd php8.2-xml php8.2-mbstring php8.2-zip php8.2 php8.2-fileinfo php8.2-ctype php8.2-simplexml php8.2-xmlreader php8.2-xmlwriter php8.2-mysql php8.2-bz2 php8.2-intl php8.2-ldap php8.2-imap php8.2-bcmath php8.2-gmp php8.2-exif php8.2-opcache php8.2-redis php8.2-memcached php8.2-fpm'}
if version in php_version:
#apt-get install php php-curl php-dom php-gd php-xml php-mbstring php-zip php-json php-fileinfo php-ctype php-simplexml php-xmlreader php-xmlwriter php-mysql php-bz2 php-intl php-ldap php-imap php-bcmath php-gmp php-exif php-opcache php-redis php-memcached
print('Checking install of php %s' % php_version[version])
number_version=php_version[version].replace('php', '')
try:
output=subprocess.check_output(['dpkg', '-l', 'php-'+number_version+'*'])
output=output.decode('utf-8')
except:
if call("sudo apt-get -y install %s && a2dismod mpm_prefork && a2enmod mpm_event proxy proxy_fcgi rewrite" % (php_packages[version]), shell=True) > 0:
print('Error, cannot install PHP...')
exit(1)
# Install php-fpm
# /etc/php/8.2/fpm/pool.d/www.conf
# sed -i 's/old-text/new-text/g' input.txt
#listen = /run/php/php8.2-fpm.sock
cmd="sudo cp /etc/php/"+number_version+"/fpm/pool.d/www.conf /etc/php/"+number_version+"/fpm/pool.d/{}.conf && sudo sed -i 's/^user = www-data/user = {}/g' /etc/php/"+number_version+"/fpm/pool.d/{}.conf && sudo sed -i 's/^group = www-data/group = {}/g' /etc/php/"+number_version+"/fpm/pool.d/{}.conf && sudo sed -i 's/\[www\]/[{}]/g' /etc/php/"+number_version+"/fpm/pool.d/{}.conf \
&& sudo sed -i 's/^listen = \/run\/php\/php"+number_version.replace('.', '\.')+"\-fpm\.sock/listen = \/run\/php\/php-"+number_version.replace('.', '\.')+"-{}.sock/g' /etc/php/"+number_version+"/fpm/pool.d/{}.conf"
cmd=cmd.format(user, user, user, user, user, user, user, user, user)
if call(cmd, shell=True) > 0:
print('Error, cannot update PHP-FPM...')
exit(1)
if call('systemctl restart php'+number_version+'-fpm.service', shell=True) > 0:
print('Error, cannot restart PHP-FPM...')
exit(1)
pass
# Reinstall php-fpm
if __name__=='__main__':
manage()

View file

@ -0,0 +1,176 @@
#!/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':
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')
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]))
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:
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!')

View file

@ -0,0 +1,34 @@
<?php
use PhangoApp\LeviathanUtils\Linux;
include('leviathanutils/vendor/autoload.php');
$user=get_current_user();
$file_wp='./bin/wp';
Linux::json_log('Installing WP Wordpress utility...', $error=0, $status=0, $progress=0, $no_progress=1);
if(is_file($file_wp)) {
Linux::json_log('Installed, updating wp...', $error=0, $status=0, $progress=0, $no_progress=1);
Linux::shell_command(['./bin/wp cli update --yes']);
}
else {
if(!is_dir('./bin')) {
mkdir('./bin');
}
Linux::shell_command(['curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar']);
Linux::shell_command(['chmod +x wp-cli.phar']);
Linux::shell_command(["mv wp-cli.phar ./bin/wp && sudo ln -s /home/${user}/bin/wp /usr/local/bin/wp"]);
}
Linux::json_log('WP utility installed successfully', $error=0, $status=0, $progress=100, $no_progress=0);