Apache 2 站点可用配置
我正在尝试在一台 Apache 服务器上编写大约 5 个网站,该服务器都在一个 IP 地址上。
例如:
- /var/www/site1
- /var/www/site2
- /var/www/site3
- /var/www/site4
- /var/www/site5
但是,如果我在站点 2 上创建一个链接,例如: /index.php,您可能希望它在 /var/www/site2/index.php 中查找...但它实际上解析为 /var/www/index.php。
是否有办法设置 Apache 来了解单独的站点文件夹需要表现并解析为单独的文件夹?
这是我目前网站上可用的文件。我相信一个相当默认的设置:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
任何帮助将不胜感激。
非常感谢。
安德鲁·巴洛
I am trying to write about 5 websites all on one Apache server which is all on one IP address.
For example:
- /var/www/site1
- /var/www/site2
- /var/www/site3
- /var/www/site4
- /var/www/site5
However, if I create a link on site 2 just using, for example. /index.php, you would expect it to look in /var/www/site2/index.php... but it actually resolves to /var/www/index.php.
Is there anyway of setting up Apache to know that the separate site folders need to behave and resolve to the separate folders?
This is my sites-available file at the moment. A fairly default setup I believe:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
Any help would be apreciated.
Thank-you kindly.
Andrew Barlow
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在
内添加一个 ServerName 指令。它将根据浏览器请求告诉服务器当前正在使用哪个虚拟主机(如果您的客户端访问 http://site1.example.com" example.com 或 http://site2.example.com,它们将连接到同一个IP,即服务器,但请求包含原始请求 url)。您必须复制
块才能为每个托管站点分配一个。每个块的不同之处主要在于它们的 ServerName 和 DocumentRoot。您可以使用“apache2ctl -S”来查看 apache 如何理解您的虚拟主机设置。
您可以使用包含此类内容的单个文件:
当然,也许可以确定所有这些名称的 dns 最终都在您的 IP 上。它不需要是子域,只要它们落在您的服务器 IP 上并且您有一个对应的 ServerName 即可。如果您需要单个站点的额外名称,可以在 ServerName 下面添加“ServerAlias secondaryname.myserver.comthirdname.myserver.com”
You need a ServerName directive inside the
<VirtualHost>
. It will tell the server which virtual host is currently in use depending on the browser request (if your client access http://site1.example.com or http://site2.example.com, they will connect to the same IP, hence server, but the request contains the original request url).You'll have to duplicate your
<VirtualHost>
block to have one per hosted site. Each block will differ by their ServerName and DocumentRoot mainly.You can use "apache2ctl -S" to see how apache understood your virtual host settings.
You can use a single file with this kind of content :
Of course, maybe sure that the dns for all of those name ends up on your IP. It needs not to be subdomains as long as they land on your server ip and you have a correspond ServerName for it. If you need extra names for a single site, you can add them with "ServerAlias secondname.myserver.com thirdname.myserver.com" below ServerName
@Zeograd 的答案可能是大多数解决方案的正确答案,但是我遇到了类似的问题,我所需要做的就是使用
a2enmod
命令启用重写模块。这是我在 Ubuntu 中通过命令行运行的:
如果 sudo service apache2 restart 不起作用,您可以尝试:
@Zeograd's answer is probably the correct answer for most solutions, however I had a similar issue and all I needed to do was enable the rewrite module with the
a2enmod
command.This is what I ran via command line in Ubuntu:
If
sudo service apache2 restart
doesn't work, you can try: