Django deploy into Apache on Ubuntu

環境

  • Ubuntu Server 16.04 LTS (HVM), SSD Volume Type
  • Apache 2.4.18
  • mod_wsgi 4.3.0
  • Python 3.5.2

專案結構

請參考此 專案。

部署流程

根據 Launch Instance 建立出實例,並使用SSH連線至實例,此範例使用Ubuntu Server

更新apt-get,確保接下來必須的套件能順利安裝。

$ sudo apt-get update

安裝Apache2

$ sudo apt-get install apache2

安裝mod_wsgi

# Python 2
$ sudo apt-get install libapache2-mod-wsgi

# Python 3
$ sudo apt-get install libapache2-mod-wsgi-py3

Apache2底下,新增一個網站配置文件。

$ sudo vim /etc/apache2/sites-available/sitename.conf
  • sitename 可自行命名。
  • 記得使用 sudo,沒使用 sudo,編輯後會無法存檔。

配置文件如下:

<VirtualHost *:80>
    ServerName 13.115.153.192 # ip or domain_name.com

    # 靜態文件網址別名設定,與對應在server中實際路徑位置
    Alias /media/ /home/ubuntu/djangosite/media/
    Alias /static/ /home/ubuntu/djangosite/static/

    # 允許設定的路徑能夠被存取
    <Directory /home/ubuntu/djangosite/media>
        Require all granted
    </Directory>
    <Directory /home/ubuntu/djangosite/static>
        Require all granted
    </Directory>

    # ip 能夠替換成 domain name 方式
    # python-home 設定虛擬環境所在路徑
    # python-path 設定專案所在路徑
    # WSGIScriptAlias 設定專案中 wsgi.py 的路徑
    WSGIDaemonProcess 13.115.153.192 python-home=/home/ubuntu/myvenv python-path=/home/ubuntu/djangosite
    WSGIProcessGroup 13.115.153.192
    WSGIScriptAlias / /home/ubuntu/djangosite/djangosite/wsgi.py

    # Directory 設定 wsgi.py 所在目錄路徑
    <Directory /home/ubuntu/djangosite/djangosite>
    <Files wsgi.py>
        Require all granted
    </Files>
    </Directory>
</VirtualHost>

設定專案存取權限,專案中目錄的權限設定為755,檔案的權限設定為644。
以上述配置文件為例,專案位於/home/ubuntu/djangosite
檔案權限設定可參考 這篇

$ cd /home/ubuntu
$ sudo chmod -R 644 djangosite
$ sudo find djangosite -type d | xargs chmod 755

Apache預設的用戶為www-data,簡單說,Apache是以www-data的身份執行系統中檔案,相關設定檔位於/etc/apache2/envvars。以下是預設的設定結果,也可以改成以其他的身份執行。

$ cat /etc/apache2/envvars
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data

給予Apache在專案中某些目錄或檔案寫入的權限。
比如說用戶上傳圖片的路徑為/home/ubuntu/djangosite/media/,讓Apachemedia目錄中能夠寫入檔案。

$ cd /home/ubuntu/djangosite 
$ sudo chgrp -R www-data media
$ sudo chmod -R g+w media

比如說資料庫位於/home/ubuntu/djangosite/db.sqlite3,設定Apache能夠寫入資料庫。

$ cd /home/ubuntu
$ sudo chgrp www-data djangosite
$ sudo chmod g+w djangosite
$ sudo chgrp www-data djangosite/db.sqlite3
$ sudo chmod g+w djangosite/db.sqlite3

設定專案中的settings.py

DEBUG = False

ALLOWED_HOSTS = [
    ......
    '13.115.153.192', # 加入 ip 或 網址
]

# 設定靜態檔案目錄和網址
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# 讓所有的 app 共用靜態文件,如 js, css
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "common_static"),
    '/home/ubuntu/djangosite/static/',
)

啟動之前的設定檔,正常就可以訪問到架設的網站。

$ sudo a2ensite sitename
or
$ sudo a2ensite sitename.conf

錯誤修正

當發現Django預設的網站界面缺少排版,輸入以下指令蒐集Django自動產生的靜態文件,如 admin 上的 css、js。

python manage.py collectstatic

若還有錯誤,請查看Apache Log訊息進行錯誤修正。

vim /var/log/apache2/error.log

錯誤修正後,需重啟Apache

sudo service apache2 restart

參考網站

https://code.ziqiangxuetang.com/django/django-static-files.html

results matching ""

    No results matching ""