apache配置python mod_wsgi

apt-get升級html

# 更新源
apt-get update

python環境安裝python

# 安裝python3
apt-get install python3
python -V
ls /usr/bin/python*
cd /usr/bin/
ln -s python3 python
python -V

# 基礎安裝
apt-get install curl wget vim
# 時區設置 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
此處會報錯
Traceback (most recent call last):
  File "get-pip.py", line 22308, in <module>
    main()
  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 <module>
  File "/tmp/tmpslqx710t/pip.zip/pip/_internal/cli/autocompletion.py", line 8, in <module>
  File "/tmp/tmpslqx710t/pip.zip/pip/_internal/cli/main_parser.py", line 7, in <module>
  File "/tmp/tmpslqx710t/pip.zip/pip/_internal/cli/cmdoptions.py", line 15, in <module>
ModuleNotFoundError: No module named 'distutils.util'

須要安裝
apt-get install python3-distutils
python get-pip.py
pip list

django安裝mysql

# django安裝
pip install django
pip install requests
pip install openpyxl
pip install six

# 安裝mysqlclient前須要安裝,不然會報下面所列錯誤
apt-get install libmysqlclient-dev python3-dev build-essential
pip install mysqlclient

錯誤
1. OSError: mysql_config not found
2. error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

apache2環境安裝linux

# 安裝 apache
sudo apt-get install apache2

# 啓動 apache
sudo service apache2 start

# 安裝 mod_wsgi
sudo apt-get install libapache2-mod-wsgi-py3

# 驗證 mod_wsgi 安裝
sudo find / -name mod_wsgi.so

# 原理
實際安裝完libapache2-mod-wsgi-py3會生成一個mod_wsgi.so的文件,位於
/usr/lib/apache2/modules/mod_wsgi.so
而apache2和它的關聯在下面
root@fe71dd7317b0:/etc/apache2# cat mods-available/wsgi.load
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so

這個地方要注意python3的版本,ubuntu16.04默認源是python3.5的,其用上面命令安裝的mod_wsgi都是3.5的,須要更改
有的經過pip安裝mod_wsgi,再修改mods-available/wsgi.load裏mod_wsgi.so的路徑便可
具體見下(但本文是Ubuntu18.04的不用關心此問題)
LoadModule wsgi_module "/usr/local/lib/python3.6/dist-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"

驗證mod_wsgisql

編輯測試腳本vim /var/www/html/wsgi_test_script.py apache

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]

配置wsgidjango

vim /etc/apache2/apache2.confbootstrap

# 最後加入
WSGIScriptAlias /test_wsgi /var/www/html/wsgi_test_script.py

restart apache2ubuntu

service apache2 restart

curl http://localhost/test_wsgi

root@5ee5ff2c92c7:/usr/bin# curl http://localhost/test_wsgi
Hello World!

# 有輸出表明成功!

 

二。經過wsgi跑django程序vim

cd /var/www

django-admin startproject zq

# config wsgi 
WSGIScriptAlias / /var/www/zq/zq/wsgi.py
WSGIPythonPath /var/www/zq
<Directory /var/www/zq/zq>
    Require all granted
</Directory>

# 注意WSGIPythonPath不配置的話,可能zq模塊找不到

# restart apache2
service apache2 restart

# test
curl http://localhost/
相關文章
相關標籤/搜索