Apache 2 站点可用配置

发布于 2024-08-15 07:53:27 字数 1578 浏览 12 评论 0原文

我正在尝试在一台 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 技术交流群。

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

发布评论

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

评论(2

唱一曲作罢 2024-08-22 07:53:27

您需要在 内添加一个 ServerName 指令。它将根据浏览器请求告诉服务器当前正在使用哪个虚拟主机(如果您的客户端访问 http://site1.example.com" example.comhttp://site2.example.com,它们将连接到同一个IP,即服务器,但请求包含原始请求 url)。
您必须复制 块才能为每个托管站点分配一个。每个块的不同之处主要在于它们的 ServerName 和 DocumentRoot。
您可以使用“apache2ctl -S”来查看 apache 如何理解您的虚拟主机设置。

您可以使用包含此类内容的单个文件:

<VirtualHost *:80>
  ServerName site1.myserver.com
  DocumentRoot /var/www/site1
  ...
</VirtualHost>

<VirtualHost *:80>
  ServerName site2.myserver.com
  DocumentRoot /var/www/site2
  ...
</VirtualHost>

<VirtualHost *:80>
  ServerName site3.myserver.com
  DocumentRoot /var/www/site3
  ...
</VirtualHost>

<VirtualHost *:80>
  ServerName site4.myserver.com
  DocumentRoot /var/www/site4
  ...
</VirtualHost>

<VirtualHost *:80>
  ServerName site5.myserver.com
  DocumentRoot /var/www/site5
  ...
</VirtualHost>

当然,也许可以确定所有这些名称的 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 :

<VirtualHost *:80>
  ServerName site1.myserver.com
  DocumentRoot /var/www/site1
  ...
</VirtualHost>

<VirtualHost *:80>
  ServerName site2.myserver.com
  DocumentRoot /var/www/site2
  ...
</VirtualHost>

<VirtualHost *:80>
  ServerName site3.myserver.com
  DocumentRoot /var/www/site3
  ...
</VirtualHost>

<VirtualHost *:80>
  ServerName site4.myserver.com
  DocumentRoot /var/www/site4
  ...
</VirtualHost>

<VirtualHost *:80>
  ServerName site5.myserver.com
  DocumentRoot /var/www/site5
  ...
</VirtualHost>

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

短暂陪伴 2024-08-22 07:53:27

@Zeograd 的答案可能是大多数解决方案的正确答案,但是我遇到了类似的问题,我所需要做的就是使用 a2enmod 命令启用重写模块。

这是我在 Ubuntu 中通过命令行运行的:

sudo a2enmod rewrite
sudo service apache2 restart

如果 sudo service apache2 restart 不起作用,您可以尝试:

sudo /etc/init.d/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:

sudo a2enmod rewrite
sudo service apache2 restart

If sudo service apache2 restart doesn't work, you can try:

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