Deploy your Django app to Cpanel host

Deploy your Django app to Cpanel host using Github repository!

The process of readying your website for deployment can be different for projects in different sizes but in this tutorial we will try a simple approach to deploy our project to host using Cpanel. If your host provider isn’t using cpanel you can still manage your database and static files using this material.

As django’s documentation says, Before you deploy your site to your server should check if you done this important tasks before deployment:

  • Expected level of security;
  • Modifying setting for your new environment;
  • Enable optional security features;
  • Performance optimizations;
  • Provide error reporting.

These steps are vital for your project since your work is going to be accessible for users around the world and being prepare for security breaches to your website is definitely an important step deployment. For this tutorial we just focus on preparing your site for deployment but please don’t forget about security measures! But for sake of any project remember to Secure DEBUG and SECRET_KEY in your settings.py.

  • It’s a good idea to use the command bellow to check your deployment status in django:
python manage.py check --deploy

I assume that now you have secured your SECRET_KEY and set DEBUG to FALSE. Rest of the process is pretty simple so just follow the instructions bellow.

Serving static files for deployment

“ Websites generally need to serve additional files such as images, JavaScript, or CSS. In Django, we refer to these files as “static files”. Django provides django.contrib.staticfiles to help you manage them.’

But as django tells you in documentation this setup works only when DEBUG is set to true, so Django takes care of static files! “This method is grossly inefficient and probably insecure, so it is unsuitable for production”. We know that setting DEBUG to TRUE in deployment is dangerous and gives lot’s of information about vulnerable pieces of our app. So you have to serve them because DEBUG is FALSE. You can apply settings bellow in your settings.py:

# Static asset configurationBASE_DIR = os.path.dirname(os.path.abspath(__file__))STATIC_ROOT = 'staticfiles'STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)

You can add other paths to STATICFILES_DIRS if you have any path other that your default app/static route for your site. By the way please just create a folder in your project folder and name it “static”. If you are using git don’t forget to place a placeholder.txt inside static folder so git keep track of that folder.

There is another step, we will use Cling to set our WSGI.py file so first install following packages using pip:

pip install dj-static
pip install static3

Now go to your wsgi.py file in your project directory and use these settings:

import osfrom django.core.wsgi import get_wsgi_applicationfrom dj_static import Cling
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ََAPPNAME.settings')application = Cling(get_wsgi_application())

Now you can easily run “python manage.py collectstatic” in cpanel terminal and serve your static files. Now let’s modify our database settings.

Setting database for deployment

We are going to use Postgre database in our server but if you wan’t to say use Mysql you just need to adjust settings bellow. For start you should first set your database in cpanel. So in cpanel main page go to :

Databases >> POSTGRESQL databases

  1. Create Database by Database Name
  2. Create a new user in Add New User
  3. Add user to database
  4. Current users >> click on synchronize grants

Now we need to set the following settings in our settings.py:

DATABASES = {  'default': {  'ENGINE': 'django.db.backends.postgresql_psycopg2',  'NAME': 'database_name',  'USER': 'database_user',  'PASSWORD': 'user_password'  'HOST': 'localhost',  'PORT': '5432',  }}

For postgresql you need to have psycopg2 So:

pip install psycopg2

Our host is ready to serve django migrate commands.

Github

If you have a repository for your project that can be helpful in your deployment to cpanel. You can easily pull changes of your github repository into your cpanel host! but first the final step on our local machine just do the following command to get the list of packages that you need to install on your cpanel host.

pip freeze > requirements.txt

***Before you push changes make sure that you have set ALLOWED_HOST to your url in settings.py***

Now just push the changes to your github repository.

In your cpanel main page go to Files >> Git Version Control

click on create and fill the following tabs with your github repository information. For Repository path just add a path that you want to store your repository on it.

You have successfully stored your files the server.

Setting up passenger_wsgi.py

Now that you have stored your files onto your host, you need to address your Cpanel on how to start your program. Before doing this you need to take few additional steps. You need to create a passenger_wsgi.py file inside your project folder:

path in Cpanel: repositories >> YOUR REPOSITORY >> passenger_wsgi.py

Now open passenger_wsgi.py and just import application object from your wsgi in your project folder:

from YOUR_PROJECT_FOLDER.wsgi import application

Save it and you are almost done!

Final steps

Go to cpanel main page and proceed to Software >> Setup Python App. Click on “Create application” and fill the following forms:

Remember to set :

Application startup file = Path to passenger_wsgi.py file in your repository. If you placed it in root of your project as I instructed just write:

passenger_wsgi.py

Application Entry point = Name of the callable object you set inside your WSGI file which in this case it is:

application

  • ** You can set Environment variables to hide your SECRET_KEY or any other important information such as captcha keys and etc.***

Now in the web applications list select the app you just created. You need to pip install requirements.txt which is a list of your packages that you just stored in that file. you can easily install them in your app page:

Just add your file and click run

*** If this won’t work you can just install them in Cpanel terminal. just follow the instruction bellow.***

In the same page at the top there is a notification that helps us easily locate our project and activate our virtual environment in cpanel terminal. So just copy the path in that notification:

In the cpanel main page go to Advance >> terminal and in the first line paste the path you just copied from application page by pressing ctrl+shift+V and Enter! You are now in virtual environment.

  • ** If you couldn’t install your package earlier now you can install them in termial, same as your local machine.***
  • ** if you can’t install psycopg2 you will get an error in migration step so just install psycopg2-binary instead:
pip install psycopg2-binary

Just like your local machine you need to migrate your django model and of course you need to run “collectstatic” to serve your static files.

So type :

python manage.py migrate

and to server static files type :

python manage.py collectstatic

Restart your app in your python app page so the changes take place

Congrats! You are done! You can proceed to app URL.

Note: This is a fairly beginners only approach. I strongly suggest deploying your django apps using nginx + gunicorn If you want to consider performance.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.