PHP 服务器在本地计算机上吗?

发布于 2024-08-09 23:15:47 字数 79 浏览 8 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

旧人九事 2024-08-16 23:15:47

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

参考:

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

You simply run the command from the terminal:

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

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

You could also add a simple Router

<?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');
}
?>

And then run the command

php -S 127.0.0.1:8000 router.php

References:

呆橘 2024-08-16 23:15:47

安装并运行 XAMPP:

更新了下载链接 https://www.apachefriends.org/download.html

Install and run XAMPP:

Updated link for download https://www.apachefriends.org/download.html

浅笑轻吟梦一曲 2024-08-16 23:15:47

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

php -S 0.0.0.0:<PORT_NUMBER>

其中 PORT_NUMBER 是从 1024 到 49151 的整数

示例:php -S 0.0.0.0:8000

注意:

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

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

  3. 如果 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

参考:

内置网络服务器

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

php -S 0.0.0.0:<PORT_NUMBER>

Where PORT_NUMBER is an integer from 1024 to 49151

Example: php -S 0.0.0.0:8000

Notes:

  1. If you use localhost rather than 0.0.0.0 you 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.

Given the following file (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:

Welcome to PHP

Reference:

Built-in web server

梦中楼上月下 2024-08-16 23:15:47

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

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

在上面的命令中:
- Artisan 是 Laravel 附带的命令行界面,它使用 serve 调用内置 php 服务器

来使用内置 Web 服务器运行。

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

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

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

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

In above command :
- Artisan is command-line interface included with Laravel which use serve to call built in php server

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.

乖乖 2024-08-16 23:15:47

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

devilbox 是一个现代且高度可定制的 dockerized PHP 堆栈,支持完整的 LAMP 和 MEAN 并在所有主要平台上运行。主要目标是轻松切换和组合本地开发所需的任何版本。它支持自动创建虚拟主机和 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

devilbox

链接:

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.

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.

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:

維他命╮ 2024-08-16 23:15:47

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

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.

時窥 2024-08-16 23:15:47

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

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

蘸点软妹酱 2024-08-16 23:15:47

MAMP(如果您使用的是 MAC)MAMP

MAMP if you are on a MAC MAMP

情仇皆在手 2024-08-16 23:15:47

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

  • Apache
  • PHP
  • MySQL
  • phpMyAdmin

它还会给你一个启动和 Apache 的停止按钮。我觉得这非常有用。

AppServ is a small program in Windows to run:

  • Apache
  • PHP
  • MySQL
  • phpMyAdmin

It will also give you a startup and stop button for Apache. Which I find very useful.

心如狂蝶 2024-08-16 23:15:47

如果您有一台带有正确软件的本地计算机:支持 PHP 的 Web 服务器,那么您没有理由不能按照您所描述的方式进行操作。

我目前正在 Windows XP 计算机上使用 XAMPP 进行此操作,并且(在家里)使用 Kubuntu 和 LAMP 堆栈进行此操作。

If you have a local machine with the right software: web server with support for PHP, there's no reason why you can't do as you describe.

I'm doing it at the moment with XAMPP on a Windows XP machine, and (at home) with Kubuntu and a LAMP stack.

后eg是否自 2024-08-16 23:15:47

另一个选择是 Zend Server 社区版

Another option is the Zend Server Community Edition.

ま昔日黯然 2024-08-16 23:15:47

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

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

吻泪 2024-08-16 23:15:47

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

您将拥有 Apache 将提供服务的 htdocs 目录(可通过 http://localhost/ 访问),并且应该能够将您的PHP 文件在那里。在我的安装中,它位于 C:\xampp\htdocs。

Use Apache Friends XAMPP. It will set up Apache HTTP server, PHP 5 and MySQL 5 (as far as I know, there's probably some more than that). You don't need to know how to configure apache (or any of the modules) to use it.

You will have an htdocs directory which Apache will serve (accessible by http://localhost/) and should be able to put your PHP files there. With my installation, it is at C:\xampp\htdocs.

东风软 2024-08-16 23:15:47

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

you can create your own server in php using code as well!

<?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";// this file is open with server when it starts!

    } 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..');

run this code then open browser to localhost:443 or whichever port you chose

冧九 2024-08-16 23:15:47

即使您的计算机上已有服务器,执行此操作的一种简洁方法是使用 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

注释:

  • < p>如果您没有安装 Docker,则 Debian/Ubuntu 和 Windows 10+ 的说明位于最后。它可以安装在 Windows 7 上,但非常烦人且不值得。对于 Windows 7,如果必须的话,我只需安装 Uniserver 或 XAMPP 或类似的东西。

  • 您可以通过在主机上的终端中运行 docker ps 来确认容器是否处于活动状态。

  • 为了在容器终止/删除后保留您的应用/代码修改,Web 根目录绑定到您运行 docker run 命令的文件夹。要更改它,请在中指定本地文件夹的路径
    --mount source='[/local/path]' 参数。 注意:由于文件夹绑定到容器,因此您在容器中所做的更改也会在主机文件夹中进行。

  • 可以使用以下命令查看日志命令(--follow 是可选的,ctrl+c 退出):

    docker 日志 php --follow
    
  • 容器中的 Web 根文件夹是 /应用程序。如果您不需要保存任何内容并且不想在 docker run 命令中指定绑定挂载,这可能会很有帮助。


  • 使用-p [主机端口]:80参数指定端口。您可能必须显式指定 -p 80:80 才能从 Web 浏览器连接到容器(至少在 Windows 10 上)。

  • 要访问容器的 bash 终端,请从主机运行此命令(输入 exit 返回主机):

    docker exec -it php /bin/bash
    
  • 您可以像在本机 Debian 上一样在容器的 bash 终端中安装软件包/Ubuntu 盒子(例如 apt install -y nano)。

  • Composer 已安装(从容器的终端运行 composer -v 进行检查)

  • 要启动其他容器,请使用 --name=[new_name 指定不同的主机端口和容器名称]-p [主机端口]:80 参数。

  • 如果您需要数据库或其他服务器,请使用 MySQL 或 MariaDB 或任何您需要的 docker 映像执行相同的操作。只需记住将数据文件夹绑定到主机文件夹,这样如果您不小心删除了 docker 映像,就不会丢失它。

  • 如何安装 Docker:

    Debian/Ubuntu 作为 root(或在每个命令之前添加 sudo):

    apt-get 更新
    apt install -y ca-证书curl gnupg lsb-release
    mkdir -p /etc/apt/keyrings
    卷曲-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) 稳定” | sudo tee /etc/apt/sources.list.d/docker.list > /dev/空
    chmod a+r /etc/apt/keyrings/docker.gpg
    apt-get 更新
    apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
    服务 docker 启动
    systemctl 启用 docker
    

    Windows 10+(在 10 上测试,应该适用于 >10):

    使用 Chocolatey,Windows 命令行包管理器。如果你坚持的话,Chocolatey 也有一个 GUI。安装后,运行:

    choco install -y docker-desktop
    

    MacChromebook 等:

    你孤身一人。但我们相信你。

A clean way to do this, even if you have existing servers on your machine, is to use Docker. Run from any terminal via docker run with a single line:

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

You will now have a running container named php serving requests on your localhost, port 80. You should be able to see your php scripts in any browser using the url http://127.0.0.1

Notes:

  • 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.

孤檠 2024-08-16 23:15:47

可以使用 PHP 内置服务器。确保在设置中放置 php.exe 路径 在此处输入图像描述
然后只需选择此选项,它将在您的浏览器中运行。
输入图片此处描述

its possible using the PHP built-in server. Make sure to place php.exe path in settings enter image description here
then just select this option and it will run in ur browser.
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文