How to simulate a domain on your local dev machine
Do you hate using "localhost" or "127.0.0.1" 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
127.0.0.1 localhost
...
# We will be adding this line!
127.0.0.1 my-made-up-domain.dev
# We could also add subdomains too!
127.0.0.1 subdomain1.my-made-up-domain.dev
127.0.0.1 subdomain2.my-made-up-domain.dev
All we are doing here is mapping our localhost/domain 127.0.0.1
to our made up domain. So, when we type in our made up domain in a browser, it will just be accessing localhost or 127.0.0.1. 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 my-made-up-domain.dev
to that command like so:
rails s -p 3000 -b my-made-up-domain.dev
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.
# FOR BASH
sudo nano .bash_profile
# OR
sudo nano .bashrc
# OR
sudo nano .profile
# FOR ZSH
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 my-made-up-domain.dev.me alias command (mmud)
alias mmud='rails s -p 3000 -b my-made-up-domain.dev'
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 my-made-up-domain.dev
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!