Python
Create a Clever Cloud account
The API of Clever Cloud uses OAuth 1 to perform authentication actions. There are two ways to signup to Clever Cloud: email or GitHub login.
Go to the Clever Cloud Console and select the method you prefer.
Email Authentication
This kind of auth requires a valid and non-temporary disposable e-mail, and a password having at least 6 characters. Do not forget to validate your email by clicking the link you will receive.
GitHub Authentication
The GitHub signup allows you to create an account or link your existing one to GitHub, in one click. This process asks the following permissions:
- Read your Public Key
- Read User Repositories
The “repository permission” is used to deploy your GitHub apps directly to Clever Cloud, with a simple step.
If you need to give access to Clever Cloud’s API to a specific GitHub organization, you can do it here.
Two Factor Authentication (2FA)
Clever Cloud supports 2FA. You can enable it here: https://console.clever-cloud.com/users/me/authentication
Please, backup your recovery codes, we won’t be able to restore access to your account if you lose access to your regular codes.
You need to add a SSH key to your Clever Cloud’s account to deploy via Git. SSH keys are used to establish a secure connection between your computer and Clever Cloud. A user can have multiple SSH keys.
I need to create my SSH key
In your Terminal, enter the following bash line:
ssh-keygen -t ed25519 -C "[email protected]"
This command creates a new ssh key using the provided email, so that the owner of the key can be identified.
When prompted in which file you want to save the key, just press enter. If it says that the file already exists, enter
n
forno
. Typels
, verify the presence of the file and jump to Add your SSH key on Clever Cloud.When asked, enter a passphrase:
Generating public/private ed25519 key pair.
Enter file in which to save the key (/your_home_path/.ssh/id_ed25519):
# Now you should enter a passphrase.
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]
Which should give you something like this:
Your identification has been saved in /your_home_path/.ssh/id_ed25519.
Your public key has been saved in /your_home_path/.ssh/id_ed25519.pub.
The key fingerprint is:
01:0e:e5:2d:ab:98:d6:17:a1:6d:f0:68:9f:d0:a2:db [email protected]
I created my account with GitHub
If your account is linked to GitHub, a panel with your GitHub SSH keys will appear in the “SSH Keys” tab. You can add any key already present in your GitHub account by clicking on the import button next to it.
Add your SSH key on Clever Cloud
Public SSH Key
To declare your public SSH Key on Clever Cloud, in the left navigation bar, go in “Profile” and in the “SSH Keys” tab, add the key by entering a name and the public SSH key. The key is the entire content of the **id_[ed25519/rsa].pub** file including the id_ed25519/ssh-rsa
part and your email.
Private SSH Key
If you want to clone a repository from a private repository, you can add a Private SSH Key to an application by creating a folder clevercloud at the root of your application and creating the file clevercloud/ssh.json
with the following content:
{
"privateKeyFile": "path/to/file"
}
Of course you need to provide a valid path to a file that contains a valid key and that you will push on the Clever Cloud git remote.
If you have trouble configuring this key you can refer to this extended documentation
Create an application on Clever Cloud
With the web console
Refer to our getting started for more details on application creation via the console.
With the Clever Tools CLI
- Make sure you have clever-tools installed locally or follow our CLI getting started guide.
- In your code folder, do
clever create --type <type> <app-name> --region <zone> --org <org>
where :type
is the type of technology you rely onapp-name
the name you want for your application,zone
deployment zone (par
for Paris andmtl
for Montreal)org
the organization ID the application will be created under.
Refer to clever create for more details on application creation with Clever Tools.
Setting up environment variables on Clever Cloud
With the Clever Cloud console
- Go to the Clever Cloud console, and find the app you want to fine tune under it’s organization.
- Find the Environment variables menu and select it.
- In this menu, you will see a form with VARIABLE_NAME and variable value fields.
- Fill them with the desired values then select Add.
- Don’t forget to “Update Changes” at the end of the menu.
With the Clever Tools CLI
- Make sure you have clever-tools installed locally. Refer to our CLI getting started.
- In your code folder, do
clever env set <variable-name> <variable-value>
Refer to environment variables reference for more details on available environment variables on Clever Cloud.
You can of course create custom ones with the interface we just demonstrated, they will be available for your application.
Configure your Python application
General configuration
Python apps can be launched in a variety of ways. You can specify how to start your application (for instance which module to run) by setting environment variables.
To select which module you want to start, use the CC_PYTHON_MODULE
environment variable.
CC_PYTHON_MODULE="mymodule:app"
The module (without .py) must be importable, i.e. be in PYTHONPATH
. Basically, you should just point to a WSGI capable object.
For example with Flask, it’s gonna be the name of your main server file, followed by your Flask object: server:app
for instance if you have a server.py
file at the root of your project with a Flask app
object inside.
Choose Python version
The default version of python on Clever Cloud is 2.7. If you want to use python 3.x instead, create an environment variable CC_PYTHON_VERSION
equal to either 3
(which will default to the most up-to-date version), 3.7
, 3.8
, 3.9
, 3.10
or 3.11
.
Note: the version is a number, do not use quotes. values allowed are 2
, 2.7
, 3
, 3.7
, 3.8
, 3.9
, 3.10
, 3.11
.
Choose Pip version
The default version of pip on Clever Cloud is 19.
If you want to use pip 9 instead, create an environment variable like CC_PIP_VERSION=9
.
Select the python backend
Currently, we support daphne
, gunicorn
, uvicorn
and uwsgi
for Python backends. If not specified, the default backend is uwsgi
.
To select one, set the CC_PYTHON_BACKEND
environment variable with either daphne
, gunicorn
, uvicorn
or uwsgi
.
Please contact the support if you need another backend.
Dependencies
If you do not have a requirements.txt
file to commit you can obtain it via the command pip freeze > requirements.txt
(or pip3 freeze > requirements.txt
if you use Python 3.x) at the root of your project folder in your terminal.
For example to install PostgreSQL and don’t want to use the pip freeze
command above you have to create a file requirements.txt
at the root of your application folder:
psycopg2>=2.7 --no-binary psycopg2
Note: We recommend using psycopg2>=2.7 --no-binary psycopg2
to avoid wsgi issues.
You can define a custom requirements.txt
file with the environnement variable CC_PIP_REQUIREMENTS_FILE
for example: CC_PIP_REQUIREMENTS_FILE=config/production.txt
.
Cached dependencies
Enabling dependencies caching
You can enable dependencies caching by adding the CC_CACHE_DEPENDENCIES=true
environment variable in your application. It is enabled by default only for rust and haskell applications.
Disabling dependencies caching
You can disable dependencies caching completely by removing the CC_CACHE_DEPENDENCIES
environment variable from the Clever Cloud console, in the Environment variables menu of your application.
Or by setting it to CC_CACHE_DEPENDENCIES=false
To fully remove cached dependencies, you have to rebuild your application from scratch.
You can select “rebuild and restart” from the Clever Cloud console or launch clever restart --without-cache
with the Clever Tools CLI.
Use setup.py
We support execution of a single setup.py
goal. Usually, this would be to execute custom tasks after the installation of dependencies.
The goal will be launched after the dependencies from requirements.txt
have been installed.
To execute a goal, you can define the environment variable PYTHON_SETUP_PY_GOAL="<your goal>"
.
Environment injection
Clever Cloud injects environment variables from your application settings as mentioned in setting up environment variables and is also injecting in your application production environment, those from your linked add-ons.
To access environment variables from your code, just get them from the environment with:
import os
os.getenv("MY_VARIABLE")
Manage your static files
To enable Nginx to serve your static resources, you have to set two environment variables.
STATIC_FILES_PATH
: should point to a directory where your static files are stored.
STATIC_URL_PREFIX
: the URL path under which you want to serve static files (e.g. /public
).
Also, you are able to use a Filesystem Bucket to store your static files. Please refer to the File System Buckets section.
Note: the path of your folder must be absolute regarding the root of your application.
Note: setting the STATIC_URL_PREFIX
to /
will cause the deployment failure.
Static files example
Here is how to serve static files, the test.png
being the static file you want to serve:
├── <app_root>
│ ├── flask-app.py
│ ├── static
│ │ └── test.png
│ └── requirements.txt
Using the environment variables STATIC_FILES_PATH=static/
and STATIC_URL_PREFIX=/public
the test.png
file will be accessed under: https://<domain.tld>/public/test.png
.
uWSGI, Gunicorn and Nginx configuration
uWSGI, gunicorn and nginx settings can be configured by setting environment variables:
uWSGI
HARAKIRI
: timeout (in seconds) after which an unresponding process is killed. (Default: 180)WSGI_BUFFER_SIZE
: maximal size (in bytes) for the headers of a request. (Defaut: 4096)WSGI_POST_BUFFERING
: buffer size (in bytes) for uploads. (Defaut: 4096)WSGI_WORKERS
: number of workers. (Defaut: depends on the scaler)WSGI_THREADS
: number of threads per worker. (Defaut: depends on the scaler)
uWSGI asynchronous/non-blocking modes
To enable uWSGI asynchronous mode, you can use these two environment variables:
UWSGI_ASYNC
: number of cores to use for uWSGI asynchronous/non-blocking modes.UWSGI_ASYNC_ENGINE
: select the asynchronous engine for uWSGI (optional).
Gunicorn
GUNICORN_WORKER_CLASS
: type of worker to use. Default tosync
. Available workersCC_GUNICORN_TIMEOUT
: gunicorn timeout. Defaults to30
Nginx
NGINX_READ_TIMEOUT
: a bit likeHARAKIRI
, the response timeout in seconds. (Defaut: 300)ENABLE_GZIP_COMPRESSION
: “on|yes|true” gzip-compress the output of uwsgi.GZIP_TYPES
: the mime types to gzip. Defaults totext/* application/json application/xml application/javascript image/svg+xml
.
Basic authentication
If you need basic authentication, you can enable it using environment variables. You will need to set CC_HTTP_BASIC_AUTH
variable to your own login:password
pair. If you need to allow access to multiple users, you can create additional environment CC_HTTP_BASIC_AUTH_n
(where n
is a number) variables.
Nginx optional configuration with clevercloud/http.json
Nginx settings can be configured further in clevercloud/http.json
. All its fields are optional.
languages
: configure a default language and redirectionserror_pages
: configure custom files for error pagesforce_https
: automatically redirect HTTP traffic to HTTPSaliases
: set up redirectionscharset
: force a specific charset
{
"languages": {
"default": {"rewrite": "en"},
"fr": {"rewrite": "en"}
},
"error_pages": {
"404": "path/to/page"
},
"force_https": true,
"aliases": {
"/path": "redirection"
},
"charset": "latin-1"
}
Using the Gevent loop engine
Whether you use uwsgi or gunicorn, you can enable the Gevent loop engine.
To do so, add the CC_PYTHON_USE_GEVENT
environment variable to your application, with the true
value.
Monitor your application with New Relic
You can use New Relic to monitor your application on Clever Cloud.
Please refer to our New Relic documentation to configure it for your application.
Celery apps
Note: Please note that Celery support is not available yet for gunicorn
.
We also support celery apps out of the box. To deploy a celery app, use the CC_PYTHON_CELERY_MODULE
environment variable:
CC_PYTHON_CELERY_MODULE="mymodule"
Requirements.txt
Celery needs to be defined as a dependency in your requirements.txt. Otherwise the deployment will be aborted if Celery support is enabled.You can also activate beat with CC_PYTHON_CELERY_USE_BEAT=true
and provide a given log dir for celery with CC_PYTHON_CELERY_LOGFILE="/path/to/logdir"
.
The CC_PYTHON_CELERY_LOGFILE
path is relative to the application’s path.
Beware of timezones with Celery Beat!
There is a bug in versions <4.2 of Celery. You need to add the CELERY_TIMEZONE = 'UTC'
environment variable.
The bug is documented here: https://GitHub.com/celery/celery/issues/4184.
Git Deployment on Clever Cloud
You need Git on your computer to deploy via this tool. Here is the official website of Git to get more information: git-scm.com
Setting up your remotes
The “Information” page of your app gives you your Git deployment URL, it looks like this:
git+ssh://[email protected]/<your_app_id>.git
- Copy it in your clipboard
Locally, under your code folder, type in
git init
to set up a new git repository or skip this step if you already have oneAdd the deploy URL with
git remote add <name> <your-git-deployment-url>
Add your files via
git add <files path>
and commit them viagit commit -m <your commit message>
Now push your application on Clever Cloud with
git push <name> master
Refer to git deployments for more details.
Linking a database or any other add-on to your application
By linking an application to an add-on, the application has the add-on environment variables in its own environment by default.
On add-on creation
Many add-ons do exist on Clever Cloud, please refer to the full list and check add-ons dedicated pages for full instructions.
During add-on creation, you will see Applications screen, with a list of your applications. You can toggle the button to Link and click next. If you finish the process of add-on creation, the application will automatically be linked to it.
Add-on already exists
In the Clever Cloud console, under the Service Dependencies menu of your application, you can use the Link addons dropdown menu to select the name of the add-on you want to link and use the add button to finish the process.
You can also link another application from the same page in the Clever Cloud console, using the Link applications dropdown menu.
More configuration
Need more configuration? To run a script at the end of your deployment? To add your private SSH key to access private dependencies?
Go check the Common configuration page.
You may want to have an advanced usage of your application, in which case we recommend you to read the Administrate documentation section.
If you can’t find something or have a specific need like using a non supported version of a particular software, please reach out to the support.