Sophomore Dev
The Dev You Say

Posts Tagged ‘PHP’

Handling Multiple Requests Concurrently: A Guide to Improving PHP’s Built-in Server Performance

Wednesday, May 3rd, 2023

This is actually the post I originally wanted to make. The reason was simple. I didn’t find a lot of info about the topic, and this is what the official PHP docs have to say:

You can configure the built-in webserver to fork multiple workers in order to test code that requires multiple concurrent requests to the built-in webserver. Set the PHP_CLI_SERVER_WORKERS environment variable to the number of desired workers before starting the server. This is not supported on Windows.

If you have ever used PHP’s built-in server for more than, let’s say testing your site’s code locally you have probably run into concurrency issues. Servers are responsible for handling a vast amount of requests concurrently. In a single-process synchronous mode, requests are dealt with individually and can block the server from executing until they complete. This can lead to delays in processing and reduce overall server performance.

To tackle this issue, PHP 7.4 introduced support for handling multiple requests concurrently. This feature relies on fork() availability and doesn’t work on Windows. The server forks new workers to serve each incoming request in this mode.

Enabling Concurrent Request Handling

You can activate concurrent request handling by setting the PHP_CLI_SERVER_WORKERS environment variable to the number of workers you want:

PHP_CLI_SERVER_WORKERS=8 php -S localhost:8080

Of you can do the same in code. For example using a wrapper script to enabled the number of workers, like so:


This command spawns eight concurrent worker processes capable of handling simultaneous requests.

Things to Keep in Mind When Using Multiple Workers

While using multiple workers can significantly improve server performance, there are some things you need to keep in mind when using them:

  1. Memory Usage – Each worker process requires an independent copy of memory, which means that enabling too many workers could lead to high memory usage and slow down your system’s performance.
  2. Database Connection Limitations – If you’re using multiple database connections, make sure that your connection pool supports concurrent access without encountering deadlocks or other synchronization issues.
  3. Shared Resources Management – When working with shared resources such as files or disk space, ensure that multiple workers do not interfere with each other while accessing these resources at the same time.

Concurrent request handling is an excellent way of improving server performance by allowing the server to handle multiple requests simultaneously. However, it’s important to keep some things in mind while using multiple workers, such as memory usage, database connection limitations, and shared resource management.

Handling concurrent requests is still marked as experimental in PHP 8.1; however, it promises better stability and performance enhancements in the future.

Exploring the Benefits of PHP Built-In Web Server

Tuesday, May 2nd, 2023

And Now for a Public Service Announcement

Well, well, well, look who’s back! It’s me, your favorite forgetful former Sophomore Dev Andrew who disappeared off the face of the internet for a couple of years (not really). Remember me? No? Yeah, I don’t blame you. Even I forgot about my blog until recently when I stumbled upon it by accident while trying to find a recipe for vegan lasagna (not really). Oopsie! So here we are, back at it again with the mediocre content and occasional spelling errors. But hey, a lot has changed in the past few years – like seriously, have you seen what’s going on in the world right now? So buckle up and let’s see if I can still string together some coherent sentences and make you laugh (or mostly – cringe) along the way.

Why am I writing about the built-in PHP web server today? Well, because I actually wanted to write about something else entirely, something that could be summed up in two sentences, and that seemed like a bad start to rebooting the site.

PHP is one of the most popular web development languages used in the world today. It provides developers with a robust set of tools to create feature-rich and dynamic web applications. One such tool that PHP offers is its built-in web server – a handy way to test applications and share local files on your network quickly.

We’ll dive deeper into what the PHP built-in web server is, how it works, and why it’s useful for developers.

What is the PHP Built-In Web Server?

The PHP built-in web server is precisely what it sounds like – a lightweight server that comes bundled with PHP installations. It allows developers to serve their web applications locally without requiring external software like Apache or Nginx.

The built-in server supports execution of PHP scripts, catch-all routing, and static files with common MIME types. It provides an integrated solution for testing and development purposes, allowing developers to work efficiently in a familiar environment.

Why Use The Built-In Web Server?

There are several benefits to using the PHP built-in web server:

  1. Simplicity: The built-in web server is easy to set up and requires no additional configuration or installation processes beyond installing PHP itself.
  2. Lightweight: Since it’s designed only for development purposes and not production use, it’s lightweight in nature, making it faster than other servers like Apache or Nginx.
  3. Security: The built-in web server has limited features compared to dedicated servers; this makes it less prone to vulnerabilities and hacking attempts.
  4. Cross-Platform Compatibility: The built-in server runs on all major operating systems; Windows, Mac OS X, Linux/Unix-based systems as well as different versions of PHP (5.x – 8.x).
  5. Integrated Debugging: Developers can take advantage of integrated debugging tools since both the application code and the webserver run on the same machine.

How to Use the PHP Built-In Web Server

Using the built-in web server is straightforward. First, ensure PHP is installed on your computer. Then open a terminal or command prompt and navigate to your project’s root directory. Finally, start the web server with the following command:

php -S localhost:8000

This command will start the built-in webserver at port 8000 of your local machine. Visit http://localhost:8000 in your browser to see your application running.

You can also specify a different port like this:

php -S localhost:8080

The PHP Built-In Web Server is a great tool for developers who want an easy-to-use, lightweight solution for testing and developing their applications locally. It provides all the necessary features required for development purposes without compromising on performance or security. If you’re looking for a convenient way to test your applications without relying on external servers, then give the built-in web server a try today!