How to Deploy Django App to GPUonCLOUD Cloud
Django is an open-source framework for Python applications. It helps to simplify the process of creating complex and database-driven web-applications, each of which can consist of a few pluggable modules. Due to such an approach, Django ensures enough simplicity and, at the same time, power, to become one of the best ways to start your web-developer career, being a truly easy-learning and high-demanded tool.
You can automatically get a preconfigured ready-to-work Django CMS instance up and running within minutes using our one-click installation widget:
The full list of applications, available for one-click installation, can be found at our Marketplace page or within the corresponding section at the dashboard.
As an alternative, you can deploy it manually with the help of the following step-by-step instruction, to take the complete control over the process. So, let’s see how to benefit on the provided at GPUonCLOUD Cloud Python hosting, implemented by means of the Apache + mod_wsgi bundle, on the example of Django CMS application hosting.
Create an Environment
First of all, let’s create an environment for Python applications hosting. To do this, follow the next steps:
1. Click the New environment button.
2. Navigate to the Python programming language tab, where the Apache application server will be selected automatically. State the resource limits, choose the region and enter the name (e.g. Django) for your environment.
-
You can also choose from the Python versions we provide by default: 2.7, 3.3, 3.4 or 3.5. While choosing, please take into consideration the compatibility of Django version you are going to use with the desirable Python version.
-
In case you are going to deploy a large and highly visited application, it makes sense to add a separate DB node to your environment. Otherwise, you can use the default SQLite database, which will be located inside the application server container.
Finally, click Create at the bottom right corner of the frame.
3. Just in a minute, your environment will appear at the dashboard.
For now, you can proceed to the Django CMS application deployment: either create and deploy a new application or deploy the already existing one.
Build and Deploy Django CMS
1. For the new Django application to be set up, you need to access your Apache application server via the GPUonCLOUD SSH Gateway first.
Tip: In case you haven’t performed similar operations before, you need to:
-
generate an SSH keypair
-
add your public SSH key to the dashboard
-
establish the connection over SSH protocol
2. After entering the container you should create a virtual environment for your application inside. In such a way, you’ll get an isolated Python environment and the ability to install packages without having the administrator privileges:
virtualenv virtenv
3. Activate and switch to the newly created virtual environment by executing the next command:
source virtenv/bin/activate
4. Now, let’s get and run the Django CMS installer script using the pip package manager:
pip install djangocms-installer
Wait a minute for the operation to be completed.
5. Then, you need to delete the preinstalled by default application and create a new Django CMS app:
rm -rf ROOT; djangocms -w -p . ROOT -s
6. After executing the abovementioned command, you will see a set of questions, which will help you to define the settings of your new application.
Let’s pay a special attention to the database configuration step:
-
In case you’ve added a separate DB node to your environment, specify the connection string to it, e.g for MySQL it will be mysql://mysql{node_id}-{env_name}.{hoster_domain}
-
If you prefer to work with the in-built SQLite database, define the desired location your local DB file will be placed at through entering the path to it (e.g. set it to be stored alongside with other projects’ data within the ROOT directory: sqlite://localhost/ROOT/project.db)
Note that Django version 1.7 and below is incompatible with Python 3.5, thus you should set it equal to 1.8 or higher in this case (or just use the stable one)
Complete the remaining wizard steps by entering your custom values or leaving the default ones (displayed at the end of strings in the square brackets).
7. When the application is configured, you’ll be asked to specify Username, Email and Password for your CMS administrator (superuser):
8. Next, enter the vim ROOT/wsgi.py command to edit the file with the mod_wsgi module entrypoint set. Replace this file content with the lines below:
import os,sys
virtenv = os.path.expanduser(‘~’) + ‘/virtenv/’
virtualenv = os.path.join(virtenv, ‘bin/activate_this.py’)
try:
if sys.version.split(‘ ‘)[0].split(‘.’)[0] == ‘3’:
exec(compile(open(virtualenv, “rb”).read(), virtualenv, ‘exec’), dict(__file__=virtualenv))
else:
execfile(virtualenv, dict(__file__=virtualenv))
except IOError:
pass
sys.path.append(os.path.expanduser(‘~’))
sys.path.append(os.path.expanduser(‘~’) + ‘/ROOT/’)
os.environ[‘DJANGO_SETTINGS_MODULE’] = ‘ROOT.settings’
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Save the made changes and exit the vim editor.
9. Then, create a symlink, that points to the file we’ve modified above, by executing the following line:
ln -sfT ~/ROOT/wsgi.py ~/ROOT/application
10. Finally, let’s link the static data. For that, perform the following steps:
-
enter the vim ROOT/settings.py command. In the opened file, find and adjust the values of the ALLOWED_HOSTS, MEDIA_ROOT, STATIC_ROOT and STATICFILES_DIRS parameters:
ALLOWED_HOSTS = [‘*’]
MEDIA_ROOT = os.path.join(DATA_DIR, ‘ROOT’, ‘media’)
STATIC_ROOT = os.path.join(DATA_DIR, ‘ROOT’, ‘static’)
STATICFILES_DIRS = (
os.path.join(BASE_DIR, ‘ROOT’, ‘static_local’),
)
Note: ALLOWED_HOSTS can contain a list of strings representing the host/domain names that this Django site can serve. In the example, we use a value of ‘*’ that will match any request’s Host header. However, you can specify a fully qualified name (not including port) or a value beginning with a period (e.g. .GPUonCLOUD.com) as a subdomain wildcard.
Save the changes and exit vim.
-
create new subdirectories for the static and media content:
mkdir ROOT/static_local; mkdir ROOT/media
-
and, lastly, run the synchronization of resources:
./manage.py collectstatic
Enter yes if system asks for confirmation and wait till this process is over.
11. That’s all! Now, you can return to the dashboard and press Open in browser button next to your environment with Django CMS.
As a result, you will see the application administration panel.
For entering, use the superuser credentials you’ve specified while creating your Django CMS application.
Deploying Existing Django Application
In case you already have the previously built and packaged Django application, the deployment procedure can be performed just in a few steps, described below.
Note: Before proceeding further, please ensure your Django package contains the following files in its root:
-
requirements.txt file with the names of all the Python modules your application requires. This file will be read by deployment script in order to automatically install the listed modules with the help of the pip manager.
-
application file with the entry point script for running your application inside a virtual environment by means of Apache mod_wsgi (an example for Django apps can be seen here)
1. Upload the package with your Django application to the GPUonCLOUD dashboard via Deployment manager.
2. Press the Deploy to button and choose a necessary environment.
3. After the operation is finished, select the Open in browser button next to your environment.
4. That’s all! Now you can enjoy working with your Django application.
As you can see, it’s quite easy to deploy Django CMS and the appropriate Django-based applications with the GPUonCLOUD Python hosting. Nevertheless, in case you face any issues with this instruction, feel free to appeal for our technical experts’ assistance at StackOverflow.