Skip to content

[Cookbook] add cookbook article for the server:run command #4000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cookbook/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The Cookbook
templating/index
testing/index
validation/index
web_server/index
web_services/index
workflow/index

Expand Down
5 changes: 5 additions & 0 deletions cookbook/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@

* :doc:`/cookbook/validation/custom_constraint`

* :doc:`/cookbook/web_server/index`

* :doc:`/cookbook/web_server/built_in`
* (configuration) :doc:`/cookbook/configuration/web_server_configuration`

* :doc:`/cookbook/web_services/index`

* :doc:`/cookbook/web_services/php_soap_extension`
Expand Down
60 changes: 60 additions & 0 deletions cookbook/web_server/built_in.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
.. index::
single: Web Server; Built-in Web Server

How to Use PHP's built-in Web Server
====================================

Since PHP 5.4 the CLI SAPI comes with a `built-in web server`_. It can be used
to run your PHP applications locally during development, for testing or for
application demonstrations. This way, you don't have to bother configuring
a full-featured web server such as
:doc:`Apache or Nginx </cookbook/configuration/web_server_configuration>`.

.. caution::

The built-in web server is meant to be run in a controlled environment.
It is not designed to be used on public networks.

Starting the Web Server
-----------------------

Running a Symfony application using PHP's built-in web server is as easy as
executing the ``server:run`` command:

.. code-block:: bash

$ php app/console server:run

This starts a server at ``localhost:8000`` that executes your Symfony application.
The command will wait and will respond to incoming HTTP requests until you
terminate it (this is usually done by pressing Ctrl and C).

By default, the web server listens on port 8000 on the loopback device. You
can change the socket passing an ip address and a port as a command-line argument:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this (for the same reason as above):

If you want the web server to bind to a different IP address or listen to a different port
just pass these as an argument to the command:


.. code-block:: bash

$ php app/console server:run 192.168.0.1:8080

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a really small section about VM's. Something like, which just comes from a few times of experience (so, I'll want someone else to give me a 👍 on this):

Using the built-in web server from inside a Virtual Machine
-----------------------------------------------------------

If you want to use the built-in web server from inside a virtual machine and then load the site
from a browser on your host machine, you'll need to listen on the `127.0.0.1` address:

.. code-block:: bash

    $ php app/console server:run 127.0.0.1:8000

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of such an addition. What do you think about a sidebar for this?

However, listening on 127.0.0.1 wouldn't work since it's the loopback device. I guess usually the internal interface is something like 192.168.0.1, 10.0.0.1 or similar.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like a sidebar.

We should have someone try this - I don't have a VM handy (and my laptop's meager HD is too small to run one), but I think I remember 127.0.0.1 working with VirtualBox.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure that this doesn't work. But I agree that we should find someone to verify this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I just had a chance to try this again. It's 0.0.0.0 that works. If you start that up inside the VM, you'll be able to access the site from outside the vm (e.g. via 192.168.56.150, if that's the address to your VM).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, 0.0.0.0 just means that you want to listen on all interfaces. That's probably the best idea because you can then access it from inside and outside the VM. Of course, if you don't plan to access the application from inside the VM, 192.168.56.150 will be sufficient too. But I don't think we should confuse people with too much information here. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree - use 0.0.0.0. And then we can say:

You can change the socket by passing an IP address and a port. For example,
the following will listen to requests that are connecting via any IP address (useful
inside virtual machines):

Command Options
---------------

The built-in web server expects a "router" script (read about the "router"
script on `php.net`_) as an argument. Symfony already passes such a router
script when the command is executed in the ``prod`` or in the ``dev`` environment.
Use the ``--router`` option in any other environment or to use another router
script:

.. code-block:: bash

$ php app/console server:run --env=test --router=app/config/router_test.php

If your application's document root differs from the standard directory layout,
you have to pass the correct location using the ``--docroot`` option:

.. code-block:: bash

$ php app/console server:run --docroot=public_html

.. _`built-in web server`: http://www.php.net/manual/en/features.commandline.webserver.php
.. _`php.net`: http://php.net/manual/en/features.commandline.webserver.php#example-401
7 changes: 7 additions & 0 deletions cookbook/web_server/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Web Server
==========

.. toctree::
:maxdepth: 2

built_in
4 changes: 4 additions & 0 deletions quick_tour/the_big_picture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ to run Symfony:

$ php app/console server:run

.. seealso::

Read more about the internal server :doc:`in the cookbook </cookbook/web_server/built_in>`.

If you get the error `There are no commands defined in the "server" namespace.`,
then you are probably using PHP 5.3. That's ok! But the built-in web server is
only available for PHP 5.4.0 or higher. If you have an older version of PHP or
Expand Down