我可以在同一台计算机上运行两个 Web 服务器吗?

发布于 2024-08-22 20:15:56 字数 230 浏览 9 评论 0原文

我刚刚发现我可以使用 Python 编写一个真正的简单的 Web 服务器。我已经有一个 Apache Web 服务器,我想在这台机器上尝试基于 Python 的 Web 服务器。但我担心如果我尝试的话会引起某种冲突。我的意思是两个网络服务器如何“决定”谁需要处理来自客户端的请求?

I just found out that I can write a really simple web server using Python. I have already an Apache web server I would like to try the Python based web server on this machine. But I am afraid that I can get some kind of conflict if I try it. I mean how two web server will "decide" who needs to server a request from a client?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

無心 2024-08-29 20:15:56

让他们监听不同的端口,你就会没事的。

默认 Web 端口为 80。当您在浏览器中打开某个 url 而不指定端口时,默认使用 80。

您可以将 Web 服务器配置为侦听不同的端口,但您还需要在 url 中明确指定它:

http://localhost:8080

选择端口时,请注意您已安装和运行的任何软件尚未使用此特定端口号在你的盒子上。否则,正如你所猜对的那样,将会发生冲突。

PS 就在几天前重新安装时,我发现 IIS 无法启动(似乎没有原因)。原来新版本的Skype占用了这个默认端口!必须删除其设置“使用端口 80 和 443 作为传入连接的替代端口”。

Make them listen to different ports and you will be fine.

The default web port is 80. When you open some url in browser without specifying a port, 80 is used by default.

You can configure your web server to listen to a different port but then you will also need to specify it explicitly in the url:

http://localhost:8080

When choosing a port pay attention that this particular port number is not yet in use by any software you have installed and running on your box. Otherwise, as you correctly guessed, there will be a conflict.

P.S. Just a few days ago doing reinstallation I got IIS not able to start (seemingly without reason). Turned out the new version of Skype occupied this default port! Had to remove its setting "Use port 80 and 443 as alternatives for incoming connections".

挖鼻大婶 2024-08-29 20:15:56

如果您实际上想运行单独的服务器来测试服务器软件,请参阅其他答案,但是...

听起来(因为您是开发人员,而不是系统管理员,对吗?)您真的只是想在同一台计算机上运行 Python 和 PHP 站点。因此,如果我正在阅读您的问题,请原谅我,但这种设置允许我在一台服务器 Apache 中使用相同端口(端口 80)在同一台计算机上运行两种类型的站点。

我在 /etc/hosts 文件(或 Windows 上的 C:\Windows\System32\drivers\etc\hosts)中创建新条目并将它们指向 127.0.0.1:

127.0.0.1      localhost

# development projects
127.0.0.1      somephpsite.com.local
127.0.0.1      www.somephpsite.com.local
127.0.0.1      otherpythonsite.com.local
127.0.0.1      www.otherpythonsite.com.local

然后在 Apache 中为每个站点添加 VirtualHosts:

<VirtualHost *:80>
    DocumentRoot "/Library/WebServer/Documents"
    ServerName localhost
</VirtualHost>

<VirtualHost *:80>
    <Directory "/Users/Robert/Projects/SomeSite/somephpsite.com">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
    DocumentRoot "/Users/Robert/Projects/SomeSite/somephpsite.com"
    ServerName somephpsite.com.local
    ServerAlias www.somephpsite.com.local
    ErrorLog "/Users/Robert/Projects/SomeSite/error.log"
    CustomLog "/Users/Robert/Projects/SomeSite/access.log" common
</VirtualHost>

<VirtualHost *:80>
    <Directory "/Users/Robert/Projects/OtherSite/otherpythonsite.com">
        Order allow,deny
        Allow from all
    </Directory>
    DocumentRoot "/Users/Robert/Projects/OtherSite/otherpythonsite.com/static"
    Alias /(.*(\.css|\.gif|\.ico|\.jpg|\.js|\.pdf|\.txt)) /Users/Robert/Projects/OtherSite/otherpythonsite.com/static/$1
    WSGIScriptAlias / /Users/Robert/Projects/OtherSite/otherpythonsite.com/wsgi.py
    ServerName otherpythonsite.com.local
    ServerAlias www.otherpythonsite.com.local
    ErrorLog "/Users/Robert/Projects/OtherSite/error.log"
    CustomLog "/Users/Robert/Projects/OtherSite/access.log" common
</VirtualHost>

因此,PHP站点像往常一样在 DocumentRoot 中运行。 Python 站点在 WSGI 中运行。它们都运行在 Apache 中。然后为了测试,我只需在我用来处理本地副本的任何浏览器中添加“.local”。

If you actually want to run separate servers to test out server software see the other answers, but...

It sounds like (because you are a developer, not a sysadmin right?) you really just want to run Python and PHP sites on the same computer. So, forgive me if I'm reading into your question, but this setup allows me to run both kinds of sites on the same computer with the same port (port 80) in one server, Apache.

I make new entries in my /etc/hosts file (or C:\Windows\System32\drivers\etc\hosts on Windows) and point them to 127.0.0.1:

127.0.0.1      localhost

# development projects
127.0.0.1      somephpsite.com.local
127.0.0.1      www.somephpsite.com.local
127.0.0.1      otherpythonsite.com.local
127.0.0.1      www.otherpythonsite.com.local

Then in Apache I add VirtualHosts for each site:

<VirtualHost *:80>
    DocumentRoot "/Library/WebServer/Documents"
    ServerName localhost
</VirtualHost>

<VirtualHost *:80>
    <Directory "/Users/Robert/Projects/SomeSite/somephpsite.com">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
    DocumentRoot "/Users/Robert/Projects/SomeSite/somephpsite.com"
    ServerName somephpsite.com.local
    ServerAlias www.somephpsite.com.local
    ErrorLog "/Users/Robert/Projects/SomeSite/error.log"
    CustomLog "/Users/Robert/Projects/SomeSite/access.log" common
