Can I run two web servers on the same computer?

后端 未结 7 968
执笔经年
执笔经年 2020-12-23 22:12

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 machi

相关标签:
7条回答
  • 2020-12-23 22:24

    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.

    0 讨论(0)
  • 2020-12-23 22:26

    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)
    0 讨论(0)
  • 2020-12-23 22:27

    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.

    0 讨论(0)
  • 2020-12-23 22:27

    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/

    0 讨论(0)
  • 2020-12-23 22:37

    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
    
    0 讨论(0)
  • 2020-12-23 22:41

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

    0 讨论(0)
提交回复
热议问题