Preface
PHP has provided a built-in web server since 5.4.
This is mainly used for local development. Cannot be used in online environments. Now I will introduce how to use this tool.
Basic Application
First we assume that the project directory is /home/baoguoxiao/www/php/demo
, and the directory accessible to the outside world is /home/baoguoxiao /www/php/demo/public
. Then the access port is 8000
, and the entry files are index.php
and index.html
. Then we can execute the following command:
cd /home/baoguoxiao/www/php/demo/public php -S localhost:8000
Then we can access it normally at this time.
So now there is a question, is it necessary to enter the public
folder every time to start the web server? In fact, we can specify the root directory, then we can use the following command:
cd /home/baoguoxiao/www/php/demo php -S localhost:8000 -t public/
So now there is a problem, that is, if we use a single entry, and still use the PATHINFO mode. Then there may be a problem with the above.
For this, we can use the following solution:
cd /home/baoguoxiao/www/php/demo php -S localhost:8000 router.php
router.php file code
/** * 对URL进行解析,并获取请求的文件名 */$uri = urldecode(parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH));/** * 判断是否存在该文件,如果不存在,则直接继续加载入口文件 */if ($uri !== "/" && file_exists(__DIR__ . "$uri")) { return false; }/** * 加载入口文件 */require_once "./index.php";
Through this routing file, we can support currently commonly used development situations .
Frame Reference
The above method is our own implementation, then we can also look at the implementation methods of related well-known frameworks.
Such as Laravel and Symfony.
Laravel
In the Installation section in Laravel, a command is introduced that can use the PHP built-in web server to achieve external access. The implemented command is:
php artisan serve
We can take a look at the relevant code:
The specific file path is: vendor/laravel/framework/src/Illuminate/Foundation/Console/ServeCommand.php
/** * 执行命令. * * @return int * * @throws \Exception */ public function handle() { // 切换路径到 public 目录 chdir(public_path()); // 在命令台进行输出相关内容 $this->line("<info>Laravel development server started:</info> <http:>host()}:{$this->port()}>"); // 执行外部程序,并且 $status 为系统的返回状态 passthru($this->serverCommand(), $status); // $status 为0 表示执行正常, 为其他大于0的数字表示出现了错误,有可能是端口被抢占了,这个时候就会接着判断是否进行再次尝试 if ($status && $this->canTryAnotherPort()) { // 对绑定的端口号加1 默认是8000, 如果失败则重试端口号为8001,再次失败重试端口号为8002,以此类推。 $this->portOffset += 1; // 再次调用此程序 return $this->handle(); } // 返回状态值 return $status; } /** * 获取完整的 server 命令. * * @return string */ protected function serverCommand() { return sprintf('%s -S %s:%s %s', // 获取PHP可执行命令的路径 ProcessUtils::escapeArgument((new PhpExecutableFinder)->find(false)), // 获取需要绑定的host $this->host(), // 获取需要绑定的端口 $this->port(), // 对需要执行的参数进行转义处理。这里的 server 就是我们之前说的路由文件,它在项目的根路径下 ProcessUtils::escapeArgument(base_path('server.php')) ); }</http:>
Translate the above command, it is actually executed
cd ./public php -S 0.0.0.0:8000 ../server.php
note:
Here we can see a difference, which is the code I wrote before. , hosts are all localhost, but what is written here is 0.0.0.0. What's the difference between these two?
In fact, the difference is very simple. For example, the IP bound to localhost I wrote before is 127.0.0.1. This is equivalent to a loopback address, so we only allow the local IP to access. And 0.0.0.0 means that we have no restrictions on IP and all IPs can be accessed.
Then let’s take a look at server.php
:
/** * Laravel - A PHP Framework For Web Artisans * * @package Laravel * @author Taylor Otwell <taylor> */ $uri = urldecode( parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ); // 这个文件允许我们从内置 PHP web 服务器中模拟 Apache 的 "mod_rewrite" 功能. // 这提供了一种测试 Laravel 应用程序的便捷方法, // 而无需在此安装"真正的" web 服务器软件。 if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) { return false; } require_once __DIR__.'/public/index.php';</taylor>
under the project root directory and find that it is the same as the routing file I wrote before. Yes, I copied it from here.
Basically this is how Larvel is implemented.
Symfony
If you are using the Symfony framework, you will find that Symfony has a component called web-server-bundle. This component has the same function as Laravel, without any help. The web server enables access to applications through a browser.
For basic operations, please refer to This page
I will mainly talk about how Symfony is implemented here.
There is a piece of code in Symfony which is Like this:
public function start(WebServerConfig $config, $pidFile = null) { // 获取默认的PID文件位置 $pidFile = $pidFile ?: $this->getDefaultPidFile(); // 判断是否在运行,如果运行则提示已经在监听了 if ($this->isRunning($pidFile)) { throw new \RuntimeException(sprintf('A process is already listening on http://%s.', $config->getAddress())); } // fork了一个子进程,如果成功,会有两个进程进行同时执行下面的文件,父进程,也就是当前执行的进程会返回子进程的PID,而子进程则返回的PID为0, // 如果失败,则子进程不会创建,并且父进程会返回的pid为-1。更多内容可查看 https://www.php.net/manual/zh/function.pcntl-fork.php $pid = pcntl_fork(); // 表示fork进程失败 if ($pid 0) { return self::STARTED; } // 从此往后是子进程运行,首先通过 posix_setsid 变为守护进程,意思是使其脱离终端的管理,自立门户,谁也没办法管理这个进程,除了PID。 if (posix_setsid() createServerProcess($config); $process->disableOutput(); $process->start(); // 判断是否运行成功 if (!$process->isRunning()) { throw new \RuntimeException('Unable to start the server process.'); } // 写入PID文件 file_put_contents($pidFile, $config->getAddress()); // 检测PID文件,如果PID文件删除了,那么进程就立即退出。 while ($process->isRunning()) { if (!file_exists($pidFile)) { $process->stop(); } sleep(1); } // 返回停止的状态 return self::STOPPED; } /** * 启动PHP内置web服务器 * @return Process The process */ private function createServerProcess(WebServerConfig $config) { // 查找PHP的可执行程序 $finder = new PhpExecutableFinder(); if (false === $binary = $finder->find(false)) { throw new \RuntimeException('Unable to find the PHP binary.'); } $xdebugArgs = ini_get('xdebug.profiler_enable_trigger') ? ['-dxdebug.profiler_enable_trigger=1'] : []; // 实例化PHP要执行的命令 php_path -dvariables_order=EGPCS -S 127.0.0.1:8000 vendor\symfony\web-server-bundle/Resources/router.php $process = new Process(array_merge([$binary], $finder->findArguments(), $xdebugArgs, ['-dvariables_order=EGPCS', '-S', $config->getAddress(), $config->getRouter()])); // 设置工作目录 $process->setWorkingDirectory($config->getDocumentRoot()); // 设置超时时间 $process->setTimeout(null); // 设置环境变量 if (\in_array('APP_ENV', explode(',', getenv('SYMFONY_DOTENV_VARS')))) { $process->setEnv(['APP_ENV' => false]); $process->inheritEnvironmentVariables(); } // 返回相关变量 return $process; }
I commented in the above code to describe how Symfony is started.
There is a problem in usingpcntl_fork
, this extension is not supported in Windows. Therefore, the Symfony framework will prompt you to use the php bin/console server:run
command to run the program.
Future Outlook
In fact, there is another way, that is, Workman is a web server implemented by itself, and it does not rely on the php -S
command. I haven't fully understood this part of the code, and I think this can be discussed in a few separate chapters. Hope to have this opportunity in the future.
Summary
Through our learning of PHP commands to achieve web server access and the analysis of Laravel and Symfony frameworks, I learned that in the development process of Windows, we can completely use this method to get rid of The dependence on the web server not only facilitates our development in the Windows environment but also allows us to learn a skill in PHP. It feels good.
If you have any questions about this, you can comment and communicate.
For more PHP-related technical articles, please visit the PHP Tutorial column to learn!
The above is the detailed content of PHP built-in web server. For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools