From 1189080d57bd1cacd7bad8a10314e3b0df9c9b41 Mon Sep 17 00:00:00 2001 From: lombax85 Date: Sun, 27 Mar 2016 22:43:49 +0200 Subject: [PATCH 1/3] Added the www. alias Added a www.* alias to the host. If you create the host site.com, now it can be accessed through site.com and www.site.com --- virtualhost.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/virtualhost.sh b/virtualhost.sh index 2bff5fa..4a32515 100755 --- a/virtualhost.sh +++ b/virtualhost.sh @@ -72,7 +72,7 @@ if [ "$action" == 'create' ] ServerAdmin $email ServerName $domain - ServerAlias $domain + ServerAlias www.$domain DocumentRoot $rootDir AllowOverride All From 9b6f0f0f12b1414737bb01cac4c11a236261030d Mon Sep 17 00:00:00 2001 From: Fabio Date: Mon, 17 Oct 2016 11:49:05 +0200 Subject: [PATCH 2/3] fix with directories for virtualhost-nginx without this fix, the virtual host was placed in /var/www//var/www/virtualhost --- virtualhost-nginx.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/virtualhost-nginx.sh b/virtualhost-nginx.sh index 1e6484f..b8d17f1 100644 --- a/virtualhost-nginx.sh +++ b/virtualhost-nginx.sh @@ -37,8 +37,6 @@ if [[ "$rootDir" =~ ^/ ]]; then userDir='' fi -rootDir=$userDir$rootDir - if [ "$action" == 'create' ] then ### check if domain already exists From 240549d76048b497962d713bbb36e177c8e43e04 Mon Sep 17 00:00:00 2001 From: Fabio Date: Mon, 17 Oct 2016 11:51:19 +0200 Subject: [PATCH 3/3] added an optional parameter host_alias --- README.md | 12 ++++++++++-- virtualhost-nginx.sh | 10 +++++++++- virtualhost.sh | 11 ++++++++++- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e7c0809..9a78d4c 100644 --- a/README.md +++ b/README.md @@ -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 ### @@ -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 diff --git a/virtualhost-nginx.sh b/virtualhost-nginx.sh index b8d17f1..0712ae6 100644 --- a/virtualhost-nginx.sh +++ b/virtualhost-nginx.sh @@ -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/' @@ -37,6 +38,13 @@ if [[ "$rootDir" =~ ^/ ]]; then userDir='' fi +### add an optional server alias, for example www.host.it +if [ "$serverAlias" != "" ]; then + optionalAlias=' '$serverAlias +else + optionalAlias='' +fi + if [ "$action" == 'create' ] then ### check if domain already exists @@ -66,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)$ { diff --git a/virtualhost.sh b/virtualhost.sh index 4a32515..441b098 100755 --- a/virtualhost.sh +++ b/virtualhost.sh @@ -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/' @@ -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 @@ -72,7 +81,7 @@ if [ "$action" == 'create' ] ServerAdmin $email ServerName $domain - ServerAlias www.$domain + $optionalAlias DocumentRoot $rootDir AllowOverride All