aboutsummaryrefslogtreecommitdiffstats
path: root/HACKING.md
blob: 452f0456c2468dbfa8681c6a0d04b4fe627102fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# Personal-site

> Production [Tested on server with Hyperbola GNU + Linux-libre]

## Python dependencies

- Django
- Pillow
- psycopg2-binary
- pytz

## Production Installation

- Clone **Personal-site**

```console
$ git clone https://git.sr.ht/~heckyel/personal-site
```

- Run `virtualenv`.

```console
$ cd personal-site && virtualenv ./venv/
```

- Activate the virtualenv.

```console
$ source ./venv/bin/activate
```

- Install dependencies through `pip`.

```console
$ pip install -r django/requirements_prod.txt
```

## Configuration Postgres

- Login as postgres

```console
$ sudo su - postgres
```

- Create base

```console
$ createdb namebase
```

- Create User (place a password for our user)

```console
$ createuser -P username
```

- Inside the database

```console
$ psql -d namebase
```

- Give permissions to the created user

```console
$ GRANT ALL PRIVILEGES ON DATABASE namebase TO username;
```

## Tips of Postgres

- List database

```console
$ psql -l
```

- Delete database

```console
$ dropdb namebase
```

## Conecting to Postgres

- Copy `settings.py.example` to `settings.py` and modify.
  Make sure to uncomment the appropriate database section (either sqlite or
  PostgreSQL).

```console
$ cp -v django/personalsite/settings.py.example django/personalsite/settings.py
```

Replace sqlite configuartion to postgres, example:

```python
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'namebase',
        'USER': 'username',
        'PASSWORD': 'pass',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}
```

- Check syntax.

```console
$ python django/manage.py check --deploy
```

- Make migrations

```console
$ python django/manage.py makemigrations
```

- Migrate changes.

```console
$ python django/manage.py migrate
```

- Create superUSER

```console
$ python django/manage.py createsuperuser
```


## Run with Apache server and wsgi

- Install WSGI for Apache

```console
$ sudo pacman -S mod_wsgi
```

- To install mod_wsgi, add the following line in `httpd.conf`, example:

```console
$ sudo nano -w /etc/httpd/conf/httpd.conf
```

   Added line:

```apacheconf
LoadModule wsgi_module modules/mod_wsgi.so
```

- Create vhosts, for example:

```console
$ sudo nano -w /etc/httpd/conf/extra/httpd-vhosts.conf
```

   and inside write the configuration, example:

```apacheconf
<IfModule ssl_module>
    <VirtualHost *:80>
        ServerAdmin example@dominio.com
        ServerName example.com
        ServerAlias example.com

        Alias /media /path/to/site/media/
        Alias /static /path/to/site/core/static/
        <Directory /path/to/site/core/static>
            Require all granted
        </Directory>

        <Directory /path/to/site/media>
            Require all granted
        </Directory>

        <Directory /path/to/site/personalsite>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>

        WSGIDaemonProcess personalsite python-home=/path/to/site/venv python-path=/path/to/site
        WSGIProcessGroup personalsite
        WSGIScriptAlias / /path/to/site/wsgi.py

    </VirtualHost>
</IfModule>
```

- Replace `ALLOWED_HOSTS = []`

   on setting.py to:

```python
ALLOWED_HOSTS = ["example.com", "localhost"]
```

- Added on setting.py:

```python
STATIC_ROOT = '/path/to/site/core/static'
```

- Generated files static of Admin Django (you must be inside the virtualenv).

```console
$ python django/manage.py collectstatic
```

- Create the media/ directory

```console
$ cd /path/to/personalsite
```

```console
$ mkdir media/
```

- Change Permition to media/ at group http

```console
$ sudo chown -R http:http media/
```

- Restart Apache server

```console
$ sudo rc-service httpd restart
```

- Done!

## Security on settings.py [SSL, HTTPS, COOKIE, etc]

```bash
# security.W004
SECURE_HSTS_SECONDS = 31536000
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True

# security.W006
SECURE_CONTENT_TYPE_NOSNIFF = True

# security.W007
SECURE_BROWSER_XSS_FILTER = True

# security.W008
SECURE_SSL_REDIRECT = True

# security.W012
SESSION_COOKIE_SECURE = True

# security.W016、security.W017
CSRF_COOKIE_SECURE = True
CSRF_COOKIE_HTTPONLY = True

# security.W019
X_FRAME_OPTIONS = 'DENY'
```

## Validation subdomain in eepsite

Uncomment in `project/urls.py`

```python
from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name="home"),
    path('filename', views.i2pfile, name='i2pfile') # eepsite
]
```

also `project/views.py`

```python
def i2pfile(request):
    return render(request, 'trabajo/filename')
```

and add `project/templates/trabajo/filename`