2017-12-14 13 views
0

"ln -s"コマンドで問題に直面していますが、centos 7 HTTPでvhostを作成する次のbashファイルがあります。CentOS 7の仮想ホストを作成するbashスクリプト

(sudo sh vhost.sh create anothersite.com my_dir)「ln -s」という出力が表示されます(/etc/httpd/sites-enabled//etc/httpd/sites-available/anothersite.com)。 .conf)、スクリプトからも分かるように、変数は同じですが、lnコマンドでは2番目の変数だけが表示されます。

のln -sは/ etc/httpdの/サイト利用可能/ $ sitesAvailabledomainの/ etc/httpdの/サイト対応/ $ sitesAvailabledomain

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

### Set default parameters 
action=$1 
domain=$2 
rootDir=$3 
owner=$(who am i | awk '{print $1}') 
email='[email protected]' 
sitesEnable='/etc/httpd/sites-enabled/' 
sitesAvailable='/etc/httpd/sites-available/' 
userDir='/var/www/' 
sitesAvailabledomain=$sitesAvailable$domain.conf 

### don't modify from here unless you know what you are doing #### 

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 $sitesAvailabledomain ]; then 
      echo -e $"This domain already exists.\nPlease Try Another one" 
      exit; 
     fi 

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

     ### create virtual host rules file 
     if ! echo " 
     <VirtualHost *:80> 
      ServerName $domain 
      ServerAlias $domain 
      DocumentRoot $rootDir 
     </VirtualHost>" > $sitesAvailabledomain 
     then 
      echo -e $"There is an ERROR creating $domain file" 
      exit; 
     else 
      echo -e $"\nNew Virtual Host Created\n" 
     fi 


     ### enable website 
     ln -s /etc/httpd/sites-available/$sitesAvailabledomain /etc/httpd/sites-enabled/$sitesAvailabledomain 

     ### restart Apache 
     /etc/init.d/httpd reload 

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


      ### restart Apache 
      service httpd restart 

      ### Delete virtual host rules files 
      rm $sitesAvailabledomain 
     fi 

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

      if [ "$deldir" == 'y' -o "$deldir" == 'Y' ]; then 
       ### Delete the directory 
       rm -rf $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 
+0

あなたのスクリプトは、cwd =/etc/httpd/sites-利用可能ですか? – niglesias

+0

私は両方を取得しますが、それぞれの中にdoubleを//入れてください '/etc/httpd/sites-available//etc/httpd/sites-available/anothersite.com.conf/etc/httpd/sites-enabled // etc/httpd /サイト利用可能/ anothersite.com.conf' その変更を修正するために 'LN -sは/ etc/httpdの/サイト利用可能/ $ sitesAvailabledomainの/ etc/httpdの/サイト対応/ $ sitesAvailabledomain' へ'ln -s/etc/httpd/sites-available $ sitesAvailabledomain/etc/httpd/sites-enabled $ sitesAvailabledomain' これは、変数' $ sitesAvailabledomain'を最後に追加する前に、それの。 –

答えて

0

ここにあなたがあなたの変数$を作成し、ということらしいですsitesAvailabledomain

sitesAvailable='/etc/httpd/sites-available/' 
sitesAvailabledomain=$sitesAvailable$domain.conf 

、それはこの時点で値= '/etc/httpd/sites-available/anothersite.com.conf' を有します。質問から$ 1 $ 2 $ 3の入力を想定します。

そして、ここでは、あなたの変数は、lnコマンドで使用します(これは - 覚えている - 値「/etc/httpd/sites-available/anothersite.com.conf」を持っている)

ln -s /etc/httpd/sites-available/$sitesAvailabledomain /etc/httpd/sites-enabled/$sitesAvailabledomain 

余分なパスで前'/etc/httpd/sites-available//etc/httpd/sites-available/anothersite.com.conf'

関連する問題