#ubuntu์์ django
---------------------------------------------------------------------------------
# ์ฐธ๊ณ
https://tutorial.djangogirls.org/ko/installation/
http://incoffee.tistory.com/6 ---> ์ด๊ฑฐ ๋ณด๊ณ ํจ
https://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html
http://pythonstudy.xyz/python/article/303-Django-%EC%84%A4%EC%B9%98
https://docs.djangoproject.com/ko/2.0/intro/tutorial01/
# ๋ฒ์ถ์ผ ํ๊ฒฝ ์ค์ ๊ด๋ จ
https://beomi.github.io/2016/12/28/HowToSetup-Virtualenv-VirtualenvWrapper/
---------------------------------------------------------------------------------
[0] apt-get ํจํค์ง ์ ๋ฐ์ดํธ
$ sudo apt-get update && apt-get upgrade -y
[1] python-pip, python-dev ์ค์น
$ sudo apt-get install python-pip python-dev
$ sudo apt-get install python3-pip python3-dev
[2] ํ์ธ
$ pip -V
$ pip3 -V
[3] pip์ผ๋ก virtualenv virtualenvwrapper ์ค์น
$ pip install virtualenv virtualenvwrapper
$ pip3 install virtualenv virtualenvwrapper
[4] virtualenv ํ๋ก์ ํธ ์์ฑ
$ virtualenv --python=ํ์ด์ฌ๋ฒ์ ๊ฐ์ํ๊ฒฝ์ด๋ฆ
# ex)
$ virtualenv --python=python3.5 ๊ฐ์ํด๋
$ virtualenv --python=python2.7 ๊ฐ์ํด๋
[5] ๊ฐ์ํ๊ฒฝ ์คํ
$ cd ๊ฐ์ํด๋/bin/
$ source activate
[6] ์ฅ๊ณ ์ค์น
- pip์ด ์ต์ ๋ฒ์ ์ธ์ง ํ์ธ
$ pip install --upgrade pip
$ pip install django~=1.11.0
[7] ์ฅ๊ณ ํ๋ก์ ํธ ์์ฑ
$ cd ๊ฐ์ํด๋
$ django-admin startproject mysite .
[8] ์ฅ๊ณ ํ๋ก์ ํธ ํ๊ฒฝ์ค์ ๋ณ๊ฒฝ
mysite/settings.py
TIME_ZONE = 'Asia/Seoul'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
ALLOWED_HOSTS = ['127.0.0.1', '.pythonanywhere.com']
์ถ๊ฐ ๋ฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ํ >> ์ด๋ฏธ ๋ด๋ถ์ sqlite3์ด ์ค์น๋์ด ์๋ค๊ณ ํจ.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
[9] ์คํ (python ์ผ๋ก ์คํํ๊ฑฐ๋ uWSGI ์ผ๋ก ์คํ)
$ python manage.py migrate ---> ์๋ฒ ์๋ ํ์ธ
$ python manage.py runserver ---> ์๋ฒ์คํ
[9-1] uWSGI๋ฅผ virtualenv์ ์ค์น
$ pip install uwsgi
[9-2] uWSGI ์คํ
$ uwsgi --socket :8001 --wsgi-file test.py
[9-3] django ํ๋ก์ ํธ ์คํ
$ python manage.py runserver
$ uwsgi --socket :8001 --module mysite.wsgi --> ์คํ ๋์์
>> (test) root@mungmange:/var/www/dev/git_project/dev_toy/py/test/mysite # uwsgi --socket :8001 --module mysite.wsgi
$ uwsgi --http :8001 --module mysite.wsgi #uwsgi --socket :8001 --wsgi-file test.py
[10] nginx ์คํฌ๋ฆฝํธ
-------------------------------------------
upstream django{
server localhost:8001;
}
server {
listen 80;
server_name localhost;
charset utf-8;
location ~ /d {
uwsgi_pass django;
include uwsgi_params; # the uwsgi_params file you installed
}
}
-------------------------------------------
$ service nginx restart
[11] ์ฐธ๊ณ ๋ก setting.py ํ์ผ ์์ ํ
$ python manage.py migrate ํด์ฃผ๊ธฐ
[12] ์ดํ๋ฆฌ์ผ์ด์ ํ๋ก์ ํธ ์์ฑ
$ python manage.py ๊ฐ์ํด๋ blog
----------------------------------------------------------------------------------------------------------------
# python ์ฟผ๋ฆฌ์
[1] shell ์คํ
(test) root@mungmange:/var/www/dev/git_project/dev_toy/py/test/mysite# python manage.py shell
[2] ๊ฐ์ฒด ์กฐํ
>>> from blog.models import Post
>>> Post.objects.all()
<QuerySet [<Post: aa>, <Post: aa>, <Post: aa>, <Post: ad>, <Post: sdf>]>