Skip to content

Instantly share code, notes, and snippets.

@mattmezza
Last active November 30, 2023 08:30
Show Gist options
  • Star 41 You must be signed in to star a gist
  • Fork 29 You must be signed in to fork a gist
  • Save mattmezza/2e326ba2f1352a4b42b8 to your computer and use it in GitHub Desktop.
Save mattmezza/2e326ba2f1352a4b42b8 to your computer and use it in GitHub Desktop.
bash script to create virtual host vhost with apache httpd and CentOs 7
#!/bin/bash
# This script is used for create virtual hosts on CentOs.
# Created by alexnogard from http://alexnogard.com
# Improved by mattmezza from http://you.canmakethat.com
# Feel free to modify it
# PARAMETERS
#
# $usr - User
# $dir - directory of web files
# $servn - webserver address without www.
# $cname - cname of webserver
# EXAMPLE
# Web directory = /var/www/
# ServerName = domain.com
# cname = devel
#
#
# Check if you execute the script as root user
#
# This will check if directory already exist then create it with path : /directory/you/choose/domain.com
# Set the ownership, permissions and create a test index.php file
# Create a vhost file domain in your /etc/httpd/conf.d/ directory.
# And add the new vhost to the hosts.
#
#
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
read -p "Enter a CNAME (e.g. :www or dev for dev.website.com) : " cname
read -p "Enter the path of directory you wanna use (e.g. : /var/www/, dont forget the /): " dir
read -p "Enter the user you wanna use (e.g. : apache) : " usr
read -p "Enter the listened IP for the server (e.g. : *): " listen
if ! mkdir -p $dir$cname_$servn; then
echo "Web directory already Exist !"
else
echo "Web directory created with success !"
fi
echo "<?php echo '<h1>$cname $servn</h1>'; ?>" > $dir$cname_$servn/index.php
chown -R $usr:$usr $dir$cname_$servn
chmod -R '755' $dir$cname_$servn
mkdir /var/log/$cname_$servn
alias=$cname.$servn
if [[ "${cname}" == "" ]]; then
alias=$servn
fi
echo "#### $cname $servn
<VirtualHost $listen:80>
ServerName $servn
ServerAlias $alias
DocumentRoot $dir$cname_$servn
<Directory $dir$cname_$servn>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>" > /etc/httpd/conf.d/$cname_$servn.conf
if ! echo -e /etc/httpd/conf.d/$cname_$servn.conf; then
echo "Virtual host wasn't created !"
else
echo "Virtual host created !"
fi
echo "Would you like me to create ssl virtual host [y/n]? "
read q
if [[ "${q}" == "yes" ]] || [[ "${q}" == "y" ]]; then
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/httpd/conf.d/$cname_$servn.key -out /etc/httpd/conf.d/$cname_$servn.crt
if ! echo -e /etc/httpd/conf.d/$cname_$servn.key; then
echo "Certificate key wasn't created !"
else
echo "Certificate key created !"
fi
if ! echo -e /etc/httpd/conf.d/$cname_$servn.crt; then
echo "Certificate wasn't created !"
else
echo "Certificate created !"
fi
echo "#### ssl $cname $servn
<VirtualHost $listen:443>
SSLEngine on
SSLCertificateFile /etc/httpd/conf.d/$cname_$servn.crt
SSLCertificateKeyFile /etc/httpd/conf.d/$cname_$servn.key
ServerName $servn
ServerAlias $alias
DocumentRoot $dir$cname_$servn
<Directory $dir$cname_$servn>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
Satisfy Any
</Directory>
</VirtualHost>" > /etc/httpd/conf.d/ssl.$cname_$servn.conf
if ! echo -e /etc/httpd/conf.d/ssl.$cname_$servn.conf; then
echo "SSL Virtual host wasn't created !"
else
echo "SSL Virtual host created !"
fi
fi
echo "127.0.0.1 $servn" >> /etc/hosts
if [ "$alias" != "$servn" ]; then
echo "127.0.0.1 $alias" >> /etc/hosts
fi
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
echo "======================================"
echo "All works done! You should be able to see your website at http://$servn"
echo ""
echo "Share the love! <3"
echo "======================================"
echo ""
echo "Wanna contribute to improve this script? Found a bug? https://gist.github.com/mattmezza/2e326ba2f1352a4b42b8"
@fredbradley
Copy link

A fantastic time saver... thank you!

@AsterAI
Copy link

AsterAI commented Dec 18, 2015

Thank you! Bro.

@diaspar
Copy link

diaspar commented May 2, 2016

@mattmezza What do you think we have to change to run this on Ubuntu?

@mattmezza
Copy link
Author

@fredbradley @AsterAI you are welcome! Hope this saved a couple of minutes at least :-)

@diaspar actually I am on this page to adapt the script on Ubuntu :-) I will test it now, then I will create the Ubuntu version

@mattmezza
Copy link
Author

@vediansoft
Copy link

vediansoft commented Jul 1, 2021

#!/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

My take on this script, thank you very much. I needed an example to do this quickly.

Changes I made is to update the DNSMasq server and reboot it for internal use-case.

Since our internal configuration is always like this I removed a bunch of questions to shorten the time I need to generate a new host.

To add: This is NOT for production, only for internal/local development/staging/acceptation server in the office.

@Downes
Copy link

Downes commented Feb 8, 2022

Edited to work on Ubuntu and Apache2

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