tomba2k

hestia-install.sh

Apr 28th, 2020
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 30.53 KB | None | 0 0
  1. export PATH=$PATH:/sbin
  2. RHOST='apt.hestiacp.com'
  3. GPG='gpg.hestiacp.com'
  4. HESTIA='/usr/local/hestia'
  5. LOG="/root/hst_install_backups/hst_install-$(date +%d%m%Y%H%M).log"
  6. memory=$(grep 'MemTotal' /proc/meminfo |tr ' ' '\n' |grep [0-9])
  7. hst_backups="/root/hst_install_backups/$(date +%d%m%Y%H%M)"
  8. arch=$(uname -i)
  9. spinner="/-\|"
  10. codename="$(cat /etc/os-release |grep VERSION= |cut -f 2 -d \(|cut -f 1 -d \))"
  11. pma_v='5.0.1'
  12. fpm_v="7.4"
  13. HESTIA_INSTALL_DIR="$HESTIA/install/deb"
  14.  
  15. nginx='yes'
  16. phpfpm='yes'
  17. multiphp='no'
  18. vsftpd='yes'
  19. proftpd='no'
  20. named='yes'
  21. mysql='yes'
  22. postgresql='no'
  23. exim='yes'
  24. dovecot='yes'
  25. clamd='yes'
  26. spamd='yes'
  27. iptables='yes'
  28. fail2ban='yes'
  29. quota='no'
  30. interactive='yes'
  31. api='yes'
  32. lang='en'
  33. port=8083
  34.  
  35. # Defining password-gen function
  36. gen_pass() {
  37.     MATRIX='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  38.     LENGTH=10
  39.     while [ ${n:=1} -le $LENGTH ]; do
  40.         PASS="$PASS${MATRIX:$(($RANDOM%${#MATRIX})):1}"
  41.         let n+=1
  42.     done
  43.     echo "$PASS"
  44. }
  45.  
  46. software="awstats bc bind bind-libs bind-utils clamav clamav-update
  47.    curl dovecot e2fsprogs exim expect fail2ban flex freetype ftp GeoIP httpd
  48.    ImageMagick iptables-services lsof mailx mc
  49.    net-tools nginx openssh-clients pcre libidn git php
  50.    php-bcmath php-cli php-common php-fpm php-gd php-imap php-mbstring
  51.    php-mcrypt phpMyAdmin php-mysql php-pdo php-pgsql php-soap
  52.    php-tidy php-xml php-xmlrpc php-opcache php-pspell php-readline
  53.    php-imagick php-intl php-json php-bz2 php-zip php-ldap php-apcu php-curl
  54.    roundcubemail rrdtool rsyslog screen
  55.    spamassassin sqlite sudo tar telnet unzip quota
  56.    sudo vim-common vsftpd which zip sysstat"
  57.  
  58. # Asking for confirmation to proceed
  59. if [ "$interactive" = 'yes' ]; then
  60.     read -p 'Would you like to continue with the installation? [Y/N]: ' answer
  61.     if [ "$answer" != 'y' ] && [ "$answer" != 'Y'  ]; then
  62.         echo 'Goodbye'
  63.         exit 1
  64.     fi
  65.  
  66.     # Asking for contact email
  67.     if [ -z "$email" ]; then
  68.         read -p 'Please enter admin email address: ' email
  69.     fi
  70.  
  71.     # Asking to set FQDN hostname
  72.     if [ -z "$servername" ]; then
  73.         read -p "Please enter FQDN hostname [$(hostname -f)]: " servername
  74.     fi
  75. fi
  76.  
  77. # Set hostname if it wasn't set
  78. if [ -z "$servername" ]; then
  79.     servername=$(hostname -f)
  80. fi
  81.  
  82. # Set FQDN if it wasn't set
  83. mask1='(([[:alnum:]](-?[[:alnum:]])*)\.)'
  84. mask2='*[[:alnum:]](-?[[:alnum:]])+\.[[:alnum:]]{2,}'
  85. if ! [[ "$servername" =~ ^${mask1}${mask2}$ ]]; then
  86.     if [ ! -z "$servername" ]; then
  87.         servername="$servername.example.com"
  88.     else
  89.         servername="example.com"
  90.     fi
  91.     echo "127.0.0.1 $servername" >> /etc/hosts
  92. fi
  93.  
  94. # Set email if it wasn't set
  95. if [ -z "$email" ]; then
  96.     email="admin@$servername"
  97. fi
  98.  
  99. # Creating backup directory tree
  100. mkdir -p $hst_backups
  101. cd $hst_backups
  102. mkdir nginx apache2 php vsftpd proftpd bind exim4 dovecot clamd
  103. mkdir spamassassin mysql postgresql hestia
  104.  
  105. # Backup nginx configuration
  106. systemctl stop nginx > /dev/null 2>&1
  107. cp -r /etc/nginx/* $hst_backups/nginx > /dev/null 2>&1
  108.  
  109. # Backup Apache configuration
  110. systemctl stop apache2 > /dev/null 2>&1
  111. cp -r /etc/apache2/* $hst_backups/apache2 > /dev/null 2>&1
  112. rm -f /etc/apache2/conf.d/* > /dev/null 2>&1
  113.  
  114. # Backup PHP-FPM configuration
  115. systemctl stop php*-fpm > /dev/null 2>&1
  116. cp -r /etc/php/* $hst_backups/php/ > /dev/null 2>&1
  117.  
  118. # Backup Bind configuration
  119. systemctl stop named > /dev/null 2>&1
  120. cp -r /etc/named* $hst_backups/named > /dev/null 2>&1
  121.  
  122. # Backup Vsftpd configuration
  123. systemctl stop vsftpd > /dev/null 2>&1
  124. cp /etc/vsftpd.conf $hst_backups/vsftpd > /dev/null 2>&1
  125.  
  126. # Backup ProFTPD configuration
  127. systemctl stop proftpd > /dev/null 2>&1
  128. cp /etc/proftpd.conf $hst_backups/proftpd > /dev/null 2>&1
  129.  
  130. # Backup Exim configuration
  131. systemctl stop exim > /dev/null 2>&1
  132. cp -r /etc/exim/* $hst_backups/exim > /dev/null 2>&1
  133.  
  134. # Backup ClamAV configuration
  135. systemctl stop clamav-daemon > /dev/null 2>&1
  136. cp -r /etc/clam* $hst_backups/clamav > /dev/null 2>&1
  137.  
  138. # Backup SpamAssassin configuration
  139. systemctl stop spamassassin > /dev/null 2>&1
  140. cp -r /etc/spamassassin/* $hst_backups/spamassassin > /dev/null 2>&1
  141.  
  142. # Backup Dovecot configuration
  143. systemctl stop dovecot > /dev/null 2>&1
  144. cp /etc/dovecot.conf $hst_backups/dovecot > /dev/null 2>&1
  145. cp -r /etc/dovecot/* $hst_backups/dovecot > /dev/null 2>&1
  146.  
  147. # Backup MySQL/MariaDB configuration and data
  148. systemctl stop mysql > /dev/null 2>&1
  149. killall -9 mysqld > /dev/null 2>&1
  150. mv /var/lib/mysql $hst_backups/mysql/mysql_datadir > /dev/null 2>&1
  151. cp -r /etc/percona-server.conf.d* $hst_backups/mysql > /dev/null 2>&1
  152. mv -f /root/.my.cnf $hst_backups/mysql > /dev/null 2>&1
  153.  
  154. # Backup Hestia
  155. systemctl stop hestia > /dev/null 2>&1
  156. cp -r $HESTIA/* $hst_backups/hestia > /dev/null 2>&1
  157. rm -rf $HESTIA > /dev/null 2>&1
  158.  
  159. # Restarting rsyslog
  160. service rsyslog restart > /dev/null 2>&1
  161.  
  162. # Checking ipv6 on loopback interface
  163. check_lo_ipv6=$(/sbin/ip addr | grep 'inet6')
  164. check_rc_ipv6=$(grep 'scope global dev lo' /etc/rc.local)
  165. if [ ! -z "$check_lo_ipv6)" ] && [ -z "$check_rc_ipv6" ]; then
  166.     ip addr add ::2/128 scope global dev lo
  167.     echo "# Vesta: Workraround for openssl validation func" >> /etc/rc.local
  168.     echo "ip addr add ::2/128 scope global dev lo" >> /etc/rc.local
  169.     chmod a+x /etc/rc.local
  170. fi
  171.  
  172. # Disabling SELinux
  173. if [ -e '/etc/sysconfig/selinux' ]; then
  174.     sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
  175.     sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
  176.     setenforce 0 2>/dev/null
  177. fi
  178.  
  179. # Disabling iptables
  180. service iptables stop
  181. service firewalld stop >/dev/null 2>&1
  182.  
  183. # Adding backup user
  184. adduser backup 2>/dev/null
  185. ln -sf /home/backup /backup
  186. chmod a+x /backup
  187.  
  188. # Set directory color
  189. echo 'LS_COLORS="$LS_COLORS:di=00;33"' >> /etc/profile
  190.  
  191. # Register /sbin/nologin and /usr/sbin/nologin
  192. echo "/sbin/nologin" >> /etc/shells
  193. echo "/usr/sbin/nologin" >> /etc/shells
  194.  
  195. # Changing default systemd interval
  196. echo "DefaultStartLimitInterval=1s" >> /etc/systemd/system.conf
  197. echo "DefaultStartLimitBurst=60" >> /etc/systemd/system.conf
  198. systemctl daemon-reexec
  199.  
  200. echo "(*) Configuring system settings..."
  201. # Enable SSH password authentication
  202. sed -i "s/rdAuthentication no/rdAuthentication yes/g" /etc/ssh/sshd_config
  203.  
  204.  
  205. # Reduce SSH login grace time
  206. sed -i "s/LoginGraceTime 2m/LoginGraceTime 1m/g" /etc/ssh/sshd_config
  207. sed -i "s/#LoginGraceTime 2m/LoginGraceTime 1m/g" /etc/ssh/sshd_config
  208.  
  209. # Set directory color
  210. if [ -z "$(grep 'LS_COLORS="$LS_COLORS:di=00;33"' /etc/profile)" ]; then
  211.     echo 'LS_COLORS="$LS_COLORS:di=00;33"' >> /etc/profile
  212. fi
  213.  
  214. # Registering /usr/sbin/nologin
  215. if [ -z "$(grep nologin /etc/shells)" ]; then
  216.     echo "/usr/sbin/nologin" >> /etc/shells
  217. fi
  218.  
  219. echo "(*) Configuring Hestia Control Panel..."
  220. # Installing sudo configuration
  221. mkdir -p /etc/sudoers.d
  222. cp -fn $HESTIA_INSTALL_DIR/sudo/admin /etc/sudoers.d/
  223. chmod 440 /etc/sudoers.d/admin
  224.  
  225. # Configuring system env
  226. echo "export HESTIA='$HESTIA'" > /etc/profile.d/hestia.sh
  227. echo 'PATH=$PATH:'$HESTIA'/bin' >> /etc/profile.d/hestia.sh
  228. echo 'export PATH' >> /etc/profile.d/hestia.sh
  229. chmod 755 /etc/profile.d/hestia.sh
  230. source /etc/profile.d/hestia.sh
  231.  
  232. # Configuring logrotate for Hestia logs
  233. cp -fn $HESTIA_INSTALL_DIR/logrotate/hestia /etc/logrotate.d/hestia
  234.  
  235. # Building directory tree and creating some blank files for Hestia
  236. mkdir -p $HESTIA/conf $HESTIA/log $HESTIA/ssl $HESTIA/data/ips \
  237.     $HESTIA/data/queue $HESTIA/data/users $HESTIA/data/firewall \
  238.     $HESTIA/data/sessions
  239. touch $HESTIA/data/queue/backup.pipe $HESTIA/data/queue/disk.pipe \
  240.     $HESTIA/data/queue/webstats.pipe $HESTIA/data/queue/restart.pipe \
  241.     $HESTIA/data/queue/traffic.pipe $HESTIA/log/system.log \
  242.     $HESTIA/log/nginx-error.log $HESTIA/log/auth.log
  243. chmod 750 $HESTIA/conf $HESTIA/data/users $HESTIA/data/ips $HESTIA/log
  244. chmod -R 750 $HESTIA/data/queue
  245. chmod 660 $HESTIA/log/*
  246. rm -f /var/log/hestia
  247. ln -s $HESTIA/log /var/log/hestia
  248. chmod 770 $HESTIA/data/sessions
  249.  
  250. # Generating Hestia configuration
  251. rm -f $HESTIA/conf/hestia.conf > /dev/null 2>&1
  252. touch $HESTIA/conf/hestia.conf
  253. chmod 660 $HESTIA/conf/hestia.conf
  254.  
  255. echo "WEB_SYSTEM='nginx'" >> $HESTIA/conf/hestia.conf
  256. echo "WEB_PORT='80'" >> $HESTIA/conf/hestia.conf
  257. echo "WEB_SSL_PORT='443'" >> $HESTIA/conf/hestia.conf
  258. echo "WEB_SSL='openssl'"  >> $HESTIA/conf/hestia.conf
  259. #echo "STATS_SYSTEM='awstats'" >> $HESTIA/conf/hestia.conf
  260. echo "WEB_BACKEND='php-fpm'" >> $HESTIA/conf/hestia.conf
  261.  
  262. installed_db_types='mysql'
  263.  
  264. db=$(echo "$installed_db_types" |\
  265.     sed "s/,/\n/g"|\
  266.     sort -r -u |\
  267.     sed "/^$/d"|\
  268.     sed ':a;N;$!ba;s/\n/,/g')
  269. echo "DB_SYSTEM='$db'" >> $HESTIA/conf/hestia.conf
  270.  
  271. echo "FTP_SYSTEM='vsftpd'" >> $HESTIA/conf/hestia.conf
  272. echo "DNS_SYSTEM='named'" >> $HESTIA/conf/hestia.conf
  273.  
  274. # Mail stack
  275. if [ "$exim" = 'yes' ]; then
  276.     echo "MAIL_SYSTEM='exim'" >> $HESTIA/conf/hestia.conf
  277.     if [ "$clamd" = 'yes'  ]; then
  278.         echo "ANTIVIRUS_SYSTEM='clamav-daemon'" >> $HESTIA/conf/hestia.conf
  279.     fi
  280.     if [ "$spamd" = 'yes' ]; then
  281.         echo "ANTISPAM_SYSTEM='spamassassin'" >> $HESTIA/conf/hestia.conf
  282.     fi
  283.     if [ "$dovecot" = 'yes' ]; then
  284.         echo "IMAP_SYSTEM='dovecot'" >> $HESTIA/conf/hestia.conf
  285.     fi
  286. fi
  287.  
  288. # Cron daemon
  289. echo "CRON_SYSTEM='crond'" >> $HESTIA/conf/hestia.conf
  290.  
  291. # Firewall stack
  292. if [ "$iptables" = 'yes' ]; then
  293.     echo "FIREWALL_SYSTEM='iptables'" >> $HESTIA/conf/hestia.conf
  294. fi
  295. if [ "$iptables" = 'yes' ] && [ "$fail2ban" = 'yes' ]; then
  296.     echo "FIREWALL_EXTENSION='fail2ban'" >> $HESTIA/conf/hestia.conf
  297. fi
  298.  
  299. # Disk quota
  300. if [ "$quota" = 'yes' ]; then
  301.     echo "DISK_QUOTA='yes'" >> $HESTIA/conf/hestia.conf
  302. fi
  303.  
  304. # Backups
  305. echo "BACKUP_SYSTEM='local'" >> $HESTIA/conf/hestia.conf
  306.  
  307. # Language
  308. echo "LANGUAGE='$lang'" >> $HESTIA/conf/hestia.conf
  309.  
  310. # Version & Release Branch
  311. echo "VERSION='1.1.1'" >> $HESTIA/conf/hestia.conf
  312. echo "RELEASE_BRANCH='release'" >> $HESTIA/conf/hestia.conf
  313.  
  314. # Installing hosting packages
  315. cp -rf $HESTIA_INSTALL_DIR/packages $HESTIA/data/
  316.  
  317. # Update nameservers in hosting package
  318. IFS='.' read -r -a domain_elements <<< "$servername"
  319. if [ ! -z "${domain_elements[-2]}" ] && [ ! -z "${domain_elements[-1]}" ]; then
  320.     serverdomain="${domain_elements[-2]}.${domain_elements[-1]}"
  321.     sed -i s/"domain.tld"/"$serverdomain"/g $HESTIA/data/packages/*.pkg
  322. fi
  323.  
  324. # Installing templates
  325. cp -rf $HESTIA_INSTALL_DIR/templates $HESTIA/data/
  326.  
  327. mkdir -p /var/www/html
  328. mkdir -p /var/www/document_errors
  329.  
  330. # Install default success page
  331. cp -rf $HESTIA_INSTALL_DIR/templates/web/unassigned/index.html /var/www/html/
  332. cp -rf $HESTIA_INSTALL_DIR/templates/web/skel/document_errors/* /var/www/document_errors/
  333.  
  334. # Installing firewall rules
  335. cp -rf $HESTIA_INSTALL_DIR/firewall $HESTIA/data/
  336.  
  337. # Configuring server hostname
  338. $HESTIA/bin/v-change-sys-hostname $servername > /dev/null 2>&1
  339.  
  340. # Generating SSL certificate
  341. echo "(*) Generating default self-signed SSL certificate..."
  342. $HESTIA/bin/v-generate-ssl-cert $(hostname) $email 'US' 'California' \
  343.      'San Francisco' 'Hestia Control Panel' 'IT' > /tmp/hst.pem
  344.  
  345. # Parsing certificate file
  346. crt_end=$(grep -n "END CERTIFICATE-" /tmp/hst.pem |cut -f 1 -d:)
  347. key_start=$(grep -n "BEGIN RSA" /tmp/hst.pem |cut -f 1 -d:)
  348. key_end=$(grep -n  "END RSA" /tmp/hst.pem |cut -f 1 -d:)
  349.  
  350. # Adding SSL certificate
  351. echo "(*) Adding SSL certificate to Hestia Control Panel..."
  352. cd $HESTIA/ssl
  353. sed -n "1,${crt_end}p" /tmp/hst.pem > certificate.crt
  354. sed -n "$key_start,${key_end}p" /tmp/hst.pem > certificate.key
  355. chown root:mail $HESTIA/ssl/*
  356. chmod 660 $HESTIA/ssl/*
  357. rm /tmp/hst.pem
  358.  
  359. # Adding nologin as a valid system shell
  360. if [ -z "$(grep nologin /etc/shells)" ]; then
  361.     echo "/usr/sbin/nologin" >> /etc/shells
  362. fi
  363.  
  364. # Install dhparam.pem
  365. #cp -fn $HESTIA_INSTALL_DIR/ssl/dhparam.pem /etc/ssl
  366. openssl dhparam -dsaparam -out /etc/ssl/dhparam.pem 4096
  367.  
  368. if [ "$nginx" = 'yes' ]; then
  369.     echo "(*) Configuring NGINX..."
  370.     rm -f /etc/nginx/conf.d/*.conf
  371.     cp -fn $HESTIA_INSTALL_DIR/nginx/nginx.conf /etc/nginx/
  372.     cp -fn $HESTIA_INSTALL_DIR/nginx/status.conf /etc/nginx/conf.d/
  373.     cp -fn $HESTIA_INSTALL_DIR/nginx/phpmyadmin.inc /etc/nginx/conf.d/
  374.     cp -fn $HESTIA_INSTALL_DIR/nginx/phppgadmin.inc /etc/nginx/conf.d/
  375.     cp -fn $HESTIA_INSTALL_DIR/logrotate/nginx /etc/logrotate.d/
  376.     mkdir -p /etc/nginx/conf.d/domains
  377.     mkdir -p /var/log/nginx/domains
  378.  
  379.     # Update dns servers in nginx.conf
  380.     dns_resolver=$(cat /etc/resolv.conf | grep -i '^nameserver' | cut -d ' ' -f2 | tr '\r\n' ' ' | xargs)
  381.     for ip in $dns_resolver; do
  382.         if [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  383.             resolver="$ip $resolver"
  384.         fi
  385.     done
  386.     if [ ! -z "$resolver" ]; then
  387.         sed -i "s/1.0.0.1 1.1.1.1/$resolver/g" /etc/nginx/nginx.conf
  388.         sed -i "s/1.0.0.1 1.1.1.1/$resolver/g" /usr/local/hestia/nginx/conf/nginx.conf
  389.     fi
  390.  
  391.     systemctl enable nginx > /dev/null 2>&1
  392.     systemctl start nginx >> $LOG
  393. fi
  394.  
  395. #----------------------------------------------------------#
  396. #                     Configure PHP-FPM                    #
  397. #----------------------------------------------------------#
  398.  
  399. if [ "$phpfpm" = 'yes' ]; then
  400.     echo "(*) Configuring PHP-FPM..."
  401.     $HESTIA/bin/v-add-web-php "$fpm_v" > /dev/null 2>&1
  402.     cp -fn $HESTIA_INSTALL_DIR/php-fpm/www.conf /etc/php-fpm.d/www.conf
  403.     systemctl enable php-fpm > /dev/null 2>&1
  404.     systemctl start php-fpm >> $LOG
  405.     update-alternatives --set php /usr/bin/php$fpm_v > /dev/null 2>&1
  406. fi
  407.  
  408. #----------------------------------------------------------#
  409. #                     Configure PHP                        #
  410. #----------------------------------------------------------#
  411.  
  412. ZONE=$(timedatectl 2>/dev/null|grep 'Time zone'|awk '{print $3}')
  413. if [ -e '/etc/sysconfig/clock' ]; then
  414.     source /etc/sysconfig/clock
  415. fi
  416. if [ -z "$ZONE" ]; then
  417.     ZONE='UTC'
  418. fi
  419. for pconf in $(find /etc/php* -name php.ini); do
  420.     sed -i "s|;date.timezone =|date.timezone = $ZONE|g" $pconf
  421.     sed -i 's%_open_tag = Off%_open_tag = On%g' $pconf
  422. done
  423.  
  424. # Cleanup php session files not changed in the last 7 days (60*24*7 minutes)
  425. echo '#!/bin/sh' > /etc/cron.daily/php-session-cleanup
  426. echo "find -O3 /home/*/tmp/ -ignore_readdir_race -depth -mindepth 1 -name 'sess_*' -type f -cmin '+10080' -delete > /dev/null 2>&1" >> /etc/cron.daily/php-session-cleanup
  427. echo "find -O3 $HESTIA/data/sessions/ -ignore_readdir_race -depth -mindepth 1 -name 'sess_*' -type f -cmin '+10080' -delete > /dev/null 2>&1" >> /etc/cron.daily/php-session-cleanup
  428. chmod 755 /etc/cron.daily/php-session-cleanup
  429.  
  430.  
  431.  
  432. #----------------------------------------------------------#
  433. #                    Configure Vsftpd                      #
  434. #----------------------------------------------------------#
  435.  
  436. if [ "$vsftpd" = 'yes' ]; then
  437.     echo "(*) Configuring Vsftpd server..."
  438.     cp -fn $HESTIA_INSTALL_DIR/vsftpd/vsftpd.conf /etc/
  439.     touch /var/log/vsftpd.log
  440.     chown root:adm /var/log/vsftpd.log
  441.     chmod 640 /var/log/vsftpd.log
  442.     touch /var/log/xferlog
  443.     chown root:adm /var/log/xferlog
  444.     chmod 640 /var/log/xferlog
  445.     systemctl enable vsftpd
  446.     systemctl start vsftpd >> $LOG
  447. fi
  448.  
  449. #----------------------------------------------------------#
  450. #                  Configure MySQL                       #
  451. #----------------------------------------------------------#
  452.  
  453. if [ "$mysql" = 'yes' ]; then
  454.     echo "(*) Configuring MySQL database server..."
  455.     mycnf="my-small.cnf"
  456.     if [ $memory -gt 1200000 ]; then
  457.         mycnf="my-medium.cnf"
  458.     fi
  459.     if [ $memory -gt 3900000 ]; then
  460.         mycnf="my-large.cnf"
  461.     fi
  462.  
  463.     # Configuring MySQL
  464.     #cp -fn $HESTIA_INSTALL_DIR/mysql/$mycnf /etc/percona-server.conf.d/mysqld.cnf
  465.     #mysql_install_db >> $LOG
  466.  
  467.     systemctl enable mysql
  468.     systemctl start mysql >> $LOG
  469.  
  470.     # Securing MySQL installation
  471.     #mpass=$(date +%s | sha256sum | base64 | head -c 16 ; echo)
  472.     mpass=`grep 'temporary password' /var/log/mysqld.log | tail -n 1 | cut -d"@" -f 2 | cut -d" " -f 2`
  473.     echo -e "[client]\npassword='$mpass'\n" > /root/.my.cnf
  474.     mysqladmin -u root password $mpass >> $LOG
  475.     chmod 600 /root/.my.cnf
  476.  
  477.     # Clear MySQL Test Users and Databases
  478.     mysql -e "DELETE FROM mysql.user WHERE User=''"
  479.     mysql -e "DROP DATABASE test" > /dev/null 2>&1
  480.     mysql -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'"
  481.     mysql -e "DELETE FROM mysql.user WHERE user='';"
  482.     mysql -e "DELETE FROM mysql.user WHERE authentication_string='';"
  483.  
  484.  
  485.     # Configuring phpMyAdmin
  486.     mysql < /usr/share/phpMyAdmin/sql/create_tables.sql
  487.     p=$(date +%s | sha256sum | base64 | head -c 16 ; echo)
  488.     fish=$(date +%s | sha256sum | base64 | head -c 16 ; echo)
  489.     mysql -e "GRANT ALL ON phpmyadmin.*
  490.        TO phpmyadmin@localhost IDENTIFIED BY '$p'"
  491.     cp -fn $HESTIA/pma/config.inc.conf /etc/phpMyAdmin/config.inc.php
  492.     sed -i "s/%blowfish_secret%/$fish/g" /etc/phpMyAdmin/config.inc.php
  493.     sed -i "s/%phpmyadmin_pass%/$p/g" /etc/phpMyAdmin/config.inc.php
  494.     chmod 777 /var/lib/phpMyAdmin/temp
  495.     chmod 777 /var/lib/phpMyAdmin/save
  496.    
  497.    
  498.     # Configuring phpMyAdmin
  499.     #cp -fn $HESTIA_INSTALL_DIR/pma/config.inc.php /etc/phpMyAdmin/
  500.     #chmod 777 /var/lib/phpMyAdmin/temp/
  501. fi
  502.  
  503.  
  504. #----------------------------------------------------------#
  505. #                      Configure Bind                      #
  506. #----------------------------------------------------------#
  507.  
  508. if [ "$named" = 'yes' ]; then
  509.     echo "(*) Configuring Bind DNS server..."
  510.     cp -fn $HESTIA_INSTALL_DIR/bind/named.conf /etc/
  511.     cp -fn $HESTIA_INSTALL_DIR/bind/named.conf.options /etc/named/
  512.     chown root:named /etc/named.conf
  513.     chown root:named /etc/bind/named.conf.options
  514.     chown named:named /var/cache/bind
  515.     chmod 640 /etc/named.conf
  516.     chmod 640 /etc/named/named.conf.options
  517.     #aa-complain /usr/sbin/named > /dev/null 2>&1
  518.     #echo "/home/** rwm," >> /etc/apparmor.d/local/usr.sbin.named 2> /dev/null
  519.     #if ! grep --quiet lxc /proc/1/environ; then
  520.     #    systemctl status apparmor > /dev/null 2>&1
  521.     #    if [ $? -ne 0 ]; then
  522.     #        systemctl restart apparmor >> $LOG
  523.     #    fi
  524.     #fi
  525.     systemctl enable bind9
  526.     systemctl start bind9
  527.  
  528.     # Workaround for OpenVZ/Virtuozzo
  529.     #if [ -e "/proc/vz/veinfo" ] && [ -e "/etc/rc.local" ]; then
  530.     #    sed -i "s/^exit 0/service bind9 restart\nexit 0/" /etc/rc.local
  531.     #fi
  532. fi
  533.  
  534.  
  535. #----------------------------------------------------------#
  536. #                      Configure Exim                      #
  537. #----------------------------------------------------------#
  538.  
  539. if [ "$exim" = 'yes' ]; then
  540.     echo "(*) Configuring Exim mail server..."
  541.     gpasswd -a exim mail > /dev/null 2>&1
  542.     cp -fn $HESTIA_INSTALL_DIR/exim/exim4.conf.template /etc/exim/exim.conf.template
  543.     cp -fn $HESTIA_INSTALL_DIR/exim/dnsbl.conf /etc/exim/
  544.     cp -fn $HESTIA_INSTALL_DIR/exim/spam-blocks.conf /etc/exim/
  545.     touch /etc/exim/white-blocks.conf
  546.  
  547.     if [ "$spamd" = 'yes' ]; then
  548.         sed -i "s/#SPAM/SPAM/g" /etc/exim/exim.conf.template
  549.     fi
  550.     if [ "$clamd" = 'yes' ]; then
  551.         sed -i "s/#CLAMD/CLAMD/g" /etc/exim/exim.conf.template
  552.     fi
  553.  
  554.     chmod 640 /etc/exim/exim.conf.template
  555.     rm -rf /etc/exim/domains
  556.     mkdir -p /etc/exim/domains
  557.  
  558.     rm -fn /etc/alternatives/mta
  559.     ln -s /usr/sbin/exim /etc/alternatives/mta
  560.     systemctl disable sendmail > /dev/null 2>&1
  561.     systemctl stop sendmail > /dev/null 2>&1
  562.     systemctl disable postfix > /dev/null 2>&1
  563.     systemctl stop postfix > /dev/null 2>&1
  564.  
  565.     systemctl enable exim
  566.     systemctl start exim4 >> $LOG
  567. fi
  568.  
  569.  
  570. #----------------------------------------------------------#
  571. #                     Configure Dovecot                    #
  572. #----------------------------------------------------------#
  573.  
  574. if [ "$dovecot" = 'yes' ]; then
  575.     echo "(*) Configuring Dovecot POP/IMAP mail server..."
  576.     gpasswd -a dovecot mail > /dev/null 2>&1
  577.     cp -rf $HESTIA_INSTALL_DIR/dovecot /etc/
  578.     cp -fn $HESTIA_INSTALL_DIR/logrotate/dovecot /etc/logrotate.d/
  579.     if [ "$release" = '18.04' ]; then
  580.         rm -fn /etc/dovecot/conf.d/15-mailboxes.conf
  581.     fi
  582.     chown -R root:root /etc/dovecot*
  583.     systemctl enable dovecot
  584.     systemctl start dovecot >> $LOG
  585. fi
  586.  
  587. #----------------------------------------------------------#
  588. #                     Configure ClamAV                     #
  589. #----------------------------------------------------------#
  590.  
  591. if [ "$clamd" = 'yes' ]; then
  592.     gpasswd -a clamav mail > /dev/null 2>&1
  593.     gpasswd -a clamav exim > /dev/null 2>&1
  594.     cp -fn $HESTIA_INSTALL_DIR/clamav/clamd.conf /etc/
  595.     systemctl enable clamd
  596.     echo -ne "(*) Installing ClamAV anti-virus definitions... "
  597.     /usr/bin/freshclam >> $LOG &
  598.     BACK_PID=$!
  599.     spin_i=1
  600.     while kill -0 $BACK_PID > /dev/null 2>&1 ; do
  601.         printf "\b${spinner:spin_i++%${#spinner}:1}"
  602.         sleep 0.5
  603.     done
  604.     echo
  605.     systemctl start clamav-daemon >> $LOG
  606. fi
  607.  
  608.  
  609. #----------------------------------------------------------#
  610. #                  Configure SpamAssassin                  #
  611. #----------------------------------------------------------#
  612.  
  613. if [ "$spamd" = 'yes' ]; then
  614.     echo "(*) Configuring SpamAssassin..."
  615.     systemctl enable spamassassin > /dev/null 2>&1
  616.     #sed -i "s/ENABLED=0/ENABLED=1/" /etc/default/spamassassin
  617.     systemctl start spamassassin >> $LOG
  618.     unit_files="$(systemctl list-unit-files |grep spamassassin)"
  619.     if [[ "$unit_files" =~ "disabled" ]]; then
  620.         systemctl enable spamassassin > /dev/null 2>&1
  621.     fi
  622. fi
  623.  
  624. #----------------------------------------------------------#
  625. #                   Configure Roundcube                    #
  626. #----------------------------------------------------------#
  627.  
  628. if [ "$dovecot" = 'yes' ] && [ "$exim" = 'yes' ] && [ "$mysql" = 'yes' ]; then
  629.     echo "(*) Configuring Roundcube webmail client..."
  630.     cp -fn $HESTIA_INSTALL_DIR/roundcube/main.inc.php /etc/roundcubemail/config.inc.php
  631.     cp -fn $HESTIA_INSTALL_DIR/roundcube/config.inc.php /etc/roundcubemail/plugins/password/
  632.     cp -fn $HESTIA_INSTALL_DIR/roundcube/hestia.php /usr/share/roundcubemail/plugins/password/drivers/
  633.     touch /var/log/roundcubemail/errors
  634.     chmod 640 /etc/roundcubemail/config.inc.php
  635.     chown root:nginx /etc/roundcubemail/config.inc.php
  636.     chmod 640 /var/log/roundcubemail/errors
  637.     chown nginx:adm /var/log/roundcubemail/errors
  638.  
  639.     r="$(date +%s | sha256sum | base64 | head -c 32 ; echo)"
  640.     rcDesKey="$(openssl rand -base64 30 | tr -d "/" | cut -c1-24)"
  641.     mysql -e "CREATE DATABASE roundcube"
  642.     mysql -e "GRANT ALL ON roundcube.*
  643.        TO roundcube@localhost IDENTIFIED BY '$r'"
  644.     sed -i "s/%password%/$r/g" /etc/roundcubemail/debian-db-roundcube.php
  645.     sed -i "s/%des_key%/$rcDesKey/g" /etc/roundcubemail/config.inc.php
  646.     sed -i "s/localhost/$servername/g" /etc/roundcubemail/plugins/password/config.inc.php
  647.     mysql roundcube < /usr/share/roundcubemail/SQL/mysql.initial.sql
  648.  
  649.     # Configure webmail alias
  650.     echo "WEBMAIL_ALIAS='webmail'" >> $HESTIA/conf/hestia.conf
  651.  
  652.     # Add robots.txt
  653.     echo "User-agent: *" > /var/lib/roundcubemail/robots.txt
  654.     echo "Disallow: /" >> /var/lib/roundcubemail/robots.txt
  655.  
  656.     if [ "$nginx" = 'yes' ]; then
  657.         systemctl restart nginx >> $LOG
  658.     fi
  659. fi
  660.  
  661. #----------------------------------------------------------#
  662. #                    Configure Fail2Ban                    #
  663. #----------------------------------------------------------#
  664.  
  665. if [ "$fail2ban" = 'yes' ]; then
  666.     echo "(*) Configuring fail2ban access monitor..."
  667.     cp -rf $HESTIA_INSTALL_DIR/fail2ban /etc/
  668.     if [ "$dovecot" = 'no' ]; then
  669.         fline=$(cat /etc/fail2ban/jail.local |grep -n dovecot-iptables -A 2)
  670.         fline=$(echo "$fline" |grep enabled |tail -n1 |cut -f 1 -d -)
  671.         sed -i "${fline}s/true/false/" /etc/fail2ban/jail.local
  672.     fi
  673.     if [ "$exim" = 'no' ]; then
  674.         fline=$(cat /etc/fail2ban/jail.local |grep -n exim-iptables -A 2)
  675.         fline=$(echo "$fline" |grep enabled |tail -n1 |cut -f 1 -d -)
  676.         sed -i "${fline}s/true/false/" /etc/fail2ban/jail.local
  677.     fi
  678.     if [ "$vsftpd" = 'yes' ]; then
  679.         #Create vsftpd Log File
  680.         if [ ! -f "/var/log/vsftpd.log" ]; then
  681.             touch /var/log/vsftpd.log
  682.         fi
  683.         fline=$(cat /etc/fail2ban/jail.local |grep -n vsftpd-iptables -A 2)
  684.         fline=$(echo "$fline" |grep enabled |tail -n1 |cut -f 1 -d -)
  685.         sed -i "${fline}s/false/true/" /etc/fail2ban/jail.local
  686.     fi
  687.  
  688.     systemctl enable fail2ban
  689.     systemctl start fail2ban >> $LOG
  690. fi
  691.  
  692.  
  693. #----------------------------------------------------------#
  694. #                       Configure API                      #
  695. #----------------------------------------------------------#
  696.  
  697. if [ "$api" = 'yes' ]; then
  698.     echo "API='yes'" >> $HESTIA/conf/hestia.conf
  699. else
  700.     rm -r $HESTIA/web/api
  701.     echo "API='no'" >> $HESTIA/conf/hestia.conf
  702. fi
  703.  
  704.  
  705. #----------------------------------------------------------#
  706. #                   Configure Admin User                   #
  707. #----------------------------------------------------------#
  708.  
  709. # Deleting old admin user
  710. if [ ! -z "$(grep ^admin: /etc/passwd)" ] && [ "$force" = 'yes' ]; then
  711.     chattr -i /home/admin/conf > /dev/null 2>&1
  712.     userdel -f admin > /dev/null 2>&1
  713.     chattr -i /home/admin/conf > /dev/null 2>&1
  714.     mv -f /home/admin  $hst_backups/home/ > /dev/null 2>&1
  715.     rm -f /tmp/sess_* > /dev/null 2>&1
  716. fi
  717. if [ ! -z "$(grep ^admin: /etc/group)" ] && [ "$force" = 'yes' ]; then
  718.     groupdel admin > /dev/null 2>&1
  719. fi
  720.  
  721. # Enable sftp jail
  722. $HESTIA/bin/v-add-sys-sftp-jail > /dev/null 2>&1
  723.  
  724. # Adding Hestia admin account
  725. $HESTIA/bin/v-add-user admin $vpass $email default System Administrator
  726. $HESTIA/bin/v-change-user-shell admin nologin
  727. $HESTIA/bin/v-change-user-language admin $lang
  728.  
  729. # Configuring system IPs
  730. $HESTIA/bin/v-update-sys-ip > /dev/null 2>&1
  731.  
  732. # Get main IP
  733. ip=$(ip addr|grep 'inet '|grep global|head -n1|awk '{print $2}'|cut -f1 -d/)
  734.  
  735. # Configuring firewall
  736. if [ "$iptables" = 'yes' ]; then
  737.     $HESTIA/bin/v-update-firewall
  738. fi
  739.  
  740. # Get public IP
  741. pub_ip=$(curl --ipv4 -s https://ip.hestiacp.com/)
  742. if [ ! -z "$pub_ip" ] && [ "$pub_ip" != "$ip" ]; then
  743.     if [ -e /etc/rc.local ]; then
  744.         sed -i '/exit 0/d' /etc/rc.local
  745.     fi
  746.  
  747.     #check_rclocal=$(cat /etc/rc.local | grep "#!")
  748.     #if [ -z "$check_rclocal" ]; then
  749.     #    echo "#!/bin/sh" >> /etc/rc.local
  750.     #fi
  751.  
  752.     echo "$HESTIA/bin/v-update-sys-ip" >> /etc/rc.local
  753.     echo "exit 0" >> /etc/rc.local
  754.     chmod +x /etc/rc.local
  755.     systemctl enable rc-local
  756.     $HESTIA/bin/v-change-sys-ip-nat $ip $pub_ip > /dev/null 2>&1
  757.     ip=$pub_ip
  758. fi
  759.  
  760. # Configuring MySQL host
  761. if [ "$mysql" = 'yes' ]; then
  762.     $HESTIA/bin/v-add-database-host mysql localhost root $mpass mysql
  763. fi
  764.  
  765. # Adding default domain
  766. $HESTIA/bin/v-add-web-domain admin $servername
  767.  
  768. # Adding cron jobs
  769. export SCHEDULED_RESTART="yes"
  770. command="sudo $HESTIA/bin/v-update-sys-queue restart"
  771. $HESTIA/bin/v-add-cron-job 'admin' '*/2' '*' '*' '*' '*' "$command"
  772. systemctl restart cron
  773.  
  774. command="sudo $HESTIA/bin/v-update-sys-queue disk"
  775. $HESTIA/bin/v-add-cron-job 'admin' '15' '02' '*' '*' '*' "$command"
  776. command="sudo $HESTIA/bin/v-update-sys-queue traffic"
  777. $HESTIA/bin/v-add-cron-job 'admin' '10' '00' '*' '*' '*' "$command"
  778. command="sudo $HESTIA/bin/v-update-sys-queue webstats"
  779. $HESTIA/bin/v-add-cron-job 'admin' '30' '03' '*' '*' '*' "$command"
  780. command="sudo $HESTIA/bin/v-update-sys-queue backup"
  781. $HESTIA/bin/v-add-cron-job 'admin' '*/5' '*' '*' '*' '*' "$command"
  782. command="sudo $HESTIA/bin/v-backup-users"
  783. $HESTIA/bin/v-add-cron-job 'admin' '10' '05' '*' '*' '*' "$command"
  784. command="sudo $HESTIA/bin/v-update-user-stats"
  785. $HESTIA/bin/v-add-cron-job 'admin' '20' '00' '*' '*' '*' "$command"
  786. command="sudo $HESTIA/bin/v-update-sys-rrd"
  787. $HESTIA/bin/v-add-cron-job 'admin' '*/5' '*' '*' '*' '*' "$command"
  788.  
  789. # Enable automatic updates
  790. $HESTIA/bin/v-add-cron-hestia-autoupdate
  791.  
  792. # Building initital rrd images
  793. $HESTIA/bin/v-update-sys-rrd
  794.  
  795. # Enabling file system quota
  796. if [ "$quota" = 'yes' ]; then
  797.     $HESTIA/bin/v-add-sys-quota
  798. fi
  799.  
  800. # Set backend port
  801. $HESTIA/bin/v-change-sys-port $port
  802.  
  803. # Set default theme
  804. $HESTIA/bin/v-change-sys-theme 'default'
  805.  
  806. # Starting Hestia service
  807. systemctl enable hestia
  808. systemctl start hestia
  809. chown admin:admin $HESTIA/data/sessions
  810.  
  811. #----------------------------------------------------------#
  812. #                   Hestia Access Info                     #
  813. #----------------------------------------------------------#
  814.  
  815. # Comparing hostname and IP
  816. host_ip=$(host $servername| head -n 1 |awk '{print $NF}')
  817. if [ "$host_ip" = "$ip" ]; then
  818.     ip="$servername"
  819. fi
  820.  
  821. echo -e "\n"
  822. echo "===================================================================="
  823. echo -e "\n"
  824.  
  825. # Sending notification to admin email
  826. echo -e "Congratulations!
  827.  
  828. You have successfully installed Hestia Control Panel on your server.
  829.  
  830. Ready to get started? Log in using the following credentials:
  831.  
  832.    Admin URL:  https://$ip:$port
  833.    Username:   admin
  834.    Password:   $vpass
  835.  
  836. Thank you for choosing Hestia Control Panel to power your full stack web server,
  837. we hope that you enjoy using it as much as we do!
  838.  
  839. Please feel free to contact us at any time if you have any questions,
  840. or if you encounter any bugs or problems:
  841.  
  842. Web:     https://www.hestiacp.com/
  843. Forum:   https://forum.hestiacp.com/
  844. GitHub:  https://www.github.com/hestiacp/hestiacp
  845.  
  846. Note: Automatic updates are enabled by default. If you would like to disable them,
  847. please log in and navigate to Server > Updates to turn them off.
  848.  
  849. Help support the Hestia Contol Panel project by donating via PayPal:
  850. https://www.hestiacp.com/donate
  851. --
  852. Sincerely yours,
  853. The Hestia Control Panel development team
  854.  
  855. Made with love & pride by the open-source community around the world.
  856. " > $tmpfile
  857.  
  858. send_mail="$HESTIA/web/inc/mail-wrapper.php"
  859. cat $tmpfile | $send_mail -s "Hestia Control Panel" $email
  860.  
  861. # Congrats
  862. echo
  863. cat $tmpfile
  864. rm -f $tmpfile
  865.  
  866. # Add welcome message to notification panel
  867. $HESTIA/bin/v-add-user-notification admin 'Welcome!' 'For more information on how to use Hestia Control Panel, click on the Help icon in the top right corner of the toolbar.<br><br>Please report any bugs or issues on GitHub at<br>https://github.com/hestiacp/hestiacp/issues<br><br>Have a great day!'
  868.  
  869. echo "(!) IMPORTANT: You must logout or restart the server before continuing."
  870. echo ""
  871. if [ "$interactive" = 'yes' ]; then
  872.     echo -n " Do you want to reboot now? [Y/N] "
  873.     read reboot
  874.  
  875.     if [ "$reboot" = "Y" ] || [ "$reboot" = "y" ]; then
  876.         reboot
  877.     fi
  878. fi
  879.  
  880. # EOF
Add Comment
Please, Sign In to add comment