Answers for "apache vhost creation automation bash script"

0

apache vhost creation automation bash script

#!/bin/bash
# Update $ip, $tld, $fqdn, $dnsmasqServ, $usrgrp
# To qualify for your internal structure
#
# This change will ssh into server where dnsmasq resides
# Take note, to make SSH work into your DNSMasq server
# that you have SSH key installed from the server you 
# are creating the new vhosts for
#
# This structure uses apache 2.4 in centos 8
#

ip="xxx.xxx.xxx.xxx"
tld=".lan"
fqdn="hostname.${tld}"
dnsmasqServ="services.${fqdn}"
usrgrp="$USER:apache"
vardir="/var/www/"


if [ "$(whoami)" != 'root' ]; then
echo "You have to execute this script as root user"
exit 1;
fi
read -p "Enter the server name your want (without www) : " servn
servn="${servn}.${fqdn}"
if ! mkdir -p /var/www/$servn/public_html; then
echo "Web directory already Exist !"
else
echo "Web directory created with success !"
fi
echo "<?php echo '<h1>$servn</h1>'; ?>" > $servn/public_html/index.php
chown -R $usergrp /var/www/$servn
chmod -R '755' /var/www/$servn
mkdir /var/log/$servn
alias=$servn

echo "#### $servn
<VirtualHost *:80>
ServerName $servn
ServerAlias $servn
DocumentRoot /var/www/$servn/public_html
<Directory $servn>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>" > /etc/httpd/sites-available/$servn.conf
if ! echo -e /etc/httpd/sites-available/$servn.conf; then
echo "Virtual host wasn't created !"
else
echo "Virtual host created !"
ln -s /etc/httpd/sites-available/$servn.conf /etc/httpd/sites-enabled/$servn.conf
fi
echo "${ip} $servn" | ssh root@$dnsmasqServ -T "cat >> /etc/hosts"
ssh root@$dnsmasqServ -T "systemctl restart dnsmasq.service"
echo "Testing configuration"
service httpd configtest
echo "Would you like me to restart the server [y/n]? "
read q
if [[ "${q}" == "yes" ]] || [[ "${q}" == "y" ]]; then
service httpd restart
fi
Posted by: Guest on August-09-2021

Code answers related to "Shell/Bash"

Browse Popular Code Answers by Language