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 1e6484f..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,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 @@ -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)$ { diff --git a/virtualhost.sh b/virtualhost.sh index 2bff5fa..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 $domain + $optionalAlias DocumentRoot $rootDir AllowOverride All