Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Setting up with the Symfony Local Web Server

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

Yo friends! It's file upload time! Woo! We are going to absolutely crush this topic... yea know... because file uploads are a critical part of the Internet. Where would we be if we couldn't upload selfies... or videos of Victor's cat... or SPAM our friends with memes!?!?! That's not a world I want to live in.

But... is uploading a file really that hard: add a file input to a form, submit, move the file onto your filesystem and... done! Meme unlocked! Well... that's true... until you start thinking about storing files in the cloud, like S3. Oh, and don't forget to add validation to make sure a user can't upload any file type - like an executable or PHP script! And you'll need to make sure the filename is unique so it doesn't overwrite other files... but also... it's kind of nice to keep the original filename... so it's not just some random hash if the user downloads it later. Oh, and once it's uploaded, we'll need a way to link to that file... except if you need to do a security check before letting the user download the file. Then you'll need to handle things in a totally different way.

Um... so wow! Things got complex! That's awesome! Because we're going to attack all of this... and more.

Downloading the Course Code

If you want to upload the maximum knowledge into your brain... you should definitely download the course code from this page and code along with me. After unzipping the file, you'll find a start/ directory that has the same code you see here. Open the README.md file for all the setup details... and a few extras.

The last setup step in our tutorials is usually to open a terminal, move into the project and run:

php bin/console server:run

to start the built in web server. You can totally do this. But, but, but! I want to show you a new tool that I'm loving: the Symfony local web server.

Downloading the Symfony Local Web Server

Find your browser and go to https://symfony.com/download. The Symfony local web server - or Symfony "client" - is a single, standalone file that is full of superpowers. At the top, you'll see instructions about how to download it. These steps are different depending on your operating system - but it should auto-select the right one.

For me, I'll copy this curl command, find my terminal, paste and enter! This downloaded a single executable file called symfony. To make sure I can type that command from anywhere, I'll move this into a global bin directory. By the way, you only need to do these steps once on your computer... so you're done forever!

Unless we've mucked things up, we should now be able to run this from anywhere: try it!

symfony

Say hello to the Symfony CLI! It lists the most popular commands, but there are a lot more - run:

symfony help

Woh. We'll talk more about this tool in another tutorial. But, to start a local web server, just say:

symfony serve

Ah. The first time you run this, you'll get an error about running: symfony server:ca:install. Let's do that:

symfony server:ca:install

You'll probably need to type in your admin password. This command installs a local SSL certificate authority... which is awesome because when we run symfony serve, it creates a local web server that supports https! Woh! We get free https locally! Sweet!

Find your browser and go to https://127.0.0.1:8000 - or localhost, it's the same thing. Say hello to The SpaceBar! This is the app we've been building in our Symfony 4 series: a news site for space-traveling friends from across the galaxy.

Try logging in with admin1@thespacebar.com and password engage. Then go to /admin/article.

This is the admin section for the articles on the site. Each article has an image... but until now, that image has basically been hardcoded. Click to edit one of the articles. Our first goal is clear: add a file upload field to this form so we can upload the article image, and then render that on the frontend.

But we're going to keep things simple to start... and take a deep and wonderful look into the fundamentals of how files are uploaded on the web and how that looks inside Symfony. Let's go!

Leave a comment!

58
Login or Register to join the conversation
Ricardo manuel de faria silva gonçalves Avatar
Ricardo manuel de faria silva gonçalves Avatar Ricardo manuel de faria silva gonçalves | posted 3 years ago

Hello,
I'm having some issues installing the server.
As expected I have the [InvalidArgumentException] Command "serve" is not defined. so I tried to run the installation command but I'm getting a [InvalidArgumentException] There are no commands defined in the "server:ca" namespace. . I also tried to reinstall symfony but I still get the same results.
Did anyone have this issue?? (Mac OS btw)

2 Reply
Facundo ariel P. Avatar

... 85 % free space and 95 % free inodes ...

It only runs as root.
As a regular user, it creates the log files inside $HOME/.symfony/log, but it cannot start the server ...

Reply

Hey Facundo,

Were you able to got it working? Probably "chown" the log file to the current user will solve the problem with permissions. I installed symfony CLI w/o sudo and it works for me. If you still have any problems with it - please, share some output so we see more context.

Cheers!

Reply
Facundo ariel P. Avatar
Facundo ariel P. Avatar Facundo ariel P. | victor | posted 2 years ago

Actually, the log directory and files are created with my user.
Here is the sequence of what I do, and the result:

$ symfony server:start -d

