在 Mac OSX 10.6 中向 etc/hosts 和 Apache httpd-vhosts.conf 添加新行的 Shell 脚本

发布于 2024-11-05 12:57:30 字数 762 浏览 0 评论 0原文

我正在使用 Mac OSX 10.6 并在其上进行 Web 开发。我对编写 shell 脚本有一点了解,但目前还不太精通。

我想要做的是编写一个 shell 脚本,该脚本将简单地请求本地站点别名和文档目录,然后将新别名附加到主机上,并在新行上添加“127.0.0.1 mysite.local”之类的内容在 etc/hosts 的底部。

然后该脚本将在 Apache 的 httpd-vhosts.conf 文件中添加如下内容:

<VirtualHost *:80>
    DocumentRoot "/Repositories/myproject/mysite.com/trunk/htdocs"
    ServerName mysite.local
    ServerAlias mysite.localhost
</VirtualHost>

然后它最终会运行命令来重新启动我的 Apache 服务器。现在我知道了重启 Apache 的终端命令,这很简单。我还知道如何从运行脚本的用户那里读取站点名称和路径。如下所示:

#!/bin/bash
read -p "New local site name: " site
read -p "Site path (ex:/Repositories/myproject/mysite.com/trunk/htdocs): " sitepath

我不知道该怎么做是将文本从终端附加到文件中。

有什么想法或有用的想法吗?

谢谢, 帕特里克

I am using Mac OSX 10.6 and doing web development on it. I know a small amount about writing shell scripts, but I am not really versed in them as of yet.

What I would like to do is to write a shell script that will simply ask for a local site alias and the document directory and it will then append the new alias onto hosts with something like "127.0.0.1 mysite.local" on a new line at the bottom of etc/hosts.

Then the script would append Apache's httpd-vhosts.conf file with something like this:

<VirtualHost *:80>
    DocumentRoot "/Repositories/myproject/mysite.com/trunk/htdocs"
    ServerName mysite.local
    ServerAlias mysite.localhost
</VirtualHost>

Then it would finally run the command to restart my Apache server. Now I know the terminal command to restart Apache, that is simple enough. I also know how to read in the site name and path from the user running the script. Such as below:

#!/bin/bash
read -p "New local site name: " site
read -p "Site path (ex:/Repositories/myproject/mysite.com/trunk/htdocs): " sitepath

What I don't know how to do is to append text to a file from terminal.

Any thoughts or helpful ideas?

Thanks,
Patrick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怂人 2024-11-12 12:57:30

未经测试,但它应该可以工作:

#!/bin/bash
read -p "New local site name: " SITE
read -p "Site path (ex:/Repositories/myproject/mysite.com/trunk/htdocs): " SITEPATH

#/etc/hosts
cp /etc/hosts /etc/hosts.original
echo -e "127.0.0.1\t${SITE}.local" >> /etc/hosts

#httpd-vhosts.conf
VHOSTSFILE="/etc/apache2/httpd-vhosts.conf"
cp $VHOSTSFILE ${VHOSTSFILE}.original
echo "<VirtualHost *:80>" >> $VHOSTSFILE
echo -e "\tDocumentRoot \"${SITEPATH}\"" >> $VHOSTSFILE
echo -e "\tServerName ${SITE}.local" >> $VHOSTSFILE
echo -e "\tServerAlias ${SITE}.localhost" >> $VHOSTSFILE
echo '</VirtualHost>' >> $VHOSTSFILE

#restart apache

>> 将输出重定向到给定文件,并将内容附加到文件中。我还使用 -e 来允许 \t 扩展为制表符。

请注意,您需要使用 sudo 运行此脚本。我还添加了在修改原始文件之前备份原始文件的命令,以防万一。

Untested, but it should work:

#!/bin/bash
read -p "New local site name: " SITE
read -p "Site path (ex:/Repositories/myproject/mysite.com/trunk/htdocs): " SITEPATH

#/etc/hosts
cp /etc/hosts /etc/hosts.original
echo -e "127.0.0.1\t${SITE}.local" >> /etc/hosts

#httpd-vhosts.conf
VHOSTSFILE="/etc/apache2/httpd-vhosts.conf"
cp $VHOSTSFILE ${VHOSTSFILE}.original
echo "<VirtualHost *:80>" >> $VHOSTSFILE
echo -e "\tDocumentRoot \"${SITEPATH}\"" >> $VHOSTSFILE
echo -e "\tServerName ${SITE}.local" >> $VHOSTSFILE
echo -e "\tServerAlias ${SITE}.localhost" >> $VHOSTSFILE
echo '</VirtualHost>' >> $VHOSTSFILE

#restart apache

>> redirects the output to the given file, appending the contents to the file. I’m also using -e to allow \t to be expanded to a tab character.

Note that you need to run this script with sudo. I've also included commands to backup the original files before modifying them, just in case.

倾城月光淡如水﹏ 2024-11-12 12:57:30

我在上面的答案中做了一些调整并做了一些额外的事情,因为这对我不起作用,但它帮助我提出了另一个解决方案。此答案仅适用于 Mac 用户。

首先进入您的 /etc/apache2/httpd.conf 并取消注释虚拟主机引用,即这一行 Include /private/etc/apache2/extra/httpd-vhosts.conf

现在创建一个 bash 文件,我在用户的家中创建了一个名为 imran 的文件,您需要将其替换为您的用户名

我将其放置在 /Users/imran 中,命名为 create_new_site.sh。我给了它可执行权限,因此可以使用 chmod +x create_new_site.sh 轻松执行它

脚本代码如下:

#!/bin/bash
read -p "New local site name (prefix to .local): " SITE
SITEPATH=$SITE

sudo chmod -R a+w /Users/imran/Sites/web

SITEPATH="/Users/imran/Sites/web/${SITEPATH}"

mkdir -p $SITEPATH

#/etc/hosts
cp /etc/hosts /etc/hosts.original
echo -e "127.0.0.1\t${SITE}.local" >> /etc/hosts

#httpd-vhosts.conf
VHOSTSFILE="/etc/apache2/extra/httpd-vhosts.conf"
cp $VHOSTSFILE ${VHOSTSFILE}.original
echo "<VirtualHost *:80>" >> $VHOSTSFILE
echo -e "\tDocumentRoot \"${SITEPATH}\"" >> $VHOSTSFILE
echo -e "\tServerName ${SITE}.local" >> $VHOSTSFILE
echo '</VirtualHost>' >> $VHOSTSFILE

echo '<?php phpinfo();' > "${SITEPATH}/phpinfo.php"

sudo chmod -R a+w $SITEPATH

#restart apache
sudo apachectl restart

echo "All done! visit, let's visit http://${SITE}.local/phpinfo.php"

完成后,您可以开始使用 sudo 创建新站点。 /create_new_site.sh。请记住,您需要位于主目录中,可以通过 cd ~ 命令进入该目录。现在假设您创建了一个名为 test 的站点。您应该能够访问 http://test.local/phpinfo.php 查看您的 虚拟主机工作。

I made some tweaks and did some extra stuff in the above answer, because this didn't work for me but it helped me to come up with another solution. This answer is only for Mac users.

First go in your /etc/apache2/httpd.conf and uncomment virtual host reference, which is this line Include /private/etc/apache2/extra/httpd-vhosts.conf

Now create a bash file I did that in my user’s home named as imran, you need to replace it with your username.

I placed it inside /Users/imran named as create_new_site.sh. I gave it executeable permissions, so it can be easily executed using chmod +x create_new_site.sh

Code for the script is as below:

#!/bin/bash
read -p "New local site name (prefix to .local): " SITE
SITEPATH=$SITE

sudo chmod -R a+w /Users/imran/Sites/web

SITEPATH="/Users/imran/Sites/web/${SITEPATH}"

mkdir -p $SITEPATH

#/etc/hosts
cp /etc/hosts /etc/hosts.original
echo -e "127.0.0.1\t${SITE}.local" >> /etc/hosts

#httpd-vhosts.conf
VHOSTSFILE="/etc/apache2/extra/httpd-vhosts.conf"
cp $VHOSTSFILE ${VHOSTSFILE}.original
echo "<VirtualHost *:80>" >> $VHOSTSFILE
echo -e "\tDocumentRoot \"${SITEPATH}\"" >> $VHOSTSFILE
echo -e "\tServerName ${SITE}.local" >> $VHOSTSFILE
echo '</VirtualHost>' >> $VHOSTSFILE

echo '<?php phpinfo();' > "${SITEPATH}/phpinfo.php"

sudo chmod -R a+w $SITEPATH

#restart apache
sudo apachectl restart

echo "All done! visit, let's visit http://${SITE}.local/phpinfo.php"

Once that is done you can start creating new sites using sudo ./create_new_site.sh. Remember that you need to be in your home directory, which you can go via cd ~ command. Now let's suppose you created a site with name test. You should be able to visit http://test.local/phpinfo.php to see your vhost working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文