Error deploying Django Application - python

I am trying to deploy my first Django application on a server with Ubuntu 14.04.1 LTS.
I installed apache and something else:
apt-get install apache2-mpm-prefork apache2-utils libexpat1 ssl-cert
Then I installed wsgi module:
apt-get install libapache2-mod-wsgi
I edited the ports.conf to listen on port 8080, as I already have another server on port 80 where I host html+js+css resources (they are correctly available).
After this preparation step, I moved my Django application on the new Apache server, specifically into the html directory. Then edited the apache2.conf file by adding the following:
WSGIScriptAlias / /var/www/html/MyProjectName/wsgi.py
WSGIPythonPath /var/www/html/MyProjectName
<Directory /var/www/html/MyProjectName>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
The rest of configuration directives is the default I get after installing Apache with the given commands.
Then, I noticed that in the mods-available folder, I had the wsgi module, but not in the mods-enabled folder, so I created a symbolink link into this last folder.
When I start Apache, if I open localhost:8080, I get an internal server error. Accessing the error.log file, I see the following:
[Mon Mar 09 16:38:20.730865 2015] [mpm_event:notice] [pid 24379:tid 140525609281408] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Mon Mar 09 16:38:20.731257 2015] [core:notice] [pid 24379:tid 140525609281408] AH00094: Command line: '/usr/sbin/apache2'
[Mon Mar 09 16:38:28.673948 2015] [:error] [pid 24383:tid 140525514131200] [client 127.0.0.1:41038] mod_wsgi (pid=24383): Target WSGI script '/var/www/html/MyProjectName/wsgi.py' cannot be loaded as Python module.
[Mon Mar 09 16:38:28.674120 2015] [:error] [pid 24383:tid 140525514131200] [client 127.0.0.1:41038] mod_wsgi (pid=24383): Exception occurred processing WSGI script '/var/www/html/MyProjectName/wsgi.py'.
[Mon Mar 09 16:38:28.674240 2015] [:error] [pid 24383:tid 140525514131200] [client 127.0.0.1:41038] Traceback (most recent call last):
[Mon Mar 09 16:38:28.674299 2015] [:error] [pid 24383:tid 140525514131200] [client 127.0.0.1:41038] File "/var/www/html/MyProjectName/wsgi.py", line 13, in <module>
[Mon Mar 09 16:38:28.674438 2015] [:error] [pid 24383:tid 140525514131200] [client 127.0.0.1:41038] from django.core.wsgi import get_wsgi_application
[Mon Mar 09 16:38:28.674480 2015] [:error] [pid 24383:tid 140525514131200] [client 127.0.0.1:41038] ImportError: No module named django.core.wsgi
Where do I start understanding the problem?
My understanding is that somehow the Apache WSGI module is not working.
For completeness, the wsgi.py file I am pointing at looks like this:
import os
import sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MyProjectName.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
sys.path.append('/var/www/')
sys.path.append('/var/www/html/')
sys.path.append('/var/www/html/MyProjectName')
The settings file is as follow:
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
SECRET_KEY = '<some_key_here>'
DEBUG = False
TEMPLATE_DEBUG = False
ALLOWED_HOSTS = []
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'anothercustomapp',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'MyProjectName.urls'
WSGI_APPLICATION = 'MyProjectName.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'MyDatabaseName',
'USER' : '<username>',
'PASSWORD' : '<password>',
}
}
SERIALIZATION_MODULES = {
'json': 'wadofstuff.django.serializers.custom_json'
}
LANGUAGE_CODE = 'it-IT'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
At the path /var/www/html/ I have the following structure:
A folder called MyProjectName, containing the wsgi.py file, as well as a settings.py file, urls.py and an empty init.py
A folder called anothercustomapp, containing urls, models, views and admin python files, other than an empty init.py
A folder called wadostuff, used to serialize to json and containing the same files as anothercustomapp (in terms of names)
================EDIT================
After installing Django and python-mysqldb with the following commands:
apt-get install python-setuptools
easy_install django
apt-get install python-mysqldb
I end up having the following error.log:
[Mon Mar 09 16:52:13.610937 2015] [mpm_event:notice] [pid 24789:tid 139911380785024] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Mon Mar 09 16:52:13.611288 2015] [core:notice] [pid 24789:tid 139911380785024] AH00094: Command line: '/usr/sbin/apache2'
[Mon Mar 09 16:52:18.056083 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] mod_wsgi (pid=24792): Target WSGI script '/var/www/html/MyProjectName/wsgi.py' cannot be loaded as Python module.
[Mon Mar 09 16:52:18.056256 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] mod_wsgi (pid=24792): Exception occurred processing WSGI script '/var/www/html/MyProjectName/wsgi.py'.
[Mon Mar 09 16:52:18.056302 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] Traceback (most recent call last):
[Mon Mar 09 16:52:18.056346 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] File "/var/www/html/MyProjectName/wsgi.py", line 14, in <module>
[Mon Mar 09 16:52:18.056485 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] application = get_wsgi_application()
[Mon Mar 09 16:52:18.056514 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] File "/usr/local/lib/python2.7/dist-packages/Django-1.7.6-py2.7.egg/django/core/wsgi.py", line 14, in get_wsgi_application
[Mon Mar 09 16:52:18.072789 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] django.setup()
[Mon Mar 09 16:52:18.072882 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] File "/usr/local/lib/python2.7/dist-packages/Django-1.7.6-py2.7.egg/django/__init__.py", line 20, in setup
[Mon Mar 09 16:52:18.088060 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Mon Mar 09 16:52:18.088166 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] File "/usr/local/lib/python2.7/dist-packages/Django-1.7.6-py2.7.egg/django/conf/__init__.py", line 46, in __getattr__
[Mon Mar 09 16:52:18.097590 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] self._setup(name)
[Mon Mar 09 16:52:18.097632 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] File "/usr/local/lib/python2.7/dist-packages/Django-1.7.6-py2.7.egg/django/conf/__init__.py", line 42, in _setup
[Mon Mar 09 16:52:18.097692 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] self._wrapped = Settings(settings_module)
[Mon Mar 09 16:52:18.097725 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] File "/usr/local/lib/python2.7/dist-packages/Django-1.7.6-py2.7.egg/django/conf/__init__.py", line 98, in __init__
[Mon Mar 09 16:52:18.097778 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] % (self.SETTINGS_MODULE, e)
[Mon Mar 09 16:52:18.097833 2015] [:error] [pid 24792:tid 139911285634816] [client 127.0.0.1:41213] ImportError: Could not import settings 'MyProjectName.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named MyProjectName.settings

