Join 75,964 users and earn money for participation

How to simulate a domain on your local dev machine

0 5 exc boost
Avatar for nolyoi
Written by   5
9 months ago

Do you hate using "localhost" or "" while your working on your projects? If so, just know that you are not alone. And here at my blog, we have the solution to your problem! That's right! We can end your suffering now! 🤣

Fortunately, it's a pretty quick fix.

Below I will show you how to do this on any unix system. This includes any Linux distributions as well as MacOS!

The first thing we will want to do is use our favorite terminal text editor (I suck at vim so I just use Nano 😝) to modify our /etc/hosts file.

I've also provided the command to access the hosts file on Mac as well as it's slightly different from the Linux command:

# Linux
sudo nano /etc/hosts

# Mac
sudo nano /private/etc/hosts

# Your host file should look something like this here. It will probably have
# a couple other entries so don't worry if it's not the exact same.
GNU nano 4.8              /etc/hosts                      localhost

# We will be adding this line!

# We could also add subdomains too!

All we are doing here is mapping our localhost/domain to our made up domain. So, when we type in our made up domain in a browser, it will just be accessing localhost or It's just a cosmetic difference, that's all!

Setting it up for Rails or other App development

If you'd like to use a simulated domain with Rails, Sinatra, Django (or another software), we can do this 2 different ways.

The first way would be to start our rails server with the standard command and just add a -b to that command like so:

rails s -p 3000 -b

But, that's kind of annoying having to type that out every time, right?

What I will do is write an alias for this command in my shellp configuration file. So that when I type "mmud" into my terminal (you can use any alias you want, it could be "chocolate rainbow" if you want it to be), it will run that rails command automatically.

To do this, we will need to edit our terminal config file.

Again we need to pull up our favorite terminal text editor (sorry vim you still suck) and modify our shell's config file. Depending on if you use Bash or ZSH, this can vary. Note: Mac users use ZSH by default.

sudo nano .bash_profile
# OR
sudo nano .bashrc
# OR
sudo nano .profile

sudo nano .zshrc

Once there, we only need to add one small line. I'd also add a comment above it so you don't forget what it does if you ever come back and edit this file later. It's a good practice to use.

# Rails alias command (mmud)
alias mmud='rails s -p 3000 -b'

That's it!

Now when I want to run my rails server on my local dev machine, I just type mmud into my terminal and it executes the rails s -p 3000 -b command. That's way neater and easier to remember right?

As I said before, you can follow these same steps for other softwares like Sinatra, Django, Phoenix, or any other framework that boots up in a similar fashion.

Hope you found this helpful! Enjoy!

$ 0.38
$ 0.38 from @TheRandomRewarder
Avatar for nolyoi
Written by   5
9 months ago
Enjoyed this article?  Earn Bitcoin Cash by sharing it! Explain
...and you will also help the author collect more tips.