本篇內(nèi)容主要講解“apache配置python mod_wsgi的方法”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“apache配置python mod_wsgi的方法”吧!
創(chuàng)新互聯(lián)公司為客戶提供專業(yè)的網(wǎng)站制作、成都做網(wǎng)站、程序、域名、空間一條龍服務(wù),提供基于WEB的系統(tǒng)開(kāi)發(fā). 服務(wù)項(xiàng)目涵蓋了網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站程序開(kāi)發(fā)、WEB系統(tǒng)開(kāi)發(fā)、微信二次開(kāi)發(fā)、成都做手機(jī)網(wǎng)站等網(wǎng)站方面業(yè)務(wù)。
apt-get升級(jí)
# 更新源 apt-get update
python環(huán)境安裝
# 安裝python3 apt-get install python3 python -V ls /usr/bin/python* cd /usr/bin/ ln -s python3 python python -V # 基礎(chǔ)安裝 apt-get install curl wget vim # 時(shí)區(qū)設(shè)置 or tzselect apt-get install tzdata date -R # 安裝pip cd tmp/ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python get-pip.py 此處會(huì)報(bào)錯(cuò) Traceback (most recent call last): File "get-pip.py", line 22308, inmain() File "get-pip.py", line 197, in main bootstrap(tmpdir=tmpdir) File "get-pip.py", line 82, in bootstrap import pip._internal File "/tmp/tmpslqx710t/pip.zip/pip/_internal/__init__.py", line 40, in File "/tmp/tmpslqx710t/pip.zip/pip/_internal/cli/autocompletion.py", line 8, in File "/tmp/tmpslqx710t/pip.zip/pip/_internal/cli/main_parser.py", line 7, in File "/tmp/tmpslqx710t/pip.zip/pip/_internal/cli/cmdoptions.py", line 15, in ModuleNotFoundError: No module named 'distutils.util' 需要安裝 apt-get install python3-distutils python get-pip.py pip list
django安裝
# django安裝 pip install django pip install requests pip install openpyxl pip install six # 安裝MySQLclient前需要安裝,否則會(huì)報(bào)下面所列錯(cuò)誤 apt-get install libmysqlclient-dev python3-dev build-essential pip install mysqlclient 錯(cuò)誤 1. OSError: mysql_config not found 2. error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
apache2環(huán)境安裝
# 安裝 apache sudo apt-get install apache2 # 啟動(dòng) apache sudo service apache2 start # 安裝 mod_wsgi sudo apt-get install libapache2-mod-wsgi-py3 # 驗(yàn)證 mod_wsgi 安裝 sudo find / -name mod_wsgi.so # 原理 實(shí)際安裝完libapache2-mod-wsgi-py3會(huì)生成一個(gè)mod_wsgi.so的文件,位于 /usr/lib/apache2/modules/mod_wsgi.so 而apache2和它的關(guān)聯(lián)在下面 root@fe71dd7317b0:/etc/apache2# cat mods-available/wsgi.load LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so 這個(gè)地方要注意python3的版本,ubuntu16.04默認(rèn)源是python3.5的,其用上面命令安裝的mod_wsgi都是3.5的,需要更改 有的通過(guò)pip安裝mod_wsgi,再修改mods-available/wsgi.load里mod_wsgi.so的路徑即可 具體見(jiàn)下(但本文是Ubuntu18.04的不用關(guān)心此問(wèn)題) LoadModule wsgi_module "/usr/local/lib/python3.6/dist-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
驗(yàn)證mod_wsgi
編輯測(cè)試腳本vim /var/www/html/wsgi_test_script.py
def application(environ, start_response): status = '200 OK' output = b'Hello World!' response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))] start_response(status, response_headers) return [output]
配置wsgi
vim /etc/apache2/apache2.conf
# 最后加入 WSGIScriptAlias /test_wsgi /var/www/html/wsgi_test_script.py
restart apache2
service apache2 restart curl http://localhost/test_wsgi root@5ee5ff2c92c7:/usr/bin# curl http://localhost/test_wsgi Hello World! # 有輸出代表成功!
二。通過(guò)wsgi跑django程序
cd /var/www django-admin startproject zq # config wsgi WSGIScriptAlias / /var/www/zq/zq/wsgi.py WSGIPythonPath /var/www/zqRequire all granted # 注意WSGIPythonPath不配置的話,可能zq模塊找不到 # restart apache2 service apache2 restart # test curl http://localhost/
到此,相信大家對(duì)“apache配置python mod_wsgi的方法”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!