Related

Django Internal Server 500 Error AWS Elastic-Beanstalk issue

How do I fix the Django Internal Server 500 error on an AWS eb deployed application?
Do I need to change my allowed hosts in my base.py file to have the ip address of the ec2 in elasticbeanstalk?
# SECURITY WARNING: keep the secret key used in production secret!
# Raises ImproperlyConfigured exception if SECRET_KEY not in os.environ
SECRET_KEY = env('SECRET_KEY')
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.auth',
'django_admin_bootstrapped',
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
...
STATIC_URL = '/static/'
ALLOWED_HOSTS = []
...
I enabled DEBUG and ran eb config, and eb logs. The eb deploy values are displayed below, and the eb logs values below that. How do you fix the issue that the "No module named django?"
Creating application version archive "app-3e17-161020_112522".
Uploading pronet/app-3e17-161020_112522.zip to S3. This may take a while.
Upload Complete.
INFO: Environment update is starting.
INFO: Deploying new version to instance(s).
INFO: New application version was deployed to running EC2 instances.
INFO: Environment update completed successfully.
Provided below are the output from eb logs.
============= i-0b11de8b8521ea5c0 ==============
-------------------------------------
/var/log/httpd/error_log
-------------------------------------
[Thu Oct 20 17:46:59.414032 2016] [:error] [pid 11268]
[remote 172.31.5.67:41390] Traceback (most recent call last):
[Thu Oct 20 17:46:59.414077 2016] [:error] [pid 11268]
[remote 172.31.5.67:41390] File "/opt/python/current/app/pronet/src/pronet/wsgi.py", line 12, in <module>
[Thu Oct 20 17:46:59.414083 2016] [:error] [pid 11268]
[remote 172.31.5.67:41390] from django.core.wsgi import get_wsgi_application
[Thu Oct 20 17:46:59.414104 2016] [:error] [pid 11268]
[remote 172.31.5.67:41390] ImportError: No module named 'django'
[Thu Oct 20 17:47:00.046644 2016] [:error] [pid 11268]
[remote 172.31.5.67:43182] mod_wsgi (pid=11268): Target WSGI script '/opt/python/current/app/pronet/src/pronet/wsgi.py' cannot be loaded as Python module.
[Thu Oct 20 17:47:00.046704 2016] [:error] [pid 11268]
[remote 172.31.5.67:43182] mod_wsgi (pid=11268): Exception occurred processing WSGI script '/opt/python/current/app/pronet/src/pronet/wsgi.py'.
[Thu Oct 20 17:47:00.046738 2016] [:error] [pid 11268]
[remote 172.31.5.67:43182] Traceback (most recent call last):
[Thu Oct 20 17:47:00.046784 2016] [:error] [pid 11268]
[remote 172.31.5.67:43182] File "/opt/python/current/app/pronet/src/pronet/wsgi.py", line 12, in <module>
[Thu Oct 20 17:47:00.046789 2016] [:error] [pid 11268]
[remote 172.31.5.67:43182] from django.core.wsgi import get_wsgi_application
[Thu Oct 20 17:47:00.046810 2016] [:error] [pid 11268]
[remote 172.31.5.67:43182] ImportError: No module named 'django'
[Thu Oct 20 17:47:01.340552 2016] [:error] [pid 11268]
[remote 172.31.5.67:43182] mod_wsgi (pid=11268): Target WSGI script '/opt/python/current/app/pronet/src/pronet/wsgi.py' cannot be loaded as Python module.
[Thu Oct 20 17:47:01.340603 2016] [:error] [pid 11268]
[remote 172.31.5.67:43182] mod_wsgi (pid=11268): Exception occurred processing WSGI script '/opt/python/current/app/pronet/src/pronet/wsgi.py'.
[Thu Oct 20 17:47:01.340653 2016] [:error] [pid 11268] [remote 172.31.5.67:43182] Traceback (most recent call last):
[Thu Oct 20 17:47:01.340701 2016] [:error] [pid 11268]
[remote 172.31.5.67:43182] File "/opt/python/current/app/pronet/src/pronet/wsgi.py", line 12, in <module>
[Thu Oct 20 17:47:01.340706 2016] [:error] [pid 11268]
[remote 172.31.5.67:43182] from django.core.wsgi import get_wsgi_application
[Thu Oct 20 17:47:01.340728 2016] [:error] [pid 11268]
[remote 172.31.5.67:43182] ImportError: No module named 'django'
link to github project. You can view all the code here
https://github.com/IantheFlyingHawaiian/Pronet
Did you do this?
pip freeze > requirements.txt
If you didn't, AWS will not know that it should install Django and other dependencies on your EC2 instances for your deployment.
In my case error with sqllite
Remove Database settings from settings.py and try deploy
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
And then I configured with AWS RDS.

