Skip to content

Commit 874ea88

Browse files
authored
Django deploy with App Platform
1 parent 56aa8f6 commit 874ea88

File tree

1 file changed

+162
-43
lines changed

1 file changed

+162
-43
lines changed

docs/deployment/app-platform-do/django.mdx

Lines changed: 162 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,84 +4,203 @@ description: How to deploy Django to App Plaform by Digital Ocean
44

55
# App Platform - Deploy Django
66

7-
<SubHeading color="#25c2a0">How to deploy Django to App Plaform by Digital Ocean</SubHeading>
7+
<SubHeading color="#25c2a0">How to deploy Django to App Platform by Digital Ocean</SubHeading>
88

9-
@Todo - General information AWS Platform
9+
DigitalOcean is a cloud infrastructure provider that offers a variety of products to help developers and businesses deploy and manage their applications. One of its key offerings is the App Platform, which simplifies the process of deploying and scaling web applications. With App Platform, users can deploy code directly from their Git repositories and choose from pre-configured language and database options, making it easy to get started without worrying about infrastructure management.
1010

1111
> Topics covered
1212
13-
- `App Plaform` Account Creation
14-
- `App Plaform` Account Settings
13+
- `App Platform` Account Creation
1514
- The project to be deployed
16-
- `App Plaform` Database set up
17-
- `App Plaform` Environment set up
18-
- `App Plaform` Domain settings
19-
- `App Plaform` SSL certificates
20-
- `App Plaform` CI/CD from Github
21-
- `App Plaform` LIVE Service monitoring
15+
- `App Platform` Setting up project
16+
- `App Platform` Deploying the application
17+
- `App Platform` LIVE Service monitoring
2218

23-
\
19+
## `App Platform` Account Creation
2420

25-
## `App Plaform` Account Creation
21+
If you want to use the DigitalOcean App Platform to deploy your app, you'll need to create a DigitalOcean account. Here are the steps to get started:
2622

27-
@Todo: Step by step presentation
23+
1. Go to DigitalOcean home page at https://www.digitalocean.com/.
24+
2. You can directly sign up from button in the hero section or click "Sign up" at the top right corner of the page.
25+
3. Select your preferred sign up method, you can sign up using Google, Github, or email.
26+
4. Fill out the survey form, which asks a few questions about your project needs, and then click "Submit".
27+
5. You will be prompted to set a payment method to verify your identity. Choose your desired payment method and fill out the form provided. If you choose payment with a card, make sure you have at least $1 for the verification. It will be refunded within a few days.
28+
6. After you have verified, create a project to start using DigitalOcean.
2829

29-
\
30+
## The project to be deployed
3031

31-
## `App Plaform` Account Settings
32+
Source project: https://github.com/app-generator/django-soft-ui-dashboard
3233

33-
@Todo: The most important things to know by a beginner
34+
If `git` isn't installed, run following command to install it.
3435

35-
\
36+
```bash
37+
sudo apt-get install git
38+
```
3639

37-
## The project to be deployed
40+
Then clone the project by running this command.
3841

39-
Source project: https://github.com/app-generator/django-soft-ui-dashboard
42+
```bash
43+
git clone https://github.com/app-generator/django-soft-ui-dashboard
44+
```
45+
46+
## `App Platform` Setting up project
47+
48+
To ensure that your project runs properly on App Platform, follow these steps to modify the settings:
49+
50+
1. Open the `core/settings.py` file in the project.
51+
52+
2. Search `ALLOWED_HOSTS` and replace the value with the following code.
53+
54+
```py
55+
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '127.0.0.1,localhost').split(',')
56+
```
57+
58+
3. Install dj-database-url which allows you to specify database connection settings in a URL format.
59+
60+
```bash
61+
pip install dj-database-url
62+
```
63+
64+
4. Find the database configuration and add the following code.
65+
66+
```py
67+
import dj_database_url
68+
```
69+
70+
```python
71+
DB_URL = os.getenv('DB_URL' , None)
72+
```
73+
74+
```python
75+
elif DB_URL:
76+
DATABASES = {
77+
"default": dj_database_url.parse(DB_URL)
78+
}
79+
```
80+
81+
5. Your complete database configuration code should look like this:
82+
83+
```python
84+
import dj_database_url
85+
...
86+
87+
# Database
88+
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases
89+
90+
DB_ENGINE = os.getenv('DB_ENGINE' , None)
91+
DB_USERNAME = os.getenv('DB_USERNAME' , None)
92+
DB_PASS = os.getenv('DB_PASS' , None)
93+
DB_HOST = os.getenv('DB_HOST' , None)
94+
DB_PORT = os.getenv('DB_PORT' , None)
95+
DB_NAME = os.getenv('DB_NAME' , None)
96+
DB_URL = os.getenv('DB_URL' , None)
97+
98+
if DB_ENGINE and DB_NAME and DB_USERNAME:
99+
DATABASES = {
100+
'default': {
101+
'ENGINE' : 'django.db.backends.' + DB_ENGINE,
102+
'NAME' : DB_NAME,
103+
'USER' : DB_USERNAME,
104+
'PASSWORD': DB_PASS,
105+
'HOST' : DB_HOST,
106+
'PORT' : DB_PORT,
107+
},
108+
}
109+
elif DB_URL:
110+
DATABASES = {
111+
"default": dj_database_url.parse(DB_URL)
112+
}
113+
else:
114+
DATABASES = {
115+
'default': {
116+
'ENGINE': 'django.db.backends.sqlite3',
117+
'NAME': 'db.sqlite3',
118+
}
119+
}
120+
```
121+
122+
6. Add `dj-database-url` to the `requirements.txt` file.
123+
124+
7. Commit your changes and push the revision to Github.
125+
126+
## `App Platform` Deploying the application
127+
128+
DigitalOcean's App Platform is a service that allows you to easily deploy your web applications. In this tutorial, we'll go over the steps to deploy an application on the App Platform.
129+
130+
### Setting up the app
131+
132+
1. Go to https://cloud.digitalocean.com/apps and click on "Create App". This will take you to a page where you can choose your service provider. For this demo, we'll choose GitHub.
133+
134+
2. Click on "Manage Access". You'll be prompted to connect your GitHub account. Connect your account and allow DigitalOcean to access your repositories.
135+
136+
3. Select the repository you want to deploy, choose the branch and source directory, and click "Next".
137+
138+
4. Click "Edit" next to the app to configure your app.
40139

