Search code examples
laravellaravel-5laravel-4

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


I'm using laravel and I don't know how to customize the default url which is "127.0.0.1:8000" or "localhost:8000" to my desired url.

My expectation is to change 127.0.0.1:8000 to sample.dev when I do php artisan serve

Do I really need to move my projects to htdocs or www?

Please help...


Solution

  • 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.