ImproperlyConfigured: The SECRET_KEY setting must not be empty in Apache and wsgi_mod

I'm trying to deploy Django project on Apache and wsgi_mod in Windows 10. What I did so far:
1. Added following in httpd.conf of Apache conf folder
WSGIScriptAlias / "C:/Bitnami/djangostack-1.10.1-0/apps/django/django_projects/project/egeirn/wsgi.py"
WSGIPythonPath "C:/Bitnami/djangostack-1.10.1-0/apps/django/django_projects/project;C:/Bitnami/djangostack-1.10.1-0/python/Lib/site-packages"
<Directory "C:/Bitnami/djangostack-1.10.1-0/apps/django/django_projects/project/egeirn">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
2. Following is the content of my wsgi.py file:
import os
import sys
from django.core.wsgi import get_wsgi_application
sys.path.append('C:/Bitnami/djangostack-1.10.1-0/apps/django/django_projects/project')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "egeirn.settings")
application = get_wsgi_application()
When I start the Apache server, it gives the following error in logs file:
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] mod_wsgi (pid=916): Target WSGI script 'C:/Bitnami/djangostack-1.10.1-0/apps/django/django_projects/project/egeirn/wsgi.py' cannot be loaded as Python module., referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] mod_wsgi (pid=916): Exception occurred processing WSGI script 'C:/Bitnami/djangostack-1.10.1-0/apps/django/django_projects/project/egeirn/wsgi.py'., referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] Traceback (most recent call last):, referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] File "C:/Bitnami/djangostack-1.10.1-0/apps/django/django_projects/project/egeirn/wsgi.py", line 18, in <module>, referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] application = get_wsgi_application(), referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] File "C:\\Bitnami\\djangostack-1.10.1-0\\apps\\django\\django-1.10.1-py2.7.egg\\django\\core\\wsgi.py", line 13, in get_wsgi_application, referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] django.setup(set_prefix=False), referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] File "C:\\Bitnami\\djangostack-1.10.1-0\\apps\\django\\django-1.10.1-py2.7.egg\\django\\__init__.py", line 22, in setup, referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING), referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] File "C:\\Bitnami\\djangostack-1.10.1-0\\apps\\django\\django-1.10.1-py2.7.egg\\django\\conf\\__init__.py", line 53, in __getattr__, referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] self._setup(name), referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] File "C:\\Bitnami\\djangostack-1.10.1-0\\apps\\django\\django-1.10.1-py2.7.egg\\django\\conf\\__init__.py", line 41, in _setup, referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] self._wrapped = Settings(settings_module), referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] File "C:\\Bitnami\\djangostack-1.10.1-0\\apps\\django\\django-1.10.1-py2.7.egg\\django\\conf\\__init__.py", line 116, in __init__, referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] raise ImproperlyConfigured("The SECRET_KEY setting must not be empty."), referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] ImproperlyConfigured: The SECRET_KEY setting must not be empty., referer: http://localhost:82/
I am using django-environ to read .env file and keep configurations in environment variables. Although I have my SECRET_KEY in the base.py file
I have used it like this in base.py file:
import environ
# three folder back (/project/egeirn/settings/ - 3 = /)
root = environ.Path(__file__) - 3
# set default values and casting
env = environ.Env(DEBUG=(bool, False),)
# reading .env file
environ.Env.read_env(os.path.join(BASE_DIR, '../../config/.env'))
SITE_ROOT = root()
# Raises ImproperlyConfigured exception if SECRET_KEY not in os.environ
SECRET_KEY = env('SECRET_KEY')
This is my .env file:
SECRET_KEY=<MY_SECRET_KEY_HERE>
Please tell me what I am doing wrong here, how come it cannot read the SECRET_KEY?
Thanks

