How to deploy a Python application
Knowledgebase Article
Knowledgebase Article
In order to deploy a Python application we need to enable Python using cPanel, please review the following article: Enabling Python in cPanel
In this example, we will cover the basics and teach you how to deploy Wagtail CMS as an example.
For those who are experienced deploying Python applications, it worth mentioning that the web server is configured with Phusion Passenger. The Python Selector basically setups all the basics for you:
Using Python Selector we have added our application base folder:
Our application will use Python 3.4 and the root path will be /home/pythontest/myapps/wagtail.
If go to that directory you will see that the selector creates by default three folders: tmp, public, logs.
All the application files that requires public access will need to be placed inside the public folder.
After we press “Setup” the selector will create the application and provide us the command to activate Python:
Now, we will continue the installation via SSH. This is what we are going to do:
a) Login via SSH to your account and activate python with the command provided by Python Selector:
pythontest@ ~ $ source /home/pythontest/virtualenv/myapps_wagtail/3.4/bin/activate (myapps/wagtail:3.4)pythontest@ ~ $
b) When it comes to installing python modules, you have two repositories easy_install and PIP. I always recommend PIP because it’s up-to-date and most used by developers. Let’s install wagtail module using PIP:
(myapps/wagtail:3.4)pythontest@ ~ $ pip install wagtail
c) Once installed, Wagtail provides a command similar to Django’s “django-admin startproject” which stubs out a new site/project. In our example, we setup our project on “myapps/wagtail”, so let’s go to “myapps/wagtail” folder. There, you will see that the Selector already configured the default structure for us where passenger_wsgi.py is the entry point (like index.html or index.php as we know from other languages), tmp folder and “public” folder which is where we need to place the files. We will now go to “public” folder and setup our project:
(myapps/wagtail:3.4)pythontest@ ~/myapps/wagtail/public $ wagtail start mywag
I called my project/site “mywag”, it can be actually any name.
If we change directory to “mywag” we will see what the installer has added for us. There are a couple of things I would like to discuss:
d) Now we need to setup the database. Wagtail uses Sqlite3 by default, however, it supports MySQL and PostGreSQL. As Wagtail is based on Django, if you would like to change the driver you will have to modify mywag/settings/base.py file. If you open that file you will see:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
You can review the documentation and change there the code to use MySQL for example: https://docs.djangoproject.com/en/2.0/ref/settings/#databases
In this example, we will use Sqlite3 just to install things quicker. Let’s execute the following command:
(myapps/wagtail:3.4)pythontest@ ~/myapps/wagtail/public/mywag $ python manage.py migrate
e) Create the super user:
(myapps/wagtail:3.4)pythontest@ ~/myapps/wagtail/public/mywag $ python manage.py createsuperuser
Username (leave blank to use 'pythontest'): kualo
Email address: t.patino@kualo.com
Password:
Password (again):
Superuser created successfully.
If your application is not based on Django, the file passenger_wsgi.py will be your entry point like index.html for HTML sites or index.php for PHP application.
As we saw earlier, Wagtail provides a wsgi.py example, we will use that and replace our file passenger_wsgi.py. However, as we talked about earlier we need to inform passenger where the application project is, for that we are going to make some changes.
sys.path.append("/home/pythontest/myapps/wagtail/public/mywag")
The above is using the path from this example, replace the path with the one from your project!
That’s all. Now you can load your domain name with /admin and login to the backend with your super user credentials.