From d27f8fb8410d2ada35f2386f8f56fd69550016d7 Mon Sep 17 00:00:00 2001 From: Bram Prieshof Date: Thu, 30 Aug 2018 07:42:06 +0000 Subject: [PATCH] Bestanden uploaden naar 'resources' meer oude bestanden --- resources/Apache_v1.0.sh | 82 +++++++++++++++++++++++++ resources/backup.sh | 107 +++++++++++++++++++++++++++++++++ resources/mysql.sh | 101 +++++++++++++++++++++++++++++++ resources/sftp_without_root.sh | 32 ++++++++++ resources/wordpress.sh | 78 ++++++++++++++++++++++++ 5 files changed, 400 insertions(+) create mode 100644 resources/Apache_v1.0.sh create mode 100644 resources/backup.sh create mode 100644 resources/mysql.sh create mode 100644 resources/sftp_without_root.sh create mode 100644 resources/wordpress.sh diff --git a/resources/Apache_v1.0.sh b/resources/Apache_v1.0.sh new file mode 100644 index 0000000..6aef42d --- /dev/null +++ b/resources/Apache_v1.0.sh @@ -0,0 +1,82 @@ +#============================================================================== +# UBUNTU 18.04 BASH SCRIPT +#============================================================================== +# top -o %MEM -> See Memory consumption +# apt-get update -y && apt-get upgrade -y && apt-get dist-upgrade -y +# do-release-upgrade -d +# +# BENCHMARK +# wget freevps.us/downloads/bench.sh -O - -o /dev/null|bash +# wget –no-check-certificate https://vhwinfo.com/vhwinfo.sh -O - -o /dev/null|bash +# +# NGINX LOG -->> tail -n 100 /var/log/nginx/error.log +# RSYNC -->> sshpass -p 'passwd' rsync /tmp/backups/. root@51.68.230.92:/tmp --delete -av --ignore-existing +# WGET .SH -->> http://sitehere.com/install.sh -v -O install.sh && ./install.sh; rm -rf install.sh +# +#============================================================================== +# UNDER DEVELOPMENT +#============================================================================== +# +#============================================================================== +# CHECKEN! +# > +#============================================================================== +echo Welk domein mag gekoppeld worden? Typ domein zonder www +read domain +echo Standaard wachtwoord +read passwd +echo administrator email +read email +apt-get update +apt-get upgrade -y +apt-get dist-upgrade -y +apt-get clean +apt-get autoremove -y +hostnamectl set-hostname $domain +sed -i 's/;preserve_hostname: false/preserve_hostname: true/g' /etc/cloud/cloud.cfg +timedatectl set-timezone Europe/Amsterdam +ufw allow OpenSSH +ufw allow 443/tcp +ufw allow 80/tcp +ufw limit ssh +echo "y" | sudo ufw enable +sed -i 's/#/vm.swappiness=10/g' /etc/sysctl.conf +sed -i 's/#/vm.vfs_cache_pressure=50/g' /etc/sysctl.conf +#-------------------# +# LAMP # +#-------------------# +install apache2 -y +apt install mysql-server-5.7 -y +mysql_secure_installation +mysql -u root -p"$passwd" -e "SELECT user,authentication_string,plugin,host FROM mysql.user;" +mysql -u root -p"$passwd" -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '"$passwd"';" +mysql -u root -p"$passwd" -e "FLUSH PRIVILEGES;" +mysql -u root -p"$passwd" -e "SELECT user,authentication_string,plugin,host FROM mysql.user;" +apt install libapache2-mod-php php-fpm php-mysql php-cgi php-common php-pear php-mbstring php-curl php-gd php-intl php-soap php-xml php-xmlrpc php-zip -y +cat < /etc/apache2/mods-enabled/dir.conf + + DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm + +EOF +systemctl restart apache2 +systemctl status apache2 +#-------------------# +# VIRTUAL HOST # +#-------------------# +rm /var/www/html +mkdir -p /var/www/"$domain"/public_html +chmod -R 755 /var/www +cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/"$domain".conf +cat < /etc/apache2/sites-available/"$domain".conf + + ServerAdmin $email + ServerName $domain + ServerAlias www.$domain + DocumentRoot /var/www/$domain/public_html + ErrorLog \${APACHE_LOG_DIR}/error.log + CustomLog \${APACHE_LOG_DIR}/access.log combined + +EOF +a2ensite $domain.conf +a2dissite 000-default.conf +systemctl restart apache2 \ No newline at end of file diff --git a/resources/backup.sh b/resources/backup.sh new file mode 100644 index 0000000..02ea366 --- /dev/null +++ b/resources/backup.sh @@ -0,0 +1,107 @@ +#!/bin/bash +#============================================================================== +SHELL=/bin/sh +PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin +# 0 2 * * * /bin/bash /tmp/backup.sh + +#sshpass -p 'ictmaatwerk' rsync /tmp/backups/. root@51.68.230.92:/tmp --delete -av --ignore-existing + +#============================================================================== +# CUSTOM SETTINGS +#============================================================================== + +# directory to put the backup files +BACKUP_DIR=/backup + +# MYSQL Parameters +MYSQL_UNAME=root +MYSQL_PWORD=Lithops2206 + +# Don't backup databases with these names +# Example: starts with mysql (^mysql) or ends with _schema (_schema$) +IGNORE_DB="(^mysql|_schema$)" + +# include mysql and mysqldump binaries for cron bash user +PATH=$PATH:/usr/local/mysql/bin + +# Number of days to keep backups +KEEP_BACKUPS_FOR=30 #days + +#============================================================================== +# METHODS +#============================================================================== + +# YYYY-MM-DD +TIMESTAMP=$(date +%F_%H:%M) + +function delete_old_backups() +{ + # Delete mysql older x days + echo "Deleting $BACKUP_DIR/*.sql.gz older than $KEEP_BACKUPS_FOR days" + find $BACKUP_DIR -type f -name "*.sql.gz" -mtime +$KEEP_BACKUPS_FOR -exec rm {} \; + # Delete html folder older x days + echo "Deleting $BACKUP_DIR/*.tar.gz older than $KEEP_BACKUPS_FOR days" + find $BACKUP_DIR -type f -name "*.tar.gz" -mtime +$KEEP_BACKUPS_FOR -exec rm {} \; +} + +function mysql_login() { + local mysql_login="-u $MYSQL_UNAME" + if [ -n "$MYSQL_PWORD" ]; then + local mysql_login+=" -p$MYSQL_PWORD" + fi + echo $mysql_login +} + +function database_list() { + local show_databases_sql="SHOW DATABASES WHERE \`Database\` NOT REGEXP '$IGNORE_DB'" + echo $(mysql $(mysql_login) -e "$show_databases_sql"|awk -F " " '{if (NR!=1) print $1}') +} + +function echo_status(){ + printf '\r'; + printf ' %0.s' {0..100} + printf '\r'; + printf "$1"'\r' +} + +function backup_database(){ + backup_file="$BACKUP_DIR/$TIMESTAMP.$database.sql.gz" + output+="$database => $backup_file\n" + echo_status "...backing up $count of $total databases: $database" + $(mysqldump $(mysql_login) $database | gzip -9 > $backup_file) +} + +function backup_databases(){ + local databases=$(database_list) + local total=$(echo $databases | wc -w | xargs) + local output="" + local count=1 + for database in $databases; do + backup_database + local count=$((count+1)) + done + echo -ne $output | column -t +} + +function hr(){ + printf '=%.0s' {1..100} + printf "\n" +} + +#============================================================================== +# RUN SCRIPT +#============================================================================== +mkdir -p /backup +chmod -R 755 /backup +# MYSQL DUMP +delete_old_backups +hr +backup_databases +hr +# DIRECTORY BACKUP +BACKUPTIME=`date +%b-%d-%y` #get the current date +DESTINATION=/backup/backup-$BACKUPTIME.tar.gz #create a backup file using the current date in it's name +SOURCEFOLDER=/var/www #the folder that contains the files that we want to backup +tar -cpzf $DESTINATION $SOURCEFOLDER #create the backup + +printf "All backed up!\n\n" \ No newline at end of file diff --git a/resources/mysql.sh b/resources/mysql.sh new file mode 100644 index 0000000..1b21b84 --- /dev/null +++ b/resources/mysql.sh @@ -0,0 +1,101 @@ +#!/bin/bash +#============================================================================== +SHELL=/bin/sh +PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin +# 0 2 * * * /bin/bash /tmp/backup.sh + +#RSYNC -> sshpass -p 'ictmaatwerk' rsync /tmp/backups/. root@51.68.230.92:/tmp --delete -av --ignore-existing + +#============================================================================== +# CUSTOM SETTINGS +#============================================================================== + +# directory to put the backup files +BACKUP_DIR=/backup + +# MYSQL Parameters +MYSQL_UNAME=root +MYSQL_PWORD=ictmaatwerk + +# Don't backup databases with these names +# Example: starts with mysql (^mysql) or ends with _schema (_schema$) +IGNORE_DB="(^mysql|_schema$)" + +# include mysql and mysqldump binaries for cron bash user +PATH=$PATH:/usr/local/mysql/bin + +# Number of days to keep backups +KEEP_BACKUPS_FOR=30 #days + +#============================================================================== +# METHODS +#============================================================================== + +# YYYY-MM-DD +TIMESTAMP=$(date +%F_%H:%M) + +function delete_old_backups() +{ + # Delete mysql older x days + echo "Deleting $BACKUP_DIR/*.sql.gz older than $KEEP_BACKUPS_FOR days" + find $BACKUP_DIR -type f -name "*.sql.gz" -mtime +$KEEP_BACKUPS_FOR -exec rm {} \; + # Delete html folder older x days + echo "Deleting $BACKUP_DIR/*.tar.gz older than $KEEP_BACKUPS_FOR days" + find $BACKUP_DIR -type f -name "*.tar.gz" -mtime +$KEEP_BACKUPS_FOR -exec rm {} \; +} + +function mysql_login() { + local mysql_login="-u $MYSQL_UNAME" + if [ -n "$MYSQL_PWORD" ]; then + local mysql_login+=" -p$MYSQL_PWORD" + fi + echo $mysql_login +} + +function database_list() { + local show_databases_sql="SHOW DATABASES WHERE \`Database\` NOT REGEXP '$IGNORE_DB'" + echo $(mysql $(mysql_login) -e "$show_databases_sql"|awk -F " " '{if (NR!=1) print $1}') +} + +function echo_status(){ + printf '\r'; + printf ' %0.s' {0..100} + printf '\r'; + printf "$1"'\r' +} + +function backup_database(){ + backup_file="$BACKUP_DIR/$TIMESTAMP.$database.sql.gz" + output+="$database => $backup_file\n" + echo_status "...backing up $count of $total databases: $database" + $(mysqldump $(mysql_login) $database | gzip -9 > $backup_file) +} + +function backup_databases(){ + local databases=$(database_list) + local total=$(echo $databases | wc -w | xargs) + local output="" + local count=1 + for database in $databases; do + backup_database + local count=$((count+1)) + done + echo -ne $output | column -t +} + +function hr(){ + printf '=%.0s' {1..100} + printf "\n" +} + +#============================================================================== +# RUN SCRIPT +#============================================================================== +mkdir -p /backup +chmod -R 755 /backup +# MYSQL DUMP +delete_old_backups +hr +backup_databases +hr +printf "All backed up!\n\n" \ No newline at end of file diff --git a/resources/sftp_without_root.sh b/resources/sftp_without_root.sh new file mode 100644 index 0000000..216f34e --- /dev/null +++ b/resources/sftp_without_root.sh @@ -0,0 +1,32 @@ +#============================================================================== +# UBUNTU 18.04 - SFTP ACCES WITHOUT ROOT OR SSH +# https://www.digitalocean.com/community/tutorials/how-to-enable-sftp-without-shell-access-on-ubuntu-18-04 +#============================================================================== +echo Username? +read user +echo domein voor toegang? +read domain +adduser $user +chown root:root /var/www +chmod 755 /var/www +chown $user:$user /var/www/$domain +cat < /etc/ssh/sshd_config +PasswordAuthentication yes +ChallengeResponseAuthentication no +UsePAM yes +X11Forwarding yes +PrintMotd no +AcceptEnv LANG LC_* +Subsystem sftp /usr/lib/openssh/sftp-server + +PermitRootLogin yes +Match User $user +ForceCommand internal-sftp +PasswordAuthentication yes +ChrootDirectory /var/www +PermitTunnel no +AllowAgentForwarding no +AllowTcpForwarding no +X11Forwarding no +EOF +systemctl restart sshd \ No newline at end of file diff --git a/resources/wordpress.sh b/resources/wordpress.sh new file mode 100644 index 0000000..bce3a64 --- /dev/null +++ b/resources/wordpress.sh @@ -0,0 +1,78 @@ +#============================================================================== +# INSTALL SECOND WEBSITE WITH WORDPRESS +#============================================================================== +echo Welk domein mag gekoppeld worden? Typ domein zonder www +read domain +echo Standaard wachtwoord? +read passwd +echo Database user en name? +read db_name db_user +echo Standaard wachtwoord +read passwd +db_pass=$(date +%s|sha256sum|base64|head -c 32) +#-------------------# +# MYSQL CONFIG # +#-------------------# +mysql -u root -p"$passwd" -e "CREATE DATABASE "$db_name" DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;" +mysql -u root -p"$passwd" -e "GRANT ALL ON "$db_name".* TO '"$db_user"'@'localhost' IDENTIFIED BY '"$db_pass"';" +mysql -u root -p"$passwd" -e "FLUSH PRIVILEGES;" +#-------------------# +# WP - INSTALL # +#-------------------# +cd /tmp +curl -LO https://wordpress.org/latest.tar.gz +tar xzvf latest.tar.gz +cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php +cp -a /tmp/wordpress/. /var/www/"$domain"/html +chown -R www-data:www-data /var/www/"$domain"/html +WPSalts=$(wget https://api.wordpress.org/secret-key/1.1/salt/ -q -O -) +cat < /var/www/"$domain"/html/wp-config.php + array( +# '127.0.0.1:11211' +# ) +#); +#define('WP_REDIS_HOST', '127.0.0.1'); +#define('WP_REDIS_PASSWORD', '$passwd'); +#define('WP_REDIS_PORT', '6379'); +require_once(ABSPATH . 'wp-settings.php'); +EOF +#-------------------# +# OPCACHE GUI # +#-------------------# +cd /tmp +curl -LO https://raw.githubusercontent.com/amnuts/opcache-gui/master/index.php +cp /tmp/index.php /tmp/opcache.php +cp -a /tmp/opcache.php /var/www/"$domain"/html +cat > /var/www/"$domain"/html/info.php <<- "EOF" + +EOF \ No newline at end of file