DEV Community

chrisgen19
chrisgen19

Posted on

Step-by-Step Guide: Assigning a Namecheap Domain to DigitalOcean Hosting with Nginx

Below is a straightforward, end-to-end guide on how to point a domain purchased on Namecheap to a DigitalOcean Droplet running Nginx. This includes:

  1. Configuring DNS records on Namecheap
  2. Setting up a DigitalOcean Droplet
  3. Installing and configuring Nginx on the server
  4. Pointing your domain to the Droplet and verifying

Feel free to pick and choose the steps you need depending on how far you’ve already progressed.

1. Purchase a Domain on Namecheap (already done, presumably)

You should have an active domain in your Namecheap account (e.g., example.com).

If you haven’t already, note down or remember your domain name.

2. Create a Droplet on DigitalOcean

Log into DigitalOcean: Head to the DigitalOcean website.

1.Log into DigitalOcean: Head to the DigitalOcean website.
2.Create Droplet:

  • Click on “Create” in the top-right corner, then select “Droplets.”
  • Choose an image. Typically, Ubuntu 22.04 (or the latest LTS) is a popular choice.
  • Choose a Droplet plan (the most basic one is often enough for small/medium websites).
  • Select a region closest to your users.
  • Under “Authentication,” choose either an SSH key (recommended) or a password.
  • Finally, choose a hostname (it can be anything you like, e.g. my-nginx-server).
  • Click Create Droplet.

3.Obtain Droplet IP Address: Once created, DigitalOcean will show you an IPv4 address for your new Droplet (e.g. 123.45.67.89). Keep this IP handy.

3. Set Up DNS at Namecheap

To point your Namecheap domain to the DigitalOcean Droplet, you need to update your domain’s DNS records. You have two main ways:

Option A: Use Namecheap’s Default Nameservers

  1. Go to Domain List in your Namecheap account.
  2. Select Manage on the domain you want to work with.
  3. In the “Nameservers” section, ensure that Namecheap Basic DNS (or Namecheap’s default) is selected.
  4. Scroll down to the “Advanced DNS” or “DNS Settings” section.
  5. Add (or edit) the following DNS records:
  • A Record:
    Host: @ (means root domain)
    Value (IP Address): 123.45.67.89 (the IP of your Droplet)
    TTL: Automatic or 300 seconds

  • A Record (for “www” subdomain if you want it to resolve to the same site):
    Host: www
    Value (IP Address): 123.45.67.89
    TTL: Automatic or 300 seconds

  1. If there are old A or CNAME records pointing somewhere else, remove them or update them to the new IP.

Option B: Use DigitalOcean’s Nameservers (Optional)

Alternatively, you can point your entire domain to DigitalOcean’s nameservers (ns1.digitalocean.com, etc.) and then manage DNS from DigitalOcean’s control panel. This is more advanced, but you might prefer to keep everything in one place. If you choose that route:

  1. Set Nameservers in Namecheap to DigitalOcean’s:
    ns1.digitalocean.com
    ns2.digitalocean.com
    ns3.digitalocean.com

  2. On DigitalOcean, go to Networking → Domains → Add Domain → Enter your domain (e.g., example.com) → Create.

  3. Add an A record for @ (root) pointing to your Droplet’s IP, and one for www pointing to the same IP.

Either approach is valid. The key is that your A record(s) for @ (root domain) and www (if desired) point to your Droplet’s IP address.
Note: DNS changes can take up to 24–48 hours to propagate worldwide, but typically it’s much faster (often 30 minutes to a few hours).

4. Log Into Your Droplet and Install Nginx

  1. SSH into your Droplet:
ssh root@123.45.67.89
Enter fullscreen mode Exit fullscreen mode

Replace root if you set a different username, and use your Droplet’s IP.

  1. Update and upgrade packages:
sudo apt-get update
sudo apt-get upgrade -y
Enter fullscreen mode Exit fullscreen mode
  1. Install Nginx:
    sudo apt-get install nginx -y

  2. Check Nginx status (optional):

systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

It should say “active (running).”

5. Configure Nginx for Your Domain

To have Nginx serve your domain properly, you need to create (or edit) a server block (also known as a virtual host) for your domain:

  1. Create a server block file (for example.com):
sudo nano /etc/nginx/sites-available/example.com
Enter fullscreen mode Exit fullscreen mode
  1. Add basic configuration (below is a simple template):
server {
    listen 80;
    listen [::]:80;

    server_name example.com www.example.com;

    root /var/www/example.com;
    index index.html index.htm index.nginx-debian.html;

    location / {
        try_files $uri $uri/ =404;
    }
}
Enter fullscreen mode Exit fullscreen mode

3.Create the directory for your web files:

sudo mkdir -p /var/www/example.com
Enter fullscreen mode Exit fullscreen mode
  1. Add a test index.html in /var/www/example.com:
echo "<h1>Hello from Nginx on DigitalOcean!</h1>" | sudo tee /var/www/example.com/index.html
Enter fullscreen mode Exit fullscreen mode
  1. Enable the server block:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode
  1. Test the Nginx configuration:
sudo nginx -t
Enter fullscreen mode Exit fullscreen mode

If you see syntax is ok, you’re good.

  1. Reload Nginx:
sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode
  1. Verify: Visit http://example.com in your browser. You should see the “Hello from Nginx on DigitalOcean!” message.

Summary of Key Steps

  1. Obtain your Droplet IP from DigitalOcean.
  2. Add/Update DNS on Namecheap (A record for @ and/or www).
  3. SSH into your Droplet and install Nginx.
  4. Set up a server block for your domain (server_name example.com).
  5. Point DNS to the Droplet IP (if not already).
  6. (Optional) Secure your site with SSL using Certbot.

Once DNS propagates, visiting your domain should display the content served by your Nginx server on DigitalOcean. If you encounter any issues, double-check DNS records, the Nginx configuration, and that your domain is spelled correctly in both Namecheap DNS settings and the Nginx server_name directive.

Top comments (0)