django-rest-framework - 如何使用 Apache2 和 mod_wsgi 在 CentOS 上部署 django API Rest
问题描述
我们正在尝试使用 Apache2 和 mod_wsgi 在 CentOS 服务器上部署 django api rest。django 应用程序只是将一个条目保存到 MySQL 数据库(post)中,并将信息作为 json 返回(get)。
关于 Django API:
- 使用 Postman 在本地进行测试可以正常工作。
- 与服务器上的数据库(MySQL)的连接正在工作(makemigrations、migrate 和 runserver)
- 在服务器中,django 项目位于:/proyecto/api/
django项目在服务端的路径和结构:
/proyecto/api/
- venv
- bin
- activate_this.py
- lib64
- python3.6
- site-packages
- src
- djangoProject
- wsgi.py
- settings.py
- djangoApp
- manage.py
WSGI.py:execfile 函数已根据 python 文档进行了更改。
import os, sys
from django.core.wsgi import get_wsgi_application
sys.path.append('/proyecto/api/src')
activate_this = '/proyecto/api/venv/bin/activate_this.py'
exec(open(activate_this).read())
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangoProject.settings')
application = get_wsgi_application()
SETTINGS.py:修改已将 debug 变为 false,并在 Allowed Host 中包含我们将要使用的“url.com”。
关于虚拟主机,httpd.conf 在 /etc/apache2/conf/httpd.conf 下,这个文件是每次 apache 重启时自动生成的,所以 djangoProject.conf 已经包含在文件夹 /etc/apache2/conf 中。 modules.d 因为这个文件夹会自动包含在 httpd.conf 中。
httpd.conf 的一部分
Include "/etc/apache2/conf.modules.d/*.conf"
# Administrator locations for safely altering httpd.conf
Include "/etc/apache2/conf.d/includes/pre_main_global.conf"
# Major Version Specific
Include "/etc/apache2/conf.d/includes/pre_main_2.conf"
配置虚拟主机的 djangoProject.conf 位于路径 /etc/apache2/conf.modules.d/ 下:
LoadModule wsgi_module /usr/local/lib64/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so
<VirtualHost *:80>
ServerName url.com
ServerAdmin info@url.com
LogLevel warn
DocumentRoot /proyecto/api/src
WSGIPassAuthorization On
WSGIScriptAlias / /proyecto/api/src/djangoProject/wsgi.py
WSGIDaemonProcess djangoapi python-path=/proyecto/api/src:/proyecto/api/venv/lib/python3.6/site-packages
WSGIProcessGroup djangoapi
<Directory /proyecto/api/src/djangoProject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
ErrorLog "/proyecto/api/apacheapilog"
CustomLog "/proyecto/api/apacheapilog" common
</VirtualHost>
因此,直到这里,如果我们重新启动 httpd 和 apache2(>>systemctl restart httpd,>>apachectl restart)它不会显示任何错误,在包括第一行(LoadModule wsgi_module ...)之前,它会返回错误。但是在使用 postman 进行测试时,主 url 返回一个 html 演示,以及 django 应用的 get 和 post 的路径:
在本地测试:localhost/api/get1 -> 返回一个 json,按预期工作。
在 prod 中进行测试:https ://url.com/ -> 返回一个 html 演示。
在 prod 中进行测试:https ://url.com/api/get1 -> 返回“在此服务器上找不到请求的 URL。并且找不到 404”。
djangoProject.conf 的日志:
[Wed Oct 20 05:51:28.941387 2021] [core:error] [pid 945041:tid 23195523553024] [client 146.148.104.215:22778] AH00124: Request exceeded the limit of it of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debul debug' to get a backtrace.
[Wed Oct 20 05:51:28.947636 2021] [wsgi:error] [pid 945041:tid 23195523553024] [client 146.148.104.215:22778] mod_wsgi (pid=945041): Request data reata read error when proxying data to daemon process: End of file found.
url.com:80 209.141.41.12 - - [20/Oct/2021:05:29:07 -0400] "GET /config/getuser?index=0 HTTP/1.1" 400 143
url.com:80 205.185.120.103 - - [20/Oct/2021:05:38:46 -0400] "GET / HTTP/1.1" 400 143
欢迎任何帮助。
解决方案
推荐阅读
- postgresql - Postgres邮政编码 - 相同英里半径的列表未显示
- asp.net-mvc - React.js 路由路径值不正确
- c++ - 在 Ubuntu 上的 Eclipse IDE 上的 Yocto 插件上未定义对“pthread_create”的引用
- c - 优化简单模板操作,将变量保存到寄存器中
- system-verilog - 如何在系统verilog中为模拟时间赋值
- html - CSS:li 不会在 ul 中居中
- c# - “System.ObjectDisposedException:无法访问计算机视觉 xamarin 应用程序的封闭流
- python-3.6 - Python如何打印值而不是提示
- artificial-intelligence - 如何检测 PDF 中的可视块?
- javascript - 无法使用 senecajs 从另一个服务中获取猫鼬模型