php 本地机器上的PHP服务器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1678010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:29:06  来源:igfitidea点击:

PHP server on local machine?

phpserverlocal

提问by Kozy

I'm trying to build a PHP site and I'm wanting to test my PHP files without uploading them to my host. Basically testing them on my own machine before I upload them. How do I do that?

我正在尝试构建一个 PHP 站点,我想测试我的 PHP 文件而不将它们上传到我的主机。在我上传它们之前,基本上在我自己的机器上测试它们。我怎么做?

采纳答案by Lukman

回答by GardenRouteGold

PHP 5.4 and later have a built-in web serverthese days.

如今,PHP 5.4 及更高版本具有内置的 Web 服务器

You simply run the command from the terminal:

您只需从终端运行命令:

cd path/to/your/app
php -S localhost:8000

Then in your browser go to http://localhost:8000and boom, your system should be up and running. (There must be an index.php or index.html file for this to work.)

然后在您的浏览器中转到http://localhost:8000并繁荣,您的系统应该启动并运行。(必须有一个 index.php 或 index.html 文件才能工作。)

回答by l3x

This is a simple, sure fire way to run your php server locally:

这是一种在本地运行 php 服务器的简单、可靠的方法:

php -S 0.0.0.0:<PORT_NUMBER>

Where PORT_NUMBER is an integer from 1024 to 49151

其中 PORT_NUMBER 是一个从 1024 到 49151 的整数

Example: php -S 0.0.0.0:8000

例子: php -S 0.0.0.0:8000

Notes:

笔记:

  1. If you use localhostrather than 0.0.0.0you may hit a connection refused error.

  2. If want to make the web server accessible to any interface, use 0.0.0.0.

  3. If a URI request does not specify a file, then either index.php or index.html in the given directory are returned.

  1. 如果您使用localhost而不是0.0.0.0您可能会遇到连接拒绝错误。

  2. 如果想让 Web 服务器可被任何界面访问,请使用0.0.0.0.

  3. 如果 URI 请求未指定文件,则返回给定目录中的 index.php 或 index.html。

Given the following file (router.php)

鉴于以下文件(router.php)

<?php
// router.php
if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) {
    return false;    // serve the requested resource as-is.
} else { 
    echo "<p>Welcome to PHP</p>";
}
?>

Run this ...

运行这个...

php -S 0.0.0.0:8000 router.php

... and navigate in your browser to http://localhost:8000/and the following will be displayed:

...并在浏览器中导航到http://localhost:8000/将显示以下内容:

Welcome to PHP

Reference:

参考:

Built-in web server

内置网络服务器

回答by Amitesh

I often use following command to spin my PHP Laravel framework :

我经常使用以下命令来旋转我的 PHP Laravel 框架:

$ php artisan serve --port=8080
or
$ php -S localhost:8080 -t public/

In above command : - Artisanis command-line interface included with Laravel which use serveto call built in php server

在上面的命令: -工匠是包含在Laravel命令行界面,使用服务调用内置的PHP服务器

To Run with built-in web server.

使用内置网络服务器运行。

 php -S <addr>:<port> -T

Here,
-S : Switch to Run with built-in web server.
-T : Switch to specify document root for built-in web server.

这里,
-S :切换到使用内置 Web 服务器运行。
-T : 切换到指定内置 web 服务器的文档根目录。

回答by Chris Sobolewski

I use WAMP. One easy install wizard, tons of modules to for Apache and PHP preconfigured and easy to turn on and off to match your remote config.

我使用WAMP。一个简单的安装向导,为 Apache 和 PHP 预配置了大量模块,并且易于打开和关闭以匹配您的远程配置。

回答by cytopia

If you want an all-purpose local development stack for any operating system where you can choose from different PHP, MySQL and Web server versions and are also not afraid of using Docker, you could go for the devilbox.

如果您想要一个适用于任何操作系统的通用本地开发堆栈,您可以从不同的 PHP、MySQL 和 Web 服务器版本中进行选择,并且不怕使用 Docker,那么您可以选择devilbox

The devilbox is a modern and highly customisable dockerized PHP stack supporting full LAMP and MEAN and running on all major platforms. The main goal is to easily switch and combine any version required for local development. It supports an unlimited number of projects for which vhosts and DNS records are created automatically. Email catch-all and popular development tools will be at your service as well. Configuration is not necessary, as everything is pre-setup with mass virtual hosting.

Devilbox 是一个现代且高度可定制的 dockerized PHP 堆栈,支持完整的 LAMP 和 MEAN 并在所有主要平台上运行。主要目标是轻松切换和组合本地开发所需的任何版本。它支持自动创建虚拟主机和 DNS 记录的无限数量的项目。电子邮件包罗万象和流行的开发工具也将为您服务。无需配置,因为所有内容都已通过大规模虚拟主机预先设置。

Getting it up and running is pretty straight-forward:

启动并运行它非常简单:

# Get the devilbox
$ git clone https://github.com/cytopia/devilbox
$ cd devilbox

# Create docker-compose environment file
$ cp env-example .env

# Edit your configuration
$ vim .env

# Start all containers
$ docker-compose up

devilbox

魔盒

Links:

链接:

回答by outis

Install XAMPP. If you're running MS Windows, WAMPis also an option.

安装XAMPP。如果您运行的是 MS Windows,WAMP也是一种选择。

回答by Joey Blake

MAMP if you are on a MAC MAMP

MAMP 如果您使用的是 MAC MAMP

回答by Jens A. Koch

If you are using Windows, then the WPN-XM Server Stackmight be a suitable alternative.

如果您使用的是 Windows,那么WPN-XM 服务器堆栈可能是一个合适的替代方案。

回答by Joe Internet

Another option is the Zend Server Community Edition.

另一种选择是Zend Server Community Edition