Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the www. alias #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ if you copy it without the .sh extension:

Basic command line syntax:

$ sudo sh /path/to/virtualhost.sh [create | delete] [domain] [optional host_dir]
$ sudo sh /path/to/virtualhost.sh [create | delete] [domain] [optional host_dir] [optional host_alias]

With script installed on /usr/local/bin

$ sudo virtualhost [create | delete] [domain] [optional host_dir]
$ sudo virtualhost [create | delete] [domain] [optional host_dir] [optional host_alias]


### Examples ###
Expand All @@ -44,6 +44,14 @@ to create a new virtual host with custom directory name:

$ sudo virtualhost create anothersite.dev my_dir

to create a new virtual host with a www alias (but no custom directory name):

$ sudo virtualhost create anothersite.dev '' www.anothersite.dev

to create a new virtual host with custom directory name and a www alias:

$ sudo virtualhost create anothersite.dev my_dir www.anothersite.dev

to delete a virtual host

$ sudo virtualhost delete mysite.dev
Expand Down
10 changes: 8 additions & 2 deletions virtualhost-nginx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ TEXTDOMAIN=virtualhost
action=$1
domain=$2
rootDir=$3
serverAlias=$4
owner=$(who am i | awk '{print $1}')
sitesEnable='/etc/nginx/sites-enabled/'
sitesAvailable='/etc/nginx/sites-available/'
Expand Down Expand Up @@ -37,7 +38,12 @@ if [[ "$rootDir" =~ ^/ ]]; then
userDir=''
fi

rootDir=$userDir$rootDir
### add an optional server alias, for example www.host.it
if [ "$serverAlias" != "" ]; then
optionalAlias=' '$serverAlias
else
optionalAlias=''
fi

if [ "$action" == 'create' ]
then
Expand Down Expand Up @@ -68,7 +74,7 @@ if [ "$action" == 'create' ]
listen 80;
root $userDir$rootDir;
index index.php index.html index.htm;
server_name $domain;
server_name $domain$optionalAlias;

# serve static files directly
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
Expand Down
11 changes: 10 additions & 1 deletion virtualhost.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ TEXTDOMAIN=virtualhost
action=$1
domain=$2
rootDir=$3
serverAlias=$4

owner=$(who am i | awk '{print $1}')
email='webmaster@localhost'
sitesEnable='/etc/apache2/sites-enabled/'
Expand Down Expand Up @@ -43,6 +45,13 @@ fi

rootDir=$userDir$rootDir

### add an optional server alias, for example www.host.it
if [ "$serverAlias" != "" ]; then
optionalAlias='ServerAlias '$serverAlias
else
optionalAlias=''
fi

if [ "$action" == 'create' ]
then
### check if domain already exists
Expand Down Expand Up @@ -72,7 +81,7 @@ if [ "$action" == 'create' ]
<VirtualHost *:80>
ServerAdmin $email
ServerName $domain
ServerAlias $domain
$optionalAlias
DocumentRoot $rootDir
<Directory />
AllowOverride All
Expand Down