使用 ubunto 18.04 Apache 在 Linux 服务器中部署 Django 项目时出现 500 内部服务器错误

问题描述 投票:0回答:1

我已经使用 18.04 部署了一个 Django 项目,在使用 Apache 之前,我使用 Portal:8000 进行了测试,当我添加 Apache 并进行了所需的更改时,它工作得很好 我收到 500 Internal Server Error 并且在日志中我得到以下信息错误

[Fri Feb 24 02:22:13.453312 2023] [wsgi:error] [pid 7610:tid 140621178033920] Traceback (most recent call last):
[Fri Feb 24 02:22:13.453355 2023] [wsgi:error] [pid 7610:tid 140621178033920] File "/home/user/project/project/wsgi.py", line 12, in <module>
[Fri Feb 24 02:22:13.453364 2023] [wsgi:error] [pid 7610:tid 140621178033920] from django.core.wsgi import get_wsgi_application
[Fri Feb 24 02:22:13.453388 2023] [wsgi:error] [pid 7610:tid 140621178033920] ModuleNotFoundError: No module named 'django'
[Fri Feb 24 02:25:31.905130 2023] [wsgi:error] [pid 7610:tid 140621161248512] mod_wsgi (pid=7610): Target WSGI script '/home/user/project/project/wsgi.py' cannot be loaded as Python module.
[Fri Feb 24 02:25:31.906124 2023] [wsgi:error] [pid 7610:tid 140621161248512] mod_wsgi (pid=7610): Exception occurred processing WSGI script '/home/user/project/project/wsgi.py'.
[Fri Feb 24 02:25:31.906647 2023] [wsgi:error] [pid 7610:tid 140621161248512] Traceback (most recent call last):
[Fri Feb 24 02:25:31.906871 2023] [wsgi:error] [pid 7610:tid 140621161248512] File "/home/user/project/project/wsgi.py", line 12, in <module>
[Fri Feb 24 02:25:31.906979 2023] [wsgi:error] [pid 7610:tid 140621161248512] from django.core.wsgi import get_wsgi_application
[Fri Feb 24 02:25:31.907098 2023] [wsgi:error] [pid 7610:tid 140621161248512] ModuleNotFoundError: No module named 'django'

这里是80端口:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        Alias /static /home/user/project/static
        <Directory /home/user/project/static>
                Require all granted
        </Directory>

        Alias /media /home/user/project/media
        <Directory /home/user/project/media>
                Require all granted
        </Directory>

        <Directory /home/user/project/project>
                <Files wsgi.py>
                        Require all granted
                </Files>
        </Directory>

        WSGIScriptAlias / /home/user/project/project/wsgi.py
        WSGIDaemonProcess project python-path=/home/user/project python-home=/home/user/project/venv
        WSGIProcessGroup project

        ServerName server_domain_name_or_IP
        <Directory /var/www/html/>
                AllowOverride All
        </Directory>
</VirtualHost>

这里是setting.py

import os
from pathlib import Path
import json
#from decouple import config


with open('/etc/config.json') as config_file:
    config=json.load(config_file)

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = config['SECRET_KEY']
............................................

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / config.get('DATABASE_NAME'),
        #'NAME': BASE_DIR / config('DATABASE_NAME'),
    }
}

这是允许的权限:

total 3120
drwxrwxr-x 12 user www-data    4096 Feb 23 04:52 .
drwxr-xr-x  7 user user     4096 Feb 22 04:29 ..
drwxr-xr-x  4 user user     4096 Feb 22 03:55 api
-rw-rw-r--  1 user www-data  561152 Feb 23 04:52 db.sqlite3
-rw-rw-r--  1 user user  2574273 Feb 23 04:22 get-pip.py
drwxr-xr-x  7 user user     4096 Feb 22 03:55 .git
drwxr-xr-x  3 user user     4096 Feb 24 02:38 project
drwxr-xr-x  3 user user     4096 Feb 22 03:55 .idea
-rwxr-xr-x  1 user user      685 Feb 22 03:55 manage.py
drwxrwxr-x  2 user www-data    4096 Feb 22 03:55 media
drwxr-xr-x  6 user user     4096 Feb 22 03:55 my_project
-rw-r--r--  1 user user      661 Feb 22 03:55 requirements.txt
drwxr-xr-x  8 user user     4096 Feb 23 04:42 static
drwxr-xr-x  5 user user     4096 Feb 22 03:55 tac
drwxr-xr-x  5 user user     4096 Feb 22 03:55 users
drwxrwxr-x  6 user user     4096 Feb 22 04:00 venv

更新: 我进行了以下更改,但仍然出现相同的错误: 在 wsgi.py

import os
import sys
from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')

# Add individual virtual environment packages at the end of sys.path; global env $
sys.path.append('/home/user/project/venv/lib/python3.8/site-packages')

# Add individual virtual environment packages at the beginning of sys.path; indiv$
sys.path.insert(0, '/home/user/project/venv/lib/python3.8/site-packages')

# Add the path to the 'project' Django project
sys.path.append('/home/user/project')

application = get_wsgi_application()

在80端口配置中:

#       Alias /static /home/user/project/static
#       <Directory /home/user/project/static>
#               Require all granted
#       </Directory>
#       Alias /media /home/user/project/media
#       <Directory /home/user/project/media>
#               Require all granted
#       </Directory>
#       <Directory /home/user/project/project>
#               <Files wsgi.py>
#                       Require all granted
#               </Files>
#       </Directory>
#       WSGIScriptAlias / /home/user/project/project/wsgi.py
#       WSGIDaemonProcess project python-path=/home/user/project:/home/user$
#       WSGIProcessGroup project

我的问题: 我做错了什么以及如何解决它。请注意,我将 Pytohn 3.8 下载到服务器并且 Django 站点运行良好,不确定哪里出了问题

python django linux apache wsgi
1个回答
0
投票

你需要设置 virt.env 和 app 文件夹的路径:

  1. 在httpd.conf中:
WSGIPythonHome "d:/..../django_project/env_folder"
WSGIPythonPath "d:/..../django_project/app_name" 

  1. 或者在 wsgi.py 中动态设置路径并从 apache *.conf 中删除 WSGIPythonHome/WSGIPythonPath:

wsgi.py:


# place this before(!!) any package import that belongs to virtual env

# replacement for WSGIPythonHome "d:/..../django_project/env_folder"
# choose one:
sys.path.append('d:/.../env_folder/lib/site-packages')              # add individual virt.environment packages at the end of sys.path;  global env packages have prio
sys.path.insert(0,'d:/.../env_folder/lib/site-packages')            # add individual virt.environment packages at the beginning of sys.path;  indiv. virt.env packages have prio over global env
    
# replacement   WSGIPythonPath "d:/..../django_project/app_name"    
sys.path.append('d:/.../django_project/app_name')                   # add indiv. app folder to search path      

# after that the other wsgi.py stuff like .. 
from django.core.wsgi import get_wsgi_application

© www.soinside.com 2019 - 2024. All rights reserved.