PHP 5.4’s New Built-in Web Server

Share this article

One of the more interesting facilities provided in PHP 5.4 is a built-in web server. It runs from the command line on Windows, Mac or Linux. You just need to ‘cd’ to the folder where your application resides then execute:


php -S localhost:8000
This will start a console-based web server. The document root is located in the current folder:
PHP 5.4.0 Development Server started at Mon Dec 19 11:56:05 2011
Listening on localhost:8000
Document root is /home/owner/myapp
Press Ctrl-C to quit
You can then open http://localhost:8000/ in your browser. If you don’t explicitly include a filename, the server will return either index.php or index.html in the root folder. All file requests are logged to the console window. It’s also possible to specify a PHP “router” file on the command line, e.g.

php -S localhost:8000 -t ./home/owner/myapp routing.php
The server analyzes the output of routing.php:
  • If the script returns false, the requested resource (or 404) is returned as-is.
  • If the script returns anything else, that output is returned to the browser.
For example, the following router script will return image files but all other requests will display “Welcome to the PHP Server!”:

<?php
if (preg_match('/.(?:png|jpg|jpeg|gif)$/', $_SERVER['REQUEST_URI']))
	return false; // serve the requested image
else {
	echo "<p>Welcome to the PHP Server!</p>";
}
PHP 5.4’s web server is intended for development purposes and I suspect it will be adopted by text editors, IDEs and possibly browser plug-ins as an easy way to test PHP code. There’s no reason why you couldn’t use it on small internal server — and some people will try — but there are certainly more appropriate solutions. However, we could see a few interesting uses such as web application demonstrations which can run from a CD. PHP 5.4 RC1 was released recently. I doubt you’ll need to wait long for the final version.

Frequently Asked Questions about PHP 5.4 Web Server

What are the key features of PHP 5.4 web server?

PHP 5.4 web server comes with a host of features that make it a preferred choice for many developers. It includes a built-in web server for testing purposes, a significant performance improvement over its predecessors, and a reduced memory footprint. It also introduces traits, a mechanism for code reuse in single inheritance languages such as PHP. Additionally, PHP 5.4 includes new language syntax including short array syntax, and it removes several legacy behaviors, making the language more consistent.

How can I install PHP 5.4 on my web server?

The installation process for PHP 5.4 varies depending on the operating system of your web server. For most Linux distributions, you can use the package manager to install PHP. For Windows servers, you can download the PHP installer from the official PHP website. After downloading, you can follow the installation instructions provided in the installer.

What are the differences between PHP 5.4 and other versions?

PHP 5.4 introduces several improvements and new features compared to previous versions. It has a built-in web server for testing purposes, traits for code reuse, and a new array syntax. It also removes several legacy behaviors, making the language more consistent. However, it’s worth noting that PHP 5.4 is no longer actively supported, and it’s recommended to use more recent versions of PHP for security and performance reasons.

How can I upgrade from PHP 5.4 to a newer version?

Upgrading from PHP 5.4 to a newer version involves several steps. First, you need to backup your current PHP configuration and files. Then, you can download the newer version of PHP from the official website and install it on your server. After installation, you need to update your server configuration to use the new version of PHP. Finally, you should test your applications to ensure they work correctly with the new version.

What are the security implications of using PHP 5.4?

PHP 5.4 is no longer actively supported, which means it doesn’t receive security updates. This can make your server vulnerable to security exploits. Therefore, it’s recommended to upgrade to a newer version of PHP that receives regular security updates.

How can I configure PHP 5.4 on my web server?

You can configure PHP 5.4 by editing the php.ini file, which is the main configuration file for PHP. This file allows you to change various settings such as error reporting, file uploads, and extensions.

What hosting providers support PHP 5.4?

Many hosting providers support PHP 5.4, including GoDaddy, HostGator, and A2 Hosting. However, due to the lack of security updates for PHP 5.4, many providers encourage their users to upgrade to a newer version of PHP.

What are the alternatives to PHP 5.4?

There are several alternatives to PHP 5.4, including newer versions of PHP such as PHP 7 and PHP 8. These versions offer improved performance, security, and new features. Other alternatives include different programming languages such as Python, Ruby, and JavaScript.

How can I troubleshoot issues with PHP 5.4?

Troubleshooting issues with PHP 5.4 involves checking the error logs, which can provide information about any errors or issues. You can also use various debugging tools available for PHP. If you’re having issues after upgrading from a previous version, it may be due to deprecated features or changes in the language syntax.

What are the performance implications of using PHP 5.4?

PHP 5.4 offers significant performance improvements over its predecessors. However, newer versions of PHP, such as PHP 7 and PHP 8, offer even greater performance improvements. Therefore, while PHP 5.4 may be faster than older versions, it’s still slower than the latest versions.

Craig BucklerCraig Buckler
View Author

Craig is a freelance UK web consultant who built his first page for IE2.0 in 1995. Since that time he's been advocating standards, accessibility, and best-practice HTML5 techniques. He's created enterprise specifications, websites and online applications for companies and organisations including the UK Parliament, the European Parliament, the Department of Energy & Climate Change, Microsoft, and more. He's written more than 1,000 articles for SitePoint and you can find him @craigbuckler.

developer toolsPHPserverweb server
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week