Crete virtualenv

1
2
3
4
5
6
7
8
9
10
11
12
13
pip install pipenv

mkdir website

cd website

pipenv shell --python 3.6

pipenv install django

pipenv install pymysql

pipenv install gunicorn

when running django have some errors. so we have to change django code.

  • one
    1
    vim /home/tonytan/.local/share/virtualenvs/website-skIUga7b/lib/python3.7/site-packages/django/db/backends/mysql/base.py

change to notes line 35, 36,

1
2
#if version < (1, 3, 13):
# raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
  • two
1
vim /home/tonytan/.local/share/virtualenvs/website-skIUga7b/lib/python3.7/site-packages/django/db/backends/mysql/operations.py

in line 146

change:

1
query = query.decode(errors='replace')

to

1
query = query.decode(errors='replace') if not isinstance(query, str) else query

Create project

  • create project & app
1
2
django-admin.py startproject website
cd website
  • change mysql package
    1
    vim website/__init__.py

the path is /website/website/website/__init__.py

add:

1
2
import pymysql
pymysql.install_as_MySQLdb()

  • sql migrate
1
2
python manage.py makemigrations
python manage.py migreate
  • running test server
1
python manage.py runserver
  • flush sql data
1
python manage.py flush
  • create super user
1
python manage.py createsuperuser
  • import & output data
1
2
python manage.py dumpdata appname > appname.json
python manage.py loaddata appname.json
  • create app
1
python manage.py startapp users

add app to settings

1
vim website/settings.py

in INSTALLED_APPS add "user",

now you can coding some app in django…

Deploy

1
2
sudo apt-get install nginx
sudo pip install supervisor

set this web nginx config

1
vim /etc/nginx/conf/website.conf

add the following code to website.conf

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
server{
listen 80;
server_name www.xxxx.com;
server_name 100.000.000.000;
charset utf-8;

location / {
try_files $uri $uri/ =404;
}

location /media {
alias /path/to/your/static/media;
}

location /static/ {
root /path/to/your/static/main/folder/;
#Note: this is static main folder not static folder
}

# this is for api
location /apis {
rewrite ^.+apis/?(.*)$ /$1 break;
proxy_pass http://127.0.0.1:8000;
proxy_pass_header Authorization;
proxy_pass_header WWW-Authenticate;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

# this is for django admin
location /admin {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

set this web supervisor config

1
vim /etc/supervisor/conf/website.conf

add the folloing code to ‘website.conf’

1
2
3
4
5
6
[program: website]
command = /home/tony/.local/share/virtualenvs/website-skIUga7b/bin/gunicorn -w 4 -b 0.0.0.0:8000 website.wsgi:application
directory = /data/website/website
user = root
stdout_logfile = /var/log/supervisor/stdout.log
stderr_logfile = /var/log/supervisor/stderr.log

reload nginx & supervisor

1
2
3
4
5
6
sudo /etc/init.d/ngins reload
sudo nginx -t

sudo /etc/init.d/supervisor reload
sudo /etc/init.d/supervisor restart
sudo /etc/init.d/supervisor status