How to Change Default Url from Localhost:8000 to Other Ip in Laravel When We Run "Php Artisan Serve"

How can I change 127.0.0.1:8000 / localhost:8000 to my desired url. (laravel)

NOTE: This is just to answer the question, scroll down more to see the other approach where we would use .test instead of .dev, so we won't get SSL errors.


To change the default host to your desired one

  1. Go to the project directory where artisan is located.
  2. Run the following command:

    php artisan serve --host=some-domain.test --port=anyPort
  3. Make sure the host exists in your etc/hosts file. To add an entry to the hosts file edit /etc/hosts/ with your favorite editor and add this line to your current /etc/hosts/ file.

    127.0.1.1  sample.dev

    If I change my /etc/hosts file it, it would look something like this:

    127.0.0.1   localhost
    127.0.1.1 sample.dev // Added line.

    // More custom hosts here.

If you run the command on port 80, it would throw an error. because it's very likely that you also use the Apache service. To make the command work you have to either:

  • A: Stop the Apache service using sudo service apache2 stop on
    Ubuntu (May change based on distros).

  • B: Use a different port, since it's for development purposes, I
    suggest you stick to 8080 or any other port that you won't use.

Now after you decided you want to stick to port 8080, the command above will change to the following:

php artisan serve --host=sample.dev --port=8080

NOTE: Those steps above are for your case, if you run those commands above, it won't work in modern browsers and will throw an SSL Error. because as of Chrome version 63, you cannot use the .dev domain without an SSL certificate. which there are ways to set up on the local environment, but not really necessary since you're in development mode anyways.

BUT, there is a domain specifically for development purposes, called .test, so do the steps above but change the domain to .test, the commands above will look like the following:

php artisan serve --host=sample.test --port=8080

This is very useful for development purposes, since you don't need to add a VirtualHost for every new project you make.

How to set a domain name with php artisan serve

You can explicitly define the host and the port with artisan serve command:

php artisan serve --host=somedomain.com --port=8001

Note: Remember to enable to port with your firewall.

Set default 'host' value for `php artisan serve`

You can do next thing:

  1. php artisan make:command CustomServeCommand
  2. Then delete all from file and use this code:

    <?php

    namespace App\Console\Commands;

    use Illuminate\Foundation\Console\ServeCommand;
    use Symfony\Component\Console\Input\InputOption;

    class CustomServeCommand extends ServeCommand
    {
    /**
    * Get the console command options.
    *
    * @return array
    */
    protected function getOptions()
    {
    return [
    ['host', null, InputOption::VALUE_OPTIONAL, 'The host address to serve the application on.', '0.0.0.0'],//default 127.0.0.1
    ['port', null, InputOption::VALUE_OPTIONAL, 'The port to serve the application on.', 8000],
    ];
    }
    }
  3. php artisan serve

Link to the core file.

Basically, you will extend default class and adapt method to fit your own needs. This way you can set host and port per requirements.

Set port for php artisan.php serve

Laravel 5.8 to 8.0 and above

Simply pass it as a paramter:

php artisan serve --port=8080

You may also bind to a specific host by:

php artisan serve --host=0.0.0.0 --port=8080 

Or (for Laravel 6+) you can provide defaults by setting SERVER_PORT and SERVER_HOST in your .env file. You might need to do php artisan cache: clear as well. (thanks @mohd-samgan-khan)

And if you want to run it on port 80, you probably need to sudo.

How can I access my Laravel app from another PC?

Why don't you use Laravel's artisan for it? Very simple:

sudo php artisan serve --host 192.168.1.101 --port 80

Now from other computers, you can type: http://192.168.1.101

Do not forget to replace the IP with your own local one. That's it.

Note: The sudo is only needed if you wanna serve on port 80.

Is it possible to use Ip address instead of localhost in laravel?

To get it to work outside of localhost, do php artisan serve --host x.x.x.x

If you want it to work without specifying the port in the browser, php artisan serve --host x.x.x.x --port 80. sudo will likely be required on mac.

Note: php artisan serve should never be used for production. It's for dev and demonstration only and won't be able to handle more than a person or two's worth of traffic.



Related Topics



Leave a reply



Submit