Configuration file for Flask application

I'm new to Python, so please bear with me.
I am attempting to create a file in which to store my configuration settings in a Flask project. However, I seem to be getting errors when I attempt to import the file.
Here's my configuration file (location: app/config.py):
database_uri = 'something here'
secret_key = something here"
And here's where I'm using it (location: app/models.py):
from app import config
...
app.config['SQLALCHEMY_DATABASE_URI'] = config.database_uri
However, I seem to be getting this error when launching the application:
[Sat Aug 08 19:00:15.539773 2015] [:error] [pid 29784] [client 188.183.57.54:64122] mod_wsgi (pid=29784): Target WSGI script '/var/www/pwforum/pwforum.wsgi' cannot be loaded as Python module.
[Sat Aug 08 19:00:15.540014 2015] [:error] [pid 29784] [client 188.183.57.54:64122] mod_wsgi (pid=29784): Exception occurred processing WSGI script '/var/www/pwforum/pwforum.wsgi'.
[Sat Aug 08 19:00:15.540146 2015] [:error] [pid 29784] [client 188.183.57.54:64122] Traceback (most recent call last):
[Sat Aug 08 19:00:15.540250 2015] [:error] [pid 29784] [client 188.183.57.54:64122] File "/var/www/pwforum/pwforum.wsgi", line 7, in <module>
[Sat Aug 08 19:00:15.540448 2015] [:error] [pid 29784] [client 188.183.57.54:64122] from app import app as application
[Sat Aug 08 19:00:15.540537 2015] [:error] [pid 29784] [client 188.183.57.54:64122] File "/var/www/pwforum/app/__init__.py", line 12, in <module>
[Sat Aug 08 19:00:15.540685 2015] [:error] [pid 29784] [client 188.183.57.54:64122] from app import views, models
[Sat Aug 08 19:00:15.540773 2015] [:error] [pid 29784] [client 188.183.57.54:64122] File "/var/www/pwforum/app/views.py", line 3, in <module>
[Sat Aug 08 19:00:15.541061 2015] [:error] [pid 29784] [client 188.183.57.54:64122] from app.models import db, User, Category, Topic, Post
[Sat Aug 08 19:00:15.541154 2015] [:error] [pid 29784] [client 188.183.57.54:64122] File "/var/www/pwforum/app/models.py", line 11, in <module>
[Sat Aug 08 19:00:15.541333 2015] [:error] [pid 29784] [client 188.183.57.54:64122] app.config['SQLALCHEMY_DATABASE_URI'] = config.database_uri
[Sat Aug 08 19:00:15.541413 2015] [:error] [pid 29784] [client 188.183.57.54:64122] AttributeError: 'module' object has no attribute 'database_uri'
Your config file should look like this:
SQLALCHEMY_DATABASE_URI = '<your-db-driver>://<user>:<pw>#<db-url>'
SECRET_KEY = '<your-very-secret-key>'
Then you can do:
app = Flask(__name__)
app.config.from_object('config')

