openSSL无法使用PHP内置的webserver

use*_*025 19 php openssl localhost

操作系统: Ubuntu 12.04 64位

PHP版本: 5.4.6-2~precision + 1

当我测试一个https页面时,我正在通过内置的webserver(php5 -S localhost:8000)编写,Firefox(16.0.1)说"问题加载:连接被中断",而终端告诉我":: 1" :37026无效请求(不支持的SSL请求)".

phpinfo()告诉我:

  • 已注册的流套接字传输:tcp,udp,unix,udg,ssl,sslv3,tls
  • [curl] SSL:是的
  • SSL版本:OpenSSL/1.0.1
  • OpenSSL的:

    OpenSSL支持:已启用

    OpenSSL库版本OpenSSL 1.0.1 2012年3月14日

    OpenSSL标题版本OpenSSL 1.0.1 2012年3月14日

是的,http页面工作得很好.

有任何想法吗?

mar*_*rio 38

请参阅内置Web服务器垫片的手册部分:http:
//php.net/manual/en/features.commandline.webserver.php

它不支持SSL加密.它适用于普通的HTTP请求.在openssl扩展和功能的支持是不相关的.它不接受请求或通过流包装器发送响应.

如果您希望SSL在其上运行,请尝试使用stunnel包装器:

php -S localhost:8000 &   
stunnel3 -d 443 -r 8080  
Run Code Online (Sandbox Code Playgroud)

无论如何,这只是为了玩弄.

  • 再上面的代码(PHP -S本地主机:8000&; stunnel3 -d 443 -r 8080)的stunnel3命令出现重定向端口443 HTTPS请求到端口8080,并且PHP的命令显示为服务请求到端口8000如若stunnel3命令重定向到端口8000而不是? (6认同)
  • 我不得不:openssl req -new -x509 -days 365 -nodes -out stunnel.pem -keyout stunnel.pem && sudo mv stunnel.pem/etc/stunnel && sudo stunnel3 -p /etc/stunnel/stunnel.pem -d 443 -r 8000 (3认同)
  • @Pacerier它仍然是最容易设置的选项.对于测试[mitmproxy](http://mitmproxy.org/)可能是一个不错的选择.对于长期设置Tinyproxy或只是普通的Apache + mod_proxy,甚至可以将HTTP转发到PHP的http服务器. (2认同)

小智 11

距离上次更新已经三年了;以下是我如何让它在 2021 年在 macOS 上运行(作为马里奥答案的扩展):

# Install stunnel
brew install stunnel

# Find the configuration directory
cd /usr/local/etc/stunnel

# Copy the sample conf file to actual conf file
cp stunnel.conf-sample stunnel.conf

# Edit conf
vim stunnel.conf
Run Code Online (Sandbox Code Playgroud)

修改stunnel.conf成这样:(其他选项都可以删除)

; **************************************************************************
; * Global options                                                         *
; **************************************************************************

; Debugging stuff (may be useful for troubleshooting)
; Enable foreground = yes to make stunnel work with Homebrew services
foreground = yes
debug = info
output = /usr/local/var/log/stunnel.log

; **************************************************************************
; * Service definitions (remove all services for inetd mode)               *
; **************************************************************************

; ***************************************** Example TLS server mode services

; TLS front-end to a web server
[https]
accept = 443
connect = 8000
cert = /usr/local/etc/stunnel/stunnel.pem
; "TIMEOUTclose = 0" is a workaround for a design flaw in Microsoft SChannel
; Microsoft implementations do not use TLS close-notify alert and thus they
; are vulnerable to truncation attacks
;TIMEOUTclose = 0
Run Code Online (Sandbox Code Playgroud)

它在端口 443 接受 HTTPS / SSL,并使用 stunnel 的默认伪造证书连接到在端口 8000 运行的本地 Web 服务器/usr/local/etc/stunnel/stunnel.pem。日志级别为info,日志输出写入到/usr/local/var/log/stunnel.log

启动隧道:

; **************************************************************************
; * Global options                                                         *
; **************************************************************************

; Debugging stuff (may be useful for troubleshooting)
; Enable foreground = yes to make stunnel work with Homebrew services
foreground = yes
debug = info
output = /usr/local/var/log/stunnel.log

; **************************************************************************
; * Service definitions (remove all services for inetd mode)               *
; **************************************************************************

; ***************************************** Example TLS server mode services

; TLS front-end to a web server
[https]
accept = 443
connect = 8000
cert = /usr/local/etc/stunnel/stunnel.pem
; "TIMEOUTclose = 0" is a workaround for a design flaw in Microsoft SChannel
; Microsoft implementations do not use TLS close-notify alert and thus they
; are vulnerable to truncation attacks
;TIMEOUTclose = 0
Run Code Online (Sandbox Code Playgroud)

启动网络服务器:

brew services start stunnel # Different for Linux
Run Code Online (Sandbox Code Playgroud)

现在您可以访问https://localhost:443您的网络服务器:截图

应该有一个证书错误,您必须单击浏览器警告,但这使您可以使用 HTTPS 请求访问本地主机以进行开发。

  • 救命的答案,谢谢。小补充:对于 m1 / Apple Silicon Mac,stunnel 配置文件似乎位于“/opt/homebrew/etc/stunnel”下。 (4认同)