Skip to content

Instantly share code, notes, and snippets.

@bxmas13
Forked from mattmezza/vhost.sh
Last active June 11, 2019 19:09
Show Gist options
  • Save bxmas13/a8e4c235b1c7bd2491eea5ab39bff403 to your computer and use it in GitHub Desktop.
Save bxmas13/a8e4c235b1c7bd2491eea5ab39bff403 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
# Further Improved by bxmas13 from https://brianchristmas.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
#
#
# Executes 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 creates a test index.php file in /directory/you/choose/domain.com/htdocs
# Create a vhost file domain in your /etc/httpd/conf.d/ directory.
# And adds the new vhost to the hosts.
#
#
if [ "$(whoami)" != 'root' ]; then
echo "You have to execute this script as root user"
exit 1;
fi
cname="www"
dir="/var/www/"
usr="apache"
listen="*"
read -p "Enter the FQDN (without www) : " servn
read -p "Enter a CNAME ([${cname}]) : " cname
read -p "Enter the path of directory you wanna use [${dir}] (add trailing slash): " dir
read -p "Enter the user you wanna use [${usr}] : " usr
read -p "Enter the listened IP for the server [${listen}]: " listen
cname=${cname:=www}
dir=${dir:=/var/www/}
usr=${usr:=apache}
listen=${listen:=*}
if ! mkdir -p $dir$servn/htdocs; then
echo "Web directory already Exist !"
else
echo "Web directory created with success !"
fi
echo "<?php echo '<h1>$cname $servn</h1>'; ?>" > $dir$cname_$servn/htdocs/index.php
chown -R $usr:$usr $dir$servn
chmod -R '755' $dir$servn
mkdir /var/log/$servn
alias=$cname.$servn
if [[ "${cname}" == "" ]]; then
alias=$servn
fi
GLOBIGNORE=*
echo "#### $cname $servn
<VirtualHost $listen:80>
ServerAdmin webmaster@$servn
ServerName $servn
ServerAlias $alias
DocumentRoot $dir$servn/htdocs
ErrorLog /var/log/httpd/${servn}_error.log
<Directory $dir$servn/htdocs>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =$cname.$servn [OR]
RewriteCond %{SERVER_NAME} =$servn
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost $listen:443>
ServerAdmin webmaster@$servn
DocumentRoot /var/www/$servn/htdocs
ServerName $servn
ServerAlias $cname.$servn
ErrorLog /var/log/httpd/${servn}_ssl_error.log
<Directory "/var/www/$servn/htdocs">
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
</IfModule>" > /etc/httpd/conf.d/${servn}.conf
if ! echo -e /etc/httpd/conf.d/${servn}.conf; then
echo "Virtual host wasn't created !"
else
echo "Virtual host created !"
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"
apachectl configtest
echo "Would you like me to restart the server [y/n]? "
read q
if [[ "${q}" == "yes" ]] || [[ "${q}" == "y" ]]; then
systemctl restart httpd
fi
echo ""
echo "======================================"
echo "Virtual host has been created!"
echo ""
echo "You should be able to see your website at http://$servn"
echo "Config file: /etc/httpd/conf.d/${servn}.conf"
echo "======================================"
echo ""
@bxmas13
Copy link
Author

bxmas13 commented Jun 4, 2019

Fixed directory/variable issues.

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