如何将“127.0.0.1:8000 / localhost:8000”更改为我想要的网址。(laravel)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53192680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:09:14  来源:igfitidea点击:

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

laravellaravel-5laravel-4

提问by gecko

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.

我正在使用 laravel,但我不知道如何将“127.0.0.1:8000”或“localhost:8000”的默认 url 自定义为我想要的 url。

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

我的期望是在执行php artisan serve时将127.0.0.1:8000更改为sample.dev

Do I really need to move my projects to htdocsor www?

我真的需要将我的项目转移到htdocswww吗?

Please help...

请帮忙...

回答by Akar

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

注意:这只是为了回答问题,向下滚动更多以查看我们将使用的其他方法.test而不是.dev,因此我们不会出现 SSL 错误。



To change the default host to your desired one

将默认主机更改为您想要的主机

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

    php artisan serve --host=some-domain.test --port=anyPort
    
  3. Make sure the host exists in your etc/hostsfile. 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/hostsfile it, it would look something like this:

    127.0.0.1   localhost
    127.0.1.1   sample.dev // Added line.
    
    // More custom hosts here.
    
  1. 转到所在的项目目录artisan
  2. 运行以下命令:

    php artisan serve --host=some-domain.test --port=anyPort
    
  3. 确保主机存在于您的etc/hosts文件中。要在 hosts 文件中添加一个条目,请/etc/hosts/使用您喜欢的编辑器编辑并将此行添加到您当前的/etc/hosts/文件中。

    127.0.1.1  sample.dev
    

    如果我更改我的/etc/hosts文件,它看起来像这样:

    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:

如果在端口 80 上运行该命令,则会引发错误。因为很有可能您也使用了 Apache 服务。要使命令工作,您必须:

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

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

  • A: 停止sudo service apache2 stop在 Ubuntu 上使用的 Apache 服务(可能会根据发行版进行更改)。

  • B: 使用不同的端口,因为它是用于开发目的,我建议你坚持使用8080或任何其他你不会使用的端口。

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

现在,在您决定要坚持使用 port 之后8080,上面的命令将更改为以下内容:

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 .devdomain withoutan SSL certificate. which there are ways to set up on the local environment, but not really necessary since you're in development mode anyways.

注意:上面的这些步骤适用于您的情况,如果您运行上面的这些命令,它将无法在现代浏览器中运行,并且会抛出一个SSL Error. 因为从 Chrome 版本 63 开始,您不能在没有SSL 证书的情况下使用.dev域。有一些方法可以在本地环境中进行设置,但实际上并不是必需的,因为无论如何您都处于开发模式。

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:

但是,有一个专门用于开发目的的域,称为.test,因此执行上述步骤但将域更改为.test,上面的命令将如下所示:

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

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

这对于开发目的非常有用,因为您不需要为VirtualHost您创建的每个新项目添加一个。

回答by Travis Britz

To Change the Hostname

更改主机名

The onlyrequired step is to add an entry to your system's hostsfile:

需要一步是一个条目添加到系统中的hosts文件:

127.0.0.1 sample.test

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

当您运行http://sample.test:80001时,您的站点将可用php artisan serve



To Remove the Port

删除端口

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

要从:8000url 中删除,您需要监听HTTP的默认端口2

php artisan serve --port=80

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

现在,您的站点可以http://sample.test从同一台机器上的浏览器访问。



1I changed the example to .test, which is a reservedtop-level domain (TLD). .devis owned by Google and will cause SSL errors when developing with the builtin PHP server used by artisandue to HSTSbeing enabled for the entire .devTLD.

1我将示例更改为.test,这是一个保留的顶级域 (TLD)。由于为整个TLD启用了HSTS.devartisan因此在使用 Google 使用的内置 PHP 服务器进行开发时会导致 SSL 错误。.dev

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.

2这仅在您的机器上没有其他应用程序已经使用端口 80 时才有效,例如另一个网络服务器,如 Apache、Nginx、IIS 等。