How to install Django CMS on your Cloud Web hosting

Vedi come Markdown

Learn how to install Django CMS with Python on your OVHcloud Cloud Web hosting

Objective

Django CMS is a content management system (CMS) written in Python and based on the Django web framework.

OVHcloud Cloud Web hosting lets you use Python as a runtime for your websites. You can therefore install and host Django CMS on it, as well as any other web application designed in this language.

In this tutorial, we will install a website with Django CMS on an OVHcloud Cloud Web hosting plan. We will then make it available behind your domain name.

Requirements

  • An OVHcloud Cloud Web hosting plan.
  • Being logged in to your , in the Web Cloud section.
  • What you need to know:
    • The basics of the Python ecosystem.
    • How to connect via SSH.
    • How to edit a file from the command line, using Vim, Emacs or Nano for example.

Instructions

Step 1: enable Python as a runtime

To access the runtimes of your Cloud Web hosting, log in to your . Click Hostings in the services bar on the left, then choose the name of the relevant Cloud Web hosting. Finally, go to the Runtimes tab.

The table that appears displays the runtimes currently added. Make sure that the Python runtime is enabled. If so, proceed to Step 2: “Associate Python with a multisite”.

django-cloud-web

If it is not, add a new one (if your plan allows it) or modify the existing runtime.

  • If you want to add a runtime: click Actions above the table, then Add a runtime.
  • If you want to modify a runtime: click the ... button to the right of the relevant runtime, then Modify.

In the window that appears, fill in the requested information with the following values from our example, or adapt them to your situation.

InformationValue to enter
Custom namePython 3
Runtimepython-3
Path to the public directorypublic
Application environmentproduction
Application launch scriptserver.py

Once the information is filled in, click Confirm. If you want more information about managing runtimes, refer to our guide “Managing Cloud Web runtimes”.

django-cloud-web

Step 2: associate Python with a multisite

Now that Python is enabled as a runtime, you need to associate it with one of your multisites. To do this, go to the Multisite tab. The table that appears contains all the domain names that have been added as a multisite.

django-cloud-web

Two columns in the table above deserve your attention. Check that the Python runtime is properly linked to the relevant domains and that the root folder is correct; use the information below if necessary. If so, proceed to Step 3: “Connect to your Cloud Web via SSH”.

ColumnDescription
Root folderThis is the root folder that must contain the source code of the relevant domain (it corresponds to the “DocumentRoot”). In our example, we choose to specify “django”. It must therefore contain our Python source code.
RuntimeThis is the runtime associated with the relevant domain. The name displayed corresponds to the “Custom name” you defined when creating the runtime. In our example, you should find “Python 3”.

If it is not, add a new multisite or modify the existing one.

  • If you want to add a multisite**: click Add a domain or subdomain to the right of the table.
  • If you want to modify a multisite**: click the ... button to the right of the relevant domain name, then Modify.

In the window that appears, fill in the requested information according to your own situation. The table below shows the values used for this tutorial.

InformationValue used as an example for this tutorial
Domaindjango.demo-cloudweb.ovh
Root folderdjango
RuntimePython 3

As for the additional options, choose the ones you want to enable. Once the information is filled in, click Next, then complete the operation. This addition can take up to one hour. However, changing the DNS configuration can take up to 24 hours to become fully effective. If you want more information about managing multisites, refer to our guide “Sharing your hosting between several websites”.

django-cloud-web

Step 3: connect to your Cloud Web via SSH

First, retrieve the information that lets you connect. To do this, go to the FTP - SSH tab. If it does not appear in the list, first click the button with three bars. The information related to your storage space is then displayed. Locate the items mentioned next to the following elements:

