#!/bin/bash ###################################################################################### #### Version 2.2 #### #### For questions or comments contact@mylinux.work #### #### Author : Phil Connor #### #### #### #### Notes : #### #### This script is a simple "helper" to install and configure Maria, #### #### PowerDNS and PowerAdmin on RedHat Based servers. #### #### There is no silver bullet. Don't expect the perfect setup, #### #### review comments and adapt the parameters to your application usage. #### #### #### #### Use this script at your OWN risk. There is no guarantee whatsoever. #### #### #### #### Usage chmod 755 then ./PdnsInstall.sh or bash PdnsInstall.sh #### ###################################################################################### ######################## #### User Variables #### ######################## MYSQL_PASS='Password@123' # <-- Your MySql root Password here MY_PDNS_USR=pdns # <-- The username for your PowerDNS connect to DB MY_PDNS_DB=powerdns # <-- The name for your PowerDNS DB MY_PDNS_PW=somepassword # <-- The password you wantt for you PowerDNS DB MY_PDNS_HOST=localhost # <-- The default here is localhost, but can be set to a remote host if you have configured that DEL_MY_CNF=Y # <-- Place a Capital Y for yes or N for no here to delete /root/.my.cnf when db_instal function is done WEB_HOST_NAME=test1.linuxcomputer.cloud # <-- The FQDN of your server goes here EMAIL=admin@$WEB_HOST_NAME # <-- This is the email you want to use for Let's Encrypt registations HTTP=nginx # <-- Choose apache or nginx --> The apache Config is in BETA TESTING please only choose nginx unless you know what your doing ########################## #### System Variables #### ########################## ip4=$(/sbin/ip -o -4 addr list eth0 | awk '{print $4}' | cut -d/ -f1) host=$(hostname -f) if [ "$(command -v lsb_release)" ]; then OS=$(lsb_release -i | awk '{print $3}' | tr '[:upper:]' '[:lower:]') OSVER=$(lsb_release -r | awk '{print $2}' | awk -F. '{print $1}') else OS=$(grep PRETTY_NAME /etc/os-release | sed 's/PRETTY_NAME=//g' | tr -d '="' | awk '{print $1}' | tr '[:upper:]' '[:lower:]') OSVER=$(grep VERSION_ID /etc/os-release | sed 's/VERSION_ID=//g' | tr -d '="' | awk -F. '{print $1}') fi ########################################################### #### Detect Package Manger from OS and OSVer Variables #### ########################################################### if [[ ${OS} = alma || ${OS} = amazon || ${OS} = centos || ${OS} = red || ${OS} = rocky || ${OS} = oracle ]]; then if [ "${OSVER}" = 7 ]; then PAKMGR="yum -y" else PAKMGR="dnf -y" fi elif [ "${OS}" = ubuntu ]; then PAKMGR="apt -y" fi ########################## #### Detect Root User #### ########################## function check_RootUser() { #if [ "$(id -u)" != "0" ]; then if [ "$(whoami)" != 'root' ]; then echo "You dont have permission to run $0 as non-root user. Use sudo su -" exit 1 fi } #################### #### Code Start #### #################### ######################### #### Install MariaDB #### ######################### function install_mysql() { { if [ ! "$(command -v mysql)" ]; then if [ "${OS}" = ubuntu ]; then ${PAKMGR} update ${PAKMGR} install mariadb-client mariadb-server else ${PAKMGR} install mariadb mariadb-server fi fi systemctl enable --now mariadb } } ###################### #### Secure MySQL #### ###################### function secure_mysql() { { if [ ! "$(command -v expect)" ]; then ${PAKMGR} install expect fi expect -f - <<-EOF set timeout 10 spawn mysql_secure_installation expect "Enter current password for root (enter for none):" send -- "\r" expect "Set root password?" send -- "y\r" expect "New password:" send -- "${MYSQL_PASS}\r" expect "Re-enter new password:" send -- "${MYSQL_PASS}\r" expect "Remove anonymous users?" send -- "y\r" expect "Disallow root login remotely?" send -- "y\r" expect "Remove test database and access to it?" send -- "y\r" expect "Reload privilege tables now?" send -- "y\r" expect eof EOF } } ################################### #### Install PowerDNS DataBase #### ################################### function pdns_db_install() { { define () { IFS=$'\n' read -r -d '' "$1" } if [ ! -f /root/.my.cnf ]; then { echo '[mysql]' echo 'user=root' echo "password=$MYSQL_PASS" } >/root/.my.cnf fi mysql -e "CREATE DATABASE $MY_PDNS_DB /*\!40100 DEFAULT CHARACTER SET utf8 */;" mysql -e "CREATE USER $MY_PDNS_USR@localhost IDENTIFIED BY '$MY_PDNS_PW';" mysql -e "GRANT ALL PRIVILEGES ON $MY_PDNS_DB.* TO '$MY_PDNS_USR'@'localhost';" mysql -e "ALTER USER '$MY_PDNS_DB'@'localhost' IDENTIFIED BY '$MY_PDNS_PW';" mysql -e "FLUSH PRIVILEGES;" touch /tmp/pdns.sql OUTFILE="/tmp/pdns.sql" define PDNS_SQL << 'EOF' CREATE TABLE domains ( id INT AUTO_INCREMENT, name VARCHAR(255) NOT NULL, master VARCHAR(128) DEFAULT NULL, last_check INT DEFAULT NULL, type VARCHAR(6) NOT NULL, notified_serial INT UNSIGNED DEFAULT NULL, account VARCHAR(40) CHARACTER SET 'utf8' DEFAULT NULL, PRIMARY KEY (id) ) Engine=InnoDB CHARACTER SET 'latin1'; CREATE UNIQUE INDEX name_index ON domains(name); CREATE TABLE records ( id BIGINT AUTO_INCREMENT, domain_id INT DEFAULT NULL, name VARCHAR(255) DEFAULT NULL, type VARCHAR(10) DEFAULT NULL, content VARCHAR(64000) DEFAULT NULL, ttl INT DEFAULT NULL, prio INT DEFAULT NULL, disabled TINYINT(1) DEFAULT 0, ordername VARCHAR(255) BINARY DEFAULT NULL, auth TINYINT(1) DEFAULT 1, PRIMARY KEY (id) ) Engine=InnoDB CHARACTER SET 'latin1'; CREATE INDEX nametype_index ON records(name,type); CREATE INDEX domain_id ON records(domain_id); CREATE INDEX ordername ON records (ordername); CREATE TABLE supermasters ( ip VARCHAR(64) NOT NULL, nameserver VARCHAR(255) NOT NULL, account VARCHAR(40) CHARACTER SET 'utf8' NOT NULL, PRIMARY KEY (ip, nameserver) ) Engine=InnoDB CHARACTER SET 'latin1'; CREATE TABLE comments ( id INT AUTO_INCREMENT, domain_id INT NOT NULL, name VARCHAR(255) NOT NULL, type VARCHAR(10) NOT NULL, modified_at INT NOT NULL, account VARCHAR(40) CHARACTER SET 'utf8' DEFAULT NULL, comment TEXT CHARACTER SET 'utf8' NOT NULL, PRIMARY KEY (id) ) Engine=InnoDB CHARACTER SET 'latin1'; CREATE INDEX comments_name_type_idx ON comments (name, type); CREATE INDEX comments_order_idx ON comments (domain_id, modified_at); CREATE TABLE domainmetadata ( id INT AUTO_INCREMENT, domain_id INT NOT NULL, kind VARCHAR(32), content TEXT, PRIMARY KEY (id) ) Engine=InnoDB CHARACTER SET 'latin1'; CREATE INDEX domainmetadata_idx ON domainmetadata (domain_id, kind); CREATE TABLE cryptokeys ( id INT AUTO_INCREMENT, domain_id INT NOT NULL, flags INT NOT NULL, active BOOL, published BOOL DEFAULT 1, content TEXT, PRIMARY KEY(id) ) Engine=InnoDB CHARACTER SET 'latin1'; CREATE INDEX domainidindex ON cryptokeys(domain_id); CREATE TABLE tsigkeys ( id INT AUTO_INCREMENT, name VARCHAR(255), algorithm VARCHAR(50), secret VARCHAR(255), PRIMARY KEY (id) ) Engine=InnoDB CHARACTER SET 'latin1'; CREATE UNIQUE INDEX namealgoindex ON tsigkeys(name, algorithm); EOF { printf "%s\n" "$PDNS_SQL" | cut -c 2- } > "$OUTFILE" if [ ${DEL_MY_CNF} != N ]; then rm -rf /root/.my.cnf fi mysql -D powerdns < /tmp/pdns.sql rm /tmp/pdns.sql } } #################################### #### Install/Configure PowerDNS #### #################################### function pdns_app_install() { { if [ "${OS}" = ubuntu ]; then if systemctl is-enabled systemd-resolved; then systemctl disable --now systemd-resolved systemctl mask systemd-resolved sed -i 's/nameserver /#nameserver /g' /etc/resolv.conf echo -e 'nameserver 8.8.8.8 \nnameserver 8.8.4.4' >> /etc/resolv.conf fi DEBIAN_FRONTEND=noninteractive ${PAKMGR} install pdns-backend-mysql fpdns bind9utils else ${PAKMGR} install epel-release ${PAKMGR} install http://rpms.remirepo.net/enterprise/remi-release-8.rpm ${PAKMGR} install pdns-backend-mysql pdns bind-utils expect -f - <<-EOF set timeout 2 spawn dnf module enable php:remi-7.4 expect "Is this ok:" send -- "y\r" expect eof EOF fi echo "" >/etc/pdns/pdns.conf cat >/etc/pdns/pdns.conf <' echo " ServerAdmin admin@n$WEB_HOST_NAME" echo " ServerName $WEB_HOST_NAME" echo " DocumentRoot /var/www/html/$WEB_HOST_NAME" echo ' #DirectoryIndex index.php' echo " #ErrorLog /var/log/httpd/$WEB_HOST_NAME-error.log" echo " #CustomLog /var/log/httpd/$WEB_HOST_NAME-access.log combined" echo '' echo ' ' echo ' ' if [ "${OS}" = ubuntu ]; then echo ' SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"' else echo ' SetHandler "proxy:unix:/var/opt/remi/php74/run/php-fpm/www.sock|fcgi://localhost"' fi echo ' ' echo ' ' echo '' } > $path if [ "${OS}" = ubuntu ]; then if ! apachectl configtest; then echo '' echo -e '\e[01;37m -----------------------------------------------------------------------------------------------------' echo -e "\e[01;31m An Error was detected with apache2, Please manually look at the configuration to comfirm it's good" >&2 echo -e '\e[01;37m -----------------------------------------------------------------------------------------------------' exit 1 fi else if ! httpd -t; then echo '' echo -e '\e[01;37m ----------------------------------------------------------------------------------------------------' echo -e "\e[01;31m An Error was detected with httpd, Please manually look at the configuration to comfirm it's good" >&2 echo -e '\e[01;37m ----------------------------------------------------------------------------------------------------' exit 1 fi fi if [ "${OS}" = ubuntu ]; then systemctl enable --now php-fpm a2dissite 000-default a2ensite $WEB_HOST_NAME systemctl enable apache2 systemctl reload apache2 else chcon -R -t httpd_sys_content_t /var/www/html/$WEB_HOST_NAME systemctl enable --now php74-php-fpm systemctl enable --now httpd fi elif [ $HTTP = nginx ]; then if [ "${OS}" = ubuntu ]; then ${PAKMGR} install build-essential php php-cli php-dev php-fpm php-gd php-intl php-json php-mysql php-pear php-xml php-xmlrpc php-mbstring gettext libmcrypt-dev pecl channel-update pecl.php.net pecl update-channels expect -f - <<-EOF set timeout 10 spawn pecl install mcrypt expect "libmcrypt prefix?" send -- "\r" expect eof EOF sed -i 's/;extension=shmop/extension=mcrypt.so/g' /etc/php/*/cli/php.ini sed -i 's/;extension=shmop/extension=mcrypt.so/g' /etc/php/*/apache2/php.ini systemctl disable --now apache2 systemctl mask apache2 if ! php -m | grep mcrypt; then echo '' echo 'mcrypt did not install correctly on this Ubuntu machine...!' exit 1 systemctl disable --now httpd systemctl mask httpd fi else ${PAKMGR} install php php-fpm php-cli php-mysqlnd php-pecl-mcrypt php-json php-intl chown apache:apache /var/lib/php/sessions systemctl disable --now httpd systemctl mask httpd fi fi ${PAKMGR} install nginx if [[ ${OS} = centos || ${OS} = red || ${OS} = oracle || ${OS} = rocky || ${OS} = alma ]]; then if ! grep "listen = /run/php-fpm/www.sock" /etc/php-fpm.d/www.conf; then sed -i '/listen = */c\listen = \/run\/php-fpm\/www.sock' /etc/php-fpm.d/www.conf fi fi if [ "${OS}" = ubuntu ]; then path=/etc/nginx/sites-available/$WEB_HOST_NAME.conf else path=/etc/nginx/conf.d/$WEB_HOST_NAME.conf fi # shellcheck disable=SC2016 { echo 'server {' echo " server_name $WEB_HOST_NAME;" echo ' listen 80;' echo '' echo " root /var/www/html/$WEB_HOST_NAME;" echo " #access_log /var/log/nginx/$WEB_HOST_NAME-access_log;" echo " #error_log /var/log/nginx/$WEB_HOST_NAME-error_log;" echo '' echo ' index index.php;' echo '' echo ' location / {' echo ' try_files $uri $uri/ /index.php?query_string;' echo ' }' echo '' echo ' location ~ \.php$ {' echo ' fastcgi_index index.php;' echo ' fastcgi_split_path_info ^(.+\.php)(.*)$;' echo ' fastcgi_keep_conn on;' echo ' include /etc/nginx/fastcgi_params;' if [ "${OS}" = ubuntu ]; then echo ' fastcgi_pass unix:/run/php/php-fpm.sock;' else echo ' fastcgi_pass unix:/run/php-fpm/www.sock;' fi echo ' fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;' echo ' }' echo '' echo ' location ~/\.ht {' echo ' deny all;' echo ' }' echo '' echo '}' } > $path if ! nginx -t; then echo '' echo -e '\e[01;37m --------------------------------------------------------------------------------------------------' echo -e "\e[01;31m An Error was detected with nginx, Please manually look at the configuration to comfirm it's good" >&2 echo -e '\e[01;37m --------------------------------------------------------------------------------------------------' exit 1 fi if [ "${OS}" = ubuntu ]; then rm /etc/nginx/sites-enabled/default ln -s /etc/nginx/sites-available/$WEB_HOST_NAME.conf /etc/nginx/sites-enabled/$WEB_HOST_NAME systemctl enable --now php-fpm systemctl enable nginx systemctl start nginx else systemctl enable --now php-fpm systemctl enable nginx chcon -R -t httpd_sys_content_t /var/www/html/$WEB_HOST_NAME systemctl start nginx fi } } ############################ #### Install PowerAdmin #### ############################ function pdns_admin_install() { { if [ ! -d /var/www/html/$WEB_HOST_NAME ]; then mkdir -p /var/www/html/$WEB_HOST_NAME fi ${PAKMGR} install git cd /var/www/html/$WEB_HOST_NAME || exit git clone https://github.com/poweradmin/poweradmin.git mv poweradmin/* . rm -rf poweradmin/ find /var/www/html/$WEB_HOST_NAME/ -type d -exec chmod 755 {} \; find /var/www/html/$WEB_HOST_NAME/ -type f -exec chmod 644 {} \; if [ "${OS}" = ubuntu ]; then chown www-data:www-data /var/www/html/$WEB_HOST_NAME/ else chown apache:apache /var/www/html/$WEB_HOST_NAME/ chown apache:apache /var/lib/php/session fi } } ########################################## #### Install Certbot and request Cert #### ########################################## install_certbot() { { if [ $HTTP = apache ]; then ${PAKMGR} install python3-certbot-apache systemctl enable --now httpd elif [ $HTTP = nginx ]; then ${PAKMGR} install python3-certbot-nginx systemctl enable --now nginx fi ################################################################################################# #### Be sure that your domain has the proper dns entry or this will not work. #### #### #### #### If your domain is not properly configured and you know it, or you just wanna #### #### test that you can get a cert uncomment this line #### #### #### #### certbot certonly --redirect --agree-tos --nginx -d $WEB_HOST_NAME -m "$EMAIL" --dry-run #### #### #### and comment out this line #### #### certbot --non-interactive --redirect --agree-tos -d $WEB_HOST_NAME -m "$EMAIL" #### ################################################################################################# if [ $HTTP = apache ]; then certbot certonly --redirect --agree-tos --apache -d $WEB_HOST_NAME -m "$EMAIL" --dry-run -v #certbot --non-interactive --redirect --agree-tos --apache -d $WEB_HOST_NAME -m "$EMAIL" systemctl restart httpd elif [ $HTTP = nginx ]; then certbot certonly --redirect --agree-tos --nginx -d $WEB_HOST_NAME -m "$EMAIL" --dry-run -v #certbot --non-interactive --redirect --agree-tos --nginx -d $WEB_HOST_NAME -m "$EMAIL" systemctl restart nginx fi if [ "${OS}" = ubuntu ]; then if ! grep "certbot" /var/spool/cron/crontab/root; then echo "0 */12 * * * root certbot -q renew" >>/etc/crontab fi else if ! grep "certbot" /var/spool/cron/root; then echo "0 */12 * * * root certbot -q renew" >>/var/spool/cron/root fi fi } } ####################### #### Final Message #### ####################### function install_complete() { { if [ ! -d /etc/letsencrypt/live/$WEB_HOST_NAME ]; then echo -e '\e[01;37m ----------------------------------------------------------------------------------------------------' echo -e '\e[01;37m -----------------------------------------------------------------------------------------------------------' echo -e "\e[01;32m You should now be able to complete the Poweradmin setup by accessing it here http://$host/install/ or by ip http://$ip4/install/" echo -e '\e[01;37m -----------------------------------------------------------------------------------------------------------' echo -e '\e[01;37m ----------------------------------------------------------------------------------------------------' else echo -e '\e[01;37m ----------------------------------------------------------------------------------------------------' echo -e '\e[01;37m -----------------------------------------------------------------------------------------------------------' echo -e "\e[01;32m You should now be able to complete the Poweradmin setup by accessing it here https://$host/install/ or by ip https://$ip4/install/" echo -e '\e[01;37m -----------------------------------------------------------------------------------------------------------' echo -e '\e[01;37m ----------------------------------------------------------------------------------------------------' fi } } ################## #### Code End #### ################## check_RootUser install_mysql secure_mysql pdns_db_install pdns_app_install webserver_install pdns_admin_install install_certbot install_complete mysql -e "CREATE DATABASE powerdns /*\!40100 DEFAULT CHARACTER SET utf8 */;" mysql -e "CREATE USER pdns@localhost IDENTIFIED BY '$MY_PDNS_PW';" mysql -e "GRANT ALL PRIVILEGES ON powerdns.* TO 'pdns'@'localhost';" # mysql -e "ALTER USER 'pdns'@'localhost' IDENTIFIED BY 'linda6!3!';" mysql -e "FLUSH PRIVILEGES;" 6xt3gXm?+5D6