uWSGI部署django應用

django-admin startproject demo

demo項目的目錄結構python

demo/
├── db.sqlite3
├── demo
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-36.pyc
│   │   ├── settings.cpython-36.pyc
│   │   ├── urls.cpython-36.pyc
│   │   └── wsgi.cpython-36.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
└── requirements.txt

1. 安裝相關工具和依賴

[root@uwsgi ~]# yum -y install git wget httpd vim # 安裝相關工具
[root@uwsgi ~]# yum -y install gcc zlib* openssl-devel # 安裝編譯工具和依賴庫

2. 安裝 Python 環境

rhel系列沒法經過yum直接安裝python3,須要源碼編譯安裝git

[root@uwsgi ~]# pwd # 查看當前目錄
/root
[root@uwsgi ~]# wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz # 下載python3.6.5
[root@uwsgi ~]# tar -zxvf Python-3.6.5.tgz # 解壓
[root@uwsgi ~]# cd Python-3.6.5

[root@uwsgi Python-3.6.5]# pwd # 查看當前目錄
/root/Python-3.6.5
[root@uwsgi Python-3.6.5]# ./configure # 編譯
[root@uwsgi Python-3.6.5]# make && make install # 安裝
[root@uwsgi Python-3.6.5]# which python3 # 查看python3的路徑
/usr/local/bin/python3

4. 安裝 uWSGI

[root@uwsgi ~]# python3 -m pip install uwsgi

5. 安裝配置虛擬環境

[root@uwsgi ~]# python3 -m pip install virtualenv # 安裝虛擬環境
[root@uwsgi ~]# virtualenv -p /usr/local/bin/python3 /venv # 配置虛擬環境
[root@uwsgi ~]# source /venv/bin/activate # 激活虛擬環境
(venv) [root@uwsgi ~]# pip install -r /root/demo/requirements.txt # 安裝項目依賴
(venv) [root@uwsgi ~]# pip freeze # 查看依賴庫是否安裝
(venv) [root@uwsgi ~]# deactivate # 取消激活虛擬環境
[root@uwsgi ~]#

6. 修改 ALLOW_HOSTS

[root@uwsgi ~]# vim demo/demo/settings.py # 編輯項目中的 settings.py
ALLOWED_HOSTS = ["192.168.41.130",]

5. 配置 uWSGI

[root@uwsgi ~]# mkdir -p /var/log/uwsgi # 建立 uwsgi 的日誌目錄
[root@uwsgi ~]# vim /etc/uwsgi.ini # 建立 uwsgi.ini 配置文件
[uwsgi]
chdir=/root/demo
http=192.168.41.130:8000
home=/venv
module=demo.wsgi:application
master=True
pidfile=/tmp/demo.pid
max-requests=5000
daemonize=/var/log/uwsgi/demo.log
env=LANG=en_US.UTF-8
buffer-size=32768

6. 啓動 uWSGI

[root@uwsgi ~]# uwsgi --init /etc/uwsgi.ini # 以 /etc/uwsgi.ini 配置啓動 uwsgi

7. 注意事項

未安裝zlib*:sql

zipimport.ZipImportError: can't decompress data; zlib not available # 編譯時報錯

未安裝openssl、openssl-devel:django

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available # pip 安裝 uwsgi 時報錯

uwsgi 沒法訪問,日誌報錯:vim

invalid request block size: 21573 (max 4096)...skip # 在 /etc/uwsgi.ini 中添加 buffer-size=32768
相關文章
相關標籤/搜索