ElementsDescription
SSH access to the clusterThe element that appears lets you retrieve two pieces of information: the server address (it starts after “ssh://” and ends before the “:”) and the connection port (the number is mentioned after the “:”). For example, we might find: ssh://sshcloud.cluster024.hosting.ovh.net:12345/, so “sshcloud.cluster024.hosting.ovh.net” as the server address and “12345” as the connection port.
Main SSH loginThis is the main SSH username created on your hosting.

If you no longer know the password of the SSH user, click the ... button to the right of the relevant user in the table, then Change password.

django-cloud-web

Now, to connect via SSH, you need to use a terminal. This tool is installed by default on macOS or Linux. A Windows environment will require installing software such as PuTTY or adding the “OpenSSH” feature. As this procedure is specific to the operating system you use, we cannot detail it in this documentation.

Here is an example of a command line you can use. Replace the elements “sshlogin”, “sshserver” and “connectionport” with the ones suited to your own situation. Once the command is sent, you will be prompted to enter the password of the SSH user.

ssh sshlogin@sshserver -p connectionport

Step 4: prepare the Python environment

We will now prepare the Python environment needed to host our Django CMS application. The goal is to isolate the Python files and the dependencies of our application from the rest of the Python files and libraries present on the system. In the Python ecosystem, this isolation is easy to set up thanks to the virtualenv utility.

From the SSH connection open on your Cloud Web, install virtualenv via pip (it is the most widely used package manager for the Python language):

democld@cloudweb-ssh:~ $ pip3 install --user virtualenv
Collecting virtualenv
  Downloading https://files.pythonhosted.org/packages/4f/ba/6f9315180501d5ac3e707f19fcb1764c26cc6a9a31af05778f7c2383eadb/virtualenv-16.5.0-py2.py3-none-any.whl (2.0MB)
    100% |################################| 2.0MB 164kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-16.5.0

Modify the PATH environment variable to add the freshly installed “virtualenv” command:

democld@cloudweb-ssh:~ $ export PATH=$PATH:~/.local/bin
democld@cloudweb-ssh:~ $ virtualenv --version
16.5.0

You can make this change to your “PATH” persistent by adding the export to the “~/.profile” file:

democld@cloudweb-ssh:~ $ echo "export PATH=$PATH:~/.local/bin" >> ~/.profile

You can now move into the “django” folder and create a virtualenv, which we will name “venv” for this tutorial.

democld@cloudweb-ssh:~ $ cd django/
 
democld@cloudweb-ssh:~/django $ virtualenv venv
Using base prefix '/usr'
New python executable in /home/democld/django/venv/bin/python3
Not overwriting existing python script /azehome/democld/django/venv/bin/python (you must use /home/democld/django/venv/bin/python3)
Installing setuptools, pip, wheel...
done.

Now that your virtualenv is in place, you can access it via the command “source < virtualenv name >bin/activate”. Once inside your virtualenv, all the packages and libraries you install will be installed only here.

When your virtualenv is activated, you see in your terminal your prompt preceded by “(venv)” (or another name, if you chose a different one when creating the virtualenv). You can exit this virtualenv at any time, using the “deactivate” command.

democld@cloudweb-ssh:~/django $ source venv/bin/activate
(venv) democld@cloudweb-ssh:~/django $

Step 5: install and configure Django CMS

Django CMS provides a utility named “djangocms-installer” to ease its installation.

Still from within your virtualenv, install this utility with pip:

(venv) democld@cloudweb-ssh:~/django $ pip install djangocms-installer
Collecting djangocms-installer
  Downloading https://files.pythonhosted.org/packages/b9/32/882c86fd8efe8c1bb802f04a8ed649e8b74ff77a83f4201b37073c6d9b8f/djangocms_installer-1.1.0-py2.py3-none-any.whl (55kB)
     |################################| 61kB 4.2MB/s
Collecting six (from djangocms-installer)
  Downloading https://files.pythonhosted.org/packages/73/fb/00a976f728d0d1fecfe898238ce23f502a721c0ac0ecfedb80e0d88c64e9/six-1.12.0-py2.py3-none-any.whl
Collecting dj-database-url>=0.4 (from djangocms-installer)
  Downloading https://files.pythonhosted.org/packages/d4/a6/4b8578c1848690d0c307c7c0596af2077536c9ef2a04d42b00fabaa7e49d/dj_database_url-0.5.0-py2.py3-none-any.whl
Requirement already satisfied: pip in ./venv/lib/python3.5/site-packages (from djangocms-installer) (19.1)
Collecting tzlocal (from djangocms-installer)
  Downloading https://files.pythonhosted.org/packages/cb/89/e3687d3ed99bc882793f82634e9824e62499fdfdc4b1ae39e211c5b05017/tzlocal-1.5.1.tar.gz
Collecting pytz (from tzlocal->djangocms-installer)
  Downloading https://files.pythonhosted.org/packages/3d/73/fe30c2daaaa0713420d0382b16fbb761409f532c56bdcc514bf7b6262bb6/pytz-2019.1-py2.py3-none-any.whl (510kB)
     |################################| 512kB 1.4MB/s
Building wheels for collected packages: tzlocal
  Building wheel for tzlocal (setup.py) ... done
  Stored in directory: /home/democld/.cache/pip/wheels/15/ae/df/a67bf1ed84e9bf230187d36d8dcfd30072bea0236cb059ed91
Successfully built tzlocal
Installing collected packages: six, dj-database-url, pytz, tzlocal, djangocms-installer
Successfully installed dj-database-url-0.5.0 djangocms-installer-1.1.0 pytz-2019.1 six-1.12.0 tzlocal-1.5.1
 
(venv) democld@cloudweb-ssh:~/django $ which djangocms
/home/democld/django/venv/bin/djangocms

Now that this utility is installed, we will use it to create our Django CMS application. Still from within your virtualenv and placed in the root directory of your site (django in this tutorial), run the following command:

djangocms -f -p . mysite -s

Here is some information about the meaning of the options (you can find all the available options via the command “djangocms --help”):

  • -f: installs and configures the “django-filer” plugins;
  • -p . : uses the current folder as the parent folder of our new Django CMS project;
  • mysite: name of your new project;
  • -s: do not check whether the current folder is empty.
(venv) democld@cloudweb-ssh:~/django $ djangocms -f -p . mysite -s
Creating the project
Please wait while I install dependencies
If I am stuck for a long time, please check for connectivity / PyPi issues
Dependencies installed
Creating the project
Operations to perform:
  Apply all migrations: admin, auth, cms, contenttypes, djangocms_column, djangocms_file, djangocms_googlemap, djangocms_link, djangocms_picture, djangocms_snippet, djangocms_style, djangocms_text_ckeditor, djangocms_video, easy_thumbnails, filer, menus, sessions, sites
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  .....
  Applying sites.0002_alter_domain_unique... OK
Creating admin user
All done!
Get into "/home/democld/django" directory and type "python manage.py runserver" to start your project
 
(venv) democld@cloudweb-ssh:~/django $ ls -l
total 778
drwxr-xr-x 2 democld democld      3 Apr 30 16:21 __pycache__
-rwxr-xr-x 1 democld democld    804 May  7 10:54 manage.py
drwxr-xr-x 2 democld democld      2 May  7 10:54 media
drwxr-xr-x 5 democld democld      9 May  7 10:54 mysite
-rw-r--r-- 1 democld democld 741376 May  7 10:55 project.db
drwxr-xr-x 2 democld democld      2 Apr 30 15:09 public
-rw-r--r-- 1 democld democld    451 May  7 10:55 requirements.txt
lrwxrwxrwx 1 democld democld     21 Apr 30 16:20 server.py -> /usr/share/ovh/app.py
drwxr-xr-x 2 democld democld      2 May  7 10:54 static
drwxr-xr-x 4 democld democld      4 May  6 11:33 venv

Django CMS is now installed on your Cloud Web.

The “manage.py” file was created automatically during this operation. Modify it with the following content:

(venv) democld@cloudweb-ssh:~/django $ cat /dev/null > manage.py
 
(venv) democld@cloudweb-ssh:~/django $ vim manage.py
import sys, os
 
cwd = os.getcwd()
INTERP = cwd+'/venv/bin/python'
if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)
  
