How do I host my Django Application on Linode?
2 Replies
Getting your Django app up and running on Linode is fairly simple. You'll need to upload your code to an instance running Django. If you want to go a step further, you can configure DNS records and enable HTTPs. I'll walk you through the whole process.
1. Create a Linode Account
First, you'll want to create an account on Linode, if you don't already have one.
2. Add Your Domain
If you've purchased a domain already, great! If not, you can do that from a few different places:
The DNS Manager guide will help you set Linode's name servers as the authoritative name servers and give you an overview of how to use the Linode DNS manager. Next, you'll want to get an A record pointed to your IP address. Since DNS records will take some time to propagate, go ahead and do this early on. You'll also need the A record configured to get the SSL certificate authenticator to pass. By the time we're done, propagation should be finished.
3. Deploy Django
Next, deploy an instance running Django. The easiest thing to do is use the app available in the Linode Marketplace. This spins up a server running Django on Debian 10 - any plan size will work!
If you want to us a different distribution, you'll need to manually install Django using resources found elsewhere on the internet:
Once it's up and running, the default Django app should be available in your browser at http://<your.ip.address>:8000
.
4. Secure Your Instance
Ok! Now, you have an instance in the cloud running Django. If you haven't already I suggest taking steps to secure your server by doing the following:
These measures will go a long way in preventing unwanted access to your instance.
5. Upload Your Code
If you already have your code in a GitHub repo, that's great! You can watch this video from the Linode Youtube channel where @CodeWithTomi walks you through pulling your code from a GitHub repo and deploying it to your instance.
If your code is on your local machine, you can upload it to your Linode using a few different methods such as scp
, rsync, Cyberduck, or Filezilla. Keep in mind the default path for the Django app files from the marketplace is /var/www/DjangoApp/
.
When you've got your code on your instance and removed the default Django app that was deployed in step 2, you can run your app with the following command:
python3 <your-app.name> runserver <your.ip.address:8000>
To run the app so that the process will continue even after you exit your terminal, you can use the following:
nohup python3 <your-app.name> runserver <your.ip.address:8000> &
Now you have your app sucessfully deployed on the internet! But it's only accesible from and IP and port number. What about a domain? And HTTPS?
6. Install Nginx
Following the guide on NGINX as a Reverse Proxy, install Nginx on your instance. You don't need to install Node though since our app in this case is Django and it's already running.
Next, you will create an Nginx reverse proxy file: /etc/nginx/conf.d/<your-file-name>
:
server {
listen 80;
listen [::]:80;
server_name <your-domain-name.com>;
location / {
proxy_pass http://<your.ip.address>:8000/;
}
}
7. Edit Host File
Along with the proper domain configurations, if you haven't done so already, you need to update your system's /etc/hosts
file in order get an SSL certificate.
# /etc/hosts
127.0.0.1 localhost
<your.ip.address> <your-domain-name.com> <your-hostname>
8. Install Certbot
Now, you can use a Linode Guide or the Certbot site to install Certbot on your instance. Certbot will automatically request (and renew), prove domain ownership, and install SSL certificates on your server. Be sure you are following the instructions for the correct distro and web server software you're using.
The utility should discover your domain on its own. And voila! Your SSL cert will be installed:
Deploying certificate
Successfully deployed certificate for <your-domain-name> to /etc/nginx/sites-enabled/default
Congratulations! You have successfully enabled HTTPS on https://<your-domain-name.com>
and the /etc/nginx/conf.d/<your-file-name>
file should now look like this:
server {
server_name <your-domain-name.com>;
location / {
proxy_pass http://<your.ip.address>:8000/;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/<your-domain-
name.com>/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/<your-domain-
name.com>/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by
Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by
Certbot
}
server {
if ($host = <your-domain-name.com>) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name <your-domain-name.com>;
return 404; # managed by Certbot
}
Now your app is accessible at https://your-domain-name.com. Yay!
But, http://<your.ip.address>:8000
is still serving your app unsecurly. 😕 What to do?
9. Enable Cloud Firewalls
Once your Create Cloud Firewall rules to Drop
incoming and outgoing connections on port 8000. This will disallow connections on the original port while still allowing HTTP and HTTPS connections. This solves the issue of possible unsecure connections being made to your application.
You've done it! Your Django app is now securely hosted on Linode.
Going Further
If you want to learn more about version control using GitHub, I suggest watching this Deploying a Django App video on the Linode Youtube channel. If you're interested in advanced monitoring for your app, you can check out this video on Uptime Kuma.
You can also check out these resources for information on connecting your Django app to a database:
Django Questions? Visit the Official Django Forum.
https://www.youtube.com/watch?v=1fjpNXK7yqc
heres a really good video tutorial. i got my django app up and running on linode with this