How to start php localhost server as example.com on command line? I've tried:
php -S localhost:80
It works. But I want it be example.com
. When I access example.com
on browser, I want it to serve as localhost. How can I do this on command line or terminal?
CodePudding user response:
Unless a single webserver is responsible for hosting websites belonging to different hostnames then this won't involve the webserver at all. (If you do need that, then the search term you are looking for is Virtual Name Hosting and PHP's built in web server doesn't support it).
(That said, if you need to access the server from a different computer then you shouldn't bind to localhost
. Replace that with the network interface people will connect on, or 0.0.0.0
for all interfaces).
You need the browser to know that when you ask for example.com
it should send the request to your computer.
This is generally done by either:
- Controlling example.com, registering it at a DNS server, and configuring that DNS server with an A record to state that it should go to the IP address of the computer hosting the web server. (Most services offering domain registration will also provide DNS services in the same bundle).
- Running a DNS server on a local network, configuring computers that need to access the site to use that DNS server (and not a public one), and configuring it to point example.com at said IP address even though you don't control it.
- Editing the
hosts
file (location varies depending on OS) for each computer that needs to access example.com to point them at the IP address on a computer-by-computer basis.
CodePudding user response:
Add your website to the hosts file.
You can find an exact tutorial for your need here: https://kb.leaseweb.com/products/hosting/web-hosting/adding-a-website-to-hosts-file-and-testing-it
Add
127.0.0.1 www.example.com
to the file etc/hosts
if you're on Linux.