실시간 위치추적 기능을 웹소켓으로 구현하였고, 서버를 배포하였다. 우선 나는 Docker를 사용하지 않았고, daphne + nginx + redis(AWS elasticache)를 사용하는 방법으로 배포하였다.
EC2 인스턴스를 생성하고, 도메인을 연결하고, EC2 우분투 서버에 접속하는 과정은 생략하겠다.
배포 서버에서 Channels를 사용하기 위해 AWS elasticache로 Redis를 배포하자.
elasticache가 사용할 보안그룹을 생성한다.
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [(get_secret('REDIS_HOST'), 6379)],
},
},
}
settings.py의 installed_apps에 daphne를 추가한다.
이 외의 channel에 대한 설정은 이전 포스트에서 모두 마쳤으니, 실행시켜보자.
$ daphne -b 0.0.0.0 -p 8000 프로젝트명.asgi:application
도메인:8000 으로 접속이 되는것을 확인할 수 있다.
$ pip uninstall channels
$ pip uninstall daphne
$ python -m pip install -U channels["daphne"]
순서로 실행한 후, 다시 실행해보면 동작한다.
$ pip install uwsgi
$ sudo vim uwsgi.ini
[uwsgi]
chdir=/home/ubuntu/프로젝트폴더명
module=프로젝트명.wsgi:application
master=True
pidfile=/tmp/project-master.pid
vacuum=True
max-requests=5000
daemonize=/home/ubuntu/프로젝트폴더명/프로젝트명/django.log
home=/home/ubuntu/가상환경경로
virtualenv=/home/ubuntu/가상환경경로
socket=/home/ubuntu/프로젝트폴더명/프로젝트명/uwsgi.sock
chmod-socket=666
$ uwsgi --ini uwsgi.ini
$ sudo apt-get install nginx
$ sudo vim /etc/nginx/nginx.conf
$ sudo vim /etc/nginx/sites-available/프로젝트명.conf
upstream daphne {
server localhost:8000;
}
server {
server_name 도메인명;
# HTTP requests
location / {
try_files $uri @proxy_to_app;
}
location @proxy_to_app {
include /etc/nginx/uwsgi_params;
uwsgi_pass unix:/home/ubuntu/폴더명/프로젝트명/uwsgi.sock;
}
# WebSocket requests
location /ws/ {
proxy_pass http://daphne;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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;
proxy_redirect off;
}
}
sudo ln -s /etc/nginx/sites-available/프로젝트명 /etc/nginx/sites-enabled/
$ sudo service nginx restart
nginx -t
명령어로 확인해보자.$ sudo apt install snapd
$ sudo apt-get remove certbot
$ sudo snap install --classic certbot
$ sudo ln -s /snap/bin/certbot usr/bin/certbot
$ sudo certbot --nginx
이 명령어를 입력하면 뭘 물어보는데, 도메인 이름을 입력하면 된다. (Y/N는 Y를 입력하자)
$ vim etc/nginx/sites-enabled/프로젝트명
listen 443 ssl; # managed by Certbot 이런식으로 뭐가 생긴것을 확인할 수 있다.
$ sudo service nginx restart
이제 프론트에서 http요청은 받아지지만, ws요청은 받아지지 않는다.
daphne를 supervisor로 관리해보자.
$ sudo apt install supervisor
우선 supervisor를 설치하자.
$ vim /etc/supervisor/conf.d/channels.conf
[fcgi-program:asgi]
socket=tcp://localhost:8000
command=/home/ubuntu/가상환경경로/bin/daphne -u /run/daphne/daphne%(process_num)d.sock --fd 0 --access-log - --proxy-headers meong_signal.asgi:application
directory=/home/ubuntu/프로젝트폴더명/프로젝트명
numprocs=2
process_name=asgi%(process_num)d
autostart=true
autorestart=true
stderr_logfile=/var/log/daphne/daphne.err.log
stdout_logfile=/var/log/daphne/daphne.out.log
user=ubuntu.log
redirect_stderr=true
$ sudo mkdir /run/daphne/
$ sudo chown ubuntu.ubuntu /run/daphne/
$ sudo supervisorctl reread
$ sudo supervisorctl update
$ sudo chmod 777 /run/daphne
$ sudo tail -f /var/log/daphne/daphne.out.log
$ sudo tail -f /var/log/daphne/daphne.err.log
$ sudo systemctl restart supervisor
$ sudo tail -n 100 /var/log/supervisor/supervisord.log
이제 서버를 실행하면 http통신과 웹소켓 통신이 모두 가능하다!