最近嘗試用

ubuntu 22.04 與 Apache 執行 Django

摸索一陣子發現 Apache 執行 django 的方案算是簡單且方便的

而且還可以配合 phpmyadmin 管理資料庫

因為 phpmyadmin 預設安裝時候就可以選擇 apache 模式

自動把設定檔案新增好了

 

參考文章:

https://www.linuxtuto.com/how-to-install-django-with-apache-on-ubuntu-22-04/

 

主要是自己要新增

/etc/apache2/sites-available/django.conf

這個設定檔案

其實檔案名稱沒有一定

apache都會讀取

重點是內容

<VirtualHost *:80>
DocumentRoot /home/djangotest/test_web
ErrorLog ${APACHE_LOG_DIR}/error.log

Alias /static /home/djangotest/test_web/static
<Directory /home/djangotest/test_web/static>
    Require all granted
</Directory>

<Directory /home/djangotest/test_web/web>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

WSGIDaemonProcess myproject python-path=/home/djangotest/test_web python-home=/home/djangotest/env
WSGIProcessGroup myproject
WSGIScriptAlias / /home/djangotest/test_web/web/wsgi.py
WSGIApplicationGroup %{GLOBAL}

</VirtualHost>

這個XML有幾個重點

 

WSGIDaemonProcess 的 python-home 要設定 python 虛擬機的路徑

換句話說

一定要新增 python 虛擬機來使用

不然會遇到很多問題

 

WSGIScriptAlias 要設定主要 django 中的app 資料夾中的 wsgi.py

 

還有就是

WSGIApplicationGroup %{GLOBAL}

這個參數要上去

給大家參考囉