首页 > 解决方案 > 如何使用 Apache2 和 mod_wsgi 在 CentOS 上部署 django API Rest

问题描述

我们正在尝试使用 Apache2 和 mod_wsgi 在 CentOS 服务器上部署 django api rest。django 应用程序只是将一个条目保存到 MySQL 数据库(post)中,并将信息作为 json 返回(get)。

关于 Django 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 的路径:

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

欢迎任何帮助。

标签: django-rest-frameworkcentosapache2mod-wsgi

解决方案


推荐阅读