运行 NodeJs 和 Apache 以及两个单独的域

Run NodeJs and Apache together with two separated domain

我有一个 Linux 服务器 (VPS),有两个域和两个 Web 应用程序,一个使用 NodeJS(聊天应用程序)编程,而另一个是 PHP .

所以,我想要做的是 运行 同一台服务器上不同域中的两个应用程序。我已经尝试了很多方法来做到这一点,但所有这些方法都适用于单域和多 URL 路径,例如:https://example.com & https://example.com/node.

但是我想要的是这样的:https://nodeappexample.com & https://apacheappexample.com.

问候

如果您使用专用服务器或 VPS,例如 DigitalOcean 或任何其他服务器,并计划在单个服务器上托管多个网站,那么您可以使用 Apache HTTP 或 Nginx 网络服务器来实现。

这假设您已经将 DNS 映射到您的主机,就像我已经映射到 DigitalOcean 的两个域一样。

bestflare.com
usefulread.com

在你实现它之前,让你了解它是如何工作的。在单个实例上拥有多个网站的概念称为虚拟服务器。虚拟服务器配置在 Web 服务器配置中定义,并基于 server/IP 地址,请求被转发到相应的文档根目录。

在 Apache 中配置虚拟主机 以托管多个域 登录 Apache HTTP 服务器 转到 apache conf 位置。 (在默认安装中——你会在这里找到它 /etc/httpd/conf/httpd.conf) 备份 httpd.conf 个文件 创建一个 VirtualHost 容器,就像我为两个域所做的那样。

<VirtualHost *:80>
ServerAdmin hello@chandank.com
   DocumentRoot /opt/htdocs/bestflare
   ServerName bestflare.com
   ErrorLog logs/bestflare.com-error_log
   CustomLog logs/bestflare.com-access_log common
</VirtualHost>
<VirtualHost *:80>
   ServerAdmin hello@chandank.com
   DocumentRoot /opt/htdocs/usefulread
   ServerName usefulread.com
   ErrorLog logs/usefulread.com-error_log
   CustomLog logs/usefulread.com-access_log common
</VirtualHost>

注意:根据您的要求更改 ServerAdmin、DocumentRoot、ServerName、ErrorLog、CustomLog 的值。

重新启动 Apache HTTP 并测试两个 url。

在 Nginx 中配置虚拟主机以托管多个域 登录 Nginx 服务器 转到 virtual.conf 位置(在默认位置 - 你会在这里找到它 /etc/nginx/conf.d/virtual.conf) 备份 virtual.conf 为 URL 创建服务器块,如下所示。

server {
listen 80;
   root /opt/htdocs/bestflare;
index index.html index.htm;
   server_name bestflare.com;
   location / {
       try_files $uri $uri/ =404;
   }
}
server {
   listen 80;
   root /opt/htdocs/usefulread;
   index index.html index.htm;
   server_name usefulread.com;
   location / {
       try_files $uri $uri/ =404;
   }
}

注意:根据您的要求更改 root server_name 的值。

重新启动 Nginx 并测试两个 URL 的 这是在单个 Web 服务器实例(如 Apache 或 Nginx)中托管多个域的快速指南。一旦您的网站上线,请不要忘记针对安全漏洞对其进行测试。