[WARNING] run "symfony server:ca:install" first if you want to run the web server with TLS support, or use "--no-tls"
to avoid this warning
Impossible to go to the background
Continue in foreground
WARNING Web Server log file cannot be tailed: unable to watch log file: no space left on device
no space left on device

Inside my $HOME/.symfony/log:

$ ls -l

total 8
drwxr-xr-x 2 facundo facundo 4096 Sep 23 10:14 355fcc099c030d9fdf46ccacf80a13a5c57d076e
-rw-r--r-- 1 facundo facundo 441 Sep 23 10:14 355fcc099c030d9fdf46ccacf80a13a5c57d076e.log

ls -l 355fcc099c030d9fdf46ccacf80a13a5c57d076e/

-rw-r--r-- 1 facundo facundo 0 Sep 23 10:14 53fb8ec204547646acb3461995e4da5a54cc7575.log

As you can see, all files and directories are created with my running user.
I even tried changing permissions to 777, but with the same result.

- freespace on disk is about 85%
- free inodes is about 95 %
- symfony cli installed with my running user
- before yesterday's update it was all runing ok
- running as root seems to work ok
- o.s. is Debian 10.5
- php version is 7.3.19
- symfony cli version is (2020-09-22T10:45:38+0000 - stable)

Is there any other test / info that might help ?

Thanks.

Reply

Hey Facundo ariel P.!

Yea, this looks super weird - definitely like a bug. And one key thing you said is that it worked *before* the update (so, I'm guessing it worked when you had 4.18.4 of the symfony binary and now 4.19.0 is broken). I'd open an issue with this info here - https://github.com/symfony/cli

Unfortunately, the symfony binary is a small piece of not-open source code... so even I can't dive into it and debug what's going on.

Cheers!

Reply
Facundo ariel P. Avatar
Facundo ariel P. Avatar Facundo ariel P. | weaverryan | posted 2 years ago

I've try it in a brand new installed system and it works.
I think it might be something with my installation. The rare thing is I think I did not change anything in the middle.
I'll keep trying to solve it.
Thanks.

Reply

Let us know if you keep getting problems

Reply

Hey Ricardo,

Are you talking about Symfony console, i.e. bin/console? If so - you're on the wrong way, you need to use Symfony CLI client that we use in this course. It's a standalone application that you need to download from https://symfony.com/download - find a way to do this for your OS, and then follow hints from the installer. When you install it and move to the "/usr/local/bin/" - you can run the server with "symfony serve" command. And "symfony server:ca:install" command will be available as well for you.

P.S. So the "bin/console" Symfony Console component and the new Symfony CLI client are different things :)

Cheers!

Reply
Ricardo manuel de faria silva gonçalves Avatar
Ricardo manuel de faria silva gonçalves Avatar Ricardo manuel de faria silva gonçalves | victor | posted 3 years ago

Hey Victor,

Had the Symfony CLI installed, ran self-update, all good, ran the symfony serve command and got that error. Couldn't figure out what was going on so I downloaded again the Symfony Cli and this time it worked. Don't know why it didn't work the first time but well, at least it's working now.

Thanks for the help.

Cheers

Reply

Hey Ricardo manuel de faria silva gonçalves!

Hmm, super weird! Good though to just reinstall from scratch - it indeed could have been some legitimate issue with the library :).

Cheers!

Reply
Kevin Avatar

Hi,

This is more of a general question, but what's the recommended way of getting set up with these older tutorials? My laptop is on php 8 now and I get all sorts of errors with I do composer install.

Reply

Hey there,

That's a good question. Usually we upgrade our tutorials so they can work with newer PHP versions but that's not always possible, and this is one of those cases, that's why we advertise this tutorial is compatible only with PHP 7. What you can do is to also install PHP 7 in your local machine. If you're using Docker, that's a trivial task to do :)

Cheers!

Reply
Kiuega Avatar

Hello, I just changed my operating system from Windows 10 to Ubuntu! (quite a difference in performance by the way).

The problem is that when I install the symfony certificate to have the https, it no longer works, I get a "NET: ERR_CERT_AUTHORITY_INVALID" with the message "Your connection is not private".

How could I fix this problem?

EDIT : OKay, fixed. the problem was that I seemed to be missing a very important package: the 'certutil'. Everything is working now!

Reply
Team P. Avatar
Team P. Avatar Team P. | posted 1 year ago

How can I enable xdebug with "symfony serve" command?

Reply

Hey Team P.!

Do you mean just "make sure it's enabled?" or for actual breakpoint debugging? If it's about the first question, the Symfony binary is pretty smart about finding various PHP binaries on your system (check symfony local:php:list) and you can choose between which version to use with a .php-version file.