sys.path.append(cwd)
sys.path.append(cwd + '/venv')
  
sys.path.insert(0,cwd+'/venv/bin')
sys.path.insert(0,cwd+'/venv/lib/python3.5/site-packages')
  
os.environ['DJANGO_SETTINGS_MODULE'] = "mysite.settings"
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

When configuring the Python runtime (see Step 1), we defined the launch script of our application as “server.py”. However, Django CMS uses a file named “manage.py”. To link Django CMS to your runtime, create a symbolic link between “server.py” and “manage.py”:

(venv) democld@cloudweb-ssh:~/django $ ln -fs manage.py server.py
(venv) democld@cloudweb-ssh:~/django $ ls -l
total 778
drwxr-xr-x 2 democld democld      3 Apr 30 16:21 __pycache__
-rwxr-xr-x 1 democld democld    804 May  7 10:54 manage.py
drwxr-xr-x 2 democld democld      2 May  7 10:54 media
drwxr-xr-x 5 democld democld      9 May  7 10:54 mysite
-rw-r--r-- 1 democld democld 741376 May  7 10:55 project.db
drwxr-xr-x 2 democld democld      2 Apr 30 15:09 public
-rw-r--r-- 1 democld democld    451 May  7 10:55 requirements.txt
lrwxrwxrwx 1 democld democld      9 May  7 11:32 server.py -> manage.py
drwxr-xr-x 2 democld democld      2 May  7 10:54 static
drwxr-xr-x 4 democld democld      4 May  6 11:33 venv

Modify the “mysite/settings.py” file with the following command, replacing “django.demo-cloudweb.ovh” with the address of your multisite (configured in Step 2):

(venv) democld@cloudweb-ssh:~/django $ sed -i "s/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = ['django.demo-cloudweb.ovh']/" mysite/settings.py

Step 7: restart the Python daemon

To restart the Python daemon, return to your . Go to the Multisite tab, click the ... button to the right of the relevant domain name, then choose Restart.

Once this is done, the application will be accessible via the domain name chosen in your multisite configuration.

django-cloud-web

Congratulations, your website using Django CMS is now available! An administrator account has been created by default, with “admin”/“admin” as the login/password. Remember to change this password.

django-cloud-web

Step 8: use HTTPS

For added security on your site, you can set up an automatic redirect from HTTP to HTTPS. To do this, while still in the django folder, create a .htaccess file with the following content:

RewriteEngine On
RewriteCond %{ENV:HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Conclusion

We have seen how to install a Python application on a Cloud Web hosting plan, following the various steps. All that remains is to use Django CMS and publish your first content!

You will find more information about Django CMS and its features in the official project documentation.

Go further

Migrating my website to OVHcloud

Putting my website online

Installing your website with the 1-click modules

Sharing your hosting between several websites

For specialised services (SEO, development, etc.), contact the OVHcloud partners.

If you would like assistance using and configuring your OVHcloud solutions, we invite you to consult our various support offers.

Join our community of users.

Questa pagina ti è stata utile?