Chris Shaw

Chris Shaw

Lead Profile All Articles

I am an ardent software developer and eager lifelong learner of code. My passion is the python language with particular emphasis in the 'django' and  'opencv' packages. I have created this blog to share my e... Read More

Tutorials


Hosting your Django 3 Project with A2 Hosting

June 5, 2020 Chris Shaw 134 views

It has been over a year since my inital post regarding hosting your Django Project with A2 Hosting and a few things have changed in that time, so it is time for an update. My earlier post is still relevent for Django versions 1.x and 2.x, but Django 2.2 was the last to support Python 3.5. Django 3.0 supports Python 3.6, 3.7, and 3.8. You can read the release notes here. The other major change is that Python App in your A2 cPanel has been updated and improved.

BUT there is a kicker. Unfortunatly the Python 3.7.3 that is on the A2 server has been compiled with SQLite 3.6.20 where Django 3.0 requires SQLite 3.8.3 or later. This means we cannot use SQLite and will use the MySQL database instead. In any case, you should not be using SQLite in a production enviroment.

Let me assume that you already have a hosting package with A2 Hosting. We will need to create the domain or subdomain, the python app and the MySQL database using the cPanel. We will then use the shell to install Django and finalize settings.

1. Setup the domain.
Every Python app on our server, requires its own domain or sub domain. Here I shall create a subdomain called project, to my primary domain, here shown as example.com. First search for domain in the cPanel finder and click on 'Subdomains'.

 

In the subdomains form, I have called the subdomain 'project', selected the main domain 'example.com' and called the Document root 'project_html'. The root name and location is personal preferance. Finally, click on the blue 'Create' button to create the subdomain. The project folder will also be created if it does not already exist. Next we can move on and create the pyton app.

 


2. Setup the Python app.

Now, type 'Python' in the search bar and then click on 'Setup Python App' to open.

 

You are presented with a list of your existing python apps. It will be blank if this is your first app. Click on the blue '+ CREATE APPLICATION' button to start creating the Python application.

In the Create Application form, there are the following fields:
Python Version: At trime of writing, 3.7.3 is the higest version and works well with Django 3.
Application Root: Use the same as the domain document root. This is the folder where we will install the Django project.
Application startup file: Leave blank now, it will auto fill on save.
Application Entry Point: Leave blank now, it will auto fill on save.
Passenger Log File: Enter a log file in you application root. This log file is very useful for debugging.

 


Click the blue 'CREATE' button to create and start the inital Python application.

 

In the dark blue bar, we have a very useful comand to enter the virtual enviroment and cd into the root folder. Simply clicking on the text copies the command to the clipboard. You can click on the 'OPEN' link to open the app in a new window. If all successful, you will see the following. Congrautlations, you have a working Python app.


 

3. Create the MySQL database.

Next, type 'mysql' in the cPanel search bar and then click on 'MySQL Databases' to open.

 

Create a mysql database

 

Create a mysql user

 

Assign the user to the database and assign fill privileges.

Our MySQL database is ready. We can now enter the shell to install and setup Django.

 

4. Install Django from the shell.

We are now ready to install django from the command line. You will need the SSH details provided by our host. Hosting sites tend to not use the default port 22, so follow their instructions carefully.

ssh -p 7822 username@server.a2hosting.com

And you will be prompted for your password.

Once in, you need to activate your virtual environment using the command we copied from the Python app.

 

username@server [~]# source /home/username/virtualenv/domain_html/3.7/bin/activate && cd /home/username/project_html

(project/html:3.7)username@server [~/project_html]#


Now we are ready to start installing Django. To install the latest version, run the command:

(project/html:3.7)username@server [~/project_html]# pip install django mysqlclient

I am not going to the details of starting a new poject. The Django docs are an excellent source of information on this subject. The command below is simply to start a new project called myproject in the current directory.

(project/html:3.7)username@server [~/project_html]# django-admin startproject myproject .

I will edit the projects 'settings.py' to add the domain to the allowed hosts and instert the MySQL database settings. These are the only changes we need to get the project live. Further tweeks can be made later.

(project/html:3.7)username@server [~/project_name]# vim myproject/settings.py

Find:

ALLOWED_HOSTS = []

And add the (sub)domain we have created:

ALLOWED_HOSTS = ['project.example.com']

Next, find the database settings:

DATABASES = {

  'default': {

    'ENGINE': 'django.db.backends.sqlite3',

    'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

  }

}

And replace with the following, inserting the database_name, database_user and databse_password appropriately:

DATABASES = {

  'default': {

    'ENGINE': 'django.db.backends.mysql',

    'NAME': 'database_name',

    'USER': 'database_user',

    'PASSWORD': 'database_password',

    'HOST': 'localhost',

    'PORT': '3306',

    'OPTIONS': {

      'sql_mode': 'STRICT_ALL_TABLES',

    },

  }

}

Write the changes and exit. The Django project is now ready.

5. Finalize settings.

Next we will edit the passenger_wsgi.py file that has been created in out root folder. I have a gist on Github of the file that I use for all my projects hosted on A2 servers. First we will move the existing file to a new name:

(project/html:3.7)username@server [~/project_name]# mv passenger_wsgi.py passenger_wsgi_original.py

Next, fetch the replacement file using wget:

(project/html:3.7)username@server [~/project_name]# wget https://gist.githubusercontent.com/cjshaw1976/13af52c51868c92baa0c712de51f9014/raw/e2d9a8d74a0b5c1ad1b6bd1ec029a7bebce3a99b/passenger_wsgi.py

We only have to make two small changes to this new file, to accommodate our project. Open the file to edit:

(project/html:3.7)username@server [~/project_name]# vim passenger_wsgi.py

Near the end, replace the two occurrences of projectname with the actual project name, in this example myproject. Write the changes and exit. Finally, we are now ready to restart the application and test.

6. Restart the application.

We now need to return to the cPanel to restart the Python app to apply the changes we have made. Fortunatly, this is very easy as there is a restart button on the Python app list. Simply click the anti-clockwise arrow icon on the approiate application to restart that app. DO NOT PRESS THE TRASH CAN ICON - this will delete the Python Application.

Now when we open the (sub)domain in a browser, we will see the django welcome page.

 

Finally, do not fear the Phusion page of death.

If you should see the unhelpful Phusion page of death, do not worry as you can now debug as you have access to the log file. You set up the location in the Python App, I suggested you keep it in the projects root folder. Take a look at the last few lines and you can normally debug the error.


blog comments powered by Disqus