If you're talking about breakpoint debugging, there shouldn't be any special setup - but check this out for details - https://github.com/symfony/... - and let me know if it helps.

Cheers!

Reply
Facundo ariel P. Avatar
Facundo ariel P. Avatar Facundo ariel P. | posted 2 years ago

I've just upgraded to the last version, and now I got

"Impossible to go to the background Continue in foreground
WARNING Web Server log file cannot be tailed: unable to watch log file: no space left on device
no space left on device "

in all my projects.

Ideas ?

Reply

Hey Facundo ariel P.

> WARNING Web Server log file cannot be tailed: unable to watch log file: no space left on device
no space left on device "

That warning makes me think that you ran out of disk space in your computer? Can you double-check that please

And, about the other message Impossible to go to the background Continue in foreground I got it too some times and I fix it by restarting the web server

Cheers!

Reply
Default user avatar
Default user avatar Michael de`Oz | posted 2 years ago

how to set server name? Namely name that used in routing annotation.

Reply

Hey Michael de`Oz

What server name are you speaking about? Is it about host name option? you can find some information here https://symfony.com/doc/cur...
or it's something else?

Cheers!

Reply
Jean M. Avatar
Jean M. Avatar Jean M. | posted 2 years ago

Hello, i don't know where ask the question.

When i learned for the first time how to uploading file, it was recommended by Fabien Potencier to create an entity for the file and manage the UploadeFile class in the entity with doctrine events, for example App\Entity\Image. Today, i always follow this paradigm for my apps and manage all the files as mapped relations with Doctrine, so the approach of this tutorial perturbs me. Do you consider is not a good practise ? if yes, why ?
Source (fifth item)

Reply

Hey Jean M.!

I just saw your question over on the other chapter and replied :). https://symfonycasts.com/sc...

Cheers!

Reply

Hello !
The installation went well. But in my browser I have a message saying that this certificate is not safe .. (I had to click on continue on this site even if it is dangerous to access it)

During the instatllation, I was suggested to install "certutil", is it necessary to solve this problem?

WARNING "certutil" is not available, so the CA can't be automatically installed in Firefox and/or Chrome/Chromium!
Install "certutil" with "apt install libnss3-tools" or "yum install nss-tools" and re-run the command
Generating a default certificate for HTTPS support

Thank you

Reply

Hey ojtouch

Yes, I think you have to install that library in order to generate a "valid" certificate for your localhost

Cheers!

Reply
Dung L. Avatar
Dung L. Avatar Dung L. | posted 3 years ago

Hello SymfonyCasts,

I use Symfony built-in local web server for dev, and got Symfony 4 Exception: "Error: Maximum execution time of 30 seconds exceeded" because the page took a long time to load data. I know that I have to change the value max_execution_time=30 to greater time allowed in the php.ini file. But how do I find this php.ini for this built in web server (https://localhost:8000/data/).

Thank you for your help!

Dung.

Reply

Hey Dung L.

I believe Symfony local web-server uses the php.ini file from your CLI installation. Just locate that file and tweak it so we can be sure :)
Also, I recommend you to read this documentation in case you want to change PHP config only to a specific project https://symfony.com/doc/cur...

Cheers!

Reply
Dung L. Avatar
Dung L. Avatar Dung L. | MolloKhan | posted 3 years ago | edited

MolloKhan thank you! that is it! made the change in php.ini - it works, as well thanks for the bonus url above, it will come handy as I dive deeper into fun Symfony! :)

1 Reply
Moh H. Avatar

Hhhm

Reply
victor Avatar victor | SFCASTS | Moh H. | posted 3 years ago | edited

Hey Moh H. ,

What's the deal? I only see "Hhhm" in your comment :)

Cheers!

Reply
Peter V. Avatar
Peter V. Avatar Peter V. | posted 3 years ago

Hi, when I go to https://localhost:8000 it says the secure connection failed, SSL_ERROR_RX_RECORD_TOO_LONG
I have Windows 7 and Firefox

Any ideas?

Reply

Hey Cavisv,

Well, first of all, you can try to load the page without SSL, i.e. use HTTP instead HTTPS for the protocol. Does the http://localhost:8000 works for you?

In this course we suggest to use "symfony serve" command instead of the old "bin/console server:run" one. Do you use "symfony serve"? What version of "symfony" CLI do you have? You can check with "symfony -v". Probably updating to the latest version may fix the problem. Also, could you try the same https://localhost:8000 URL in Google Chrome instead of Firefox? Does it work there?

Cheers!

Reply
Peter V. Avatar

After doing a Symfony update it works!
Only there remains an error SEC_ERROR_UNKNOWN_ISSUER
But I can click through that.
Thanks

