Blame view

README.md 6.05 KB
43785de6   Miguel Barão   - added documenta...
1
# Getting Started
a96cd2c7   Miguel Barão   - added logging s...
2

1e8f0810   Miguel Barão   update setup.py a...
3

5d859a41   Miguel Barão   update documentation
4
5
6
## Requirements

Before installing the server, we will need to install python with some additional packages.
93e13002   Miguel Barão   updates README.md.
7

a96cd2c7   Miguel Barão   - added logging s...
8
### Install python3.6 with sqlite3 support
39126690   Miguel Barão   removes npm and n...
9
10
11
12

This can be done using the system package management, downloaded from [http://www.python.org](), or compiled from sources.

- Installing from the system package manager:
f540e673   Miguel Barão   - added README.md
13
    ```sh
5d859a41   Miguel Barão   update documentation
14
15
    sudo port install python36                # MacOS
    sudo pkg install python36 py36-sqlite3    # FreeBSD
39126690   Miguel Barão   removes npm and n...
16
    sudo apt install ?not available yet?      # Linux, install from source
443a1eea   Miguel Barão   Update to latest ...
17
    ```
39126690   Miguel Barão   removes npm and n...
18
- Installing from source:
5d859a41   Miguel Barão   update documentation
19
20
     Download from [http://www.python.org]() and

43785de6   Miguel Barão   - added documenta...
21
    ```sh
39126690   Miguel Barão   removes npm and n...
22
    unxz Python-3.6.tar.xz
43785de6   Miguel Barão   - added documenta...
23
    tar xvf Python-3.6.tar
39126690   Miguel Barão   removes npm and n...
24
25
    cd Python-3.6
    ./configure --prefix=$HOME/.local/bin
c54ee176   Miguel Barão   update javascript...
26
27
    make && make install
    ```
39126690   Miguel Barão   removes npm and n...
28
29

    This will install python locally under `~/.local/bin`. Make sure to add it to your `PATH` (edit `~/.profile` in MacOS or FreeBSD).
1e8f0810   Miguel Barão   update setup.py a...
30

93e13002   Miguel Barão   updates README.md.
31
32
### Install pip

43785de6   Miguel Barão   - added documenta...
33
If the `pip` command is not yet installed, run
59213008   Miguel Barão   - fixed user path...
34

443a1eea   Miguel Barão   Update to latest ...
35
```sh
a96cd2c7   Miguel Barão   - added logging s...
36
python3.6 -m ensurepip --user
43785de6   Miguel Barão   - added documenta...
37
```
39126690   Miguel Barão   removes npm and n...
38
39

This will install pip in your user account under `~/.local/bin`.
1e8f0810   Miguel Barão   update setup.py a...
40
In the end you should be able to run `pip3 --version` and `python3 -c "import sqlite3"` without errors (sometimes `pip3` is `pip`, `pip3.6` or `pip-3.6`).
43785de6   Miguel Barão   - added documenta...
41

f540e673   Miguel Barão   - added README.md
42
If you want to always install python modules on the user account (recommended), edit the pip configuration file `~/.config/pip/pip.conf` (FreeBSD, Linux) or `Library/Application Support/pip/pip.conf` (MacOS) and add the lines
443a1eea   Miguel Barão   Update to latest ...
43
44

```ini
39126690   Miguel Barão   removes npm and n...
45
[global]
5d859a41   Miguel Barão   update documentation
46
user = yes
1e8f0810   Miguel Barão   update setup.py a...
47
48
49
50
51
```

### Install additional python packages:

```sh
5dae401a   Miguel Barão   update README.md
52
pip3 install --user \
f540e673   Miguel Barão   - added README.md
53
    tornado \
43785de6   Miguel Barão   - added documenta...
54
55
56
57
    sqlalchemy \
    pyyaml \
    pygments \
    markdown \
f540e673   Miguel Barão   - added README.md
58
    bcrypt \
39126690   Miguel Barão   removes npm and n...
59
    networkx
f540e673   Miguel Barão   - added README.md
60
```
43785de6   Miguel Barão   - added documenta...
61

39126690   Miguel Barão   removes npm and n...
62
These are usually installed under
43785de6   Miguel Barão   - added documenta...
63

f540e673   Miguel Barão   - added README.md
64
- `~/.local/lib/python3.6/site-packages/` in Linux/FreeBSD.
c3eba869   Miguel Barão   update README.md
65
- `~/Library/python/3.6/lib/python/site-packages/` in MacOS.
a96cd2c7   Miguel Barão   - added logging s...
66

443a1eea   Miguel Barão   Update to latest ...
67
## Installation
1e8f0810   Miguel Barão   update setup.py a...
68

a96cd2c7   Miguel Barão   - added logging s...
69
Replace USER by your bitbucket username:
443a1eea   Miguel Barão   Update to latest ...
70

39126690   Miguel Barão   removes npm and n...
71
```sh
f540e673   Miguel Barão   - added README.md
72
cd path/to/some/directory
39126690   Miguel Barão   removes npm and n...
73
74
git clone https://USER@bitbucket.org/mjsb/aprendizations.git
cd aprendizations
5dae401a   Miguel Barão   update README.md
75
76
```

93e13002   Miguel Barão   updates README.md.
77
## Configuration
1e8f0810   Miguel Barão   update setup.py a...
78

5dae401a   Miguel Barão   update README.md
79
80
### Database

93e13002   Miguel Barão   updates README.md.
81
The user data is maintained in a sqlite3 database file. We first need to create the database:
b4ceb7fc   Miguel Barão   minor change in l...
82

93e13002   Miguel Barão   updates README.md.
83
84
85
```sh
cd aprendizations
./initdb.py --help          # for the available options
39126690   Miguel Barão   removes npm and n...
86
./initdb.py                 # show current database or initialize empty if nonexisting
93e13002   Miguel Barão   updates README.md.
87
./initdb.py inscricoes.csv  # add students from CSV, passwords are the numbers
f540e673   Miguel Barão   - added README.md
88
./initdb.py --add 1184 "Ana Bola"           # add new user (password=1184)
59213008   Miguel Barão   - fixed user path...
89
90
./initdb.py --update 1184 --pw alibaba      # update password
```
443a1eea   Miguel Barão   Update to latest ...
91
92

### SSL Certificates
c288f8c6   Miguel Barao   - updated documen...
93

443a1eea   Miguel Barão   Update to latest ...
94
95
96
We need certificates for https. Certificates can be self-signed or validated by a trusted authority.

Self-signed can be used for testing, but browsers will complain. LetsEncrypt issues trusted and free certificates, but the server must have a fixed IP and a registered domain name in the DNS (dynamic DNS does not work).
c288f8c6   Miguel Barao   - updated documen...
97

93e13002   Miguel Barão   updates README.md.
98
#### Selfsigned
f540e673   Miguel Barão   - added README.md
99

b4ceb7fc   Miguel Barão   minor change in l...
100
Generate a selfsigned certificate and place it in `aprendizations/certs`.
59213008   Miguel Barão   - fixed user path...
101

43785de6   Miguel Barão   - added documenta...
102
103
104
```sh
openssl req -x509 -newkey rsa:4096 -keyout privkey.pem -out cert.pem -days 365 -nodes
```
59213008   Miguel Barão   - fixed user path...
105

93e13002   Miguel Barão   updates README.md.
106
107
108
#### LetsEncrypt

```sh
bbc1c506   Miguel Barão   - documentation u...
109
sudo pkg install py27-certbot       # FreeBSD
43785de6   Miguel Barão   - added documenta...
110
```
443a1eea   Miguel Barão   Update to latest ...
111

5d859a41   Miguel Barão   update documentation
112
Shutdown the firewall and any server running. Then run the script to generate the certificate:
43785de6   Miguel Barão   - added documenta...
113

bbc1c506   Miguel Barão   - documentation u...
114
```sh
443a1eea   Miguel Barão   Update to latest ...
115
116
sudo pfctl -d                                   # disable pf firewall (FreeBSD)
sudo certbot certonly --standalone -d www.example.com
bbc1c506   Miguel Barão   - documentation u...
117
sudo pfctl -e; sudo pfctl -f /etc/pf.conf       # enable pf firewall
43785de6   Miguel Barão   - added documenta...
118
```
93e13002   Miguel Barão   updates README.md.
119
120

Certificates are saved under `/usr/local/etc/letsencrypt/live/www.example.com/`. Copy them to `aprendizations/certs` and change permissions to be readable:
43785de6   Miguel Barão   - added documenta...
121

bbc1c506   Miguel Barão   - documentation u...
122
```sh
443a1eea   Miguel Barão   Update to latest ...
123
124
125
sudo cp /usr/local/etc/letsencrypt/live/www.example.com/cert.pem .
sudo cp /usr/local/etc/letsencrypt/live/www.example.com/privkey.pem .
chmod 400 cert.pem privkey.pem
bbc1c506   Miguel Barão   - documentation u...
126
```
43785de6   Miguel Barão   - added documenta...
127

443a1eea   Miguel Barão   Update to latest ...
128
Renews can be done running `certbot renew` (untested!).
43785de6   Miguel Barão   - added documenta...
129
130
131
132


### Testing

bbc1c506   Miguel Barão   - documentation u...
133
The application includes a small example in `demo/demo.yaml`. Run it with
5d859a41   Miguel Barão   update documentation
134
135
136
137

```sh
./serve.py demo/demo.yaml
```
443a1eea   Miguel Barão   Update to latest ...
138
139

and open a browser at [https://127.0.0.1:8443](). If it everything looks good, check at the correct address `https://www.example.com` (requires port forward in the firewall).
39126690   Miguel Barão   removes npm and n...
140

5d859a41   Miguel Barão   update documentation
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

### Firewall configuration

Ports 80 and 443 are only usable by root. For security reasons it is better to run the server as an unprivileged user on higher ports like 8080 for http and 8443 for https. For this, we can configure port forwarding in the firewall to redirect incoming tcp traffic from 80 to 8080 and 443 to 8443.

#### FreeBSD and pf

Edit `/etc/pf.conf`:

```sh
ext_if="em0"                # this should be the correct network interface
rdr on $ext_if proto tcp from any to any port 80 -> 127.0.0.1 port 8080
rdr on $ext_if proto tcp from any to any port 443 -> 127.0.0.1 port 8443
```

93e13002   Miguel Barão   updates README.md.
156
Under virtualbox with guest additions use `ext_if="vtnet0"`.
c55940af   Miguel Barão   - minor change in...
157

39126690   Miguel Barão   removes npm and n...
158
Edit `/etc/rc.conf`
443a1eea   Miguel Barão   Update to latest ...
159

f540e673   Miguel Barão   - added README.md
160
```sh
43785de6   Miguel Barão   - added documenta...
161
pf_enable="YES"
3ec49458   Miguel Barão   fix README.md and...
162
pf_flags=""
39126690   Miguel Barão   removes npm and n...
163
pf_rules="/etc/pf.conf"
43785de6   Miguel Barão   - added documenta...
164

59213008   Miguel Barão   - fixed user path...
165
# optional logging:
443a1eea   Miguel Barão   Update to latest ...
166
167
pflog_enable="YES"
pflog_flags=""
93e13002   Miguel Barão   updates README.md.
168
pflog_logfile="/var/log/pflog"
443a1eea   Miguel Barão   Update to latest ...
169
170
```

c288f8c6   Miguel Barao   - updated documen...
171
Reboot or `sudo service pf start`.
59213008   Miguel Barão   - fixed user path...
172
173

## Troubleshooting
443a1eea   Miguel Barão   Update to latest ...
174
175
176

To help with troubleshooting, use the option `--debug` when running the server. This will increase logs in the terminal and will present the python exception errors in the browser.

59213008   Miguel Barão   - fixed user path...
177
178
179
180
181
Logging levels can be adjusted in `config/logger.yaml` and `config/logger-debug.yaml`.


#### UnicodeEncodeError

43785de6   Miguel Barão   - added documenta...
182
The server should not generate this error, but when using external scripts to generate questions or to correct, these scripts can print unicode strings to stdout. If the terminal does not support unicode, python will generate this exception.
93e13002   Miguel Barão   updates README.md.
183

43785de6   Miguel Barão   - added documenta...
184
185
186
- FreeBSD fix: edit `~/.login_conf` to use UTF-8, for example:

```sh
59213008   Miguel Barão   - fixed user path...
187
me:\
5d859a41   Miguel Barão   update documentation
188
    :charset=UTF-8:\
59213008   Miguel Barão   - fixed user path...
189
    :lang=en_US.UTF-8:
43785de6   Miguel Barão   - added documenta...
190
```
59213008   Miguel Barão   - fixed user path...
191

43785de6   Miguel Barão   - added documenta...
192
193
194
195
- Debian fix: check `locale`...

## FAQ

59213008   Miguel Barão   - fixed user path...
196
- Which students did at least one topic?
43785de6   Miguel Barão   - added documenta...
197
198
199
200
201

```sh
sqlite3 students.db "select distinct student_id from studenttopic"
```

59213008   Miguel Barão   - fixed user path...
202
- How many topics had each student done?
ea4ff4d2   Miguel Barão   - radio: added su...
203

59213008   Miguel Barão   - fixed user path...
204
```sh
39126690   Miguel Barão   removes npm and n...
205
206
sqlite3 students.db "select student_id, count(topic_id) from studenttopic group by student_id order by count(topic_id) desc"
```
93e13002   Miguel Barão   updates README.md.
207
208
209
210

- Which questions have more wrong answers?

```sh
39126690   Miguel Barão   removes npm and n...
211
212
sqlite3 students.db "select count(ref), ref from answers where grade<1.0 group by ref order by count(ref) desc"
```
443a1eea   Miguel Barão   Update to latest ...

39126690   Miguel Barão   removes npm and n...

a203d3cc   Miguel Barão   - new http server...

5d859a41   Miguel Barão   update documentation

39126690   Miguel Barão   removes npm and n...

5d859a41   Miguel Barão   update documentation

59213008   Miguel Barão   - fixed user path...

443a1eea   Miguel Barão   Update to latest ...

5dae401a   Miguel Barão   update README.md

43785de6   Miguel Barão   - added documenta...

443a1eea   Miguel Barão   Update to latest ...

5dae401a   Miguel Barão   update README.md

43785de6   Miguel Barão   - added documenta...

443a1eea   Miguel Barão   Update to latest ...

43785de6   Miguel Barão   - added documenta...

443a1eea   Miguel Barão   Update to latest ...

59213008   Miguel Barão   - fixed user path...

443a1eea   Miguel Barão   Update to latest ...

5dae401a   Miguel Barão   update README.md

59213008   Miguel Barão   - fixed user path...

443a1eea   Miguel Barão   Update to latest ...

59213008   Miguel Barão   - fixed user path...

43785de6   Miguel Barão   - added documenta...

f75a344f   Miguel Barão   - README.md updat...

443a1eea   Miguel Barão   Update to latest ...

db04e658   Miguel Barão   - added option to...

43785de6   Miguel Barão   - added documenta...

f75a344f   Miguel Barão   - README.md updat...

93e13002   Miguel Barão   updates README.md.

f75a344f   Miguel Barão   - README.md updat...

43785de6   Miguel Barão   - added documenta...

d187aad4   Miguel Barão   - adds courses

43785de6   Miguel Barão   - added documenta...

f75a344f   Miguel Barão   - README.md updat...

d187aad4   Miguel Barão   - adds courses

f75a344f   Miguel Barão   - README.md updat...

43785de6   Miguel Barão   - added documenta...

93e13002   Miguel Barão   updates README.md.

f75a344f   Miguel Barão   - README.md updat...

93e13002   Miguel Barão   updates README.md.

443a1eea   Miguel Barão   Update to latest ...

f75a344f   Miguel Barão   - README.md updat...

93e13002   Miguel Barão   updates README.md.

443a1eea   Miguel Barão   Update to latest ...

93e13002   Miguel Barão   updates README.md.