How to Deploy Django Application on Cloud?

How to Deploy Django Applications on Cloud?

This guide provides a concise introduction to setting up a Django Python application. By following these instructions, you will gain the skills to create dynamic and feature-rich web applications, harnessing the power and versatility of Django, a widely acclaimed web framework.

Let’s Begin Building Your Next Exciting Web Project!

Step 1. To create a new environment with the specified configurations, follow these steps:

  • Click on the Create New Environment.

New Environment Deploy

  • Choose the desired Python version for your project; Here, we are selecting Python 3.11.4.
  • Select your preferred database for the project. We have opted for MySQL version 8.0.33.
  • Following that, you can now select the cloudlets and scaling limit for your project. We have chosen Reserved cloudlets with a value of 4 and set the scaling limit to 6.
  • Keep all the other settings unchanged, and then click the “Create” button at the bottom right corner. Wait while the environment is being created.

Configure Python

Step 2. Deploying the project

Deploying the project

  • Click on the button highlighted in the picture above.
  • Browse the local file and select the file node-crud.zip
  • Next, click on the “Deploy” button at the bottom right corner. The deployment process may take some time, so wait until the project is fully deployed.
  • Upon completion of the deployment process, you will see a folder “ROOT” created within the deployment section.

Deployment Root Process

Step 3. Update the DB credentials

  • Open or edit the file “project_folder/settings.py” and implement the following changes.
  • To open the file, click on the icon depicted in the picture below.

Deployment Section

Replace your_host with your actual host, your_user with your actual user name, and your_password with the actual password (please check your inbox for the credentials).

Setting Python

Step 4. Kindly place your “wsgi.py” file inside the “ROOT” folder.

Note: If configuring your own deployment archive, please ensure that your Django package includes the following files in its root:
  • requirements.txt file with the names of all the Python modules that your application needs. The deployment script will read this file and automatically install the listed modules using the pip manager.
  • wsgi.py file with the entry point script for running your application within a virtual environment using the mod_wsgi for Apache.

Execute The Following Commands in The Terminal

pip install -r requirements.txt
python3 manage.py makemigrations
python3 manage.py migrate

 Python Virtual Environment

Step 5. Edit the mod_wsgi module (the /var/www/webroot/ROOT/wsgi.py file) by replacing its content with the lines below:

import os,sys
virtenv = os.path.expanduser('~') + '/virtenv/'
virtualenv = os.path.join(virtenv, 'bin/activate_this.py')
try:
if sys.version.split(' ')[0].split('.')[0] == '3':
exec(compile(open(virtualenv, "rb").read(), virtualenv, 'exec'), dict(__file__=virtualenv))
else:
execfile(virtualenv, dict(__file__=virtualenv))
except IOError:
pass
sys.path.append(os.path.expanduser('~'))
sys.path.append(os.path.expanduser('~') + '/ROOT/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'ROOT.project_folder_name.settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Step 6. Also, adjust the next parameters in the


var/www/webroot/ROOT/project_folder_name/settings.py  configuration file:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static_local'),
)

Step 7. Create new subdirectories for the static/media content and run the synchronization of resources:

Run the following commands in the terminal

mkdir ROOT/static_local
mkdir ROOT/media
python manage.py collectstatic

Step 8. Fantastic! Your Django application is now up and running within a few minutes!

Account login

In summary, the Django Python application setup entails installing Django and Python, creating a virtual environment, configuring databases, defining models, implementing views and templates, mapping URLs, and finally, testing and deploying the application. Adhering to these steps equips developers to create dynamic and efficient web applications with Django, catering to a wide range of user requirements.