apache配置python mod_wsgi

apt-get升级

# 更新源
apt-get update

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安装

# 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环境安装

# 安装 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_wsgi

编辑测试脚本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!

# 有输出代表成功!

 

二。通过wsgi跑django程序

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/

 

转载于:https://my.oschina.net/u/914655/blog/3078037

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值