In Parts 1-3 of this tutorial series, we set up a custom domain name for our web application, we began to set up our Ubuntu server using a Digital Ocean Droplet, and we created a PostgreSQL database which can communicate with Django. In Part 4, we will continue setting up our server and clone our project onto it.
Creating a Virtual Environment on the Server
Much like on your local development environment, it is best practice to set up your Django project on a virtual environment on your Ubuntu server. First, we need to upgrade pip, and then we can install virtualenv.
$ sudo -H pip3 install --upgrade pip $ sudo -H pip3 install virtualenv
With virtualenv installed, we need to create a root project directory on the server, and cd into that directory. I will call mine example_project for the rest of this tutorial series, but you should name it according to your own project name.
$ mkdir ~/example_project $ cd ~/example_project
Note that the ~ represents the user's home folder in Bash, so you are creating the root project directory within the home foler of your user.
Your prompt should reflect that you are now within the root project directory that you just created. From here, we will create a virtual environment. I will call my virtual environment example_env, but again you should name it whatever you want.
$ virtualenv example_env
Much like on your local development environment, this creates a directory called example_env (or whatever yours is called) within your root project directory. A local version of Python and pip are installed within that directory.
To continue, activate the virtual environment you just created with the following command:
$ source example_env/bin/activate
Your prompt should now change to reflect that you are operating within your virtual environment. It should look something like this:
Now we are ready to clone our project onto our server.
Cloning Your Project to the Server
To clone your Django project onto your Droplet, I assume you already have Git set up on your local project, and that you are able to push your project with Git onto a remote repository, like GitHub. If you do not have this set up already, you will need to do that to continue.
To clone your project, you will need to jump back into your project on your local computer. In a terminal (or command prompt, Powershell, etc.) enter your root project directory and activate your virtual environment. Run the following commands to push the project (and all the requisite settings we have updated throughout this tutorial series) to your remote repository.
$ git add . $ git commit -m "message" $ git push origin master $ git remote -v
Your project is now up-to-date on your remote repository. You will notice that the last command provided you with output, either a URL for your remote repository or an SSH fetch and push address. Keep the URL or the SSH push address handy for now. We will need it soon. If you use SSH, the output should look something like this:
origin firstname.lastname@example.org:<your_username>/<your_project>.git (fetch) origin email@example.com:<your_username>/<your_project>.git (push)
Now, jump back onto your server, or Droplet. In order your server to pull your project from your remote repository, you will need to set up an SSH key for your server and connect it to GitHub, or whatever service you are using.
First, type the following commands to generate an SSH key. The ~ sign in Bash, if you do not know, refers to the user's home folder. So, in the first command, you are entering the .ssh directory in your user's home folder. Then you are generating a new SSH key.
$ cd ~/.ssh $ ssh-keygen
You can simply hit enter at the prompt to save the key in the default file location. You will also be prompted to create a passphrase for your SSH key. This is a good idea, but if you do not want a passphrase then you can simply hit enter twice to bypass it.
Now that your SSH key is generated, we need to copy it and enter it into your GitHub settings, or the settings of whatever service you are using. To copy your SSH key, enter the following command:
$ cat ~/.ssh/id_rsa.pub
Your terminal should output the key, which will begin with ssh-rsa and end with example_user@example-droplet. Highlight and copy the entire key. Now you need to register this key with your remote repository service. I will walk you through the process using GitHub.
Once you are logged in, click your profile icon in the upper right corner, and click Settings.
In the sidebar menu to the left, click on SSH and GPG keys. Then click on the green New SSH key button to the upper right. In the form that appears, give your key a specific title so that you know it belongs to this project server. Then copy the entire key into the Key text box. It should begin with ssh-rsa and end with <your_username>@<your-droplet-name>. Finally, click the green Add SSH key button below.
Now, go back to your server's terminal window. Enter the following command:
$ ssh -T firstname.lastname@example.org
Type yes and press Enter to confirm the connection. You should see a message like the following:
Warning: Permanently added 'github.com,188.8.131.52' (RSA) to the list of known hosts. Hi <your_username>! You've successfully authenticated, but GitHub does not provide shell access.
You are now ready to clone your project onto your server! First, cd back into your root project directory and activate your virtual environment:
$ cd ~/example_project $ source example_env/bin/activate
Check for the (example_env) before the terminal prompt to make sure your virtual environment is activated. Then type in the following commands. On the second command, paste in the push SSH address from above. It should look something like email@example.com:<your_github_username>/<your_project>.git.
$ git init $ git remote add origin <git_push_SSH_address_from_above> $ git pull origin master
Your Django project should now be successfully cloned onto your Droplet! Before we end Part 4, there are a few more things we should do.
First, your .env file with your secret key and database credentials should not have been cloned (the .env file should be listed in your .gitignore file, see Part 3). So, you will have to create a copy of your .env file on your server manually, but this is very easy. Make sure you are within your project's root directory, then run the following commands:
$ touch .env $ ls -a <***to make sure the .env file was created***> $ nano .env
The first command will create the file, the second command should list all files and directories (including hidden ones, like .env) in your root project directory. You should see the new .env file there. The third command will open the file for editing. Copy the contents of your .env file in your local project directly into the editor window:
With your sensitive information copied into the new .env file, type Ctrl-X (or Cmd-X for Mac users, I assume). Then type Y to save, and press enter.
Installing Project Dependencies
Now that your .env file is saved within your project on your server, you should install your necessary dependencies. If you have been following along with this tutorial series, you should already have all your dependencies listed in a requirements.txt file, which should have been cloned onto your server (see Part 3). You can install your project's dependencies by typing in the following command:
$ pip install -r requirements.txt
You should see your dependencies installing in the terminal window.
Completing Project Setup
There are a few more things you should set up on your server before we deploy it with Gunicorn and NGINX. First, run the following commands to migrate your project database:
$ python manage.py makemigrations $ python manage.py migrate
Next, create a superuser to administer your project with the following command:
$ python manage.py createsuperuser
You will have to enter a username and email address for your superuser, then confirm a password. Note that, while the terminal will not show that you are typing, it is registering your keystrokes.
Finally you should run the collectstatic command to collect your project's static files and compile them in the STATIC_ROOT directory specified in your project's settings.py file.
$ python manage.py collectstatic
Depending on your static files settings in your Django project, you should now have a new directory with all your static files.
Excellent! You should now have your complete Django project cloned onto your server and ready to deploy! In Part 5, we will configure Gunicorn and NGINX to handle HTTP requests, serve static files, and manage multiple instances of our project for live deployment. You can find Part 5 here.
Full Deploying a Django Project tutorial series: