44

I dont seem to understand why we need to run a Laravel app with php artisan serve vs just running it with Apache or nginx. I know that under development, we use artisan to fire up the site and after deployment to a server, you use the webserver to load up the site.

Whats the use of running the app in artisan in the first place?

captainblack
  • 4,107
  • 5
  • 50
  • 60
  • 7
    You don't **need** to do it. It's for cases when you don't have web server available for some reason. That reason could be anything, so to cover the cases when server is unavailable or it's misconfigured - you can use `php artisan serve`. – Mjh Nov 30 '16 at 15:46
  • 3
    Why isn't there enough documentation on the same? – captainblack Nov 30 '16 at 15:49
  • 2
    Why would there be? Nothing implied you MUST use it, it simply exists for cases when you *might* need it. Certain features don't need to be documented to a point where every possible use scenario is covered. You got nginx / Apache? Great! You don't and you want to quickly check something or test a part of your project? No problem, quick `php artisan serve` does the job, it takes a few seconds to type that into CLI to have you up and running. No need to overthink this particular feature. – Mjh Nov 30 '16 at 16:03
  • @Mjh I have xampp local server do I need it? – PHPFan Dec 13 '22 at 17:13

6 Answers6

57

The serve command is just a shortcut for the PHP Built-in Webserver, something PHP has out of the box, so the point of using it is to start testing your application as fast as you could, you just need to install PHP, Composer and your application is up (if you don't need anything else, of course). But if you already have Nginx installed, there is no point at all, just use it.

It's not wise to use the Builtin Webserver in production.

someOne
  • 1,975
  • 2
  • 14
  • 20
Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
  • 2
    Thank you for the explanation. But it is strange, I am executing the equivalent command, and it doesn't load the assets: `php -S 127.0.0.1:8000 C:/wamp/www/pym/server.php` Do you know why? – JCarlosR Sep 11 '17 at 19:59
  • 3
    I experienced the same issue as @JCarlosR, but was able to resolve it by running 'php -S' from the public domain or adding '-t public' to the end of the command. – Libertie Feb 22 '20 at 20:28
13

One advantage of using php artisan serve over a typical webserver during development is you can use Psysh as a debugger (Laravel Tinker) to set a breakpoint.

For example, at the line of code I want to break at I type:

eval(\Psy\sh());

Then I hit the page that will run that section of code and when it gets to that line it will break into a Psy Shell repl (in the commandline window where I started php artisan serve). Then I can inspect variables, etc. at that point of execution. It's very useful for debugging. As far as I know, you can't do this running Apache/Nginx. It has to be with artisan serve (or running automated tests).

More info here:

https://tighten.co/blog/supercharge-your-laravel-tinker-workflow

http://psysh.org/

user938883
  • 126
  • 2
  • 5
2

Purpose: The purpose of using Php artisan serve (PHP builtin server) is just for testing and easy starting your project it should not be used in real website deployment.

Asset Not working: Always put your index file in public it's the beauty and security of Laravel framework and your assets will always working. if you are bore to use your custom URL like C:/wamp/www/pym/server.php then use Virtual host locally but don't but don't put your index outside the Public folder. if you really want to use index at your Root directory then you should customize your all asset() and url() helper functions and should put your exact url Example asset('/login') should be changed to asset('localhost/yourprojectroot/login').

0
php artisan serve --host your_server_ip --port 8000

copy that http://your_server_ip:8000 and run it into the browser

0

Aside from the best answer here.

You can see the logs directly where you execute the php artisan serve, so useful in debugging.

Jovylle
  • 859
  • 7
  • 17
-3

Well, was looking for the same answer but couldn't find any that is satisfying so , if your also unsatisfied just like me try running the link returned when you run

php artisan serve

it returns

Laravel development server started: <http://127.0.0.1:8000>

copy that /http://127.0.0.1:8000 and run it into the browser , guess what it returns );the page that u first got when you installed laravel for the first time or i guess it will return the page in the routes folder which was set as /home directory or file(default home page).

In brief:

php artisan serve

starts the serve,forexample its like when your going to drive a car and you start the engine before driving whereby you can start the engine and drive at the same time ,its not neccessary to do so but depends.So to me that's php artisan serve CLI.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Madrine
  • 17