The SLS framework deployment scheme has been upgraded. You can use an SCF HTTP-triggered function to quickly deploy your Django service to the cloud.
Note:What are the differences between SLS console deployment and direct function deployment?
Both SLS console deployment and function deployment can be based on HTTP-triggered functions, and quick deployment is usually used for web frameworks.
This document introduces the SLS console deployment scheme. You can also complete the deployment in CLI by referring to Deploying Web Function on Command Line.
Run the following command to confirm that Django has been installed in your local environment.
python -m pip install Django
Create the Hello World
sample project locally.
django-admin startproject helloworld && cd helloworld
The directory structure is as follows:
$ tree
. manage.py Manager
|--***
| |-- __init__.py Package
| |-- settings.py Settings file
| |-- urls.py Route
| `-- wsgi.py Deployment
Run the python manage.py runserver
command locally to start the bootstrap file. Below is the sample code:
$ python manage.py runserver
July 27, 2021 - 11:52:20
Django version 3.2.5, using settings 'helloworld.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Visit http://127.0.0.1:8000
in a browser, and you can access the sample Django project locally as shown below:
You need to make simple modifications to the locally created project, so that the project can be quickly deployed through an HTTP-triggered function. The project modification steps for Django are as follows:
1. Install the dependency package
As the Django dependency library is not provided in the standard cloud environment of SCF, you must install the dependencies and upload them together with the project code. Please create the requirements.txt
file first with the following content:
Django==3.1.3
Run the following installation command:
pip install -r requirements.txt -t .
Note:As the
db.sqlite3
library is referenced in the initialized default project, you need to install this dependency at the same time or comment out theDATABASES
field in thesetting.py
file of the project.
2. (Optional) Configure the scf_bootstrap
file.
Note:You can also complete the configuration in the console.
The listening port in the HTTP-triggered function must be 9000, so you need to change the listening address and port. To do so, you need to create the scf_bootstrap
file in the root directory of the project and add the following content to the file to configure environment variables, specify service start commands, and so on to make sure that your service can be started normally through the file:
#!/bin/bash
/var/lang/python3/bin/python3 manage.py runserver 9000
After the file is created, you need to run the following command to modify the executable permission of the file. By default, the permission 777
or 755
is required for the service to start normally. Below is the sample code:
chmod 777 scf_bootstrap
Note:
- In the SCF environment, only files in the
/tmp
directory are readable/writable. We recommend you select/tmp
when outputting files. If you select other directories, write will fail due to the lack of permissions.- If you want to output environment variables in logs, you need to add the
-u
parameter before the bootstrap command, such aspython -u app.py
.
After the local configuration is completed, run the following command to start the service (take running the command in the scf_bootstrap
directory as an example) and make sure that your service can be normally started locally.
Note:Be sure to change the
python
path to the local path during local testing.
./scf_bootstrap
3. Console upload
Log in to the SLS console, select Web Application > Django Framework, and select Local Upload or Code Repository Pull as the upload mode.
You can configure the scf_bootstrap
file in the console. When the configuration is completed, the console automatically generates the scf_bootstrap
file and packages it and the project code for deployment.
Note:The actual
scf_bootstrap
file in your project prevails. If thescf_bootstrap
file already exists in your project, its content will not be overwritten.
When the configuration is completed, click Complete to deploy your Django project.
In Advanced Configuration, you can perform more application management operations, such as creating layers, binding custom domains, and configuring environment variables.
Was this page helpful?