NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#!/bin/bash
### Set Language
TEXTDOMAIN=virtualhost

### Set default parameters
action=$1
domain=$2
rootDir=$3
owner=$(who am i | awk '{print $1}')
sitesEnable='/etc/nginx/sites-enabled/'
sitesAvailable='/etc/nginx/sites-available/'
userDir='/var/www/'

if [ "$(whoami)" != 'root' ]; then
echo $"You have no permission to run $0 as non-root user. Use sudo"
exit 1;
fi

if [ "$action" != 'create' ] && [ "$action" != 'delete' ]
then
echo $"You need to prompt for action (create or delete) -- Lower-case only"
exit 1;
fi

while [ "$domain" == "" ]
do
echo -e $"Please provide domain. e.g.dev,staging"
read domain
done

if [ "$rootDir" == "" ]; then
rootDir=${domain//./}
fi

### if root dir starts with '/', don't use /var/www as default starting point
if [[ "$rootDir" =~ ^/ ]]; then
userDir=''
fi

rootDir=$userDir$rootDir

if [ "$action" == 'create' ]
then
### check if domain already exists
if [ -e $sitesAvailable$domain ]; then
echo -e $"This domain already exists.nPlease Try Another one"
exit;
fi

### check if directory exists or not
if ! [ -d $userDir$rootDir ]; then
### create the directory
mkdir $userDir$rootDir
### give permission to root dir
chmod -R a+rx $userDir$rootDir
### write test file in the new domain dir
if ! echo "<?php echo phpinfo(); ?>" > $userDir$rootDir/phpinfo.php
then
echo $"ERROR: Not able to write in file $userDir/$rootDir/phpinfo.php. Please check permissions."
exit;
else
echo $"Added content to $userDir$rootDir/phpinfo.php."
fi
fi

### create virtual host rules file
if ! echo "server {
listen 80;
root $userDir$rootDir;
index index.php index.html index.htm;
server_name $domain;

# serve static files directly
location ~* .(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}

# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}

# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}

# removes trailing 'index' from all controllers
if ($request_uri ~* index/?$) {
rewrite ^/(.*)/index/?$ /$1 permanent;
}

# catch all
error_page 404 /index.php;

location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}

location ~ /.ht {
deny all;
}

}" > $sitesAvailable$domain
then
echo -e $"There is an ERROR create $domain file"
exit;
else
echo -e $"nNew Virtual Host Createdn"
fi

### Add domain in /etc/hosts
if ! echo "127.0.0.1 $domain" >> /etc/hosts
then
echo $"ERROR: Not able write in /etc/hosts"
exit;
else
echo -e $"Host added to /etc/hosts file n"
fi

if [ "$owner" == "" ]; then
chown -R $(whoami):www-data $userDir$rootDir
else
chown -R $owner:www-data $userDir$rootDir
fi

### enable website
ln -s $sitesAvailable$domain $sitesEnable$domain

### restart Nginx
service nginx restart

### show the finished message
echo -e $"Complete! nYou now have a new Virtual Host nYour new host is: http://$domain nAnd its located at $userDir$rootDir"
exit;
else
### check whether domain already exists
if ! [ -e $sitesAvailable$domain ]; then
echo -e $"This domain dont exists.nPlease Try Another one"
exit;
else
### Delete domain in /etc/hosts
newhost=${domain//./\.}
sed -i "/$newhost/d" /etc/hosts

### disable website
rm $sitesEnable$domain

### restart Nginx
service nginx restart

### Delete virtual host rules files
rm $sitesAvailable$domain
fi

### check if directory exists or not
if [ -d $userDir$rootDir ]; then
echo -e $"Delete host root directory ? (s/n)"
read deldir

if [ "$deldir" == 's' -o "$deldir" == 'S' ]; then
### Delete the directory
rm -rf $userDir$rootDir
echo -e $"Directory deleted"
else
echo -e $"Host directory conserved"
fi
else
echo -e $"Host directory not found. Ignored"
fi

### show the finished message
echo -e $"Complete!nYou just removed Virtual Host $domain"
exit 0;
fi
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.