Skip to content

Instantly share code, notes, and snippets.

@gerswin
Last active December 15, 2015 20:17
Show Gist options
  • Save gerswin/5da7e4753f55edde8e76 to your computer and use it in GitHub Desktop.
Save gerswin/5da7e4753f55edde8e76 to your computer and use it in GitHub Desktop.
ubuntu servers
#!/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
# Improved for Ubuntu by gerswin from http://gerswin.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/apache2/ssl/ 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. : dev.website.com) : " cname
cname=${cname:=servn}
read -p "Enter the path of directory you wanna use (e.g. : /var/www/, dont forget the / default /var/www/): " dir
dir=${dir:="/var/www/"}
read -p "Enter the user you wanna use (e.g. :default www-data) : " usr
usr=${usr:="www-data"}
read -p "Enter the listened IP for the server (e.g. : *): " listen
listen=${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
echo "#### $cname $servn
<VirtualHost $listen:80>
ServerName $servn
ServerAlias $cname
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/apache2/sites-available/$cname_$servn.conf
if ! echo -e /etc/apache2/sites-available/$cname_$servn.conf; then
echo "Virtual host wasn't created !"
else
echo "Virtual host created !"
fi
read -p "Would you like me to create ssl virtual host [y/n]? " q
echo q
if [ "$q" = "y" ]; then
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/$cname_$servn.key -out /etc/apache2/ssl/$cname_$servn.crt
if ! echo -e /etc/apache2/ssl/$cname_$servn.key; then
echo "Certificate key wasn't created !"
else
echo "Certificate key created !"
fi
if ! echo -e /etc/apache2/ssl/$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/apache2/ssl/$cname_$servn.crt
SSLCertificateKeyFile /etc/apache2/ssl/$cname_$servn.key
ServerName $servn
ServerAlias $cname
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/apache2/sites-available/ssl.$cname_$servn.conf
if ! echo -e /etc/apache2/sites-available/ssl.$cname_$servn.conf; then
echo "SSL Virtual host wasn't created !"
else
echo "SSL Virtual host created !"
fi
fi
sudo a2ensite $cname_$servn.conf
if [ "$q" = "y" ]; then
sudo a2ensite ssl.$cname_$servn.conf
fi
echo "127.0.0.1 $servn" >> /etc/hosts
if [ "$cname" != "$servn" ]; then
echo "127.0.0.1 $cname" >> /etc/hosts
fi
echo "Testing configuration"
apache2ctl configtest
service apache2 restart
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/gerswin/5da7e4753f55edde8e76"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment