Introduction
This article shows how to install a Django application on an account.
Procedure
Step 1 — Preparation of the Python Application
If you have not already done so, add a Python application via the N0C panel. We recommend using the latest Django-compatible version of Python.
For the sake of clarity through the article, we will say that our application is called myapp.
Step 2 — Connect in SSH
As the rest of the operations will be carried out in SSH, please see how to create an SSH key and connect to an account remotely.
Step 3 — Activate the Python Environment
To access the Python environment of the application you’ve just created, you need to call the environment of the application.
For example:
source /home/abcdef/virtualenv/myapp/3.11/bin/activate
Once the environment is active, navigate to your application folder using the file manager.
Step 4 — Django Installation
To use Django, you have to install the Python dependencies needed to run the application. The following command does just that:
pip install Django
When installation is complete, a confirmation message is displayed.
By default, Django is configured with SQLite. However, this SQL engine is not recommended for a production environment. With the following command, you will install the mysqlclient library to connect to MySQL (you can also use PostgreSQL if you prefer):
pip install mysqlclient
When installation is complete, a confirmation message is displayed.
New Installation
If you want to install a new Django application, you can use the django-admin CLI to initialize your application.
Initialization in the folder you have chosen for your Python application is done by running the command below. You can change “myapp” to the desired application name:
cd myapp django-admin startproject myapp .
Please note that the dot at the end of the order is important.
Existing Installation
If you already have a Django application and wish to install it on your account, you will need to upload the files to a subfolder of the folder chosen for the Python application. In this tutorial, we will use “myapp” as the subfolder and application name, but you can change it to suit your preference.
Step 5 — Django Configuration
Now that your application files have been installed, it is time to configure them.
Database Creation
If you have not already done so, please follow this guide to create a SQL database with a user.
Once the database has been created, in the file manager, modify the Django configuration file using the information provided in the interface for managing your database. In our case, this is the file myapp/myapp/settings.py :
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'name of the database', 'USER': 'name of the user with priviledges', 'PASSWORD': 'your password', 'HOST': '127.0.0.1', 'PORT': '3306', } }
For more information on configuring your Django application, we recommend that you consult the official Django documentation: https://docs.djangoproject.com/fr/2.2/ref/settings/#databases.
Defining Authorized Hosts
Once again, you need to modify Django’s configuration file to specify the domain name that this Django site can serve. This is a security measure to prevent attacks on the HTTP Host header, which are possible even in many apparently secure web server configurations.
In the file manager, edit the file myapp/myapp/settings.py to enclose your domain name in square brackets:
ALLOWED_HOSTS = ["domain name"]
Server (Passenger)
We assume that servers use Passenger to run Python. Passenger is responsible for automatically starting your application when someone visits your site. For this reason, you should not launch your application manually.
To connect your application with Passenger, you will need to modify the “passenger_wsgi.py” file in the folder you have chosen for your Python application.
To do this, use the file manager to replace the entire contents of the file with the code below. Note that you must modify “myapp” if you have chosen a different name for your application:
import myapp.wsgi application = myapp.wsgi.application
Step 6 — Test
Check that your Django application is working correctly by using its URL, as it appears in the application management interface of the MG Panel.
Tips
- If you make any changes to your application’s code, be sure to restart the Python application via the MG Panel for the changes to take effect.
- If you get errors when you visit the site where your application is installed, we recommend you follow the guide on how to debug a Node.js, Ruby or Python application.