41-
\
140+
5. Edit the "Run Command", change it to the following command, then click "Save":
42141

43-
## `App Plaform` Database set up
142+
```bash
143+
gunicorn --worker-tmp-dir /dev/shm core.wsgi
144+
```
44145

45-
@Todo:
146+
6. Click "Edit" next to "HTTP Request Routes" and change "Routes" to `/`, then save and click "Back" to go back to the resource page.
46147

47-
- How to set up a PostgreSQL database + user credentials
48-
- How to set up a MySql database + user credentials
148+
### Adding resources
49149

50-
\
150+
1. Click "Edit" next to "Add Resource" and select "Database". Enter your database name and click "Add".
151+
2. Click "Add Resource" again, select "Detect from Source Code" to set up the static file, and select the same repository as in the previous step.
152+
3. Click "Edit" next to the new resource, change the configuration to the following, then save and click "Back":
153+
- Name: static (or anything else)
154+
- Resource Type: Static Site
155+
- Output Directory: staticfiles
156+
- HTTP Request Routes: /static
157+
4. Click "Next" to set up environment variables.
51158

52-
## `App Plaform` Environment set up
159+
### Setting up environment variables
53160

54-
@Todo:
161+
1. Click "Edit" next to the your app name, then add these variables.
55162

56-
- How to specify the Python version to be used
57-
- How to set the environment variables used by the app
163+
- `DATABASE_URL`: `${<NAME_OF_YOUR_DATABASE>.DATABASE_URL}`
164+
- `ALLOWED_HOSTS`: `${APP_DOMAIN}`
165+
- `SECRET_KEY`: enter your secret key and check "Encrypt"
166+
- `DEBUG`: True (set to False when your app is running properly)
167+
2. Click "Save" and then click "Next".
58168

59-
\
169+
### Configuring app information
60170

61-
## `App Plaform` Domain settings
171+
1. If you want to set a name for your app and select the project, click "Edit" under "App Info"
172+
2. If you want to choose the server region, click "Edit" under "Region". Choose the region nearest to your location.
173+
3. Click "Next" to continue the process.
62174

63-
@Todo: How to park a domain or a subdomain to the service
175+
### Deploying your app
64176

65-
\
177+
1. Review your configuration, then click "Create Resources" at the bottom of the page when you are sure.
66178

67-
## `App Plaform` SSL certificates
179+
2. Once the build process completes, go to "Console" tab and run the following commands.
68180

69-
@Todo: How to enhance the service with valid certificates (not self signed)
181+
```bash
182+
python manage.py migrate
183+
python manage.py createsuperuser
184+
```
70185

71-
\
186+
Congratulations, your application is now deployed on DigitalOcean App Platform!
72187

73-
## `App Plaform` CI/CD from Github
188+
## `App Platform` LIVE Service monitoring
74189

75-
@Todo: This section explains how to link an automatic deploy from Github on new commits
190+
We'll use UptimeRobot to monitor the app. UptimeRobot is a popular online service that monitors the uptime and performance of websites, servers, and other online services.
76191

77-
\
192+
Go to [UptimeRobot](https://uptimerobot.com/) create an account or sign in if you already have account. Create monitor by clicking "Add New Monitor" button then follow this config:
78193

79-
## `App Plaform` LIVE Service monitoring
194+
- Monitor Type: HTTP(s)
195+
- Friendly Name: django-AWS (or anything else you want)
196+
- URL (or IP): your_domain
80197

81-
@Todo: How to check out the service health and uptime
198+
Keep the rest of the config as default and click "Create Monitor"
82199

83-
\
200+
UptimeRobot will now start monitoring your app. You can view the monitor status and performance data on the UptimeRobot dashboard
84201

85202
## Links & Resources
86203

87-
@Todo: mention at least two valuable resources (articles or docs)
204+
- 👉 More [Django Starters](https://appseed.us/apps/django/) - provided by `AppSeed` (free & paid)
205+
- 👉 Free [Support](https://appseed.us/support/) via Email & Discord
206+
- [Django](https://docs.djangoproject.com/en/4.2/releases/4.2) official documentation

0 commit comments

Comments
 (0)