Deploying Django on a Single Debian Or Ubuntu Server
Total Page:16
File Type:pdf, Size:1020Kb
The Unix administration guide for Django developers Deploying Django on a single Debian or Ubuntu server Antonis Christofides CHAPTER 1 Getting started 1.1 Introduction I want you to understand how Django deployment works, and in order for you to understand it we’ll need to experiment. So you will need an experi- mental Debian or Ubuntu server. You could create a virtual machine on your personal system, but it will be easier and more instructive if you have a vir- tual machine on the network. So go to Hetzner, Digital Ocean, or whatever is your favourite provider, and get a virtual server. In the rest of this book I will be using $SERVER_IPv4_ADDRESS to denote the ip address of the server on which your Django project is running; so you must mentally replace $SERVER_IPv4_ADDRESS with “1.2.3.4” or whatever the address of your server is. Likewise with $SERVER_IPv6_ADDRESS, if your server has one. If you find the above confusing, maybe it’s because you don’t know what this book is about. “Deployment” means installing your Django application on production. This book doesn’t teach you how to develop with Django; you need to already know that. If you don’t, you need to read another book. If you are really looking to deploy your Django application, and you can already create a Debian or Ubuntu server, login to it with ssh, use scp to c copy files, use basic commands like ls, and understand some basic encryption principles, that is, what is a public and private key, you can probably skip most of this chapter. Otherwise, I’ll take you step by step, right from getting a virtual server, logging in to it, and using essential GNU/Linux commands. 1.2 Getting a server Until recently, I used to create test servers on my laptop using virtualbox and/or vagrant. However, virtual servers on the cloud have become so cheap that it is usually better to hire one there. It’s faster to set up, and you don’t need to worry about NAT. The other time I needed a Ubuntu server for a brief test. I created one on DigitalOcean within a couple of minutes; I made my test; and then I destroyed the server, after about half an hour. DigitalOcean’s charge for that was $0.01. The cool thing about DigitalOcean is that you can get a test server for only as long as you need it, and get charged only for the number of hours for which the server exists. In other providers you usually pay for the whole month. (Note: I am not affiliated with DigitalOcean, and I am not using their referrals program.) So, if you don’t already have a cloud VM provider, sign up on https:// digitalocean.com and create a droplet. DigitalOcean calls its servers droplets, but they are just virtual machines. In order to create a droplet, you need to choose the operating system and some other things. If you don’t want to know much about your options for the operating system, just choose Ubuntu 16.04 64 bit. Tip: Debian or Ubuntu? These two operating systems are practically the same system. You have prob- ably already chosen one of the two to work with, and there is no reason to reconsider. If you haven’t chosen yet, and you want to know nothing about this, go ahead and pick up the latest LTS version of Ubuntu, which currently is 16.04 (and will continue to be so until April 2018). The reason I recommend Ubuntu is mostly that it is more popular and there- fore has better support by virtual server providers. Ubuntu’s Long Term Sup- port versions also have five years of support instead of only three for Debian (though recently Debian has started to offer LTS support but it’s kind of unof- ficial). On the other hand I feel that Ubuntu sometimes rushes a bit too much to get the latest software versions in the operating system release, whereas Debian can be more stable; but this is just a feeling, I have no hard data. I use Debian, but this is a personal preference because sometimes I’m too much of a perfectionist (with deadlines) and I want things my own way. In Ubuntu’s version numbering, the first number is the year and the second is the month; so 16.04 was released in April 2016. The LTS versions are the ones released in April of even years, so the next LTS version will be 18.04. I don’t see why someone would use the 32-bit version, which can support only up to 4 GB of RAM, so choose the 64-bit version. Don’t choose a non-LTS version; support for these lasts less than a year, and it is too little. Besides operating system, you also need to choose size, data center, IPv6, SSH keys, and host name. The size of the server depends on how heavy the application is. For our pur- pose, which is testing Django deployment, the smallest one is usually more than enough. In fact, 512 MB of RAM and 20 GB of disk space are some- times enough for small applications in production. Choose the data center that is nearest to you. I like my servers to have IPv6, so I always turn that on. Don’t specify SSH keys yet, unless you are comfortable with them already. I devote the whole next section to SSH keys. Finally, choose a host name. Usually, when it is for testing, I look at the time and if it’s 17:02 I name the server test1702. For production, if I don’t have anything better, I choose names of Greek rivers at random. Hit the big green Create button and your server will be ready after one or two minutes. DigitalOcean will create a password for your server and email it to you. In order to login from Unix (such as Linux or Mac OS X), open a terminal and type this: ssh root@[server ip address] The first time you attempt this, it will warn you that the authenticity of the host cannot be established; tell it “yes”, you are sure you want to continue connecting. It will then ask for the password. The first time you connect, it may force you to change the password. Note that when you type a password, nothing at all is shown, no bullets or other placeholders, it’s as if you are typing nothing, but it is actually registering your keystrokes. You can logout of the server by entering exit at its command prompt. Ctrl+D also works. From Windows you first need to install an SSH client. The most popular one is PuTTY, which you can download from http://putty.org/. It’s a single file, putty.exe. Each time you execute it, it will launch its configuration window. Type the server ip address in the “Host Name (or IP address)” field and click Open. The first time you attempt this, it will warn you that the authenticity of the host cannot be established; tell it Yes, you trust the host. It will then ask for the user name (“login as:”), which is root, and the password. The first time you connect, the server may force you to change the password. Note that when you type a password, nothing at all is shown, no bullets or other placeholders, it’s as if you are typing nothing, but it is actually registering your keystrokes. Eventually you will want to copy and paste text from and to PuTTY. Just selecting text automatically copies it to the clipboard, and pasting is just right- clicking. You can logout of the server by entering exit at its command prompt. Ctrl+D also works. 1.3 Introduction to SSH keys You have deadlines. Learning about SSH keys doesn’t seem to be urgent. You can live without them, can’t you? Is it worth to spend an hour to learn about them? The answer is yes. If you log on to a server 12 times per day (a conservative estimate), and it takes on the average 5 seconds to type your password (and retype it if it’s wrong), that’s one minute. You will have paid off your investment in three months. But there are more savings; when creat- ing a droplet on DigitalOcean you will just be ticking a box and you will be ready to login. Otherwise you will be needing to wait for the email to come, copy and paste your password, and go through the process of changing the password. SSH keys can also be used on GitHub and other services. Finally, a little understanding of public key cryptography will later help you setup HTTPS, which is based on the same principles. So let’s start. You will first create a pair of keys, which we call the public key and the private key. Let’s just do it first. You won’t be understanding what we are doing, but I will explain it afterwards. On Unix, such as Ubuntu or Mac OS X, just enter the command ssh-keygen, which stands for ssh key generator. It will ask you a couple of questions: 1. It will ask where to store the keys. Since we are just testing, I suggest to store them in /tmp/id_rsa. 2. It will ask for a passphrase. For the time being, do not use a passphrase. We will come to the passphrase later on.