Internal Server Error: Target WSGI script cannot be loaded as Python module AND IOError: [Errno 2] No such file or directory: 'client_secrets.json'

I am trying to run a Linux server configuration for a catalog project. I am using: Apache2, Flask, and SQLAlchemy and I had to install and configure PostgreSQL. The site I am trying to run is the Public IP Address: http://52.27.140.219/. I am happy to provide greater detail. I am new with Linux and many things discussed here, but I believe my work is strong, except for the end problem below.
Important steps in Terminal:
From:
(venv)grader#ip-10-20-8-44:/var/www/catalog/catalog$ sudo service apache2 restart
[sudo] password for grader:
* Restarting web server apache2 [ OK ]
Error Logs
(venv)grader#ip-10-20-8-44:/var/www/catalog/catalog$ sudo tail -20 /var/log/apache2/error.log
[Wed Jul 01 16:51:36.873041 2015] [:error] [pid 14600:tid 140294116747008] [client 73.221.39.5:60704] Traceback (most recent call last):
[Wed Jul 01 16:51:36.873059 2015] [:error] [pid 14600:tid 140294116747008] [client 73.221.39.5:60704] File "/var/www/catalog/catalog.wsgi", line 7, in <module>
[Wed Jul 01 16:51:36.873105 2015] [:error] [pid 14600:tid 140294116747008] [client 73.221.39.5:60704] from catalog import app as application
[Wed Jul 01 16:51:36.873117 2015] [:error] [pid 14600:tid 140294116747008] [client 73.221.39.5:60704] File "/var/www/catalog/catalog/__init__.py", line 35, in <module>
[Wed Jul 01 16:51:36.873257 2015] [:error] [pid 14600:tid 140294116747008] [client 73.221.39.5:60704] open('client_secrets.json', 'r').read())['web']['client_id']
[Wed Jul 01 16:51:36.873279 2015] [:error] [pid 14600:tid 140294116747008] [client 73.221.39.5:60704] IOError: [Errno 2] No such file or directory: 'client_secrets.json'
[Wed Jul 01 16:53:36.405496 2015] [mpm_event:notice] [pid 14596:tid 140294245513088] AH00491: caught SIGTERM, shutting down
[Wed Jul 01 16:53:37.387879 2015] [mpm_event:notice] [pid 14715:tid 140705447798656] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Wed Jul 01 16:53:37.387939 2015] [core:notice] [pid 14715:tid 140705447798656] AH00094: Command line: '/usr/sbin/apache2'
[Wed Jul 01 17:35:23.312436 2015] [mpm_event:notice] [pid 14715:tid 140705447798656] AH00491: caught SIGTERM, shutting down
[Wed Jul 01 17:35:24.360945 2015] [mpm_event:notice] [pid 15120:tid 140719357745024] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Wed Jul 01 17:35:24.361007 2015] [core:notice] [pid 15120:tid 140719357745024] AH00094: Command line: '/usr/sbin/apache2'
[Wed Jul 01 17:35:35.741239 2015] [:error] [pid 15123:tid 140719262549760] [client 73.221.39.5:61118] mod_wsgi (pid=15123): Target WSGI script '/var/www/catalog/catalog.wsgi' cannot be loaded as Python module.
[Wed Jul 01 17:35:35.741269 2015] [:error] [pid 15123:tid 140719262549760] [client 73.221.39.5:61118] mod_wsgi (pid=15123): Exception occurred processing WSGI script '/var/www/catalog/catalog.wsgi'.
[Wed Jul 01 17:35:35.741302 2015] [:error] [pid 15123:tid 140719262549760] [client 73.221.39.5:61118] Traceback (most recent call last):
[Wed Jul 01 17:35:35.741317 2015] [:error] [pid 15123:tid 140719262549760] [client 73.221.39.5:61118] File "/var/www/catalog/catalog.wsgi", line 7, in <module>
[Wed Jul 01 17:35:35.741359 2015] [:error] [pid 15123:tid 140719262549760] [client 73.221.39.5:61118] from catalog import app as application
[Wed Jul 01 17:35:35.741369 2015] [:error] [pid 15123:tid 140719262549760] [client 73.221.39.5:61118] File "/var/www/catalog/catalog/__init__.py", line 35, in <module>
[Wed Jul 01 17:35:35.741483 2015] [:error] [pid 15123:tid 140719262549760] [client 73.221.39.5:61118] open('client_secrets.json', 'r').read())['web']['client_id']
[Wed Jul 01 17:35:35.741501 2015] [:error] [pid 15123:tid 140719262549760] [client 73.221.39.5:61118] IOError: [Errno 2] No such file or directory: 'client_secrets.json'
When I sudo nano /etc/apache2/sites-available/catalog.conf, I have:
WSGIPythonPath /var/www/catalog/catalog/venv/:/var/www/catalog/catalog/v$
<VirtualHost *:80>
ServerName 52.27.140.219
ServerAdmin admin#52.27.140.219
ServerAlias c-73-221-39-5.hsd1.wa.comcast.net
WSGIScriptAlias / /var/www/catalog/catalog.wsgi
<Directory /var/www/catalog/catalog/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/catalog/catalog/static
<Directory /var/www/catalog/catalog/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Don't use a relative path name to the file. Instead construct an absolute path name. This is needed because the current working directory of the process will not be where your code is located. Read:
https://code.google.com/p/modwsgi/wiki/ApplicationIssues#Application_Working_Directory
So as Graham said, eventually, you end up with something like:
APP_PATH = '/var/www/catalog/
CLIENT_ID = json.loads(open(APP_PATH + 'client_secrets.json', 'r').read())['web']['client_id']

