Skip to content

Instantly share code, notes, and snippets.

@hadifarnoud
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadifarnoud/1f57bd90f671c0dbc45d to your computer and use it in GitHub Desktop.
Save hadifarnoud/1f57bd90f671c0dbc45d to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script is used for create virtual hosts on CentOs.
# Created by alexnogard from http://alexnogard.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>$2</h1>'; ?>" > $cname.$servn/index.php
chown -R $usr:$usr $cname.$servn/
chmod -R '755' $cname.$servn
mkdir /var/log/$cname.$servn/
echo "<VirtualHost $listen:80>
ServerName $servn
ServerAlias $cname.$servn
DocumentRoot $dir$cname.$servn
<Directory $homedir$cname.$servn>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</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 127.0.0.1 $servn >> /etc/hosts
echo 127.0.0.1 $cname.$servn >> /etc/hosts
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment