How to add a domain name to Directus
It's important to have a domain name for Directus so you're not exposing your IP address and port to the public. When Directus is installed it does not ask for your domain name nor does it provide a way to create one. This article will cover how you can create a domain name and assign it to Directus.
You'll need to purchase a domain name from a supplier and set up the DNS record to use your server. Then update your proxy web server with the server name and forward traffic to your Directus application.
Let's break this down into some easy steps. First you need a domain name.
Purchase a Domain Name
There are a lot of companies out there that offer domain names. I find the simplest method is to use the same company as your hosting platform and often you'll get the first year free and if lucky the second year too. When you do this at the same time, the DNS records will automatically be created for you.
Here are some hosting providers that can do this. Most offer managed VPS options but if you want to maintain the server yourself, you can go for an unmanaged option which is a bit cheaper.
- Liguid Web VPS Hosting
- Namecheap VPS Hosting
- InMotion VPS Hosting (Unmanaged)
- A2 Hosting VPS
- BlueHost
- GoDaddy VPS (This link will get you 30% off. To see VPS options, click on the website menu, then click All Hosting. Scroll to the options and under VPS click Learn more)
- InterServer VPS
- Hostinger VPS
- eUKhost VPS
If you already have a server and you want to purchase an domain name, here are some companies that have good deals:
Make sure to edit your DNS and create a new A record.
Type Name IP Address TTL
A @ 000.000.000.000 1h
Now that you have a domain name and it's pointing to your server, you will need to set up Nginx.
Configure Nginx
If you haven't already installed Nginx, run the following command relative to your OS.
sudo yum install nginx
sudo dnf install nginx
sudo apt-get install nginx
Create a new Nginx config file, where directus.example.com is your new domain name.
vim /etc/nginx/conf.d/directus.example.com.conf
Copy and Paste the following template and replace the server_name and proxy_pass port with your details. You will also need to supply your SSL configuration. If you don't need SSL, remove the listening port 443 line.
server {
listen 80;
listen 443 ssl http2;
server_name directus.example.com;
# Include your SSL details here #
location / {
proxy_pass http://127.0.0.1:8055;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
add_header X-Cache $upstream_cache_status;
proxy_connect_timeout 30s;
proxy_read_timeout 86400s;
proxy_send_timeout 30s;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
access_log /var/log/nginx/directus.example.com.log;
error_log /var/log/nginx/directus.example.com.error.log;
}
Save and close your configuration file. If you are unfamilar with vim, make sure to exit the editor mode by pressing ESC, the type :wq and press ENTER.
Test your nginx configuration using:
nginx -t
If unsuccessful, the output will say where the error occured. Address the issue and repeat.
Restart Nginx to apply the changes.
sudo service nginx restart
You can now access Directus from your domain name (assuming the DNS is configured and port 80 and 443 are open on the firewall).
What's next?
It's very important to secure your webiste with an SSL certificate that will encrypt all the data between the client and server. I've written an article about how you can add a certificate to Directus. It's worth a read.