How to set environ variables in local development with virtualenv

I am currently trying to publish via ZIP my django app to an AWS elasticbeanstalk.
As I have a local and online development enviroment I would like to make use of the environ variables, that are used anyways by AWS.
For my development environments I am already using virtualenv. Apache runs with wsgi enabled and is supposed to use such environments. Unfortunately, it does not know the environ that I have set in the bin/activate.
Django throws a 500 error, since it cannot connect to the database as it does not have access to the environ vars:
1:53.862779 2015] [wsgi:error] [pid 20639] [remote 127.0.0.1:51896] KeyError: 'RDS_DB_NAME'
If I activate my env and look into the env vars:
(myenv)[bs#debian-gohan:]/var/www/vhosts/mysite $ env | grep RDS
RDS_HOSTNAME=localhost
... and I get the others as well...
My Apache Vhost is:
<VirtualHost *:80>
WSGIDaemonProcess mysite python-path=/home/bs/envs/myenv/bin/python2.7:/home/bs/envs/myenv/lib/python2.7/site-packages
WSGIProcessGroup mysite
WSGIScriptAlias / /var/www/vhosts/main-page/mysite/wsgi.py
Does anyone have an idea why the environ vars are not seen by the apache ?
EDIT 1:
I have tried to use SetEnv in the Apache config, like this:
SetEnv RDS_HOSTNAME "localhost"
Unfortunately, this did not work. I then removed the ProcessGroup and only used a simple WSGIPythonPath. That did not work either.
The error in the apache log remains the same:
[Thu Mar 26 11:30:34.046807 2015] [wsgi:info] [pid 23012] [client 127.0.0.1:55231] mod_wsgi (pid=23012, process='', application='mysite.dbz.dev|'): Loading WSGI script '/var/www/vhosts/main-page/mysite/wsgi.py'.
[Thu Mar 26 11:30:34.178717 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] mod_wsgi (pid=23012): Target WSGI script '/var/www/vhosts/main-page/mysite/wsgi.py' cannot be loaded as Python module.
[Thu Mar 26 11:30:34.178812 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] mod_wsgi (pid=23012): Exception occurred processing WSGI script '/var/www/vhosts/main-page/mysite/wsgi.py'.
[Thu Mar 26 11:30:34.178924 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] Traceback (most recent call last):
[Thu Mar 26 11:30:34.178947 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/var/www/vhosts/main-page/mysite/wsgi.py", line 17, in <module>
[Thu Mar 26 11:30:34.179111 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] application = get_wsgi_application()
[Thu Mar 26 11:30:34.179157 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/usr/local/lib/python2.7/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Thu Mar 26 11:30:34.179319 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] django.setup()
[Thu Mar 26 11:30:34.179363 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 20, in setup
[Thu Mar 26 11:30:34.179606 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Thu Mar 26 11:30:34.179651 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 46, in __getattr__
[Thu Mar 26 11:30:34.179774 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] self._setup(name)
[Thu Mar 26 11:30:34.179789 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 42, in _setup
[Thu Mar 26 11:30:34.179918 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] self._wrapped = Settings(settings_module)
[Thu Mar 26 11:30:34.179959 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 94, in __init__
[Thu Mar 26 11:30:34.180072 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] mod = importlib.import_module(self.SETTINGS_MODULE)
[Thu Mar 26 11:30:34.180112 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
[Thu Mar 26 11:30:34.180220 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] __import__(name)
[Thu Mar 26 11:30:34.180246 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/var/www/vhosts/main-page/mysite/../mysite/settings.py", line 94, in <module>
[Thu Mar 26 11:30:34.180400 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] 'NAME': os.environ['RDS_DB_NAME'],
[Thu Mar 26 11:30:34.180443 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/usr/lib/python2.7/UserDict.py", line 23, in __getitem__
[Thu Mar 26 11:30:34.180613 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] raise KeyError(key)
[Thu Mar 26 11:30:34.180665 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] KeyError: 'RDS_DB_NAME'
Set like this
<VirtualHost hostname:80>
...
SetEnv VARIABLE_NAME variable_value
...
</VirtualHost>
In wsgi script.
os.environ.setdefault("variable", "value")
To override variable,
os.environ['var']="val"

Categories