Have you ever came across the word “localhost” before? If you have ever started a localhost server on your computer, You may have noticed that it is not a web address. Now let’s dive into what is localhost and how to set it up on your computer.
As the name suggests localhost is a local webserver just like a website address and it loops back on your computer.
Whenever you open a localhost server, your browser loops back to your local network to make a request. Unlike other websites where you type in the web address e.g. google.com and your browser takes you to the IP address of google.com
To understand more about how rerouting works, check out this tutorial on How DNS works.
To keep it simple and straightforward, we need to clear some things out before we dive in any further.
Things to remember (What is localhost)
- Localhost (aka Loopback server) request can be made by any computer system or browser.
- When you make a loopback request and type localhost, your browser converts this request into the IP 127.0.0.1 and it queries your local server.
- The available IP addresses for your local server is huge. It ranges from 127.0.0.0 to 127.255.255.255
- Considering the huge number of IP addresses available on, you can start the localhost on any of these IP addresses.
- The IP addresses starting from 127.XXX.XXX.XXX are reserved because your computer will always make a loopback query and it will never reach the actual address. This is why no website or computer has an IP starting from 127.
- Some tools let you start the localhost by just writing the name of the localhost as a domain. Furthermore, you do not need to write the complete IP address.
- Generally, the IP address 127.0.0.1 is used for the localhost. This IP is set to default unless you change the loopback server into something else. e.g. 127.0.0.132
- It is a loopback request so having an internet connection is not necessary. Although you need to connect your device to the internet in case of CDN (Content Delivery Network) requests. If you fail to connect your device to the internet, the CDN parts will throw the error 404 Not found.
Now, let’s get to the part where we learn what is localhost.
What is localhost
Like I mentioned before, Localhost is a local server. If you make any request to the local server, the request will loop back on your computer and query with the server instead of going to the internet.
Localhost has the available IP range starting from 127.0.0.0 to 127.255.255.255
It means that you can start 255x255x255 localhost servers on your computer at the same time. (It is just a Joke, Never try it. It will break your computer)
Why do we need a localhost
Localhost allows you to create a web server on your local computer. You can test your apps, website, or tools that you are creating for the world. Instead of uploading it on the internet and letting people see your unfinished and buggy version of your work, you can test it offline.
Another reason why the localhost can be useful that some frameworks and applications need localhost to run tests on their applications. e.g. PHP frameworks need a webserver to run and test their working. You can easily create a loopback server and test your applications whenever needed.
Learn how to create a localhost server
There are multiple ways to create a localhost server. Moreover, there are two types of localhost servers.
- Simple HTTP server
- Special servers for server-side languages
Now let me explain what are these two types of localhost servers and how to set them up for your computer
Simple HTTP server
The HTTP server is a local server that you can create using languages like python. If you have been using a MAC OS or a Linux, it should be easy to find and run an HTTP server. Although most Linux Distros and MAC OS come with python pre-installed, you may still need to check it if you have Python installed on your local machine.
$python -V ###Checks the version of python ###If there is any package named python is available, it will print the version
you can check if python is installed on your computer by this command. It should return a number
For Windows, you need to download python from a trusted source (generally the official website for python).
After downloading, run the following command in your CMD (these are the commands for windows)
$python3 ##or $py -3
Now that everything is all set, you can start a simple HTTP server with this
$python3 -m http.server ### OR ### $python -m http.server
Special server for server-side languages
These servers require a lot more than a simple HTTP server as they can handle multiple complicated requests and commands.
To set up a Special Server for PHP you need to install Xampp as it comes with everything you need.
Xampp can handle PHP requests, MySQL database, and much more. There are other features like tomcat and mercury. So there is everything you would ever need for a PHP based project.
First, you need to download Xampp from a trusted source (generally official website)
After downloading and installation process, you need to click on start and you can start your XAMPP server.
Read more: How to install XAMPP on your computer
In the end
This is all about the localhost and how can you set one up. This process does not take a lot of time but it can save you the trouble of uploading your data on the internet over and over again to make any changes. you can simply update the data offline and once you are done with it, you can create a zip file and put it all back together on your Webserver.
If you run into any trouble regarding this article, simply drop a comment so I and my team can fix it for you.