Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PHP's Built-In Web Server with Symfony's Production Mode

I just created a new Symfony 3.4 application. Per Symfony's docs, I can server my application using PHP's built-in web-server by running

php bin/console server:start

However -- this appears to run the application in development mode. This means exception printing is on, the developer toolbar shows up, etc.

Is there a way to use the built-in web server with Symfony's production mode? This isn't to serve an application to the public (which wouldn't be a good idea) but instead because I want to test an issue that may only crop up in production mode.

like image 980
Alan Storm Avatar asked Feb 16 '18 23:02

Alan Storm


People also ask

What is PHP built in web server?

Built-in web server ¶ This web server is designed to aid application development. It may also be useful for testing purposes or for application demonstrations that are run in controlled environments. It is not intended to be a full-featured web server.

How stop PHP built in server?

You will now see some feedback in your terminal letting you know that your PHP server is listening for traffic on http://mysite.local:8080. To stop the server simply hit Ctrl+C in your running terminal.

What is Symfony server?

The Symfony server traverses the directory structure up to the root directory, so you can create a . php-version file in some parent directory to set the same PHP version for a group of projects under that directory. Run the command below if you don't remember all the PHP versions installed on your computer: 1 2 3 4 5.


2 Answers

Instead of symfony's server command you can run PHP's built-in web-server directly with the following command:

php -S localhost:8000 -t web/ app.php

This assumes that you are currently in your project directory and that the web/ directory hosts your app.php file.

-t denotes the document root. In this example that is web/.

app.php is specified as the router script and will handle your request.

More info on PHP's built-in web server command

Now when you go to localhost:8000 it will be serving your Symfony project in Production mode.

This doesn't run the server in the background, but you can easily search google for how to do that.

like image 115
hcoat Avatar answered Sep 19 '22 12:09

hcoat


If you are using Symfony 4 you need to enable the server bundle for all environments in the bundles.php file. By default it's only turned on for dev.

<?php

return [
    // ...
    Symfony\Bundle\WebServerBundle\WebServerBundle::class => ['all' => true],
];
like image 41
Ryan Avatar answered Sep 18 '22 12:09

Ryan