Symfony Local Web Server ... reloaded

Symfony Local Web Server ... reloaded

9a22d09f92d50fa3d2a16766d0ba52f8?s=128

Fabien Potencier

December 06, 2018
Tweet

Transcript

  1. 3.

    The state of local web servers with Symfony php -S

    Many limitations Docker Might be slow/inconvenient Nginx/Apache Need setup Vagrant Not convenient
  2. 4.

    My ideal setup? Local Web Server and local files Remote

    services Works well on the major platforms Fast and convenient
  3. 5.

    PHP built-in server is almost perfect ... but it has

    too many limitations • Only one connection at a time • No HTTP-2 support • No SSL • Does not re-read env vars if they change • Needs some tweaks to make it work with Symfony (special router) • …
  4. 7.

    …via the new symfony binary • Introduced last week with

    • Binary with no dependencies, not even PHP
 No global Composer changes • Does more than just the Cloud • symfony security:check
 without calls to security.symfony.com • symfony new [--demo] [--full] ...
 to create new Symfony projects • and … https://symfony.com/cloud/download
  5. 8.

    A Symfony local web server # From your project's directory

    $ symfony server:start # Done! First port available starts at 8000
  6. 9.

    A Symfony local web server $ symfony server:start -d #

    open in the browser $ symfony open:local # tail the logs $ symfony server:log Start in the background
  7. 10.

    Some features • No need to be root • Uses

    PHP-FPM whenever possible
 fallbacks to PHP-CGI/php -S (on Windows for instance) • HTTP-2 support out of the box • HTTP Link/pre-load/push support • Works with any PHP project
 Use it for plain HTML websites, Symfony ones, Laravel, Drupal, Magento, … • Fire and forget • Great developer experience…
  8. 12.

    Why? • Testing a new PHP version or a different

    configuration? • Different projects might need different PHP versions • Most machines have several PHP versions installed
 Think homebrew for instance
  9. 13.
  10. 14.

    Use any of them locally # Change the PHP version

    for the current directory $ echo "7.1" > .php-version # Or any 7.x version? $ echo "7" > .php-version # Done!
  11. 15.

    Each web server can have its own PHP version #

    Detect the PHP version to use for this directory $ symfony server:start Algorithm (current directory and up) 1/ .php-version? 2/ .symfony.cloud.yaml? 3/ $PATH
  12. 16.

    The web server uses the “best” backend possible # Detect

    the best possible backend $ symfony server:start Algorithm 1/ PHP-FPM 2/ PHP-CLI (also FastCGI) 3/ php -S
  13. 17.

    Works also in your terminal # Use the symfony wrapper

    $ symfony php -v $ symfony php -r "echo 'Symfony!';" Algorithm (current directory and up) 1/ .php-version? 2/ .symfony.cloud.yaml? 3/ $PATH
  14. 18.

    Configure PHP per directory $ symfony php -r "echo ini_get('date.timezone');"

    Europe/Paris # Create a php.ini file at the project's directory $ cat php.ini [Date] date.timezone = Europe/Lisbon $ symfony php -r "echo ini_get('date.timezone');" Europe/Lisbon # Works with the web server as well
  15. 19.

    # Using the wrapper all the time? # Create a

    copy named php $ cd ~/.symfony/bin/ && cp symfony php # Running php now runs the symfony wrapper! # Works with other PHP binaries $ cd ~/.symfony/bin/ && cp symfony pecl $ cd ~/.symfony/bin/ && cp symfony php-fpm $ cd ~/.symfony/bin/ && cp symfony php-config Want to drink the Kool-Aid?
  16. 22.

    All the logs are parsed as semantic JSON logs and

    "humanized" From the web server From Symfony From PHP
  17. 24.

    # Use it even if you are using another web

    server # to get pretty Symfony logs $ symfony server:log --file=/path/to/var/log/dev.log
  18. 26.

    Why? • More similar to production • Detect mixed-content early

    • Having HTTPS is sometimes a requirement: stripe.js?
  19. 27.

    How? # From anywhere $ symfony server:ca:install # Done! $

    ls ~/.symfony/certs default.p12 rootCA-key.pem rootCA.pem +Creates a local Certificate Authority +Registers it on your system trust store +Registers it on Firefox +Creates a default certificate
 for localhost/127.0.0.1/...
  20. 30.

    Why? • Useful when a project behaviour depends on the

    domain name
 Think sub-domains like lisbon2018.symfony.com vs paris2019.symfony.com • No need to remember the ports
 ports can change, is it 8000 or 8001? • Stable endpoint across machines
 Think oauth2 redirect URL for instance • Use “regular” ports: 80 for HTTP and 443 for HTTPS • More similar to the production settings
  21. 31.

    Optional, independent from the web server # From anywhere $

    symfony proxy:start # Done ...almost! No need to be root
  22. 32.

    + Nothing to install + No need to mess up

    with your DNS + Same configuration on all platforms + Does not intercept regular traffic
  23. 33.

    How does it work? $ curl http://127.0.0.1:7080/proxy.pac // Only proxy

    *.wip requests // Configuration file in ~/.symfony/proxy.json function FindProxyForURL (url, host) { if (dnsDomainIs(host, '.wip')) { return 'PROXY 127.0.0.1:7080'; } return 'DIRECT'; }
  24. 34.

    Optional, independent from the web server # Attach a domain

    to a directory $ symfony proxy:domain:attach symfony.com --dir=~/Code/symfony.com # Start the web server if needed $ symfony server:start -d # Done! https://symfony.com.wip/ is now an “alias” for https://127.0.0.1:8xxx/
  25. 35.

    Proxy Configuration { "tld": "wip", "port": 7080, "domains": { "*.symfony.com":

    ".../github/symfonycorp/symfony.com", "certif": ".../symfonycorp/certification.symfony.com", "certification": ".../symfonycorp/certification.symfony.com", "demo": "/tmp/demo", "cloud.symfony.com": ".../github/symfonycorp/symfony.com", "symfony.com": ".../github/symfonycorp/symfony.com" } } ~/.symfony/proxy.json .wip or anything else .sf? Wildcard support Multiple domains per directory TLS certificates created on demand
  26. 36.
  27. 40.

    Manage long-running commands # Run yarn in the background $

    symfony run -d yarn encore dev --watch $ symfony server:log $ symfony server:status Web server listening on https://127.0.0.1:8001 Command "/usr/local/bin/yarn encore dev --watch" running with PID 93398 $ symfony server:stop command logs stops the web server
 and the commands
  28. 43.

    https://demo.com/ Project hosted on SymfonyCloud # Init a project and

    deploy to prod $ symfony project:init # Deploy a cluster of containers PHP, services like DB, TLS, ...
 for the master branch/production env $ symfony deploy # Make it live $ symfony domain:attach demo.com ENV=prod DEBUG=false
  29. 44.

    https://some-bug-xtf33gy-ombjum625h7iw.eu.s5y.io/ Step 1: Create a branch # Create a new

    Git branch for the code # Deploy a new cluster of containers # Copy data (DB, files, ...) byte to byte $ symfony env:create some-bug # Open the new deployed URL # Exact copy of production $ symfony open:remote # Debug logs $ symfony log # Or switch to dev/debug with $ symfony debug Exact copy of production ENV=prod DEBUG=false
  30. 45.

    https://demo.com.wip/ Step 2: Run it locally # Attach a local

    domain for fun $ symfony proxy:domain:attach demo.com # Run the local web server $ symfony server:start -d # Open the local website $ symfony open:local ENV=dev DEBUG=true
  31. 46.

    https://demo.com.wip/ Step 3: Connect the remote services # Open a

    tunnel with the "some-bug" env $ symfony tunnel:open # Refresh the page in the browser # It works! ENV=dev DEBUG=true
  32. 47.

    .env APP_ENV=dev APP_DEBUG=1 APP_SECRET=67d829bf61dc5f87a73fd814e2c9f629 DATABASE_URL=sqlite:///%kernel.project_dir%/data/database.sqlite MAILER_URL=null://localhost 47 How does it

    work? Environment variables FTW! + The local web server detects the tunnel + It exposes the services as env vars in PHP-FPM + Symfony and SymfonyCloud share the same convention + Symfony reads the env vars + You win!
  33. 49.

    prod Debugging like a pro With production data But in

    a safe isolated environment Local Web prod DB
  34. 50.

    prod Debugging like a pro With production data But in

    a safe isolated environment Local Web prod DB some-bug some-bug Web DB symfony env:create Duplicate infra/data Create a local Git branch 1
  35. 51.

    prod Debugging like a pro With production data But in

    a safe isolated environment Local Web prod DB DATABASE_URL=postgres://127.0.0.1:30002/main symfony tunnel:open 2 some-bug some-bug Web DB symfony env:create Duplicate infra/data Create a local Git branch 1
  36. 52.

    prod Debugging like a pro With production data But in

    a safe isolated environment Local Web prod DB DATABASE_URL=postgres://127.0.0.1:30002/main symfony tunnel:open 2 some-bug some-bug Web symfony server:start 3 some-bug some-bug Web DB symfony env:create Duplicate infra/data Create a local Git branch 1
  37. 53.

    prod Debugging like a pro With production data But in

    a safe isolated environment Local Web prod DB DATABASE_URL=postgres://127.0.0.1:30002/main symfony tunnel:open 2 some-bug some-bug Web symfony server:start 3 some-bug some-bug Web DB symfony env:create Duplicate infra/data Create a local Git branch 1 symfony env:sync
  38. 54.

    https://some-bug-xtf33gy-ombjum625h7iw.eu.s5y.io/ Step 4: Fix the issue # Fix the code

    # Update the DB schema # Change/Add some services $ git commit ... # Deploy on the remote env $ symfony deploy # Check that everything is fine $ symfony open:remote # If the fix takes some time # Sync back the data from prod $ symfony env:sync ENV=prod DEBUG=false
  39. 55.

    https://demo.com/ === https://some-bug-xtf33gy-ombjum625h7iw.eu.s5y.io/ Step 5: Deploy with confidence # Merge

    everything back (code + infra) to production $ git checkout master $ git merge some-bug # Deploy production # Fast -> same container images $ symfony deploy ENV=prod DEBUG=false
  40. 56.

    Bonus: Works in a terminal as well $ symfony tunnel:open

    $ symfony php -r 'echo $_SERVER["DATABASE_URL"];' postgres://main:main@127.0.0.1:30002/main?sslmode=disable&charset=utf8&serverVersion=10 # Env vars are useful with bin/console as well $ symfony console ... # Works for "some" other commands as well (connects to the SymfonyCloud DB) $ symfony run psql SymfonyCloud Environment variables are set
  41. 57.

    My ideal setup! Local Web Server and local files Remote

    services Works well on the major platforms Fast and convenient