DEV Community

Cover image for Digital Ocean: Setup DNS, Nginx web hosting & G Suite for emails
Thibaut Tiberghien
Thibaut Tiberghien

Posted on

Digital Ocean: Setup DNS, Nginx web hosting & G Suite for emails

Originally posted on Medium on May 8, 2016.

YMMV: The code below is given for the environment and use-case I had at the time of writing, adapt it for your requirements.

Stack

  • Web hosting on a Digital Ocean droplet, running Ubuntu 16.04
  • Nginx web server, serving www.planecq.com from /var/www/planecq.com/html
  • G Suite for email
  • DNS settings on Digital Ocean

1. DNS

Forward DNS management to Digital Ocean

Go to your domain name registrar, search for the DNS settings and set the following external nameservers:

ns1.digitalocean.com
ns2.digitalocean.com
ns3.digitalocean.com

This may take a while to propagate, up to 48h, but usually around an hour or two for me.

Direct web traffic for your domain to your droplet

Login to cloud.digitalocean.com, go to Networking, then Domains. Add a domain and link it to the relevant droplet. After that, click “view” for your newly added domain and add a CNAME record for www pointing towards your main A record, in my case planecq.com., with the trailing dot. You should be good to go.

Direct email traffic for your domain to G Suite

Login to cloud.digitalocean.com, go to Networking, then Domains. Click “view” for your domain, then “MX”, then hit the “Add Gmail MX records”. You’re done!

2. Web Hosting

Setup Nginx to host your website

Install Nginx:

sudo apt-get update
sudo apt-get install -y nginx
sudo update-rc.d nginx defaults

It should be running after install, you can check with

sudo service nginx status

Add the site’s config file (/etc/nginx/sites-available/planecq.com)

server {
       listen       80;
       listen       [::]:80;
       server_name  planecq.com;
       return       301 $scheme://www.planecq.com$request_uri;
}

server {
       listen 80 default;
       listen [::]:80 default;

       root /var/www/planecq.com/html;

       index index.min.html index.html;

       server_name www.planecq.com;

       gzip_static on;
       gzip_proxied no-cache no-store private expired auth;
       gzip_http_version 1.0;

       location / {
                try_files $uri $uri/ =404;
       }
}

Note: you can only have one default server, so in case you are co-hosting several websites, remember to choose the one you want.

Enable your site:

sudo ln -s /etc/nginx/sites-available/planecq.com /etc/nginx/sites-enabled/planecq.com

Disable the default Nginx site:

sudo rm /etc/nginx/sites-enabled/default

You can play around with more settings in your site’s config file or in Nginx config (/etc/nginx/nginx.conf) to affect all of your websites.

You can now copy your website’s files to /var/www/planecq.com/html and it should work once the DNS is propagated. You can already try to hit the site using the IP of your droplet.

Top comments (0)