%_
Jan 19, 2023

Setting Up A Custom Domain Name for Django Development Server on a Mac

Setting up a custom domain name for the Django development server on a Mac is a simple process that can be completed in a few steps.

  1. Make sure that the Django development server is running on your local machine by navigating to the root directory of your Django project and running the following command:
./manage.py runserver
  1. Open the terminal and edit the /etc/hosts file by running the command:
sudo nano /etc/hosts

This file is used to map hostnames to IP addresses on a local machine.

  1. Add the following line to the /etc/hosts file, replacing myproject.local with the custom domain name that you would like to use:
127.0.0.1 myproject.local
  1. Save and exit the /etc/hosts file.

  2. Open the settings.py file in your Django project and add the custom domain name to the ALLOWED_HOSTS list:

ALLOWED_HOSTS = ['myproject.local']

Alternatively, you can set it to ALLOWED_HOSTS = ['*'] for development mode.

Finally, open your web browser and navigate to http://myproject.local:8000 (or the port number you are using) to access your Django project on the custom domain name.

Setting up a custom domain name using nginx

To set up a custom domain name for the Django development server on a Mac using nginx, follow these additional steps:

  1. Install nginx on your Mac by running the command:
brew install nginx
  1. Create a new nginx configuration file for your Django project by running the command:
sudo nano /opt/homebrew/etc/nginx/servers/myproject.conf
  1. Add the following code to the myproject.conf file, replacing myproject.local with the custom domain name that you would like to use:
server {
    listen 80;
    listen [::]:80;
    server_name myproject.local;
    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
  1. Save and exit the myproject.conf file.

  2. Restart nginx by running the command:

brew services restart nginx

Finally, open your web browser and navigate to http://myproject.local to access your Django project on the custom domain name.

Good to know

To stop nginx, run the command:

sudo brew services stop nginx

To find the nginx configuration file path, run the command:

nginx -t