php-kit / php-server
使PHP内置的Web服务器更容易、更实用地使用
README
为运行PHP Web应用程序而设计的轻量级开发Web服务器
简介
为什么你应该使用这个工具而不是“真正的”Web服务器呢?
自5.4版本以来,PHP提供了一个内置的Web服务器,它可以作为轻量级且安装简便的开发Web服务器非常有用。使用它,你不需要在你的机器上安装和配置Apache、NGINX或其他任何Web服务器,只是为了能够查看本地静态网站或PHP网站/应用程序。
通过使用PHP-Server,这个嵌入的服务器变得更加实用和简单,使其成为“真正”的Web服务器的合适替代品,足以满足你大部分的PHP开发需求。
功能
此工具
-
比普通Web服务器更轻量,对系统资源要求更低。
-
比Web服务器 + PHP集成更简单。
-
提供一组命令,使控制PHP内置Web服务器更加容易。
-
默认在后台运行服务器(它不会阻塞你的终端,甚至可以关闭它)。
-
在您登录计算机时自动启动服务器。
-
允许您浏览所有本地网站和目录。
-
通过以下方式扩展服务器的功能:
-
一个自定义路由器,
-
自动为没有index.php或index.html文件的目录URL生成目录索引页面,让您能够浏览目录结构以查找要打开的网站。
-
支持“虚拟URL”(又称“清洁URL”或“虚荣URL”),通过自动将虚拟路径重定向到应用程序的index.php,从而进一步路由。
-
-
从PHP脚本中访问环境变量
-
限制
不要在生产服务器上使用此工具,因为php-server缺少许多其他Web服务器提供的先进功能和安全性。
一些(阻止器)限制包括:
- 它一次只处理一个请求,因此无法扩展;
- 它不会为静态文件发送缓存头,因此浏览器缓存被禁用;
- 它没有高级mod-rewrite或.htaccess配置支持(但请参阅关于虚拟URL的上述注释)。
安装
运行时要求
- PHP >= 5.4
- Composer
- BASH命令行shell
操作系统兼容性
- Mac OS X(首选)
- Linux
- Windows通过Git BASH、MSYS2或Cygwin
关于Windows兼容性
尽管此工具是通过Composer安装的,但其大部分是用BASH编写的,因此您需要BASH才能运行它。
BASH在Windows上不可用,并且cmd.exe(Windows终端)不兼容。
一种解决方案是安装Git for Windows,它提供Git BASH,并使用它运行此工具。
另一种方法是安装MSYS2或Cygwin;两者都提供了许多Unix工具的Windows端口,包括BASH。
安装
安装先决条件
首先,您需要安装PHP,如果您还没有在机器上安装它。
对于OS X,您可以使用在php-osx.liip.ch提供的惊人的PHP安装程序。
要安装Composer,请遵循https://getcomposer.org.cn/download上的说明。
安装 php-server
在命令行中,键入
composer global require php-kit/php-server
就是这样!
提示:请确保您的
$PATH
环境变量包括~/.composer/vendor/bin
,否则终端将无法找到Composer全局安装的可执行文件,如这个。
您可以在
~/.profile
、~/.bash_profile
或~/.bashrc
中编辑您的路径;使用您的机器上存在的第一个。
用法
在任何目录的终端中键入php-server
,以显示可用命令及其语法。
您将得到以下输出
NAME
php-server -- a lightweight development web server for running PHP web applications
SYNTAX
php-server command [options]
php-server [--help]
COMMAND
start - Starts the web server.
SYNTAX
php-server start [-p|--port] [-a|--address] [-l|--log] [-n|--no-log] [-g|--global] [-r|--root] [-f|--foreground] [-x|--executable]
OPTIONS
-p, --port The TCP/IP port the web server will listen on.
[default: 8000]
-a, --address The IP address the web server will listen on.
[default: localhost]
-l, --log The path of a log file where the server's logging output will be saved.
[default: ~/.php-server.log]
-n, --no-log Disable logging.
-g, --global If specified, the server will serve all sites under the root web directory.
-r, --root The root web directory's name or path. If it's a name, the directory will be searched for
starting at the current directory and going upwards. Alternatively, you may specify the
full path.
[default: Sites]
-f, --foreground Don't run the server as a background process.
-x, --executable The path to the PHP interpreter.
[default: searched on $PATH]
COMMAND
stop - Stops the web server.
COMMAND
restart - Stops the web server and starts it again with the same options as before.
COMMAND
status - Checks if the web server is running.
COMMAND
install - Installs the server as a system user agent that is auto-started on login.
The server runs in --global mode.
This works on macOS only!
SYNTAX
php-server install [-p|--port] [-a|--address] [-l|--log] [-n|--no-log] [-r|--root] [-e|--env]
OPTIONS
-e, --env The path to a script that will set environment variables.
This is required if you specify a port < 1024, as the server will run from the root user
account and it will not inherit the current user's environment.
[default: ~/.profile - if the file exists and port < 1024]
COMMAND
uninstall - Shuts down and uninstalls a previously installed user agent.
This works on macOS only!
COMMAND
self-update - Updates this tool to the latest version.
本地模式
如果您运行php-server start
,它将使当前目录在https://:8000
上可用。您可以使用此模式在特定的HTTP端口上打开特定的PHP网站/应用程序或静态网站。您还可以在不同的端口上同时启动多个网站。
全局模式
如果您运行php-server start -g
,它将使安装在公共网页目录下的所有网站可用(默认为~/Sites
,以兼容Mac OS X,但您可以指定其他路径)。
永久性Web服务器
您可以将操作系统配置为在系统启动过程中以及在您登录后以全局模式启动php-server
。这样,您将得到一个非常好的本地Apache或NGINX替代品,它始终准备好为您提供服务。
在macOS上,您可以使用内置的install
命令配置服务器在系统启动时以及您登录时以以下方式运行:
- 作为
daemon
- 在root
用户帐户下,如果HTTP端口小于1024,则启动。 - 作为
user agent
- 在当前用户帐户下,当用户登录时,用于其他所有端口。
使用uninstall
命令停止服务器并防止其再次自动启动。
目前,没有内置支持在其他操作系统上设置服务器以自动运行。
目录索引
如果您在浏览器中打开https://:8000
(使用默认的php-server配置),您将看到所有可用的文件夹/网站的目录列表。从那里,您可以点击文件夹名称来导航目录树,直到到达包含index.php
或index.html
文件的文件夹;在这种情况下,网站/应用程序将被启动。
兼容性
PHP-Server与大多数PHP网站/应用程序兼容,即使它们似乎需要Apache的mod-rewrite
才能运行(例如:大多数Laravel应用程序运行得很好)。除非您需要高级Web服务器配置/功能,否则它应该可以“开箱即用”。
许可
此工具是开源软件,根据MIT许可授权。请参阅附带的LICENSE
文件。
版权 © Cláudio Silva和Impactwave, Lda。