Django Tutorial #7: Deploy

Please note that this post may contain affiliate links, and for every purchase you make, at no extra cost to you, a commission fee will be rewarded to me.

Deploy New Server

To deploy a Django project, first, we need to get a new server. There are a lot of companies that you can choose from. The one that I am using is Vultr.

Click Here to Claim Your $100 Coupon on Vultr

www.vultr.com

You can choose the type of instance based on your own needs. Here, we’ll just choose the “Compute Cloud” option.

And then choose the location. Remember, different locations may have different price options.

As for the server type, you can choose any system you are familiar with. However, if you want to follow this tutorial and install aaPanel as your server’s control panel, please choose Cent OS. The panel might not work on other systems.

And that’s it, pick a name for your server, and Deploy! It may take a few minutes.

Connect to Your Server Via SSH Client

Use PuTTY, XShell, or other SSH clients that you like to connect to your server. Here, I will use XShell as an example. Your login information can be found on the server information page.

Open Xshell, a session window should pop up. Create a new session.

First, give it a name, it can be anything you want. And in the “Host”, Type in the IP address of your server. “Port Number” should be 22.

Then go to “Authentication” page, and type in your user name and password, they can be found on the server information page.

And now you should be able to connect to your server.

To make your server more secure, it’s better to use an SSH key to log in instead of passwords.

Setup SSH Keys For Better Security

Go to Tools and choose “New User Key Wizard”.

Choose a key type and key length. Longer keys are more secure.

After it is generated, you can give it a name and a password.

Now, you need to save a public key and register it on your server.

Click on save as a file.

Go to the “root” directory of your server. If you installed Xftp and Xshell as a bundle, just click the green button on the top bar of Xshell.

Add an “.ssh” folder.

Copy the public key you just saved to this folder, and rename it authorized_keys.

Restart ssh service.

CentOS

sudo systemctl restart sshd

Ubuntu

sudo service ssh restart

Now you should be able to log in to your server with the SSH key pair. Remember to properly store the user key and the public key, if you lose them you will not be able to log in again.

Disable Password Login

However, now you can still use your password to log in, which means the hackers can as well. So, we need to disable the password login.

Go to /etc/ssh

Edit the sshd_config file.

Find PasswordAuthentication

Change the value to no

Restart the ssh service again. Now you can only log in using the SSH key pair.

Install a Control Panel For Your Server

There is a lot you can choose from. Here I’ll use aaPanel as an example.

aaPanel Demo

Run the shellcode on the official site of aaPanel. Again, make sure CentOS 7 is installed on your VPS. aaPanel is developed on CentOS, using other systems may cause errors.

After the installation process is finished. Follow the instructions on the screen and log into the panel.

From now on, everything should be straightforward. Just install the latest version of the Apache server and Python. You can install MySQL if you want. It is more robust and secure. But if you don’t, Django will use SQLite by default. You can also install other tools, but for now, these are all we need.

Run Deployment Check

Before we deploy our project, we need to check a few things. Go to terminal and run manage.py check --deploy. This command will check the SECRET_KEY, DEBUG, ALLOWED_HOSTS and a few other sensitive settings for you.

Please check your own settings against this documentation: https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

Deploy Your Django Project

At this point, we have two options, we can either deploy the project using the Python Manager that comes with aaPanel, or we can deploy the project manually. Since this tutorial is for beginners and deploying a Django project manually is a little confusing. We’ll try the Python Manager here.

I tested this tool on my own server and it worked for me. However, since it is still under development, there is a chance that it won’t work for you. In that case, please refer to this post on how to deploy Django project manually.

First, go to “Files”, and create a new directory.

After that, upload your entire project, not including “venv”, to the folder you just created.

Next, open the Python Manager and add a new project.

“Path” should point to the dir that contains manage.py. “Version” is the version of Python that you installed.

The startup module is can be either gunicorn or uwsgi. They are both interfaces that allow Django and the webserver to communicate. gunicorn did not work for me, so maybe you should try uwsgi first.

The startup dir should point to the folder that contains wsgi.py. And finally, the “Port” can be anything you want. Once you click confirm, it will take a few minutes to install all dependencies.

Finally, map the domain to the site and we are good to go! Assuming you already have one, if not, you can register a new domain name at NameCheap.

In the final section of this tutorial, we will add a few optional functions to our blog.

Next Post: Django Tutorial #8: Optional Functions

Leave a Reply

Your email address will not be published. Required fields are marked *