Python Django: hướng dẫn đưa dự án django lên server production
Xin chào mọi người, trong bài viết này mình sẽ hướng dẫn các bạn cách đưa một project django lên môi trường thực tế (production). Hệ điều hành mình sử dụng là Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-122-generic x86_64), Django version 3.1
Điều kiện tiên quyết: Source code đã chạy được ở môi trường develop và được lưu trữ ở git, ở đây mình sử dụng PostgreSQL version 10.15 làm hệ cơ sở dữ liệu.
1. Cài đặt git
1 |
apt install git |
2. Clone source code từ git
1 |
git clone url_git django_project |
3. Cài đặt NGINX và curl
1 |
apt install nginx curl |
4. Cài đặt python 3.6
1 |
apt install python3-pip python3.6 libpq-dev |
5. Cài đặt Postgres
1 |
apt install postgresql postgresql-contrib |
6. Tạo Cơ sở dữ liệu và Người dùng PostgreSQL
1 2 3 4 5 6 7 8 |
sudo -u postgres psql; CREATE DATABASE django; CREATE USER django_user WITH PASSWORD 'Z@$a_44mpSpz'; ALTER ROLE django_user SET client_encoding TO 'utf8'; ALTER ROLE django_user SET default_transaction_isolation TO 'read committed'; ALTER ROLE django_user SET timezone TO 'UTC'; GRANT ALL PRIVILEGES ON DATABASE django TO django_user; Gõ lệnh logout: \q |
7. Tạo môi trường ảo cho project django của bạn
1 2 3 4 5 6 |
sudo -H pip3 install --upgrade pip sudo -H pip3 install virtualenv cd /var/www/django_project virtualenv env source env/bin/activate pip install -r requirements.txt |
8. Cấu hình project
1 2 3 |
Thay đổi thông tin kết nối với database trong file settings.py ALLOWED_HOSTS = ['*'] # Thay đổi bằng IP domain của bạn STATIC_ROOT = os.path.join(BASE_DIR, 'static/') |
9. Hoàn thành cấu hình project
1 2 3 4 |
python manage.py makemigrations python manage.py migrate python manage.py createsuperuser -> Username: superadmin/aU64CD9vd*Ma python manage.py collectstatic |
Hiện tại thì project của bạn vẫn đang chạy với port 8000, để chuyển qua port 80 (http) or 443 (https) thì ta cấu hình như sau
1 2 3 |
cd /var/www/django_project gunicorn --bind 0.0.0.0:8000 django_project.wsgi deactivate |
10. Tạo systemd Socket and Service Files cho Gunicorn
Tạo file gunicorn socket
1 |
sudo nano /etc/systemd/system/gunicorn.socket |
với nội dung như sau
1 2 3 4 5 6 7 8 |
[Unit] Description=gunicorn socket [Socket] ListenStream=/run/gunicorn.sock [Install] WantedBy=sockets.target |
Tạo service files
1 |
sudo nano /etc/systemd/system/gunicorn.service |
với nội dung
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[Unit] Description=gunicorn daemon Requires=gunicorn.socket After=network.target [Service] User=root Group=www-data WorkingDirectory=/var/www/django_project ExecStart=//var/www/django_project/env/bin/gunicorn \ --access-logfile - \ --workers 3 \ --bind unix:/run/gunicorn.sock \ django_project.wsgi:application [Install] WantedBy=multi-user.target |
11. Kiểm tra trạng thái và bật socket
1 2 |
sudo systemctl start gunicorn.socket sudo systemctl enable gunicorn.socket |
12. Cấu hình NGINX Proxy Pass đến Gunicorn
Gõ lệnh
1 |
sudo nano /etc/nginx/sites-available/django_project |
với nội dung file như sau:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
server { listen 80; server_name domain_name; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /var/www/django_project; } location / { include proxy_params; proxy_pass http://unix:/run/gunicorn.sock; } } |
13. Tạo symlink đến nginx
1 |
sudo ln -s /etc/nginx/sites-available/django_project /etc/nginx/sites-enabled |
Khi có sự thay đổi code thì vui lòng restart lại gunicorn
1 |
sudo systemctl restart gunicorn |
Nếu thay đổi file Gunicorn socket hoặc service files vui lòng chạy lại cách lệnh này để cập nhật
1 2 |
sudo systemctl daemon-reload sudo systemctl restart gunicorn.socket gunicorn.service |
Nếu bạn có thay đổi nginx config file thì chạy lệnh dưới, để restart lại NGINX
1 |
sudo nginx -t && sudo systemctl restart nginx |
Hi vọng sẽ giúp được bạn. Thanks