如何在同一台服务器上托管 2 个 Web 应用程序

如何在同一台服务器上托管 2 个 Web 应用程序

我目前正在为一款名为 EVE Online 的游戏配置 2 个第三方应用程序,其中一个是 Pathfinder,另一个是公司管理应用程序。

例如,如果我想访问 Pathfinder,我的 URL 将为mydomain.com/pathfinder,但如果想访问其他应用程序,我的 URL 将为mydomain.com/otherapp

Pathfinder 位于DocumentRoot文件夹 ( /var/www/pathfinder) 中,其他应用程序DocumentRoot也应位于该文件夹中 ( /var/www/otherapp)

    <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 mydomain.com

ServerAdmin [email protected]
DocumentRoot /var/www/pathfinder

# 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
RewriteEngine on
RewriteCond %{SERVER_NAME} =mydomain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

答案1

创建两个VirtualHosts(最好将它们放在单独的文件中)

<VirtualHost *:80>
    ServerName pathfinder.dev
    ServerAlias www.pathfinder.dev

    DocumentRoot /var/www/pathfinder
    <Directory /var/www/pathfinder>
        AllowOverride All
        Order Allow,Deny
        Allow from All
        #... 
    </Directory>
    #...
</VirtualHost>

<VirtualHost *:80>
    ServerName otherapp.dev
    ServerAlias www.otherapp.dev

    DocumentRoot /var/www/otherapp
    <Directory /var/www/otherapp>
        AllowOverride All
        Order Allow,Deny
        Allow from All
        #...
    </Directory>
    #...
</VirtualHost>

编辑您的主机文件/etc/hosts并添加以下行:

127.0.0.1   pathfinder.dev
127.0.0.1   otherapp.dev

保存更改并重新启动 apache 服务器。打开浏览器并转到您的第一个项目http://pathfinder.dev和第二个应用程序http://otherapp.dev

答案2

您使用了 Alias /pathfinder /var/www/pathfinder 和 Alias /otherapp /var/www/otherapp。您不需要使用虚拟主机。您可以浏览 yourdomain.com/pathfinder 和 yourdomain.com/otherapp。您不能在同一服务器上使用虚拟主机和别名。

答案3

您很可能需要配置两个不同的虚拟主机,您可以使用子域将它们分开或侦听最适合您的不同端口。

您可能还想使用一些控制面板,例如这个,如果您对配置文件不是完全确定,可以管理 Apache、域和子域。

问候。

相关内容