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

时间:2023-01-22 01:11:03

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?

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

6 个解决方案

#1


27  

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.

默认Web端口为80.当您在浏览器中打开某个URL而未指定端口时,默认情况下使用80。

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:

您可以将Web服务器配置为侦听其他端口,但您还需要在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".

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

#2


8  

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.

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

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 ).

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

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.

这将阻止冲突的发生。 80端口的所有内容都会打到旧服务器。一切到8080(或你决定运行你的服务器的任何端口)都会打到你的简单python服务器。

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

要点击其他服务器,请使用以下网址: -

http://localhost:8080/

HTTP://本地主机:8080 /

#3


8  

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.

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

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:

我在/ 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

Then in Apache I add VirtualHosts for each site:

然后在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>

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.

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

#4


1  

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.

您无法在同一端口(默认为80)中打开两个Web服务器,如果您希望制作两个或更多Web服务器,则必须使用不同的端口。

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.

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

#5


1  

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.

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

#6


1  

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

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

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).

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

Server A:

服务器A:

http://localhost

Server B:

服务器B:

https://localhost

#1


27  

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.

默认Web端口为80.当您在浏览器中打开某个URL而未指定端口时,默认情况下使用80。

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:

您可以将Web服务器配置为侦听其他端口,但您还需要在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".

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

#2


8  

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.

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

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 ).

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

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.

这将阻止冲突的发生。 80端口的所有内容都会打到旧服务器。一切到8080(或你决定运行你的服务器的任何端口)都会打到你的简单python服务器。

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

要点击其他服务器,请使用以下网址: -

http://localhost:8080/

HTTP://本地主机:8080 /

#3


8  

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.

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

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:

我在/ 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

Then in Apache I add VirtualHosts for each site:

然后在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>

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.

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

#4


1  

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.

您无法在同一端口(默认为80)中打开两个Web服务器,如果您希望制作两个或更多Web服务器,则必须使用不同的端口。

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.

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

#5


1  

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.

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

#6


1  

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

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

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).

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

Server A:

服务器A:

http://localhost

Server B:

服务器B:

https://localhost