Reply

Hey Cavisv,

Great, glad it helps! Unfortunately, I don't use Windows, can't help more with this error. If you still have this issue - feel free to report it to Symfony, I think they can help with it.

Cheers!

Reply
Peter V. Avatar

Thanks for the quick reply!

Yes, I was already using http, that works fine.

I use "symfony serve"

symfony -v gives "Symfony CLI version v4.5.2 (c) 2017-2019 Symfony SAS"

I already tried Chrome, that gives ERR_SSL_PROTOCOL_ERROR

Reply

Hello guys, I was wondering if it is possible to set the IP to 0.0.0.0? I see I can set the --port=80. It's handy so that phones/devices on my network can easily connect.

Reply

Hey Skylar

Yes, you totally can. Just run bin/console server:start 0.0.0.0:80

Cheers!

Reply

Thank you for the reply. But I wanted to user Symfony Serve to get https. AFAIK, the console server only does http.

Reply

Hey Scottie,

Yes, Symfony server via bin/console supports only HTTP, you need to use "symfony serve" for HTTPS support. And you can specify a port explicitly like:
$ symfony serve --port=80

You can try it, but probably you would need to run this command with sudo to grant access for this, though it depends on your OS.

P.S. to know more about any command - run the command with --help at the end, like:
$ symfony serve --help

What about the IP - not sure if it's possible, at least I don't see it in the help output.

Cheers!

Reply

Thank you for your, Victor. I was hoping for a secret command line argument like sudo /home/sgutman/.symfony/bin/symfony serve --port=80 --host:0.0.0.0. Unforturnately, that did not work. Maybe in the future. Again, thanks.

Reply

Hello Ryan, i have an error during setup. i cant migrate and also tried schema update too and same error

An exception occurred while executing 'CREATE TABLE tag (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, slug VARCHAR(255) NOT NULL, UNIQUE INDEX UNIQ_389B783989D9B62 (slug), PRIMARY KEY(id
)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB':

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

Reply

Hey msthzn

That's a problem between your DB version and the DB charset. You have 2 options

1) Update to MySQL 5.6 or higher

2) Set the field's length to 181 (or is it 191? I don't remember, but you can play with that number)

Cheers!

1 Reply
Steve-D Avatar
Steve-D Avatar Steve-D | posted 3 years ago

HI. When running symfony serve I get

[InvalidArgumentException]
Command "serve" is not defined.

Am I missing something?

Cheers

Steve

Reply
Steve-D Avatar

Never-mind. I needed to download and install the "read the instructions in full" command...

Reply

Hey Steve,

Haha, glad you found the solution ;)

Cheers!

1 Reply
Dan_M Avatar

Hey Guys,

I downloaded and installed the symfony cli on my Windows machine. When I try to use the new symfony server to browse my application, which is built with webpack encore, my stylesheet (and presumably my .js assets) is not loaded. (It still works with php bin/console server:run.)

Is there a configuration step I'm missing to use the symfony server with webpack encore assets?

Thanks!

Reply

Yo Dan_M!

Hmm... interesting! Well, let's do some digging :). Ultimately, your Webpack Encore assets are just normal, boring, physical files inside your public/ directory after they're built - so nothing "weird" should be happening. I'd try this:

1) Restart the new symfony server and browser to your app
2) View source (or inspect element), copy one of the paths to your assets - css or JS - and surf to it - so it'll be something like https://localhost/build/app.css. Do you see the contents or some error?

The first difference that comes to mind with the two setups is that the symfony server gives you https, instead of http. I can't think why that would effect things - but it possibly may. Also, do you see any errors in your browser's console?

Cheers!

Reply
Dan_M Avatar

Hey Ryan,

My template has:


{% block stylesheets %}
<link rel="stylesheet" href="{{ asset('build/app.css') }}"/>
{% endblock %}

When I view source, I see

<link rel="stylesheet" href="/build/app.73e7f49c.css"/>

When I click on the link, the stylesheet is downloaded as an attachment, and I see the following in my console:

Resource interpreted as Stylesheet but transferred with MIME type application/x-css: "https://localhost:8000/build/app.73e7f49c.css".

I don't get it!

Dan

Reply

Hey Dan,

Hm, what browser do you use? Could you try in a different browser? Or in incognito mode for example? And what HTML doctype do you have in your base layout, is it:


<!doctype html>
<html>...</html>

Try to explicitly specify type of the loaded file as:


<link rel="stylesheet" type="text/css" href="{{ asset('build/app.css') }}">

Does it help?

Cheers!

Reply
Dan_M Avatar

Victor,

