0

I have deployed a Django Project using 18.04 and before using Apache I was testing using Portal:8000 and it worked perfectly fine when I added Apache and made the required changes I am getting 500 Internal Server Error and in the log I am getting the following 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'

Here is the port 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>

Here is the 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'),
    }
}

Here is the allowed permissions:

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

Update: I have made the following changes but still the same error: in the 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()

in the port 80 config:

#       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

My question: What am I doing wrong and how to fix it. Note that I downloaded Pytohn 3.8 to the server and the Django site was working perfectly fine, not sure what went wrong

A_K
  • 731
  • 3
  • 15
  • 40
  • You might find an answer in https://stackoverflow.com/questions/6454564/target-wsgi-script-cannot-be-loaded-as-python-module – AMG Feb 24 '23 at 03:48

1 Answers1

0

you need to set paths to virt.env and app folders:

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

  1. alternatively set paths in wsgi.py dynamically and remove WSGIPythonHome/WSGIPythonPath from apache *.conf:

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

Razenstein
  • 3,532
  • 2
  • 5
  • 17