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


安装并运行XAMPP: http://www.apachefriends.org/en/xampp.html


安装XAMPP。如果你用的是MS Windows, WAMP也是一种选择。


使用Apache Friends XAMPP。它将设置Apache HTTP服务器,PHP 5和MySQL 5(据我所知,可能还有更多)。你不需要知道如何配置apache(或任何模块)来使用它。

你会有一个Apache提供的htdocs目录(可以通过http://localhost/访问),应该可以把你的PHP文件放在那里。在我的安装中,它位于C:\xampp\htdocs。


如果你有一台本地机器,有正确的软件:支持PHP的web服务器,没有理由你不能像你描述的那样做。

目前我正在一台Windows XP机器上使用XAMPP,(在家里)使用Kubuntu和LAMP堆栈。


如果你在MAC MAMP上


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


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


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


现在PHP 5.4及以后的版本都有内置的web服务器。

你只需在终端上运行命令:

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

然后在浏览器中转到http://127.0.0.1:8000,系统应该已经启动并运行了。(必须有index.php或index.html文件才能工作。)

您还可以添加一个简单的路由器

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

然后执行命令

php -S 127.0.0.1:8000 router.php

引用:

https://www.php.net/manual/en/features.commandline.webserver.php https://www.php.net/manual/en/features.commandline.options.php


这是一个简单的,确保本地运行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服务器


如果你想要一个适用于任何操作系统的通用本地开发堆栈,你可以从不同的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


AppServ是一个在Windows中运行的小程序:

Apache PHP MySQL phpMyAdmin

它还将为Apache提供启动和停止按钮。我觉得这很有用。


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

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

在上述命令中: Artisan是Laravel中包含的命令行界面,它使用服务调用内置的php服务器

使用内置的web服务器运行。

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

在这里, -S:切换到运行内置web服务器。 -T:开关 为内置web服务器指定文档根目录。


您还可以使用代码在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或任何您选择的端口


即使您的机器上有现有的服务器,也可以使用Docker。通过docker Run从任何终端运行一行代码:

docker run --name=php -d -it -p 80:80 --mount type=bind,source='/absolute/path/to/your/php/web/root/folder/',target=/app webdevops/php-nginx-dev

现在,您将有一个名为php的运行容器在本地主机端口80上为请求提供服务。您应该能够在使用url http://127.0.0.1的任何浏览器中看到您的php脚本

注:

If you don't have Docker installed, instructions for Debian/Ubuntu and Windows 10+ are at the end. It can be installed on Windows 7 but it's quite annoying and not worth it. For windows 7, if you must, I'd just install Uniserver or XAMPP or something like that. You can confirm that the container is live by running docker ps in a terminal on the host machine. In order to keep your app/code modifications after the container is terminated/removed, the web root is bound to the folder where you ran the docker run command. To change it, specify the path to your local folder in the --mount source='[/local/path]' parameter. Note: Because the folder is bound to the container, changes you make in the container will also be made in the host folder. Logs can be viewed using the following command (--follow is optional, ctrl+c to exit): docker logs php --follow The web root folder in the container is /app. This may be helpful if you don't need to save anything and don't feel like specifying a bind mount in the docker run command. The port is specified using the -p [host port]:80 parameters. You may have to explicitly specify -p 80:80 in order to be able to connect to the container from a web browser (at least on Windows 10). To access the container's bash terminal run this from the host machine (type exit to return to host): docker exec -it php /bin/bash You can install packages in the container's bash terminal the same way that you would on a native Debian/Ubuntu box (e.g. apt install -y nano). Composer is already installed (run composer -v from container's terminal to inspect) To launch an additional container, specify a different host port and container name using the --name=[new_name] and -p [host port]:80 parameters. If you need a database or other server, do the same thing with a docker image for MySQL or MariaDB or whatever you need. Just remember to bind the data folder to a host folder so you don't lose it if you accidentally delete your docker image(s). How to install Docker: Debian/Ubuntu as root (or add sudo before each of these commands): apt-get update apt install -y ca-certificates curl gnupg lsb-release mkdir -p /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null chmod a+r /etc/apt/keyrings/docker.gpg apt-get update apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin service docker start systemctl enable docker Windows 10+ (tested on 10, should work on >10): Use Chocolatey, a command-line package manager for Windows. Chocolatey also has a gui if you insist. Once installed, run: choco install -y docker-desktop Mac, Chromebook, etc: You are alone. But we believe in you.