Can I run two web servers on the same computer?

牧云@^-^@ 提交于 2020-06-24 03:00:08

问题


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?


回答1:


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




回答2:


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.




回答3:


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/




回答4:


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.




回答5:


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.




回答6:


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


来源:https://stackoverflow.com/questions/2319697/can-i-run-two-web-servers-on-the-same-computer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!