Error while uploading Django project to a Apache s

2019-08-14 02:38发布

问题:

I created a droplet(cloud server) on DigitalOcean and with no-ip.com I gave it the hostname - project.ddns.net.By ssh(ing) into the droplet I installed pip and virtualenv.

Inside /var/www/ I created a virtualenv and cloned the repository from github of my project.The directory struture is -

project_revamped  (root of the project)
->requirements
  ->base.txt
  ->dev.txt
->project (django project)
   ->static
   ->media
   ->apps (folder which contains apps)
   ->manage.py
   ->project
      ->urls.py
      ->settings
          ->base.py
          ->dev.py  

By following the official Django documentation I created httpd.conf in the /etc/apache2 path and included it in apache2.conf.

My httpd.confs reads as this -

WSGIScriptAlias / /var/www/project_revamped/project/project/wsgi.py
WSGIPythonPath     /var/www/project_revamped/project:/var/www/.virtualenvs/projectenv/local/l    ib/python2.7/site-packages
<Directory /var/www/project_revamped/project/project>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

and my wsgi.py reads as this -

import os
import sys

#Add the app's directory to the python path
sys.path.append('/var/www/project_revamped/project')
sys.path.append('/var/www/project_revamped/project/project')

os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings.dev'

#Activate your virtualenv
activate_env =     os.path.expanduser('/var/www/.virtualenvs/typesetenv/bin/activate_this.py')
execfile(activate_env, dict(__file__=activate_env))

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()

After changing the files I finally gave the commands - service apache2 reload service apache2 restart

However after doing these things right the corresponding ip says there is some problem the server and sends 500 error.I guess the problem is somewhere in my configuration because apache server was responding working fine.After I include django project with it the problem starts.

After checking the error logs I found these error messages -

mod_wsgi (pid=29458): Target WSGI script '/var/www/project_revamped/project/project/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=29458): Exception occurred processing WSGI script '/var/www/project_revamped/project/project/wsgi.py'.
 File "/var/www/project_revamped/project/project/wsgi.py", line 28, in     <module>
[:error] [pid 29458:tid 140073924572928] [client 103.16.70.147:33613]     application = get_wsgi_application()

Can anybody please help me here in the configuration? I am stuck in this for past 2 days and every different article on the internet tells the different story.

回答1:

add this line to you wsgi.py file : sys.path.append('/var/www/project_revamped/project') before os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings.dev' and check if it works.



回答2:

You may have a look at my VirtualHost configuration for Apache with mod_wsgi. This template can be automatically filled during project-creation, but you may be able to fill/replace variables and strip it down to your needs:

<VirtualHost *:80>
    # This is name based virtual hosting. So place an appropriate server name
    #   here. Example: django.devsrv.local
    ServerName  [[SERVER_NAME]]
    ServerAdmin webmaster@localhost

    # This alias makes serving static files possible.
    #   Please note, that this is geared to our settings/common.py
    #   In production environment, you will propably adjust this!
    Alias /static/  {{ project_directory }}/run/static/

    # This alias makes serving media files possible.
    #   Please note, that this is geared to our settings/common.py
    #   In production environment, you will propably adjust this!
    Alias /media/  {{ project_directory }}/run/media/

    # Insert the full path to the wsgi.py-file here
    WSGIScriptAlias /   {{ project_directory }}/{{ project_name }}/wsgi.py

    # PROCESS_NAME specifies a distinct name of this process
    #   see: https://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIDaemonProcess
    # PATH/TO/PROJECT_ROOT is the full path to your project's root directory, 
    #   containing your project files
    # PATH/TO/VIRTUALENV/ROOT: If you are using a virtualenv specify the full
    #   path to its directory.
    #   Generally you must specify the path to Python's site-packages.
    WSGIDaemonProcess   {{ project_name }}  python-path={{ project_directory }}:{{ project_directory }}/../lib/python2.7/site-packages

    # PROCESS_GROUP specifies a distinct name for the process group
    #   see: https://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIProcessGroup
    WSGIProcessGroup    {{ project_name }}

    # Serving static files from this directory
    #   Please note, that this is geared to our settings/common.py
    #   In production environment, you will propably adjust this!
    <Directory {{ project_directory }}/run/static>
        Options -Indexes
        Order deny,allow
        Allow from all
    </Directory>

    # Serving media files from this directory
    #   Please note, that this is geared to our settings/common.py
    #   In production environment, you will propably adjust this!
    <Directory {{ project_directory }}/run/media>
        Options -Indexes
        Order deny,allow
        Allow from all
    </Directory>

    LogLevel warn

    # PROJECT_NAME is used to seperate the log files of this application
    ErrorLog    ${APACHE_LOG_DIR}/{{ project_name }}_error.log
    CustomLog   ${APACHE_LOG_DIR}/{{ project_name }}_access.log combined
</VirtualHost>

You may see my project-skeleton on GitHub and here is the documentation of the Apache2-conf on RTD.org