Skip to content

Instantly share code, notes, and snippets.

@govza
Last active February 4, 2023 18:00
Show Gist options
  • Save govza/5024018 to your computer and use it in GitHub Desktop.
Save govza/5024018 to your computer and use it in GitHub Desktop.
#!/bin/bash
# MySQL root password
KEYLOCATION="/modx/.ssh/key.pem"
REMOTEUSERSERVER="[email protected]"
ROOTPASS="password"
TIMEZONE="Europe/Moscow"
MYSQLPASS=`< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c12`
SFTPPASS=`< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c12`
PASSWORD=`< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c12`
##############
echo "Enter username for site and database:"
read USERNAME
echo "Enter domain"
read DOMAIN
##############
echo "Creating user and home directory..."
useradd $USERNAME -m -G sftp -s "/bin/false" -d "/var/www/$USERNAME"
if [ "$?" -ne 0 ]; then
echo "Can't add user"
exit 1
fi
echo $SFTPPASS > ./tmp
echo $SFTPPASS >> ./tmp
cat ./tmp | passwd $USERNAME
rm ./tmp
##############
mkdir /var/www/$USERNAME/$DOMAIN
mkdir /var/www/$USERNAME/tmp
mkdir /var/www/$USERNAME/backup/
chmod -R 755 /var/www/$USERNAME/
chown -R $USERNAME:$USERNAME /var/www/$USERNAME/
chown root:root /var/www/$USERNAME
echo "Creating vhost file"
echo "
upstream backend-$USERNAME {server unix:/var/run/php5-$USERNAME.sock;}
server {
listen 80;
server_name $DOMAIN www.$DOMAIN;
root /var/www/$USERNAME/$DOMAIN;
access_log /var/log/nginx/$USERNAME-access.log;
error_log /var/log/nginx/$USERNAME-error.log;
index index.php index.html;
rewrite_log on;
if (\$host != '$DOMAIN' ) {
rewrite ^/(.*)$ http://$DOMAIN/\$1 permanent;
}
location ~* ^/core/ {
deny all;
}
location / {
try_files \$uri \$uri/ @rewrite;
}
location @rewrite {
rewrite ^/(.*)$ /index.php?q=\$1;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
fastcgi_pass backend-$USERNAME;
}
location ~* ^.+\.(jpg|jpeg|gif|png|js|ico|bmp)$ {
access_log off;
expires 10d;
break;
}
location ~ /\.ht {
deny all;
}
}
" > /etc/nginx/sites-available/$USERNAME.conf
ln -s /etc/nginx/sites-available/$USERNAME.conf /etc/nginx/sites-enabled/$USERNAME.conf
##############
echo "Creating php5-fpm config"
echo "[$USERNAME]
listen = /var/run/php5-$USERNAME.sock
listen.mode = 0666
user = $USERNAME
group = $USERNAME
chdir = /var/www/$USERNAME
php_admin_value[upload_tmp_dir] = /var/www/$USERNAME/tmp
php_admin_value[soap.wsdl_cache_dir] = /var/www/$USERNAME/tmp
php_admin_value[upload_max_filesize] = 100M
php_admin_value[post_max_size] = 100M
php_admin_value[open_basedir] = /var/www/$USERNAME/
php_admin_value[disable_functions] = exec,passthru,shell_exec,system,proc_open,popen,curl_multi_exec,parse_ini_file,show_source
php_admin_value[cgi.fix_pathinfo] = 0
php_admin_value[date.timezone] = $TIMEZONE
php_admin_value[apc.cache_by_default] = 0
pm = ondemand
pm.max_children=10
pm.process_idle_timeout=30
pm.max_requests = 50
" > /etc/php5/fpm/pool.d/$USERNAME.conf
##############
echo "Creating config.xml"
echo "<modx>
<database_type>mysql</database_type>
<database_server>localhost</database_server>
<database>$USERNAME</database>
<database_user>$USERNAME</database_user>
<database_password>$MYSQLPASS</database_password>
<database_connection_charset>utf8</database_connection_charset>
<database_charset>utf8</database_charset>
<database_collation>utf8_unicode_ci</database_collation>
<table_prefix>modx_</table_prefix>
<https_port>443</https_port>
<http_host>$DOMAIN</http_host>
<cache_disabled>0</cache_disabled>
<inplace>1</inplace>
<unpacked>0</unpacked>
<language>ru</language>
<cmsadmin>$USERNAME</cmsadmin>
<cmspassword>$PASSWORD</cmspassword>
<cmsadminemail>admin@$DOMAIN</cmsadminemail>
<core_path>/var/www/$USERNAME/$DOMAIN/core/</core_path>
<context_mgr_path>/var/www/$USERNAME/$DOMAIN/manager/</context_mgr_path>
<context_mgr_url>/manager/</context_mgr_url>
<context_connectors_path>/var/www/$USERNAME/$DOMAIN/connectors/</context_connectors_path>
<context_connectors_url>/connectors/</context_connectors_url>
<context_web_path>/var/www/$USERNAME/$DOMAIN/</context_web_path>
<context_web_url>/</context_web_url>
<remove_setup_directory>1</remove_setup_directory>
</modx>" > /var/www/$USERNAME/config.xml
#############
echo "Creating utilities upload_mysql"
echo "
mysqldump --opt -C -uroot -p$ROOTPASS $USERNAME | ssh -C -i $KEYLOCATION $REMOTEUSERSERVER mysql -C -uroot -p$ROOTPASS $USERNAME
" > /var/www/$USERNAME/upload_mysql.sh
chmod +x /var/www/$USERNAME/upload_mysql.sh
echo "Creating utilities upload_rsync"
echo "#!/bin/bash
sudo rm -rf /var/www/$USERNAME/$DOMAIN/core/cache/*;
sudo rsync -avz --delete -progress -e \"ssh -i $KEYLOCATION\" --rsync-path=\"sudo rsync\" /var/www/$USERNAME/$DOMAIN/ $REMOTEUSERSERVER:/var/www/$USERNAME/$DOMAIN/;
" > /var/www/$USERNAME/upload_rsync_files.sh
chmod +x /var/www/$USERNAME/upload_rsync_files.sh
echo "Creating utilities download_mysql"
echo "
ssh -C -i $KEYLOCATION $REMOTEUSERSERVER mysqldump --opt -C -uroot -p$ROOTPASS $USERNAME | mysql -C -uroot -p$ROOTPASS $USERNAME
" > /var/www/$USERNAME/download_mysql.sh
chmod +x /var/www/$USERNAME/download_mysql.sh
echo "Creating utilities download_rsync"
echo "#!/bin/bash
sudo rsync -chavzP --exclude='core/cache' --stats --delete -e \"ssh -i $KEYLOCATION\" $REMOTEUSERSERVER:/var/www/$USERNAME/$DOMAIN/ /var/www/$USERNAME/$DOMAIN/;
sudo rm -rf /var/www/$USERNAME/$DOMAIN/core/cache/*;
" > /var/www/$USERNAME/download_rsync_files.sh
chmod +x /var/www/$USERNAME/download_rsync_files.sh
echo "Creating backup scripts"
echo "#!/bin/bash
sudo cp -rf /var/www/$USERNAME/$DOMAIN/config.core.php /var/www/$USERNAME/backup/;
sudo cp -rf /var/www/$USERNAME/$DOMAIN/core/config/config.inc.php /var/www/$USERNAME/backup/;
mysqldump --opt -C -uroot --password=$ROOTPASS $USERNAME modx_users > modx_users.sql;
" > /var/www/$USERNAME/backup/backuporiginalconf.sh
chmod +x /var/www/$USERNAME/backup/backuporiginalconf.sh
echo "Creating backup scripts 2"
echo "#!/bin/bash
sudo cp -rf /var/www/$USERNAME/backup/config.core.php /var/www/$USERNAME/$DOMAIN/;
sudo cp -rf /var/www/$USERNAME/backup/config.core.php /var/www/$USERNAME/$DOMAIN/connectors/;
sudo cp -rf /var/www/$USERNAME/backup/config.core.php /var/www/$USERNAME/$DOMAIN/manager/;
sudo cp -rf /var/www/$USERNAME/backup/config.inc.php /var/www/$USERNAME/$DOMAIN/core/config/;
mysql -C -uroot --password=$ROOTPASS $USERNAME < /var/www/$USERNAME/backup/modx_users.sql
" > /var/www/$USERNAME/backup/insertoriginalconf.sh
chmod +x /var/www/$USERNAME/backup/insertoriginalconf.sh
#############
echo "Reloading nginx"
service nginx reload
echo "Reloading php5-fpm"
service php5-fpm reload
##############
echo "Creating database"
Q1="CREATE DATABASE IF NOT EXISTS $USERNAME DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;;"
Q2="GRANT ALTER,DELETE,DROP,CREATE,INDEX,INSERT,SELECT,UPDATE,CREATE TEMPORARY TABLES,LOCK TABLES ON $USERNAME.* TO '$USERNAME'@'localhost' IDENTIFIED BY '$MYSQLPASS';"
Q3="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}${Q3}"
mysql -uroot --password=$ROOTPASS -e "$SQL"
##############
echo "Installing MODx"
cd /var/www/$USERNAME/$DOMAIN/
echo "Getting modx.zip..."
sudo -u $USERNAME cp /modx/modx.zip ./
echo "Unzipping file..."
sudo -u $USERNAME unzip "./modx.zip" -d ./ > /dev/null
ZDIR=`ls -F | grep "\/" | head -1`
if [ "${ZDIR}" = "/" ]; then
echo "Failed to find directory..."; exit
fi
if [ -d "${ZDIR}" ]; then
cd ${ZDIR}
echo "Moving out of temp dir..."
sudo -u $USERNAME mv ./* ../
cd ../
rm -r "./${ZDIR}"
echo "Removing zip file..."
rm "./modx.zip"
cd "setup"
echo "Running setup..."
sudo -u $USERNAME php ./index.php --installmode=new --config=/var/www/$USERNAME/config.xml
echo "Done!"
else
echo "Failed to find directory: ${ZDIR}"
exit
fi
echo "#!/bin/bash
echo \"Set permissions for /var/www/$USERNAME/$DOMAIN...\";
echo \"CHOWN files...\";
chown -R $USERNAME:$USERNAME \"/var/www/$USERNAME/$DOMAIN\";
echo \"CHMOD directories...\";
find \"/var/www/$USERNAME/$DOMAIN\" -type d -exec chmod 0755 '{}' \;
echo \"CHMOD files...\";
find \"/var/www/$USERNAME/$DOMAIN\" -type f -exec chmod 0644 '{}' \;
" > /var/www/$USERNAME/chmod
chmod +x /var/www/$USERNAME/chmod
echo "
Сайт $DOMAIN
Панель менеджера доступна по адресу: http://$DOMAIN/manager/
Логин: manager
Пароль: $PASSWORD (вы можете сменить его в панели менеджера)
Панель администратора (для веб-мастеров)
Логин: $USERNAME
Пароль: $PASSWORD
Пользователь SFTP: $USERNAME
Пароль для доступа по SFTP: $SFTPPASS
База MYSQL (localhost): $USERNAME
Пароль для базы MYSQL: $MYSQLPASS" > /var/www/$USERNAME/pass.txt
cat /var/www/$USERNAME/pass.txt
@airborneradomevolo
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment