티스토리 뷰

INTRO : 용어 정리

[[Raspbian]] path :  /home/[UserID]

[[virtualenv]] path :  /home/[UserID]/.virtualenvs

[[django project]] path :  /home/[UserID]/.virtualenvs/jbe/[django_project]

django_project : django project name

[[local고정IP]] : Raspberrypi가 놓여진 공유기 내부 ip

[[공인IP]] : 공유기가 설치된 외부접속 공인IP(http://www.findip.kr/로 확인 가능)

[[PORT.No.]] : Port-forwarding을 통해 열어둔 port

 

1. unix socket 연결 설정 : django project nginx.conf 생성 및 수정

nginx(webserver) <-> unix socket <-> wsgi <-> django

$ nano [[django_project]]/django_project_nginx.conf

--nanoEditor : django_project_nginx.conf
## django_project_nginx.conf

# upstream(proxy) setting
upstream django {
    server unix://[[django_project]]/django_project.socket;

}

# configuration of the server
server {
    # the port your site will be served on
    listen      [[PORT.No.]];

    # the domain name it will serve for
    server_name [[local고정IP]];
    charset     utf-8;

    # max upload size :
    client_max_body_size 75M;

    # Django project mediafiles path
    location /media  {
        alias [[django_project]]/mediafiles;
    }
    # Django project staticfiles path
    location /static {
        alias [[django_project]]/staticfiles;
    }


    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        # uwsgi_params path
        include     [[django_project]]/uwsgi_params;
    }
}
--nanoEditor


## 심볼릭링크 추가
$ sudo ln -s [[[django_project]]/django_project_nginx.conf /etc/nginx/sites-enabled/


## (참고)심볼릭링크 파일 제거
$ sudo rm -f [filename]


## nginx restart
$ sudo /etc/init.d/nginx restart

 

2. uwsgi socket Test

[[django_project]]/django_project/settings.py : DEBUG = True 확인 --> Test 완료 후 False로 변경, django 로켓 화면 확인

unix socket Test는 virtualenv(가상환경)에서 서버를 띄워야한다. 필자는 5종의 Error Issue를 만났고 포스트 밑에 정리해 두었다.

 

(jbe)[[django_project]]$ uwsgi --socket django_project.sock --module django_project.wsgi --chmod-socket=666

*** Starting uWSGI 2.0.18 (32bit) on [Tue Dec 10 15:57:21 2019] ***
compiled with version: 8.3.0 on 16 July 2019 12:06:45
os: Linux-4.19.75-v7+ #1270 SMP Tue Sep 24 18:45:11 BST 2019
....

clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: [[django_project]]
detected binary path: [[virtualenv]]/jbe/bin/uwsgi
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 7336
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address django_project.sock fd 3
Python version: 3.7.3 (default, Apr  3 2019, 05:39:12)  [GCC 8.2.0]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0xdcffa8
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 64400 bytes (62 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 2 seconds on interpreter 0xdcffa8 pid: 11572 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 11572, cores: 1)

 

 

django 구동 확인 : browser 주소창 공인IP:PORT.No.

 

 

3. django_project.ini 설정 및 uwsgi 접속

(jbe)[[django_project]]$ touch django_project.ini
(jbe)[[django_project]]$ nano django_project.ini

--nanoEditor : django_project.ini
## django_project.ini settings
[uwsgi]

# django-related settings
# the base directory (full path)
chdir         = [[django_project]]/
# django’s wigs file (just module name)
module        = django_project.wsgi
# the virtualenv (full path)
home          = [[virtualenv]]/jbe/

# process-related settings
# master
master        = true
# maximum number of worker processes
processes = 2
# the socket (use the full path to be safe)
socket        = [[django_project]]/jinozpiblog.sock
# … with appropriate permissions - may be need
chmod-socket  = 666
# clear environment on exit
vacuum        = true
--nanoEditor

(jbe)[[django_project]]$ uwsgi --ini django_project.ini

*** Starting uWSGI 2.0.18 (32bit) on [Wed Dec 11 11:11:20 2019] ***
compiled with version: 8.3.0 on 16 July 2019 12:06:45
os: Linux-4.19.75-v7+ #1270 SMP Tue Sep 24 18:45:11 BST 2019
....
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: [[django_project]]
detected binary path: [[virtualenv]]/jbe/bin/uwsgi
chdir() to [[django_project]]
your processes number limit is 7336
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address [[django_project]]/django_project.sock fd 3
Python version: 3.7.3 (default, Apr  3 2019, 05:39:12)  [GCC 8.2.0]
Set PythonHome to [[virtualenv]]/jbe/
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x2066670
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 193200 bytes (188 KB) for 2 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x2066670 pid: 14752 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 14752)
spawned uWSGI worker 1 (pid: 14753, cores: 1)
spawned uWSGI worker 2 (pid: 14754, cores: 1)

 

 

django 구동 확인 : browser 주소창 공인IP:PORT.No.

 

 

 

------------------ 이하 Error Issue 정리 --------------------

Error Issue : 결론 외부접속 시 가상환경 + uwsgi 경로/권한 + settings.py [[공인IP]] + DEBUG=True 

Error #1: 권한문제 664 일때 uwsgi는 정상으로 보이나 502 Bad Gateway를 뿜어냄

(jbe)[[django_project]]$ uwsgi --socket django_project.sock --module django_project.wsgi --chmod-socket=664

*** Starting uWSGI 2.0.18 (32bit) on [Tue Dec 10 16:10:12 2019] ***
compiled with version: 8.3.0 on 16 July 2019 12:06:45
os: Linux-4.19.75-v7+ #1270 SMP Tue Sep 24 18:45:11 BST 2019
....
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: [[django_project]]
detected binary path: [[virtualenv]]/jbe/bin/uwsgi
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 7336
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address django_project.sock fd 3
Python version: 3.7.3 (default, Apr  3 2019, 05:39:12)  [GCC 8.2.0]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1baafa8
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 64400 bytes (62 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x1baafa8 pid: 11643 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 11643, cores: 1)

 

 

Error #2: 가상환경이 아니고 socket/wsgi 경로 미지정 시 Internal Server Error 뿜어냄

[[django_project]]$ uwsgi --socket django_project.sock --module django_project.wsgi --chmod-socket=666

*** Starting uWSGI 2.0.18 (32bit) on [Tue Dec 10 15:58:00 2019] ***
compiled with version: 8.3.0 on 16 July 2019 12:06:45
os: Linux-4.19.75-v7+ #1270 SMP Tue Sep 24 18:45:11 BST 2019
....
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: [[django_project]]
detected binary path: /usr/local/bin/uwsgi
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 7336
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address django_project.sock fd 3
Python version: 3.7.3 (default, Apr  3 2019, 05:39:12)  [GCC 8.2.0]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1d79580
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 64400 bytes (62 KB) for 1 cores
*** Operational MODE: single process ***
Traceback (most recent call last):
  File "./django_project/wsgi.py", line 17, in 
    from django.core.wsgi import get_wsgi_application
ModuleNotFoundError: No module named 'django'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 11573, cores: 1)

 

 

Error #3: 가상환경이 아니고 socket/wsgi 경로 지정이 옳바르더라도 python module Error로 Internal Server Error 뿜어냄

$ uwsgi --socket [[django_project]]/django_project.sock --module [[django_project]]/django_project.wsgi --chmod-socket=666

*** Starting uWSGI 2.0.18 (32bit) on [Tue Dec 10 16:17:32 2019] ***
compiled with version: 8.3.0 on 16 July 2019 12:06:45
os: Linux-4.19.75-v7+ #1270 SMP Tue Sep 24 18:45:11 BST 2019
....
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: [[django_project]]
detected binary path: [[virtualenv]]/jbe/bin/uwsgi
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 7336
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address [[django_project]]/django_project.sock fd 3
Python version: 3.7.3 (default, Apr  3 2019, 05:39:12)  [GCC 8.2.0]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0xce1008
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 64400 bytes (62 KB) for 1 cores
*** Operational MODE: single process ***
ModuleNotFoundError: No module named '/home/[UserID]/'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 11663, cores: 1)
--- no python application found, check your startup logs for errors ---

 

 

Error #4: DEBUG = False, settings.py ALLOWED_HOSTS = [['local고정IP']] Not Found 뿜어냄

Error #5: DEBUG = Ture, settings.py ALLOWED_HOSTS = [['local고정IP']] --> DisallowedHost

반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
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
글 보관함