我试图建立一个PHP网站,我想测试我的PHP文件,而不是上传到我的主机。基本上是在上传之前先在我自己的机器上测试。我怎么做呢?


当前回答

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

其他回答

您还可以使用代码在PHP中创建自己的服务器!

<?php
set_time_limit(0);

$address = '127.0.0.1';
$port =4444;
$server = '$address + $port';

// <-- Starts Server
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($sock, $address, $port) or die('Could not bind to address');
echo "\n Server is running on port $port waiting for connection... \n\n";

while(1)
{
    socket_listen($sock);
    $client = socket_accept($sock);

    $input = socket_read($client, 443);

    $incoming = array();
    $incoming = explode("\r\n", $input);

    $fetchArray = array();
    $fetchArray = explode(" ", $incoming[0]);

    $file = $fetchArray[1];
    if($file == "/"){ 

   

$file = "src/browser.php";//当服务器启动时,这个文件是打开的!

    } else {

        $filearray = array();
        $filearray = explode("/", $file);
        $file = $filearray[1];
    }

echo $fetchArray[0] . " Request " . $file . "\n"; 

// <-- Control Header
$output = "";
$Header = "HTTP/1.1 200 OK \r\n" .
"Date: Fri, 31 Dec 1999 23:59:59 GMT \r\n" .
"Content-Type: text/html \r\n\r\n";

$Content = file_get_contents($file);
$output = $Header . $Content;

    socket_write($client,$output,strlen($output));
    socket_close($client);

}
print('server running..');

运行这段代码,然后打开浏览器到localhost:443或任何您选择的端口

另一种选择是Zend服务器社区版。

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

devilbox是一个现代的、高度可定制的dockerized PHP堆栈,支持完整的LAMP和MEAN,可以在所有主要平台上运行。主要目标是轻松切换和组合本地开发所需的任何版本。它支持无限数量的项目,自动为其创建vhosts和DNS记录。电子邮件全能和流行的开发工具也将为您服务。配置是不必要的,因为所有的东西都是预先设置的大规模虚拟主机。

让它运行起来非常简单:

# 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

链接:

Github: https://github.com/cytopia/devilbox 网站:http://devilbox.org

这是一个简单的,确保本地运行php服务器的方法:

php -S 0.0.0.0:<PORT_NUMBER>

PORT_NUMBER为1024 ~ 49151的整数

例如:php -S 0.0.0.0:8000

注:

如果你使用localhost而不是0.0.0.0,你可能会碰到一个 拒绝连接错误。 如果想让任何接口都可以访问web服务器,请使用0.0.0.0。 如果URI请求没有指定 文件,则index.php或index.html在给定目录下都是 返回。

给定以下文件(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>";
}
?>

运行这个…

php -S 0.0.0.0:8000 router.php

... 并在浏览器中导航到http://localhost:8000/,将显示如下:

Welcome to PHP

参考:

内置web服务器

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