</VirtualHost>

<VirtualHost *:80>
    <Directory "/Users/Robert/Projects/OtherSite/otherpythonsite.com">
        Order allow,deny
        Allow from all
    </Directory>
    DocumentRoot "/Users/Robert/Projects/OtherSite/otherpythonsite.com/static"
    Alias /(.*(\.css|\.gif|\.ico|\.jpg|\.js|\.pdf|\.txt)) /Users/Robert/Projects/OtherSite/otherpythonsite.com/static/$1
    WSGIScriptAlias / /Users/Robert/Projects/OtherSite/otherpythonsite.com/wsgi.py
    ServerName otherpythonsite.com.local
    ServerAlias www.otherpythonsite.com.local
    ErrorLog "/Users/Robert/Projects/OtherSite/error.log"
    CustomLog "/Users/Robert/Projects/OtherSite/access.log" common
</VirtualHost>

So, the PHP sites run in the DocumentRoot like they always do. And the Python sites run in WSGI. And they both run in Apache. Then to test, I just add ".local" in whatever browser I'm using to work on my local copy.

温暖的光 2024-08-29 20:15:56

Web 服务器绑定到特定端口。通常,这是端口 80。如果未明确指定端口,则这是浏览器将尝试访问的端口。

您可以让备用服务器在不同的端口上运行(8080 或 8081 似乎是 Web 服务器的流行替代端口,但选择权在您手中)。

这将阻止冲突的发生。发送到端口 80 的所有内容都会到达您的旧服务器。所有访问 8080(或您决定在其上运行服务器的任何端口)的内容都将到达您的简单 python 服务器。

要访问其他服务器,请使用如下 URL:-

http://localhost:8080/

A web server is tied to a specific port. Normally, this is port 80. If the port is not explicitly specified, this is the port that a browser will attempt to hit.

You can get your alternate server to run on a different port ( 8080 or 8081 seem to be popular alts for web servers, but the choice is yours ).

This'll stop the conflict from happening. Everything going to port 80 hits your old server. Everything going to 8080 ( or whatever port you decide to run your server on ) will hit your simple python server.

To hit your other server, use a URL like :-

http://localhost:8080/

十级心震 2024-08-29 20:15:56

您不能在同一端口(默认为80)打开两个Web服务器,如果您想创建两个或多个Web服务器,则必须使用不同的端口。

如果您使用 DNS,则可以轻松设置 Web 服务器以使用不同的网站响应不同的请求,如果您需要为子域或不同的域设置不同的网站,这可能会很有用。

You cannot open two web servers in the same port (which default is 80), if you desire to make two or more web servers, you have to use different ports.

If you are using a DNS, you could easily set up your web server to respond with different web sites to different requests, that could be useful if you need to have different web sites for subdomains or different domains.

绮筵 2024-08-29 20:15:56

是的,您可以通过两种方法在同一台计算机上运行两个不同的服务器(当然还有另一种方法是在同一IP上运行不同的端口号)

  1. 您可以运行两个服务器,一个在本地主机上,另一个在您的IP地址上。 的节点代码

与您共享此服务器 1:

app.listen(PORT, "localhost", () => {
  console.log("Server is running on ${PORT} in ${process.env.NODE_ENV}");
});

服务器 2:

app.listen(PORT, "youy computer ip", () => {
  console.log(server is running on ${PORT} in ${process.env.NODE_ENV});
});

  1. 您可以通过不同的端口号在同一本地主机上运行两个不同的服务器,或者在同一端口上运行两个不同的本地主机(本地主机范围127.0.0.0-127.255.255.255

Yes, you can run two different servers on the same computer via two methods (of course there is another method of running on the same IP with a different port number)

  1. You can run two servers, one on localhost, another one on your IP address. Sharing you the node code for this

Server 1:

app.listen(PORT, "localhost", () => {
  console.log("Server is running on ${PORT} in ${process.env.NODE_ENV}");
});

Server 2:

app.listen(PORT, "youy computer ip", () => {
  console.log(server is running on ${PORT} in ${process.env.NODE_ENV});
});

  1. You can run two different servers on the same localhost via different port numbers or two different localhosts on the same port (localhost ranges 127.0.0.0-127.255.255.255)
樱娆 2024-08-29 20:15:56

网络服务器对于谁服务连接请求没有发言权(此任务仍然在操作系统级别)。此外,如果没有特殊的套接字选项,套接字必须绑定到接口、互联网地址和端口的唯一组合。

The webservers would have no say in who services a connection request (this task is still at the operating system level). Furthermore, without special socket options, the socket must be bound to a unique combination of interface, internet address and port.

初心 2024-08-29 20:15:56

我建议您专用一台服务器来服务 https(端口 443)请求。

这样您就可以避免其他人提到的端口冲突,同时也不要求用户在浏览器中输入任何奇怪的内容(任意端口号)。您甚至可以让每个服务器将流量重定向到另一个服务器(例如,http 服务器收到对其知道 https 服务器处理的主机名的 http 请求,因此它可以将请求重定向到具有相同主机名的 https,从而将请求传输到合适的服务器)。

服务器A:

http://localhost

服务器B:

https://localhost

I would suggest you dedicate one server to serving https (port 443) requests.

That way you can avoid the port conflict others have mentioned while also not requiring users to type anything strange into their browsers (arbitrary port numbers). You can even have each server redirect traffic to the other (e.g. the http server recieves an http request for a host name it knows the https server handles so it can redirect the request to https witht the same host name, thereby transferring the request to the appropriate server).

Server A:

http://localhost

Server B:

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