HenryM
HenryM

Reputation: 5793

Django SECRET_KEY called in os.environ but isn't an environment variable

I've been asked to look at a Django project where the developer has disappeared. I cannot work out how they've set the SECRET_KEY. In settings.py we have SECRET_KEY = os.environ['SECRET_KEY']

It is running on a Ubuntu 14.04.05 server. I understand the code above to mean the SECRET_KEY has been set as an environment setting so in terminal I type printenv to see the environment settings and SECRET_KEY isn't there.

How might it be being set?

EDIT

nginx.conf

user www-data;
worker_processes 1;
pid /run/nginx.pid;

events {
        worker_connections 1024;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##
        client_body_buffer_size 10K;
        client_header_buffer_size 1k;
        client_max_body_size 8m;
        large_client_header_buffers 2 1k;
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        client_body_timeout 12;
        client_header_timeout 12;
        keepalive_timeout 15;
        send_timeout 10;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";


        # gzip_vary on;
        gzip_proxied     expired no-cache no-store private auth;
        gzip_comp_level 2;
        gzip_min_length  1000;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # nginx-naxsi config
        ##
        # Uncomment it if you installed nginx-naxsi
        ##

        #include /etc/nginx/naxsi_core.rules;

        ##
        # nginx-passenger config
        ##
        # Uncomment it if you installed nginx-passenger
        ##

        #passenger_root /usr;
        #passenger_ruby /usr/bin/ruby;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

uwsgi_params

uwsgi_param     QUERY_STRING            $query_string;
uwsgi_param     REQUEST_METHOD          $request_method;
uwsgi_param     CONTENT_TYPE            $content_type;
uwsgi_param     CONTENT_LENGTH          $content_length;

uwsgi_param     REQUEST_URI             $request_uri;
uwsgi_param     PATH_INFO               $document_uri;
uwsgi_param     DOCUMENT_ROOT           $document_root;
uwsgi_param     SERVER_PROTOCOL         $server_protocol;
uwsgi_param     UWSGI_SCHEME            $scheme;

uwsgi_param     REMOTE_ADDR             $remote_addr;
uwsgi_param     REMOTE_PORT             $remote_port;
uwsgi_param     SERVER_PORT             $server_port;
uwsgi_param     SERVER_NAME             $server_name;

Upvotes: 0

Views: 1505

Answers (1)

Abraham Anun
Abraham Anun

Reputation: 11

There is a really simple hack I use: It worked for me, but am still testing different environments. This is how I did it. You set variables when activating your virtual environment.

Just open your bin/activate file that you source for activating the virtual environment and add something like this to the end of it: If your virtual environment folder is, for example, "virtual", then you activate your virtual environment using this command:

source virtual/bin/activate

So, now you know where to locate your "activate" file, which is located in the virtual environment. Open it with a text editor and add the lines below at the end of the file. NOTE: replace the characters between the quotes with your secret key.

export APP_SECRET_KEY="OiJjajJjZXlraGcwMDBrMndtdWJ6NDFsNWt4"
export APP_DEBUG="1"
...

Now everytime you activate the environment (source virtenv/bin/activate), all configuration variables will be set and available.

Upvotes: 1

Related Questions