Creating a Python App using the Django framework
From the cPanel SOFTWARE section, click the Setup Python App icon.
Click the + CREATE APPLICATION button. For this tutorial, we are going to create a simple application using the DJANGO framework. Complete the fields.
In our example, we will install the application in
/home/krystald/myapp, and serve it from
http://krystaldemo.co.uk/djangoapp, using Python version 3.7.
Once the application has been created, you will be shown the following information, which includes how to switch to the virtual environment for your python application from the command line.
Log into your cPanel account via SSH.
Enter the virtual environment command line above.
[[email protected] ~]$ source /home/krystald/virtualenv/myapp/3.7/bin/activate && cd /home/krystald/myapp
(myapp:3.7)[[email protected] myapp]$
You will notice that the command prompt changes, and is now prefixed with (
myapp:3.7). This tells us that our shell environment is now setup for our application's own python installation. List the files in the current directory, which should look like this.
(myapp:3.7)[[email protected] myapp]$ ls -la
drwxr-xr-x 4 krystald krystald 4096 Oct 16 08:18 .
drwx--x--x 26 krystald krystald 4096 Oct 16 08:18 ..
-rw-r--r-- 1 krystald krystald 333 Oct 16 08:18 passenger_wsgi.py
drwxr-xr-x 2 krystald krystald 4096 Oct 16 08:18 public
drwxr-xr-x 2 krystald krystald 4096 Oct 16 08:18 tmp
If we browse to our URL, in this case
http://krystaldemo.co.uk/djangoapp, we will see a simple message giving the Python version.
This is because cPanel created a basic application inside,
passenger_wsgi.py. This file is used by Passenger, a web application server that uses the WSGI (Web Server Gateway Interface) convention to provide the glue between your python application and our webservers.
Now, install the DJANGO framework - As of 16/10/2019, the latest version of DJANGO requires a later version of sqlite3 than that which ships with Centos 7, so we are not installing the most current version.
$ pip install Django==2.1.*
Downloading https://files.pythonhosted.org/packages/c4/e1/9c31da650533111ec044c2e540be72287d87c7b0ddb076640f943d900f98/Django-2.1.13-py3-none-any.whl (7.3MB)
|████████████████████████████████| 7.3MB 5.4MB/s
Requirement already satisfied: pytz in /home/krystald/virtualenv/myapp/3.7/lib/python3.7/site-packages (from Django==2.1.*) (2019.3)
Installing collected packages: Django
Successfully installed Django-2.1.13
Now we will create an empty DJANGO project. This needs to be done outside of our application path,
/home/krystald/myapp, because the project creates an unnecessary parent directory. Enter the following commands.
$ cd ~/
$ django-admin startproject dproj
$ cp -a dproj/. myapp/
$ rm -rf dproj
$ cd myapp
This will create the basic files needed to establish the DJANGO application and move them into your Python application directory, which should now have a structure like this:
| |-- __init__.py
| |-- settings.py
| |-- urls.py
| `-- wsgi.py
We can test the installation is working
$ python myproject/manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
October 16, 2019 - 07:34:52
Django version 2.1.13, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Ctrl-c to exit. Now we need to make some minor configuration changes to get the DJANGO framework running. Firstly, we should address the unapplied migrations reported above:
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying sessions.0001_initial... OK
To avoid an
DisallowedHost error, we need to ensure that DJANGO knows the hostname our site is going to use. We do this by editing the
$ nano dproj/settings.py
and change the ALLOWED_HOSTS entry to include your domain name, as follows:
ALLOWED_HOSTS = ['krystaldemo.co.uk']
y to exit, saving changes.
Next, we need to tell Passenger where to find the application start point.
$ nano passenger_wsgi.py
Edit the contents as follows:
wsgi = imp.load_source('wsgi', 'dproj/wsgi.py')
application = wsgi.application
y to exit, saving changes.
At this point, you should be able to load (in this case)
http://krystaldemo.co.uk/djangoapp/ which will return a Page not found (404) error. This is normal because no application URLs have yet been added.
If all has gone well,
http://krystaldemo.co.uk/djangoapp/admin should now display the Django administration screen.