Skip to content

Commit cee9175

Browse files
author
Benjamin
committed
Added commands for configuring Nginx
1 parent 58e5b5c commit cee9175

File tree

1 file changed

+117
-0
lines changed
  • section9/lectures/139_setting_up_nginx_and_our_rest_api

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
## Setting up Nginx
2+
To install and cofigure Nginx on your server, follow the following instructions.
3+
4+
First, you need to update your server by running
5+
6+
```bash
7+
sudo apt-get update
8+
```
9+
10+
To install Nginx, run
11+
12+
```bash
13+
sudo apt-get install nginx
14+
```
15+
16+
Allow Nginx access through firewall (otherwise incoming requests will be blocked by the firewall)
17+
18+
```bash
19+
sudo ufw enable
20+
sudo ufw allow 'Nginx HTTP'
21+
```
22+
23+
Also since we have enabled firewall, remember to allow ssh through the firewall, else you will get locked out of the server.
24+
25+
```bash
26+
sudo ufw allow ssh
27+
```
28+
29+
You can check firewall status by using
30+
31+
```bash
32+
sudo ufw status
33+
```
34+
35+
To check Nginx status, use
36+
37+
```bash
38+
systemctl status nginx
39+
```
40+
41+
The following commands stop, start and restart Nginx respectively.
42+
43+
```bash
44+
systemctl stop nginx
45+
systemctl start nginx
46+
systemctl restart nginx
47+
```
48+
49+
### Configuring Nginx
50+
Create a new file for the items REST API configuration.
51+
52+
```bash
53+
sudo vi /etc/nginx/sites-available/items-rest.conf
54+
```
55+
56+
Press "i" key (insert mode), copy and paste the following to the file
57+
58+
```bash
59+
server {
60+
listen 80;
61+
real_ip_header X-Forwarded-For;
62+
set_real_ip_from 127.0.0.1;
63+
server_name localhost;
64+
65+
location / {
66+
include uwsgi_params;
67+
uwsgi_pass unix:/var/www/html/items-rest/socket.sock;
68+
uwsgi_modifier1 30;
69+
}
70+
71+
error_page 404 /404.html;
72+
location = 404.html {
73+
root /usr/share/nginx/html;
74+
}
75+
76+
error_page 500 502 503 504 50x.html;
77+
location = /50x.html {
78+
root /usr/share/nginx/html;
79+
}
80+
}
81+
```
82+
83+
After writing and quiting the file (escape key then :wq enter) enable the configuration by running
84+
85+
```bash
86+
sudo ln -s /etc/nginx/sites-available/items-rest.conf /etc/nginx/sites-enabled/
87+
```
88+
89+
### Create the socket.sock file and clone the items rest app
90+
Create a directory/folder for the app
91+
92+
```bash
93+
sudo mkdir /var/www/html/items-rest
94+
```
95+
96+
Since the director was created with root user, give access to the unix user ("jose" in our case) by making the user the owner of the directory.
97+
98+
```bash
99+
sudo chown jose:jose /var/www/html/items-rest
100+
```
101+
102+
Got to the directory, clone the app and install dependencies. Run the following commands one by one in that order.
103+
104+
```bash
105+
cd /var/www/html/items-rest
106+
git clone https://github.com/schoolofcode-me/stores-rest-api.git .
107+
mkdir log
108+
sudo apt-get install python-pip python3-dev libpq-dev
109+
pip install virtualenv
110+
virtualenv venv --python=python3.5
111+
source venv/bin/activate
112+
pip install -r requirements.txt
113+
```
114+
115+
116+
117+

0 commit comments

Comments
 (0)