How to deploy a Django Application and set your Python Environment
How to deploy a Django web application on Mochahost.com.
NOTE: This article applies for Linux Shared (Business + Mocha packages ONLY).Step-By-Step:
1. Login to Cpanel and in Software/Services area choose Setup Python App.
2. Setup new application form will appear. Choose Python
version for your application, application folder name (App
Directory) and App URI for accessing your application. Click Setup to create an application.
NOTE: We recommend that you save your applications inside an inner folder. For example ‘python_apps’ could be a good name for it, so your application directory would be “/home/username/python_apps/app” (type just python_apps/app in the ‘ App Directory’ field) and the virtual environment for that app into the "/home/username/virtualenv" folder would looks like: “python__apps_app”.
- After a little while a new application entry will be appended to
the web-page. Inside "/home/username/public_html" folder a new .htaccess will be created, which has directives that point to your app directory
3. You can edit the URI for the application, wsgi handler. If you click
Edit - the value is converted to input field and thus becomes
editable. When editing is complete, click Save.
4. Wsgi entry is to specify python wsgi application entry point. Once you have created your app, the app directory '/home/username/python_apps/app' will contain the following structure:
--- restart.txt (empty file)
In the WGI file location field, specify an absolute path to that file adding ‘:app’ at the end, for example: ‘/home/username/python_apps/app/passenger_wsgi.py:app’ (it shouldn't be necessary to change this field, as it is taken by default by the app).
5. When Show control is clicked, python extensions section will be
expanded. It gives the ability to Add or Remove python modules.
- When start typing in input field, appropriate hints are shown in
- Choose the entry you want from drop-down and click
- If you click Delete, the corresponding module entry will
PS. In addition to setting uri and wsgi, the Python version can be changed as well by changing the value in select drop-down.
6. No changes are applied to application environment until Update
button is clicked. Before the Update button is clicked, all
changes can be reverted with Reset button.
- Click Remove to delete the application - the application folder
itself will remain unmoved.
Let’s say your app directory is ‘pytho_apps/app’.
Optional: if you’d like to see errors that are not managed by Django application add the following line into your .htaccess (you can find it inside the /home/username/public_html folder):
1. Type Django in Modules empty box
2. Choose Django version.
3. Execute the command next to "Command for entering to virtual environment" to activate the virtual environment. For example source /home/username/virtualenv/app/2.7/bin/activate and click Run.
4. To create the Django application execute command "/home/username/virtualenv/app/2.7/bin/django-admin startproject app". This will create the Django project in the same folder where ‘passenger_wsgi.py’ is. Now, your app directory would have the following content:
5. Remove file passenger_wsgi.py from your app directory. Copy wsgi.py from ‘your_app_directory/app’ (if you have followed the initial recommendations it would be /home/username/python_apps/app/app’), rename it to ‘passenger_wsgi.py’ and paste it outside this folder, which means in the app directory where the original ‘passenger_wsgi.py’ file was removed.
If you had already set the ‘WGI file location’ field, you can follow the link in the ‘App URI’ field, and you should see the ‘It worked’ message from Django:
On the other hand, If you had not set ‘WGI file location’ yet, once you set it (with absolute path as it was explained above ‘/home/username/python_app/app/passenger_wsgi.py:app’), the content of the file will be changed, resulting in a 500 error page. So, all you need to do is repeat step 6.
PS: In order for your Static Content to be processed you need to upload the files in the /static folder under your application directory. The STATIC_ROOT variable in settings.py defines the single folder you want to collect all your static files into.