You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -6,10 +6,10 @@ This is a tutorial on deploying our sample app onto DigitalOcean, a cloud comput
6
6
If you are a first time learner, we do encourage you to go through the whole tutorial. However, if you are just looking for help on a specific task, here are the links to the sections you may want to refer to:
-[Setting up nginx](DigitalOcean%20Tutorial.md#nginx)
12
+
-[Setting up uWSGI](DigitalOcean%20Tutorial.md#uwsgi)
13
13
14
14
# DigitalOcean
15
15
## Introduction
@@ -60,31 +60,33 @@ If you have successfully followed the tutorial so far, then you have finished al
60
60
61
61
## Connecting to our server
62
62
63
-
First, we need to SSH our server. Simply use the recommend:
63
+
First, we need to SSH our server. Simply use the command:
64
64
65
65
```
66
66
ssh root@<your server ip>
67
67
```
68
68
69
-
and you will be asked for the root password. Beware that `SSH` command only works on Mac, not on windows. However, there are plenty of softwares that you can use to SSH from windows, `putty` is one popular option.
69
+
and you will be asked for the root password. Beware that `SSH` command only works on Unix, not on Windows. However, there are plenty of softwares that you can use to SSH from Windows, [putty](http://www.putty.org/) is one popular choice.
70
70
71
71
## Installing required packages
72
72
73
-
After connecting to our server and logged in as root user, it is recommended to run the command
73
+
After connecting to our server and logged in as root user, it is recommended to run the command below first to get all the available updates:
74
74
75
75
```
76
76
apt-get update
77
77
```
78
78
79
-
first to get all the available updates. Then we use the following command to install our required packages:
79
+
Then we use the following command to install our required packages:
80
80
81
81
```
82
82
apt-get install postgresql postgresql-contrib
83
83
```
84
84
85
+
Note that this is a just an example to install different packages using one command, we may need more packages in the following sections.
86
+
85
87
## Creating another user
86
88
87
-
Since the `root` user is the most powerful, we may want to limit access to it to improve security. So in this section, we will create a new user and config it to "act like" a `root` user but with certain limitation. And we will be logging as this user from now on. It is highly recommended to do so, but if you choose not to follow and simply want to login as the `root` user nonetheless, you may click [here to skip to the next section](DigitalOcean%20Tutorial.md#configuring-our-user-for-postgresql).
89
+
Since the `root` user is the most powerful, we may want to limit access to it to improve security. So in this section, we will create a new user and config it to "act like" a `root` user but with certain limitation. And we will be logging as this user from now on. It is highly recommended to do so, but if you choose not to follow and simply want to login as the `root` user anyway, you may click [here to skip to the next section](DigitalOcean%20Tutorial.md#configuring-postgres).
88
90
89
91
### Hello John Doe
90
92
@@ -159,11 +161,17 @@ to enable our modifications.
159
161
160
162
Now we've created a new user `johndoe`, given it temporary super user privilege and enabled SSH for this user. Next, we'll be learning to link this user to our PostgreSQL database.
161
163
162
-
## Configuring our user for PostgreSQL
164
+
## Configuring Postgres
163
165
164
-
Since PostgreSQL allows its own user to interact with the database, we will need to create an according user and configure it to access the database.
166
+
Since Postgres allows its own user to interact with the database, we will need to create an according user and configure it to access the database.
165
167
166
-
### Creating a PostgreSQL user
168
+
### Installing PostgreSQL
169
+
170
+
```
171
+
apt-get install postgresql postgresql-contrib
172
+
```
173
+
174
+
### Creating a Postgres user
167
175
168
176
We use the following command to create the user:
169
177
@@ -172,7 +180,7 @@ sudo -i -u postgres
172
180
createuser johndoe -P
173
181
```
174
182
175
-
After inputing and confirming the password, we now have created a Postgres user. Notice that we use the same username `johndoe` to create the PostgreSQL user, since by default, Postgres only allows the unix user with the same name as its Postgres user to interact with it.
183
+
After inputing and confirming the password, we now have created a Postgres user. Notice that we use the same username `johndoe` to create the Postgres user, since by default, Postgres only allows the unix user with the same name as its Postgres user to interact with it.
176
184
177
185
### Creating a PostgreSQL database for our user
178
186
@@ -243,8 +251,9 @@ to enable password authentication.
243
251
244
252
*** important: *** SQLAlchemy will ***NOT*** work unless we do this modification.
245
253
246
-
## Setting up nginx
254
+
## Nginx
247
255
256
+
Nginx [engine x] is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server. In this tutorial, we use nginx to direct traffic to our server. Nginx can be really helpful in scenarios like running our app on multiple thread, and it is of great performance that we do not need to worry about it slowing our app down. More details about nginx can be found [here](https://nginx.org/en/).
248
257
### Installing nginx
249
258
250
259
```
@@ -266,15 +275,208 @@ sudo ufw add 'Nginx HTTP'
266
275
sudo ufw add ssh
267
276
```
268
277
269
-
Remember that the second line is just a precaution, it should be added already, but we don't want to get blocked out of the server!
278
+
***important:***Remember that the second line, adding SSH rules, is not related to nginx configuration, but since we're activating the firewall, we don't want to get blocked out of the server!
270
279
271
280
At last, if the UFW (Ubuntu Firewall) is inactive, use the command below to activate it:
272
281
273
282
```
274
283
sudo ufw enable
275
284
```
276
285
286
+
And at last, if we want to check if nginx is running, we may use the command:
287
+
288
+
```
289
+
systemctl status nginx
290
+
```
291
+
292
+
Some other helful command options for system controller are:
293
+
294
+
```
295
+
systemctl start <service_name>
296
+
systemctl restart <service_name>
297
+
systemctl stop <service_name>
298
+
```
299
+
300
+
### Configure nginx for our app
301
+
302
+
Before deploying our app onto the server, we need to configure nginx for our app. Use the below command to create a config file for our app:
303
+
304
+
```
305
+
sudo vi /etc/nginx/sites-available/items-rest.conf
306
+
```
307
+
308
+
Note that `items-rest` is what we named our service, you may change it accordingly, but remember to remain consistent throughout the configurations.
309
+
310
+
Next, we input the below text into `items-rest.conf` file. ***Remember to change your service name accordingly in this file as well***
Basically, the above config allows nginx to send the request, coming from our user, to our app. It also sets up some error pages for our service using nginx predefined pages.
278
339
340
+
And at last, in order to enable our configuration, we need to do something like this:
Note that there's a trailing space and period (` .`)at the end, which tells git the destination is the current folder. If you're not in this folder `/var/www/html/items-rest/`, remember to switch to it or explicitly specify it in the git command. And for the following commands in this section, we all assume that we are inside the folder `/var/www/html/items-rest/` unless specified otherwirse.
370
+
371
+
In order to store logs, we need to create a log folder, (under `/var/www/html/items-rest/`):
372
+
373
+
```
374
+
mkdir log
375
+
```
376
+
377
+
Then we will install a bunch of tools we need to set up our app:
After it is installed, we can create a virtualenv:
390
+
391
+
```
392
+
virtualenv venv --python==python3.5
393
+
```
394
+
395
+
Note that Ubuntu usually comes with `Python3.5` and it is what we used in the sample code, if you choose to use different version of Python, feel free to change it accordingly, and it will be the Python version inside your virtualenv.
396
+
397
+
To activate virtualenv:
398
+
399
+
```
400
+
source venv/bin/activate
401
+
```
402
+
403
+
You should see `(venv)` appears at the start of your command line now. And we assume that we are in virtualenv for all the following commands in this section unless specified otherwise.
404
+
405
+
Next, use the command below to install the specified dependencies:
406
+
407
+
```
408
+
pip install -r requirements.txt
409
+
```
410
+
411
+
Note that `requirement.txt` is a text file that includes all the dependencies that we created in our Git folder. If you haven't done so, we highly recommend you to. We will not cover this because we don't want to diverge and confuse our readers.
412
+
413
+
And we will set up uWSGI in next section and finish deploying our app.
414
+
415
+
416
+
## uWSGI
417
+
418
+
We will be using uWSGI to run the app for us, in this way, we can run it in multiple threads within multiple processes. It also allow us to log more easily. More details on uWSGI can be found [here](https://uwsgi-docs.readthedocs.io/en/latest/).
419
+
420
+
First, we define a uWSGI service in the system by:
421
+
422
+
```
423
+
sudo vi /etc/systemd/system/uwsgi_items_rest.service
424
+
```
425
+
426
+
And the content we are going to input is shown below:
We will explain the basic idea of these configs. Each pair of square brackets `[]` defines a `section` which can contain some properties.
445
+
446
+
The `Unit` section simply provides some basic description and can be helpful when looking at the logs.
447
+
448
+
The `Service` section contains several properties related to our app. The `Environment` properties defines all the environment variables we need in our code. In our sample code, we want to retrieve the DATABASE_URL from system environment. And this is the place where you should keep all your secrets, such as secret keys and credentials. Beware that the `DATABASE_URL` should follow the format:
0 commit comments