This tutorial will show you how to install and configure Django in a Python virtual environment within a WebApp.
If you do not have a WebApps section in your hosting Control Panel, then this tutorial is not suitable for your particular hosting environment. You can submit a support ticket through our ticketing system if you need assistance.
We will set up a Django production environment. We will use Python 3, pip 3, Django, virtualenv, Python's MySQL client (mysqlclient), WhiteNoise, Gunicorn, and uWSGI
in order to provide you with the tools necessary for running web applications with Django.
Prerequisites:
Both of these actions can be performed through the SSH Access section of the hosting account's Control Panel.
To start, you will need to create a directory for your Django project. You can create the directory using the File Manager in your hosting Control Panel. In this example, we will create directory called "django". This directory needs to be created in the /private directory on your account.
Next, you need to create a WebApp for your Django project. Running Django as a WebApp allows the application supervisor on the server to manage the application. You can create a WebApp for Django using the WebApps section of the hosting Control Panel as follows:
It is now time to install the packages required for your Django project. To do so, follow these steps:
Logging Into Your Account via SSH using Putty
Logging Into Your Account via SSH using Terminal in Mac OS
myusername@s501:/home/myusername$ sureapp project shell Django
myusername@s501 [django:custom/-] /home/myusername/private/django$ echo "" >> /home/$USER/.bashrc ; echo "# Additional executables path" >> /home/$USER/.bashrc ; echo "export PATH=$HOME/.local/bin:\$PATH" >> /home/$USER/.bashrc ; . /home/$USER/.bashrc
myusername@s501 [django:custom/-] /home/myusername/private/django$ pip install virtualenv
myusername@s501 [django:custom/-] /home/myusername/private/django$ virtualenv /home/$USER/private/django/django1
myusername@s501 [django:custom/-] /home/myusername/private/django$ source django1/bin/activate
(django1) myusername@s501 [django:custom/-] /home/myusername/private/django/django1$ pip install Django
(django1) myusername@s501 [django:custom/-] /home/myusername/private/django/django1$ export MYSQLCLIENT_CFLAGS=-I/usr/local/mysqlX/include MYSQLCLIENT_LDFLAGS="-L/usr/local/mysqlX/lib -lperconaserverclient"
(django1) myusername@s501 [django:custom/-] /home/myusername/private/django/django1$ pip install mysqlclient
(django1) myusername@s501 [django:custom/-] /home/myusername/private/django/django1$ django-admin startproject mysite
By default, Django is configured to use SQLite for its database engine. In order to use MySQL with your Django application, you will need to follow these steps:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'myusername_databasename',
'USER': 'djangouser',
'PASSWORD': 'djangodbpassword',
'HOST': 'localhost',
'PORT': '3308',
}
}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'myusername_databasename',
'USER': 'djangouser',
'PASSWORD': 'djangodbpassword',
'HOST': 'localhost',
'PORT': '3308',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
},
}
}
ALLOWED_HOSTS = ['localhost','mydomain.com','www.mydomain.com']
(django1) myusername@s501 [Django:custom/-] /home/myusername/private/django/django1/mysite$ python manage.py migrate
In order to access the Django administration interface, you will need to create an administrator user for Django.
(django1) myusername@s501 [Django:custom/-] /home/myusername/private/django/django1/mysite$ python manage.py createsuperuser
Congratulations! You have successfully installed Django and set it up to use a MySQL database in "STRICT_TRANS_TABLES" sql mode. Run the development web server with the following command:
(django1) myusername@s501 [Django:custom/-] /home/myusername/private/django/django1/mysite$ python manage.py runserver 0:10101
You can now check whether the site is working as you expect at http://www.mydomain.com/admin/.
In order to have the Django content served, you will need to use some sort of web server. In our environment, this can be done by using Gunicorn or uWSGI.
(django1) myusername@s501 [django:custom/-] /home/myusername/private/django/django1/mysite$ pip install whitenoise
(django1) myusername@s501 [django:custom/-] /home/myusername/private/django/django1/mysite$ pip install gunicorn
import os
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
'whitenoise.middleware.WhiteNoiseMiddleware',
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
DEBUG = False
(django1) myusername@s501 [django:custom/-] /home/myusername/private/django/django1/mysite$ ./manage.py collectstatic
source /home/myusername/private/django/django1/bin/activate && cd /home/myusername/private/django/django1/mysite && gunicorn mysite.wsgi
(django1) myusername@s501 [django:custom/-] /home/myusername/private/django/django1$ pip install uwsgi
ALLOWED_HOSTS = ['localhost','mydomain.com','www.mydomain.com','DEFAULT_IP_ADDRESS']
DEBUG = False
(django1) myusername@s501 [django:custom/-] /home/myusername/private/django/django1/mysite$ ./manage.py collectstatic
[uwsgi]
chdir=/home/myusername/private/django/django1/mysite
home=/home/myusername/private/django/django1
http-socket=:10101
module=mysite.wsgi:application
die-on-term = true
master=1
vacuum=1
max-requests=5000
processes=8
threads=4
static-map=/static=/home/myusername/private/django/django1/mysite/staticfiles
source /home/myusername/private/django/django1/bin/activate && uwsgi /home/myusername/private/django/django1/uwsgi.ini
You can visit http://www.mydomain.com/admin to make sure that Django is running as expected.
All prices are in USD. No setup fees. Minimum contract period for shared hosting services - 12 months. Full prepayment for the contract period. 100-day money-back guarantee. No automatic renewal. Fees for domain registrations and SSL certificates cannot be refunded in case of an early contract termination.
ICDSoft 2001-2024 © All rights reserved
Terms of Use
|
Legal notice
|
Privacy
|
Reseller terms