Skip to content

Instantly share code, notes, and snippets.

@kodekracker
Last active December 10, 2017 18:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kodekracker/90b80645d8b60e99b61f to your computer and use it in GitHub Desktop.
Save kodekracker/90b80645d8b60e99b61f to your computer and use it in GitHub Desktop.
A shell script file to create a virtual host on apache server to host your website , just copy this script in your root directory of website and set the SERVER_IP,HOST_NAME variable in the file, change the executable permission and run it.This will automatically add your virtual-host and runs your website at a specified host name.
#!/bin/bash
# Author : Akshay Pratap Singh
# Email-id : pratapakshay0@gmail.com
# These variable are sets by user
SERVER_IP="127.0.0.1"
HOST_NAME="example.com"
DIR_PATH=$(pwd)
APACHE_DIR_PATH="/etc/apache2/sites-available"
FILE_DATA=$(cat <<EOF
<VirtualHost *:80>
ServerName ${HOST_NAME}
ServerAlias www.${HOST_NAME}
ServerAdmin webmaster@localhost
DocumentRoot ${DIR_PATH}
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory "${DIR_PATH}">
Options Indexes FollowSymLinks MultiViews
Order allow,deny
Allow from all
AllowOverride All
# New directive needed in Apache 2.4.3:
Require all granted
</Directory>
</VirtualHost>
EOF
)
echo "Creating conf file...."
sudo touch ${APACHE_DIR_PATH}/${HOST_NAME}.conf
sudo chmod 777 ${APACHE_DIR_PATH}/${HOST_NAME}.conf
sudo echo "${FILE_DATA}" >> ${APACHE_DIR_PATH}/${HOST_NAME}.conf
echo "Setting virtualhost...."
sudo a2ensite ${HOST_NAME}.conf > /dev/null
echo "Setting new virtual-host...."
sudo cp /etc/hosts /etc/hosts.save
sudo chmod 777 /etc/hosts
sudo echo "${SERVER_IP} ${HOST_NAME}" >> /etc/hosts
echo "Enabling Rewrite Mode...."
sudo a2enmod rewrite
echo "List of Apache Mode activated...."
sudo apache2ctl -M
echo
echo "Restarting apache2...."
sudo service apache2 restart > /dev/null
echo "All Done....!!!"
echo
echo "Goto http://$HOST_NAME , to check."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment