httpd 本地虚拟主机示例
httpd local virtualhost example
-- 原文post ---
我想设置一个开发环境来使用一些 apache 功能。我在 Fedora 上 运行 httpd。
我添加到主机本地重定向
# cat /etc/hosts
127.0.0.1 example1.com
127.0.0.1 example2.com
# cmkdir /var/www/example1; echo "Hello from /var/www/example1/index.html" > /var/www/example1/index.html
# cmkdir /var/www/example2; echo "Hello from /var/www/example2/index.html" > /var/www/example2/index.html
# cmkdir /var/www/example2/sub ; echo "Hello from /var/www/example2/sub/index.html" > /var/www/example2/sub/index.html
# cvi /etc/httpd/conf/httpd.conf
<VirtualHost _default_:80>
DocumentRoot "/var//www/html"
</VirtualHost>
<VirtualHost 127.0.0.1:80>
DocumentRoot "/var/www/example1"
ServerName example1.com
</VirtualHost>
<VirtualHost 127.0.0.1:80>
DocumentRoot "/var/www/example2"
ServerName example2.com
</VirtualHost>
<VirtualHost 127.0.0.1:80>
DocumentRoot "/var/www/example2/sub"
ServerName sub.example2.com
ServerPath "/sub/"
RewriteEngine On
RewriteRule "^(/sub/.*)" "/var/www/example2"
</VirtualHost>
# capachectl -t ; apachectl restart
# curl localhost
Hello from /var/www/html/index.html
# curl example1.com
Hello from /var/www/example1/index.html
# curl example2.com
Hello from /var/www/example2/index.html
# curl sub.example2.com
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
... ( lots of stuff different from the one i echoed) ...
如果我在本地 firefox 中做同样的事情 - localhost 按预期工作,example1.com 按预期工作,但 sub.example2.com 将我重定向到 example2.com.
你能帮我弄清楚如何配置本地子域吗?什么东西少了?基于https://httpd.apache.org/docs/2.4/vhosts/examples.html我相信我所做的是正确的。
-- 编辑/更新 ---
如果我按照新手的以下建议只更改重写规则,而不对上面的设置进行任何其他更改:
<VirtualHost 127.0.0.1:80>
DocumentRoot "/var/www/example2/sub"
ServerName sub.example2.com
ServerPath "/sub/"
# RewriteEngine On
# RewriteRule "^(/sub/.*)" "/var/www/example2"
</VirtualHost>
我得到:
# curl sub.example2.com
curl: (6) Could not resolve host: sub.example2.com
如果我
# cat /etc/hosts | grep sub
127.0.0.1 example2.com sub.example2.com
它按预期工作
#curl example2.com
Hello from /var/www/example2/index.html
# curl sub.example2.com
Hello from /var/www/example2/sub/index.html
这似乎仍然是一个奇怪的设置。我不想为每个子域创建 /etc/hosts 记录...难道不能仅通过 httpd VirtualHost 设置来处理这种情况,而无需在本地更改 DNS 设置,甚至更糟 - 添加 C域的 DNS 中的记录(如果不是本地主机)?任何暗示我做错了什么?如何在不修改 dns 设置的情况下让 sub.example1.com 工作?
此致,
帕维尔
删除重写规则,它用于重定向,所以你有一个循环。
<VirtualHost 127.0.0.1:80> DocumentRoot "/var/www/example2/sub" ServerName sub.example2.com ServerPath "/sub/"
我相信我找到了问题的答案......
** 如何 运行 本地子域?**
/etc/hosts 不支持通配符(*.example2.com),需要设置一个本地 dns 代理服务器,例如 dnsmasq。否则出于开发目的,您必须在 /etc/hosts 中逐一列出(然后维护 :/ )子域以用于本地开发目的。
如何通过域的官方 DNS 记录 运行 子域?
似乎最懒惰的方法是设置具有以下内容的 DNS 设置:
example2.com A the-server-IP
example2.com MX 0 example2.com
*.example2.com CNAME example2.com
如果有更聪明的方法,期待您的评论。
如果您同意这是要走的路 - 请接受答案,以便其他成员知道这是要走的路。
此致,帕维尔
如果您 运行 进入 problems.This dns 是否可以正常浏览,请告诉我,因为对于所有无法解析的名称,它会检查 googles dns 并将其保存到它的文件。
install bind
apt-get install bind9 -y
cd /etc/bind
vim named.conf.options
And uncomment forwarders and two rows bellow and instead of 0.0.0.0 enter google's dns IP (8.8.8.8).
service bind9 restart
vim named.conf.options
zone "YOURDOMAIN NAME" {
type master;
file "db.site.com";
notify yes;
};
cp db.local /var/cache/bind/db.site.com
cd /var/cache/bind/
vim db.site.com
$TTL 604800
@ IN SOA admin. admin.itlink.edu. (
2 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL
;
IN NS ns.YOURDOMAINNAMEHERE.
IN A 192.168.1.10 replace this with the IP of your PC that has apache installed
ns A 192.168.1.10 replace this with the IP of your PC that has apache installed
www A 192.168.1.10 replace this with the IP of your PC that has
service bind9 restart
-- 原文post ---
我想设置一个开发环境来使用一些 apache 功能。我在 Fedora 上 运行 httpd。
我添加到主机本地重定向
# cat /etc/hosts
127.0.0.1 example1.com
127.0.0.1 example2.com
# cmkdir /var/www/example1; echo "Hello from /var/www/example1/index.html" > /var/www/example1/index.html
# cmkdir /var/www/example2; echo "Hello from /var/www/example2/index.html" > /var/www/example2/index.html
# cmkdir /var/www/example2/sub ; echo "Hello from /var/www/example2/sub/index.html" > /var/www/example2/sub/index.html
# cvi /etc/httpd/conf/httpd.conf
<VirtualHost _default_:80>
DocumentRoot "/var//www/html"
</VirtualHost>
<VirtualHost 127.0.0.1:80>
DocumentRoot "/var/www/example1"
ServerName example1.com
</VirtualHost>
<VirtualHost 127.0.0.1:80>
DocumentRoot "/var/www/example2"
ServerName example2.com
</VirtualHost>
<VirtualHost 127.0.0.1:80>
DocumentRoot "/var/www/example2/sub"
ServerName sub.example2.com
ServerPath "/sub/"
RewriteEngine On
RewriteRule "^(/sub/.*)" "/var/www/example2"
</VirtualHost>
# capachectl -t ; apachectl restart
# curl localhost
Hello from /var/www/html/index.html
# curl example1.com
Hello from /var/www/example1/index.html
# curl example2.com
Hello from /var/www/example2/index.html
# curl sub.example2.com
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
... ( lots of stuff different from the one i echoed) ...
如果我在本地 firefox 中做同样的事情 - localhost 按预期工作,example1.com 按预期工作,但 sub.example2.com 将我重定向到 example2.com.
你能帮我弄清楚如何配置本地子域吗?什么东西少了?基于https://httpd.apache.org/docs/2.4/vhosts/examples.html我相信我所做的是正确的。
-- 编辑/更新 ---
如果我按照新手的以下建议只更改重写规则,而不对上面的设置进行任何其他更改:
<VirtualHost 127.0.0.1:80>
DocumentRoot "/var/www/example2/sub"
ServerName sub.example2.com
ServerPath "/sub/"
# RewriteEngine On
# RewriteRule "^(/sub/.*)" "/var/www/example2"
</VirtualHost>
我得到:
# curl sub.example2.com
curl: (6) Could not resolve host: sub.example2.com
如果我
# cat /etc/hosts | grep sub
127.0.0.1 example2.com sub.example2.com
它按预期工作
#curl example2.com
Hello from /var/www/example2/index.html
# curl sub.example2.com
Hello from /var/www/example2/sub/index.html
这似乎仍然是一个奇怪的设置。我不想为每个子域创建 /etc/hosts 记录...难道不能仅通过 httpd VirtualHost 设置来处理这种情况,而无需在本地更改 DNS 设置,甚至更糟 - 添加 C域的 DNS 中的记录(如果不是本地主机)?任何暗示我做错了什么?如何在不修改 dns 设置的情况下让 sub.example1.com 工作?
此致, 帕维尔
删除重写规则,它用于重定向,所以你有一个循环。
<VirtualHost 127.0.0.1:80> DocumentRoot "/var/www/example2/sub" ServerName sub.example2.com ServerPath "/sub/"
我相信我找到了问题的答案......
** 如何 运行 本地子域?**
/etc/hosts 不支持通配符(*.example2.com),需要设置一个本地 dns 代理服务器,例如 dnsmasq。否则出于开发目的,您必须在 /etc/hosts 中逐一列出(然后维护 :/ )子域以用于本地开发目的。
如何通过域的官方 DNS 记录 运行 子域?
似乎最懒惰的方法是设置具有以下内容的 DNS 设置:
example2.com A the-server-IP
example2.com MX 0 example2.com
*.example2.com CNAME example2.com
如果有更聪明的方法,期待您的评论。 如果您同意这是要走的路 - 请接受答案,以便其他成员知道这是要走的路。
此致,帕维尔
如果您 运行 进入 problems.This dns 是否可以正常浏览,请告诉我,因为对于所有无法解析的名称,它会检查 googles dns 并将其保存到它的文件。
install bind
apt-get install bind9 -y
cd /etc/bind
vim named.conf.options
And uncomment forwarders and two rows bellow and instead of 0.0.0.0 enter google's dns IP (8.8.8.8).
service bind9 restart
vim named.conf.options
zone "YOURDOMAIN NAME" {
type master;
file "db.site.com";
notify yes;
};
cp db.local /var/cache/bind/db.site.com
cd /var/cache/bind/
vim db.site.com
$TTL 604800
@ IN SOA admin. admin.itlink.edu. (
2 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL
;
IN NS ns.YOURDOMAINNAMEHERE.
IN A 192.168.1.10 replace this with the IP of your PC that has apache installed
ns A 192.168.1.10 replace this with the IP of your PC that has apache installed
www A 192.168.1.10 replace this with the IP of your PC that has
service bind9 restart