Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 884
schutte Avatar asked Dec 01 '22 14:12

schutte


2 Answers

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.

like image 149
Akar Avatar answered May 20 '23 13:05

Akar


To Change the Hostname

The only required step is to add an entry to your system's hosts file:

127.0.0.1 sample.test

Your site will be available from http://sample.test:80001 when you run php artisan serve.


To Remove the Port

To remove :8000 from the url, you need to be listening on the default port2 for HTTP:

php artisan serve --port=80

Now your site will be available at http://sample.test from a browser on the same machine.


1I changed the example to .test, which is a reserved top-level domain (TLD). .dev is owned by Google and will cause SSL errors when developing with the builtin PHP server used by artisan due to HSTS being enabled for the entire .dev TLD.

2This only works if there are no other applications already using port 80 on your machine, e.g. another webserver like Apache, Nginx, IIS, etc.

like image 21
Travis Britz Avatar answered May 20 '23 13:05

Travis Britz