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.
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
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
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.
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:
Run the following installation command:
pip install -r requirements.txt -t .
db.sqlite3library is referenced in the initialized default project, you need to install this dependency at the same time or comment out the
DATABASESfield in the
setting.pyfile of the project.
2. (Optional) Configure the
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:
/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
755 is required for the service to start normally. Below is the sample code:
chmod 777 scf_bootstrap
- In the SCF environment, only files in the
/tmpdirectory are readable/writable. We recommend you select
/tmpwhen 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
-uparameter before the bootstrap command, such as
python -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.
Be sure to change the
pythonpath to the local path during local testing.
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.
scf_bootstrapfile in your project prevails. If the
scf_bootstrapfile 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.