The doctype is just as you showed. I added the explicit type definition, and that did not change anything.

I use Chrome normally, but I tested in Edge and got the same result.

When I run encore dev-server, the stylesheet link resolves as:

<link rel="stylesheet" type="text/css" href="http://localhost:8080/build/app.css"/>

The stylesheet loads and everything works perfectly.

When I run encore production, the stylesheet link resolves as:

<link rel="stylesheet" type="text/css" href="/build/app.73e7f49c.css"/>

and the stylesheet won't load and throws the warning I showed earlier.

To leave no stone unturned, I changed my template so it would render as

href="localhost:8080/build...

but that did not make a difference.

I hope you can see a problem that I can't. Thanks!

Reply

Hey Dan_M !

Awesome digging :). I think this is a problem with the new Symfony server -that's my instinct. There are a few reasons. When you use Encore in the "normal" mode (not dev-server), it writes physically files. So... if there is *any* issue downloading then, that's really the fault of your "web server". I further think this because you said that bin/console server:run *does* serve them correctly - the problem is only with the symfony serve command. When you use the `dev-server` command in Encore, your files are served by a totally separate Node server - so it *does* make sense that they would work in that context, but not in the normal "build" context.

Let's experiment: try running the server like this:


symfony serve --no-tls

That will start the web server simply in http instead of https. I don't have any specific reason why I think that might make a difference - but let's try it. Beyond that, I think we should explore having you open an issue about this. Oh, one other thing to try is simply creating a `public/build/foo.css` file manually and putting some CSS in it. If you manually add a link tag in your template to this, does it download? Or do you get the same error.

Cheers!

Reply
Dan_M Avatar
Dan_M Avatar Dan_M | weaverryan | posted 3 years ago | edited

Hey weaverryan

I tried symfony server --no-tls and got the same result. I also tried to call the compiled css file directly as <link rel="stylesheet" type="text/css" href="/build/app.css"/> with the same result. Then I replaced app.css with a very simple css file, just in case. Again, same result.

This perplexing!

Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

This tutorial is built on Symfony 4 but works great in Symfony 5!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": "^7.1.3",
        "ext-iconv": "*",
        "aws/aws-sdk-php": "^3.87", // 3.87.10
        "composer/package-versions-deprecated": "^1.11", // 1.11.99
        "knplabs/knp-markdown-bundle": "^1.7", // 1.7.1
        "knplabs/knp-paginator-bundle": "^2.7", // v2.8.0
        "knplabs/knp-time-bundle": "^1.8", // 1.9.0
        "league/flysystem-aws-s3-v3": "^1.0", // 1.0.22
        "league/flysystem-cached-adapter": "^1.0", // 1.0.9
        "liip/imagine-bundle": "^2.1", // 2.1.0
        "nexylan/slack-bundle": "^2.0,<2.2.0", // v2.1.0
        "oneup/flysystem-bundle": "^3.0", // 3.0.3
        "php-http/guzzle6-adapter": "^1.1", // v1.1.1
        "sensio/framework-extra-bundle": "^5.1", // v5.2.4
        "stof/doctrine-extensions-bundle": "^1.3", // v1.3.0
        "symfony/asset": "^4.0", // v4.2.3
        "symfony/console": "^4.0", // v4.2.3
        "symfony/flex": "^1.9", // v1.17.6
        "symfony/form": "^4.0", // v4.2.3
        "symfony/framework-bundle": "^4.0", // v4.2.3
        "symfony/orm-pack": "^1.0", // v1.0.6
        "symfony/security-bundle": "^4.0", // v4.2.3
        "symfony/serializer-pack": "^1.0", // v1.0.2
        "symfony/twig-bundle": "^4.0", // v4.2.3
        "symfony/validator": "^4.0", // v4.2.3
        "symfony/web-server-bundle": "^4.0", // v4.2.3
        "symfony/yaml": "^4.0", // v4.2.3
        "twig/extensions": "^1.5" // v1.5.4
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.0", // 3.1.0
        "easycorp/easy-log-handler": "^1.0.2", // v1.0.7
        "fzaninotto/faker": "^1.7", // v1.8.0
        "symfony/debug-bundle": "^3.3|^4.0", // v4.2.3
        "symfony/dotenv": "^4.0", // v4.2.3
        "symfony/maker-bundle": "^1.0", // v1.11.3
        "symfony/monolog-bundle": "^3.0", // v3.3.1
        "symfony/phpunit-bridge": "^3.3|^4.0", // v4.2.3
        "symfony/profiler-pack": "^1.0", // v1.0.4
        "symfony/var-dumper": "^3.3|^4.0" // v4.2.3
    }
}