Can I run two web servers on the same computer?
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.开发者_StackOverflow I mean how two web server will "decide" who needs to server a request from a client?
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".
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.
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/
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.
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.
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
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)
- 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});
});
- 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
)
精彩评论