说明:通过conda安装python环境时,anaconda安装程序的python版本需与django的环境python版本尽量一致(比如3.9与3.9,最后的小版本可以有一定出入),否则会出问题,如failed to get the python codec of the file system等。

django项目通过apache+mod_wsgi部署。
1、编译mod_wsgi,参考我之前的博客,编译mod_wsgi。
linux下mod_wsgi安装(linux下mod_wsgi安装_shendygis的专栏-CSDN博客);
2、导入模块
进入httpd的module路径:/etc/httpd/conf.modules.d/
添加mod_wsgi模块配置文件,

10-wsgi.conf内容如下:
LoadModule wsgi_module modules/mod_wsgi.so
3、配置mod_wsgi.
httpd 的配置文件在etc/httpd/conf/httpd.conf文件中,可以添加自己的配置文件在conf.d中。

为了方便,我自己为本项目添加了一个配置文件,存放在etc/httpd/conf.d/django_mod_wsgi.conf,通过上面配置,会自动读取conf.d下的conf文件,
文件内容如下:
#port
Listen 8000
#spark has it's own signal,can't set Off,if set on,wsgi app will ignore signal,and an message "mod_wsgi (pid=60783): Callback registration for signal 2 ignored." occured
#WSGIRestrictSignal Off
<VirtualHost *:8000>
# requet daemon process error,maybe,set process Group to empty string(%{GLOBAL} means empty string)
WSGIProcessGroup %{GLOBAL}
# time out time,shen
TimeOut 999999
#http AUTHORIZATION from client,shen
WSGIPassAuthorization On
#old authorization method(not used),SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
#WSGIDaemonProcess,threads(default 15 if not set),
WSGIDaemonProcess django_rest python-home=/opt/cloudera/anaconda3/envs/env_django python-path=/usr/local/scrsapp_apache/scrs_backend_rest_service threads=20,lang=en_US.UTF-8
WSGIProcessGroup django_rest
#static files,alias name same as setting.py,and must absolute path in py file,shen
Alias /static/ /usr/local/scrsapp_apache/scrs_backend_rest_service/static/
<Directory /usr/local/scrsapp_apache/scrs_backend_rest_service/static>
Require all granted
</Directory>
#script,tag as script
WSGIScriptAlias / /usr/local/scrsapp_apache/scrs_backend_rest_service/scrs_backend_rest_service/wsgi.py process-group=django_rest
<Directory /usr/local/scrsapp_apache/scrs_backend_rest_service/scrs_backend_rest_service>
Require all granted
<Files wsgi.py>
#Require all granted
</Files>
</Directory>
# mod_wsgi end
</VirtualHost>
TimeOut 设置超时时间,我的spark分析需要长时间,所以需要设置timeout,否则运行会出现“Timeout when reading response headers from daemon process”错误。
WSGIPassAuthorization On,设置的token才能在后端候获取到。
从配置文件看出,我的django项目路径为/usr/local/scrs_backend_rest_service,因此,只需要把项目拷贝到这个地方即可,
但切记,拷贝后要设置项目路径权限(很重要),否则出错,权限设置如下:

同时更改用户为apache权限,linux命令如下:
chown -R apache:apache /usr/local/scrsapp_apache/
具体用哪个用户和组可以参考httpd.conf里面,如下:

不同的可能会有不同:

本文详细介绍了如何在Linux环境下使用Apache和mod_wsgi部署Django项目,包括编译mod_wsgi、配置Apache模块、设置WSGI模块、处理超时和授权问题、设置项目路径权限以及解决运行时可能出现的错误。此外,还涉及了SSL证书配置、访问控制、静态文件处理和权限设置等关键步骤。
+mod_wsgi部署&spm=1001.2101.3001.5002&articleId=112359780&d=1&t=3&u=9d14900afc1147a9b5be7de7af2393ba)
1374

被折叠的 条评论
为什么被折叠?



