Apache/Django筆記
預設有兩個Django project : locallibrary (study from MDN), demoblog,放在/var/www/html下面。
因為不打算連外、只有server-client溝通,所以只有在client /etc/hosts加入 target server的URL對應。
server的 /etc/httpd/conf.d/下建立 vhost.conf:
<virtualHost *:80 >
ServerName demoblog.[domain name]
DocumentRoot /var/www/html/demoblog
WSGIDaemonProcess demoblog processes=1 threads=5 home=/var/www/html/demoblog/
WSGIProcessGroup demoblog
WSGIScriptAlias / /var/www/html/demoblog/demoblog/wsgi.py
<Directory "/var/www/html/demoblog">
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/html/demoblog/demoblog/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
<virtualHost *:80>
ServerName locallibrary.[domain name]
DocumentRoot /var/www/html/locallibrary
WSGIDaemonProcess locallibrary processes=1 threads=5 home=/var/www/html/locallibrary/
WSGIProcessGroup locallibrary
WSGIScriptAlias / /var/www/html/locallibrary/locallibrary/wsgi.py
<Directory "/var/www/html/locallibrary">
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/html/locallibrary/locallibrary/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
主要的重點就在於 WSGI將之 daemon化。
引用網路上查到的文章解釋WSGI:
"在傳統的 web server 中,像是 NGINX, APACHE 是無法去執行 Python web applicatION 的。因此需要一個『能夠執行』的 web server 來達到這個目的,而這種 web server 也叫做 WSGI server"
而在 Django部份,我拿其中一個demoblog作為範例:
- django-admin startproject diyblog " under /var/www/html
- 在/var/www/html/demoblog/ 下,執行 "./manage.py startapp blog"
- Setup diyblog/settings, urls
- Add static files(JS, CSS...etc.) into demoblog -->
- import os.path
- PROJECT_ROOT = os.path.normpath(os.path.dirname(BASE_DIR))
- STATIC_ROOT = os.path.join(BASE_DIR, 'static')
- 並且在 url.py 裡面的urlpatterns 的中括號尾巴加上: + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
b. ./manage.py collectstatic
記得重新啟動Web 伺服器
其餘大致上就按照 MDN上面的作法來實現了