PHP HTTP 服务器?端口 80, 443-444, 1000-3000, 8000-9000。(非 Apache)

0 投票
2 回答
1768 浏览
提问于 2025-04-16 04:50

我很快就要在服务器上升级到Linux Debian 6.0 "Squeeze",我想知道怎么用Python在多个不同的端口上作为网络服务器,来处理不同的事情。

Ports            Directory           Description
80, 443          /var/www/sitegen/   Take all domains and generate a site from the SQL DB
444, 1000-3000   /var/www/manager/   Take 444 as a PHP server manager and the rest to be forwarded to serial hardware.
8000-9000        The VMs DIR         Forward the port to port 80 (or 443 by settings) on the VMs.

这意味着端口443可以用来支持多个网站(这些网站使用的是相同的代码,只是在SQL数据库中有所不同)。

2 个回答

2

这不是一个关于PHP的问题,因为PHP解释器并不会直接监听端口。在Linux系统上,PHP通常是在Apache这个软件里运行的。Apache可以设置成监听多个端口,甚至可以为每个虚拟主机单独设置。

另外,要注意的是,HTTPS的特性让多个虚拟主机无法使用各自的SSL证书同时监听同一个端口。每个虚拟主机都需要自己的证书,并且要监听不同的端口。

此外,把特定的端口发送到运行在同一台机器上的虚拟机,这和网页服务器没有关系,更不用说执行环境了。这涉及到在虚拟网络中配置端口转发,同时还要在你的虚拟机中配置本地网页服务器。

0

在Python中:

import os
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

class myHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write("This is working")

def main():
    try:
        server = HTTPServer(("", 8080), myHandler)
        print "Sever is up.."
        server.serve_forever()
    except KeyboardInterrupt:
        print
        print "Bye, Bye!"
        server.socket.close()

if __name__ == "__main__":
    main()

撰写回答