Helpex - Trao đổi & giúp đỡ Đăng nhập

Tìm hiểu cách triển khai ứng dụng Django


Trong bài đăng này, chúng ta sẽ đi qua các bước để triển khai ứng dụng Django trên máy chủ sản xuất. Tôi đang sử dụng máy chủ AWS ec2, phiên bản Ubuntu 20.04 và Python 3.8. Các bước giống nhau đối với hầu hết các phiên bản Ubuntu và Python, tuy nhiên, cú pháp có thể khác nhau dựa trên phiên bản bạn đang sử dụng.

Các bước

  1. Cài đặt Apache2.
  2. Liệt kê thư mục của dự án và đường dẫn của tệp.
  3. Thu thập các tệp tĩnh.
  4. Di chuyển cơ sở dữ liệu.
  5. Thay đổi quyền và quyền sở hữu của các tệp cơ sở dữ liệu và các thư mục khác.
  6. Thực hiện các thay đổi trong tệp cấu hình Apache.
  7. Kích hoạt trang web.
  8. Cài đặt mod WSGI trong Apache2.
  9. Khởi động lại Máy chủ Apache.

Bước 1: Cài đặt Apache 2

Sau đây là các lệnh để cài đặt máy chủ Apache 2 trên phiên bản Ubuntu.

sudo apt update
sudo apt install apache2

Bước 2: Liệt kê ra thư mục / đường dẫn tệp của dự án

Điều quan trọng là liệt kê đường dẫn dự án để thực hiện bước tiếp theo. Liệt kê tên và đường dẫn của dự án Django của bạn, tên và đường dẫn ứng dụng, đường dẫn vị trí của môi trường và đường dẫn tệp WSGI.

Directories 

Folder Name - /home/ubuntu/Demo

Project Name - DemoProject
Project Path - /home/ubuntu/Demo/DemoProject

Application Path - DemoApp
Application Path - /home/ubuntu/Demo/DemoProject/DemoApp

Environment Folder Path - /home/ubuntu/Demo/demo_env
Wsgi File Path - /home/ubuntu/Demo/DemoProject/DemoProject/wsgi.py

Bước 3: Thu thập tệp tĩnh

Django cung cấp cơ chế thu thập các tệp tĩnh vào một nơi để chúng có thể được phân phát dễ dàng.

Mở Setting.pybằng lệnh sau:

vi Demo/DemoProject/DemoProject/settings.py

# Add below code in settings.py file
import os 
STATIC_URL = '/static/' 
STATIC_ROOT = os.path.join(BASE_DIR, "static/") 
STATICFILES=[STATIC_ROOT]

Kích hoạt nguồn và thu thập các tệp tĩnh bằng các lệnh sau:

source Demo/my_env/bin/activate
python Demo/DemoProject/manage.py collectstatic

Bước 4: Di chuyển cơ sở dữ liệu

Di chuyển cơ sở dữ liệu bằng lệnh MakeMigrationMigrate:

python Demo/DemoProject/manage.py makemigrations
python Demo/DemoProject/manage.py migrate

Bước 5: Thay đổi quyền và quyền sở hữu

Nếu bạn đang sử dụng cơ sở dữ liệu SQLite, thì hãy thay đổi quyền của tệp SQLite. Ngoài ra, thay đổi quyền sở hữu của các thư mục dự án Django.

Các lệnh sau sẽ thay đổi quyền và quyền sở hữu của các tệp và thư mục.

chmod 664 ~/Demo/DemoProject/db.sqlite3

sudo chown :www-data ~/Demo/DemoProject/db.sqlite3

sudo chown :www-data ~/Demo/DemoProject

sudo chown :www-data ~/Demo/DemoProject/DemoProject

Bước 6: Các thay đổi trong tệp cấu hình Apache 

Chúng tôi cần thực hiện một vài thay đổi trong tệp 000-default.conf. Tuy nhiên, trước đó, hãy sao lưu tệp. Sau đây là các lệnh để mở tệp và tạo bản sao lưu của tệp.

# Go to the location - 
cd /etc/apache2/sites-available

# Take a backup of file
sudo cp 000-default.conf 000-default.conf_backup

# Open conf file using Vi
sudo vi 000-default.conf


Thêm mã dưới đây vào tệp:

Alias /static /home/ubuntu/Demo/DemoProject/static

        <directory home/ubuntu/demo/demoproject/static="">

            Require all granted

        </directory>

        <directory home/ubuntu/demo/demoproject/demoproject="">

            <files wsgi.py="">

                Require all granted

            </files>

        </directory>

        WSGIPassAuthorization On

        WSGIDaemonProcess DemoProject python-path=/home/ubuntu/Demo/DemoProject/ python-home=/home/ubuntu/Demo/demo_env

        WSGIProcessGroup DemoProject

        WSGIScriptAlias / /home/ubuntu/Demo/DemoProject/DemoProject/wsgi.py
<virtualhost *:80="">

        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf

        Alias /static /home/ubuntu/Demo/DemoProject/static
        <directory home/ubuntu/demo/demoproject/static="">
            Require all granted
        </directory>

        <directory home/ubuntu/demo/demoproject/demoproject="">
            <files wsgi.py="">
                Require all granted
            </files>
        </directory>

        WSGIPassAuthorization On
        WSGIDaemonProcess DemoProject python-path=/home/ubuntu/Demo/DemoProject/ python-home=/home/ubuntu/Demo/demo_env
        WSGIProcessGroup DemoProject
        WSGIScriptAlias / /home/ubuntu/Demo/DemoProject/DemoProject/wsgi.py

</virtualhost>

Bước 7: Kích hoạt trang web

Bây giờ kích hoạt tệp conf ở trên bằng cách sử dụng a2ensitelệnh.

cd /etc/apache2/sites-available/

sudo a2ensite 000-default.conf

Bước 8: Cài đặt mod WSGI trong Apache 2

Cài đặt thư viện mod WSGI cho máy chủ Apache 2 bằng lệnh sau. Sau khi cài đặt, kích hoạt WSGI.

sudo apt-get install libapache2-mod-wsgi-py3
sudo a2enmod wsgi

Bước 9: Khởi động lại máy chủ Apache

Khởi động lại máy chủ Apache bằng lệnh sau:

sudo service apache2 restart

Và bạn đã hoàn thành!

3 hữu ích 0 bình luận 7.2k xem chia sẻ

Có thể bạn quan tâm

loading