linux - 在 centos 7 中创建虚拟主机的 bash 脚本

标签 linux bash apache awk

我有以下在 centos 7 HTTP 中创建虚拟主机的 bash 文件,我在“ln -s”命令中遇到了问题。

如果我运行(sudo sh vhost.sh create anothersite.com my_dir),我会看到“ln -s”输出(/etc/httpd/sites-enabled//etc/httpd/sites-available/anothersite.com。 conf),正如您从脚本中看到的,变量是相同的,但在 ln 命令中,只有第二个变量显示它。

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

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

### Set default parameters
action=$1
domain=$2
rootDir=$3
owner=$(who am i | awk '{print $1}')
email='webmaster@localhost'
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

最佳答案

看来,您在这里创建变量 $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'

关于linux - 在 centos 7 中创建虚拟主机的 bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47820552/

相关文章:

linux - 如何默认在 docker 容器中运行 iptables 脚本

java - 无法识别的选项 : - Could not create the Java virtual machine

bash - 计算 bash 中的功率

apache - 如何在 Coldfusion8 (Apache 2.2) 中清除缓存和缓存的模板页面

apache - 如何使用 Apache WebDAV 将 Subversion 限制为特定的 URL?

linux - 如何阻止访问者直接访问我网站中的目录?

php - move_uploaded_file() 函数不会将上传的文件保存在服务器上

linux - bash:显示绝对路径,并在提示中解析符号链接(symbolic link)

bash - 意外标记附近的语法错误 'elif'

linux - Bash 脚本并行处理有限数量的命令