By carmada
Hi Guys,
I have a site with a set of sub-domains. When I visit one of those sub-domains including the actual domain, it sometimes shows an Internal Server Error on the browser and when I check the apache error.log it tells that it throws an ImportError :
[Tue Oct 11 06:47:10.999837 2016] [:error] [pid 13114] [client 0.0.0.0:58735] mod_wsgi (pid=13114): Target WSGI script '/var/www/html/api.ai-labs.co/ai_labs_apps/wsgi.py' cannot be loaded as Python module.
[Tue Oct 11 06:47:11.000377 2016] [:error] [pid 13114] [client 0.0.0.0:58735] mod_wsgi (pid=13114): Exception occurred processing WSGI script '/var/www/html/api.ai-labs.co/ai_labs_apps/wsgi.py'.
[Tue Oct 11 06:47:11.000478 2016] [:error] [pid 13114] [client 0.0.0.0:58735] Traceback (most recent call last):
[Tue Oct 11 06:47:11.000552 2016] [:error] [pid 13114] [client 0.0.0.0:58735] File "/var/www/html/api.ai-labs.co/ai_labs_apps/wsgi.py", line 27, in <module>
[Tue Oct 11 06:47:11.000675 2016] [:error] [pid 13114] [client 0.0.0.0:58735] application = get_wsgi_application()
[Tue Oct 11 06:47:11.000737 2016] [:error] [pid 13114] [client 0.0.0.0:58735] File "/usr/local/lib/python2.7/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application
[Tue Oct 11 06:47:11.000853 2016] [:error] [pid 13114] [client 0.0.0.0:58735] django.setup()
[Tue Oct 11 06:47:11.000913 2016] [:error] [pid 13114] [client 0.0.0.0:58735] File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 17, in setup
[Tue Oct 11 06:47:11.001017 2016] [:error] [pid 13114] [client 0.0.0.0:58735] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Tue Oct 11 06:47:11.001076 2016] [:error] [pid 13114] [client 0.0.0.0:58735] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 55, in __getattr__
[Tue Oct 11 06:47:11.001269 2016] [:error] [pid 13114] [client 0.0.0.0:58735] self._setup(name)
[Tue Oct 11 06:47:11.001330 2016] [:error] [pid 13114] [client 0.0.0.0:58735] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 43, in _setup
[Tue Oct 11 06:47:11.001395 2016] [:error] [pid 13114] [client 0.0.0.0:58735] self._wrapped = Settings(settings_module)
[Tue Oct 11 06:47:11.001450 2016] [:error] [pid 13114] [client 0.0.0.0:58735] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 99, in __init__
[Tue Oct 11 06:47:11.001526 2016] [:error] [pid 13114] [client 0.0.0.0:58735] mod = importlib.import_module(self.SETTINGS_MODULE)
[Tue Oct 11 06:47:11.001581 2016] [:error] [pid 13114] [client 0.0.0.0:58735] File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
[Tue Oct 11 06:47:11.001689 2016] [:error] [pid 13114] [client 0.0.0.0:58735] __import__(name)
[Tue Oct 11 06:47:11.001765 2016] [:error] [pid 13114] [client 0.0.0.0:58735] ImportError: No module named project.settings
I’ll just post the files of the two out of five applications because I really can’t figure out the error and I’m very bad at deploying to the actual server
1st application: accounts This is the 1st application’s apache2/sites-available/accounts.ai-labs.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName accounts.ai-labs.co
# ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/accounts.ai-labs.co/project
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
<Directory /var/www/html/accounts.ai-labs.co/project>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Alias /static /var/www/html/accounts.ai-labs.co/project/static
<Directory /var/www/html/accounts.ai-labs.co/project/static>
Require all granted
</Directory>
Alias /static /var/www/html/accounts.ai-labs.co/project/media
<Directory /var/www/html/accounts.ai-labs.co/project/media>
Require all granted
</Directory>
<Directory /var/www/html/accounts.ai-labs.co/project/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /var/www/html/accounts.ai-labs.co/project/project/wsgi.py
</VirtualHost>
This is the 1st application’s wsgi.py
"""
WSGI config for project project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/
"""
import os
import sys
import site
site.addsitedir('/var/www/html/accounts.ai-labs.co/lib/python2.7/site-packages')
sys.path.append('/var/www/html/accounts.ai-labs.co/project')
sys.path.append('/var/www/html/accounts.ai-labs.co/project/project')
from django.core.wsgi import get_wsgi_application
from django.conf import settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
activate_env=os.path.expanduser("/var/www/html/accounts.ai-labs.co/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))
application = get_wsgi_application()
2nd application: blogs This is the 2nd application’s apache2/sites-available/blogs.ai-labs.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName blogs.ai-labs.co
# ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/blogs.ai-labs.co
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
<Directory /var/www/html/blogs.ai-labs.co>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Alias /static /var/www/html/blogs.ai-labs.co/static
<Directory /var/www/html/blogs.ai-labs.co/static>
Require all granted
</Directory>
Alias /static /var/www/html/blogs.ai-labs.co/media
<Directory /var/www/html/blogs.ai-labs.co/media>
Require all granted
</Directory>
<Directory /var/www/html/blogs.ai-labs.co/ai_labs_blogs>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /var/www/html/blogs.ai-labs.co/ai_labs_blogs/wsgi.py
</VirtualHost>
This is the 2nd application’s wsgi.py
"""
WSGI config for ai_labs_blogs project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
"""
import os
import sys
import site
site.addsitedir('/var/www/html/.virtualenvs_copy/ai-labs-website-pure-django/local/lib/python2.7/site-packages')
sys.path.append('/var/www/html/blogs.ai-labs.co')
sys.path.append('/var/www/html/blogs.ai-labs.co/ai_labs_blogs')
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ai_labs_blogs.settings")
activate_env=os.path.expanduser("/var/www/html/.virtualenvs_copy/ai-labs-website-pure-django/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))
application = get_wsgi_application()
How do I resolve this?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
There are a couple possible solutions for this issue. In general this type of error is related to the directory path wsgi is using and can be corrected in some cases by renaming a folder and in others by adding a line (as mentioned in the link) to your wsgi.py to manually set the path.
Heya,
The error message you’re seeing indicates an issue with the DJANGO_SETTINGS_MODULE not being found, specifically No module named project.settings. This can happen due to incorrect paths or environment setup in your WSGI configuration or Apache settings
Make sure your WSGI script is activating the correct virtual environment.
In the first application, you’re using /var/www/html/accounts.ai-labs.co/bin/activate_this.py to activate the environment. Similarly, in the second application, you have a different path: /var/www/html/.virtualenvs_copy/ai-labs-website-pure-django/bin/activate_this.py.
Ensure that these virtual environments exist and are accessible by Apache. If these paths are incorrect or the environments are not set up properly, the modules may not be found, causing the error.
Verify that the virtual environment has the required Python packages installed, including Django.
In both your wsgi.py files, you’re appending paths to sys.path:
sys.path.append('/var/www/html/accounts.ai-labs.co/project')
sys.path.append('/var/www/html/accounts.ai-labs.co/project/project')
And:
sys.path.append('/var/www/html/blogs.ai-labs.co')
sys.path.append('/var/www/html/blogs.ai-labs.co/ai_labs_blogs')
Ensure that the paths you’re adding are correct, and the Django project is located in those directories. If any of these directories are incorrect or if there are typos, Python will not be able to find the project.settings module.
The issue could also be related to how Apache is configured to handle WSGI requests. Review your accounts.ai-labs.conf and blogs.ai-labs.conf files to ensure that:
wsgi.py file.wsgi.py file include Require all granted to allow access.For example:
<Directory /var/www/html/accounts.ai-labs.co/project/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Since you already have access to Apache’s error logs, ensure you’re checking the logs for both the web server and WSGI errors. Look for any permission errors, missing files, or other issues that might be causing the ImportError.
To ensure that your virtual environment is working correctly, SSH into the server and activate the virtual environment manually:
source /var/www/html/accounts.ai-labs.co/bin/activate
Then, try running Django commands like python manage.py runserver to see if the environment is correctly set up.
Ensure that the Apache user (typically www-data or apache) has read access to the virtual environment and project directories. You may need to adjust file permissions:
sudo chown -R www-data:www-data /var/www/html/
sudo chmod -R 755 /var/www/html/
After making changes, restart Apache to ensure that all configuration changes are applied:
sudo service apache2 restart
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.