为 apache 上的不同域设置不同的 data/folders

Set different data/folders for different domains on apache

所以我安装了 Ubuntu 16 和 LAMP 以及其他一些东西(比如 FTP 服务器...)。

我的 VPS 的 IP 是 1.2.3.4.

我有 2 个域,example.com 和 mydomain.com - 这两个域的 A 记录都指向 1.2.3.4

如果我在浏览器中访问 1.2.3.4 和 example.com 和 mydomain.com,所有这三个都显示相同的数据,特别是 /var/www/html 的内容 - 如果我更改内容该目录的影响 example.com 和 mydomain.com 以及 1.2.3.4.

现在如何为域设置其他文件夹以供读取?我想要 example.com 和 mydomain.com 上的不同数据 - 我想让它们从服务器上的不同文件夹中读取。请问我该如何设置?

你想要实现的在技术上定义为设置虚拟主机,可以使用以下步骤设置:

在您的 Apache 配置目录下,通常位于 /etc/apache2/,您会发现一个名为 sites-enabled 的目录。


对于您的每个域,您都需要配置一个特殊的配置文件,以便将它们指向正确的方向。文件名通常是 your-domain.conf.

以下是文件示例:

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port
    # that the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    ServerName your_domain
    ServerAlias www.your_domain
    ServerAdmin webmaster@your_domain
    DocumentRoot website_directory

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
    <Directory website_directory>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
    </Directory>
</VirtualHost>

保存文件后您需要重新启动 Apache 服务器
/etc/init.d/apache2 restart

为您的每个域重复上述步骤。
如果一切顺利,您的网站将在您访问您的域时显示。