|
| 1 | +Symfony Local Web Server |
| 2 | +======================== |
| 3 | + |
| 4 | +You can run Symfony applications with any web server (Apache, nginx, the |
| 5 | +internal PHP web server, etc.) However, Symfony provides its own web server to |
| 6 | +make you more productive while developing your apps. |
| 7 | + |
| 8 | +This server is not intended for production use and it supports HTTP/2, TLS/SSL, |
| 9 | +automatic generation of security certificates, local domains, and many other |
| 10 | +features that sooner or later you'll need when developing web projects. |
| 11 | + |
| 12 | +Installation |
| 13 | +------------ |
| 14 | + |
| 15 | +The Symfony server is distributed as a free installable binary without any |
| 16 | +dependency and support for Linux, macOS and Windows. Go to `symfony.com/download`_ |
| 17 | +and follow the instructions for your operating system. |
| 18 | + |
| 19 | +Getting Started |
| 20 | +--------------- |
| 21 | + |
| 22 | +The Symfony web server is started once per project, so you may end up with |
| 23 | +several instances (each of them listening to a different port). This is the |
| 24 | +common workflow to serve a Symfony project: |
| 25 | + |
| 26 | +.. code-block:: terminal |
| 27 | +
|
| 28 | + $ cd my-project/ |
| 29 | + $ symfony server:start |
| 30 | +
|
| 31 | + [OK] Web server listening on http://127.0.0.1:.... |
| 32 | + ... |
| 33 | +
|
| 34 | + # Now, browse the given URL in your browser, or run this command: |
| 35 | + $ symfony open:local |
| 36 | +
|
| 37 | +Running the server this way makes it display the log messages in the console, so |
| 38 | +you can't run other commands. If you prefer, you can run the Symfony server in |
| 39 | +the background: |
| 40 | + |
| 41 | +.. code-block:: terminal |
| 42 | +
|
| 43 | + $ cd my-project/ |
| 44 | +
|
| 45 | + # start the server in the background |
| 46 | + $ symfony server:start -d |
| 47 | +
|
| 48 | + # continue working and running other commands... |
| 49 | +
|
| 50 | + # show the latest log messages |
| 51 | + $ symfony server:log |
| 52 | +
|
| 53 | +Enabling TLS |
| 54 | +------------ |
| 55 | + |
| 56 | +Browsing the secure version of your apps locally is important to early detect |
| 57 | +problems with mixed content and to run libraries that only run in HTTPS. |
| 58 | +Traditionally this has been painful and complicated to set up, but the Symfony |
| 59 | +server automates everything. First, run this command: |
| 60 | + |
| 61 | +.. code-block:: terminal |
| 62 | +
|
| 63 | + $ symfony server:ca:install |
| 64 | +
|
| 65 | +This command creates a local certificate authority, registers it in your system |
| 66 | +trust store, registers it in Firefox (this is required only for that browser) |
| 67 | +and creates a default certificate for ``localhost`` and ``127.0.0.1``. In other |
| 68 | +words, it does everything for you. You can now browse your local app using |
| 69 | +HTTPS instead of HTTP. |
| 70 | + |
| 71 | +Different PHP Settings Per Project |
| 72 | +---------------------------------- |
| 73 | + |
| 74 | +Selecting a Different PHP Version |
| 75 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 76 | + |
| 77 | +If you have multiple PHP versions installed in your computer, you can tell |
| 78 | +Symfony which one to use creating a file called ``.php-version`` at the project |
| 79 | +root dir: |
| 80 | + |
| 81 | +.. code-block:: terminal |
| 82 | +
|
| 83 | + $ cd my-project/ |
| 84 | +
|
| 85 | + # use a specific PHP version |
| 86 | + $ echo "7.2" > .php-version |
| 87 | +
|
| 88 | + # use any PHP 7.x version available |
| 89 | + $ echo "7" > .php-version |
| 90 | +
|
| 91 | +This other command is useful if you don't remember all the PHP versions |
| 92 | +installed in your computer: |
| 93 | + |
| 94 | +.. code-block:: terminal |
| 95 | +
|
| 96 | + $ symfony local:php:list |
| 97 | +
|
| 98 | +Overriding PHP Config Options Per Project |
| 99 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 100 | + |
| 101 | +You can change the value of any PHP runtime config option per project creating a |
| 102 | +file called ``php.ini`` at the project root dir. Add only the options you want |
| 103 | +to override: |
| 104 | + |
| 105 | +.. code-block:: terminal |
| 106 | +
|
| 107 | + $ cd my-project/ |
| 108 | +
|
| 109 | + # this project only overrides the default PHP timezone |
| 110 | + $ cat php.ini |
| 111 | + [Date] |
| 112 | + date.timezone = Asia/Tokyo |
| 113 | +
|
| 114 | +Running Commands with Different PHP Versions |
| 115 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 116 | + |
| 117 | +When running different PHP versions, it's useful to use the main ``symfony`` |
| 118 | +command as a wrapper of the ``php`` command to always select the most |
| 119 | +appropriate PHP version according to the project which is running the commands: |
| 120 | + |
| 121 | +.. code-block:: terminal |
| 122 | +
|
| 123 | + # runs the command with the default PHP version |
| 124 | + $ php -r "..." |
| 125 | +
|
| 126 | + # runs the command with the PHP version selected by the project |
| 127 | + # (or the default PHP version if the project didn't select one) |
| 128 | + $ symfony php -r "..." |
| 129 | +
|
| 130 | +If you are using this wrapper frequently, consider aliasing the ``php`` command |
| 131 | +to it: |
| 132 | + |
| 133 | +.. code-block:: terminal |
| 134 | +
|
| 135 | + $ cd ~/.symfony/bin |
| 136 | + $ cp symfony php |
| 137 | + # now you can run "php ..." and the "symfony" command will be executed instead |
| 138 | +
|
| 139 | +Local Domain Names |
| 140 | +------------------ |
| 141 | + |
| 142 | +By default, projects are accessible at some random port of the ``12.7.0.0.1`` |
| 143 | +local IP. However, sometimes is preferable to associate a domain name to them: |
| 144 | + |
| 145 | +* It's more convenient when you work continuously on the same project because |
| 146 | + port numbers can change but domains don't; |
| 147 | +* The behavior of some apps depend on their domains/subdomains; |
| 148 | +* To have stable endpoints, such as the local redirection URL of Oauth2. |
| 149 | + |
| 150 | +Setting up the Local Proxy |
| 151 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 152 | + |
| 153 | +Local domains are possible thanks to a local proxy provided by the Symfony |
| 154 | +server. First, start the proxy: |
| 155 | + |
| 156 | +.. code-block:: terminal |
| 157 | +
|
| 158 | + $ symfony proxy:start |
| 159 | +
|
| 160 | +If this is the first time you run the proxy, you must follow these additional steps: |
| 161 | + |
| 162 | +* Open the **network configuration** of your operating system; |
| 163 | +* Find the **proxy settings** and select the **"Automatic Proxy Configuration"**; |
| 164 | +* Set the following URL as its value: ``https://127.0.0.1:7080/proxy.pac`` |
| 165 | + |
| 166 | +Defining the Local Domain |
| 167 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 168 | + |
| 169 | +By default, Symfony proposes ``.wip`` (for *Work in Progress*) as the local |
| 170 | +domains (but you can choose any other domain and TLD you like). Define a local |
| 171 | +domain for a project as follows: |
| 172 | + |
| 173 | +.. code-block:: terminal |
| 174 | +
|
| 175 | + $ cd my-project/ |
| 176 | + $ symfony proxy:domain:attach my-domain.wip |
| 177 | +
|
| 178 | +If you have installed the local proxy as explained in the previous section, you |
| 179 | +can now browse ``https://my-domain.wip`` to access to your local project with |
| 180 | +the new custom domain. |
| 181 | + |
| 182 | +.. tip:: |
| 183 | + |
| 184 | + Browse the https://127.0.0.1:7080 URL to get the full list of local project |
| 185 | + directories, their custom domains, and port numbers. |
| 186 | + |
| 187 | +When running console commands, add the ``HTTPS_PROXY`` env var to make custom |
| 188 | +domains work: |
| 189 | + |
| 190 | +.. code-block:: terminal |
| 191 | +
|
| 192 | + $ HTTPS_PROXY=https://127.0.0.1:7080 curl https://my-domain.wip |
| 193 | +
|
| 194 | +Long-Running Commands |
| 195 | +--------------------- |
| 196 | + |
| 197 | +Long-running commands, such as the ones related to compiling front-end web |
| 198 | +assets, block the terminal and you can't run other commands. The Symfony server |
| 199 | +provides a ``run`` command to wrap them as follows: |
| 200 | + |
| 201 | +.. code-block:: terminal |
| 202 | +
|
| 203 | + # compile Webpack assets using Symfony Encore ... but do that in the |
| 204 | + # background to not block the terminal |
| 205 | + $ symfony run -d yarn encore dev --watch |
| 206 | +
|
| 207 | + # continue working and running other commands... |
| 208 | +
|
| 209 | + # from time to time, check the command logs if you want |
| 210 | + $ symfony server:log |
| 211 | +
|
| 212 | + # and you can also check if the command is still running |
| 213 | + $ symfony server:status |
| 214 | + Web server listening on ... |
| 215 | + Command "yarn ..." running with PID ... |
| 216 | +
|
| 217 | + # stop the command (and the whole server) when you are finished |
| 218 | + $ symfony server:stop |
| 219 | +
|
| 220 | +Bonus Features |
| 221 | +-------------- |
| 222 | + |
| 223 | +The Symfony server is much more than a local web server and it includes other |
| 224 | +useful features. |
| 225 | + |
| 226 | +Looking for Security Vulnerabilities |
| 227 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 228 | + |
| 229 | +Instead of installing the :doc:`Symfony Security Checker </security/security_checker>` |
| 230 | +as a dependency of your projects, you can use this command from the Symfony server: |
| 231 | + |
| 232 | +.. code-block:: terminal |
| 233 | +
|
| 234 | + $ symfony security:check |
| 235 | +
|
| 236 | +This command uses the same vulnerability database as the Symfony Security |
| 237 | +Checker but it also caches that information to keep checking security when |
| 238 | +it's not possible to access to that public database. |
| 239 | + |
| 240 | +Creating Symfony Projects |
| 241 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 242 | + |
| 243 | +In addition to the `different ways to install Symfony`_, you can use this |
| 244 | +command from the Symfony server: |
| 245 | + |
| 246 | +.. code-block:: terminal |
| 247 | +
|
| 248 | + # creates a new project based on the Symfony Skeleton |
| 249 | + $ symfony new my_project_name |
| 250 | +
|
| 251 | + # creates a new project based on the Symfony Website Skeleton |
| 252 | + $ symfony new --full my_project_name |
| 253 | +
|
| 254 | + # creates a new project based on the Symfony Demo application |
| 255 | + $ symfony new --demo my_project_name |
| 256 | +
|
| 257 | +SymfonyCloud Integration |
| 258 | +------------------------ |
| 259 | + |
| 260 | +The local Symfony server provides full, but optional, integration with |
| 261 | +`SymfonyCloud`_, a service optimized to run your Symfony apps on the cloud. |
| 262 | +It provides features such as creating environments, backups/snapshots, and |
| 263 | +even access to a copy of the production data in your local machine to help |
| 264 | +you debug any issues. |
| 265 | + |
| 266 | +`Read SymfonyCloud technical docs`_. |
| 267 | + |
| 268 | +.. _`symfony.com/download`: https://symfony.com/download |
| 269 | +.. _`different ways to install Symfony`: https://symfony.com/download |
| 270 | +.. _`SymfonyCloud`: https://symfony.com/cloud/ |
| 271 | +.. _`Read SymfonyCloud technical docs`: https://symfony.com/doc/master/cloud/intro.html |
0 commit comments