centos7上django项目使用httpd(apache)+mod_wsgi的守护进程方式部署-----更改python共享库
最近在研究django项目在centos7上的部署,采用httpd+mod_wsgi的部署方式,官方推荐是以mod_wsgi的守护进程模式进行部署,刚开始踩了很多坑,最后终于成功现总结如下,有误的地方请指正:
首先安装,httpd其实就是apache服务器,其在centos上的名字就是httpd,部署之前先要安装httpd、mod_wsgi、httpd-devel命令为:
yum install httpd
yum install -y httpd-devel
yum install mod_wsgi
Apache服务的配置说明
主配置文件是/etc/httpd/conf/httpd.conf
其他配置文件存储在/etc/httpd/conf.d/目录,使用vim编辑即可:
本案例项目名以test为例进行介绍,项目目录在根目录,虚拟环境在根目录,取名为env。
1、主配置文件httpd.conf添加内如下配置:
wsgi_module modules/mod_wsgi.so
然后Listen更改为本机ip:80,如:Listen 192.168.1.1:80
2、然后 执行 vim /etc/httpd/conf.d/test.conf新建test.conf文件,里面配置内容为:
WSGIDaemonProcess test processes=2 threads=15 display-name=%{GROUP} python-home=/env python-path=/test
WSGIProcessGroup test
WSGIScriptAlias / /test/test/wsgi.py
Alias /static/ /test/static/
<Directory /test>
Require all granted
</Directory>
注意:python-home这一项是因为我得项目依赖环境在虚拟环境中,如果没有虚拟环境而是在主机中不需要进行这一项配置。
到这里就配置完成了,systemctl restart httpd.service重启apache服务器然后在浏览器就可以访问了。
注意:如果python 是通过源码安装则可能会报无法导入python下面site-packages的包的问题如图:
因为mod_wsgi的安装需要使用到python的共享库,编译安装python时要启用共享库,而目前Linux下的共享库路径配置不正确。默认的linux共享库搜索路径为/lib和/usr/lib两个目录(不包含子目录),若共享库不在这两个路径(最典型的就是/usr/local/lib),不能被自动动态链接到,解决方法方法如下:
1、下载源码编译安装python
./congifure --enable-shared
make &&make install
2、添加python链接库到路径
sudo vi /etc/ld.so.conf
/usr/local/lib #添加路径到文件
sudo /sbin/ldconfig -v
具体的linux共享库配置可以参考:https://blog.****.net/ithaibiantingsong/article/details/81325363