Data plotting Web application, developed at NCAR EOL.
The following is for RedHat systems, such as CentOS or Fedora.
This is the same as step one in setting up a development server. See README-devel.md.
We'll call that $DJROOT. Files for production server at EOL have been put on /var/django:
export DJROOT=/var/django
sudo mkdir $DJROOT
sudo chgrp apache $DJROOT
sudo chmod g+sw $DJROOTAdd yourself to the apache group on the server machine. Once you've done that, the sequence is the same as on a development server:
cd $DJROOT
git clone https://github.com/ncareol/ncharts.git
cd nchartsSee README-devel.md for instructions on setting up a pipenv virtual environment with the appropriate packages installed.
You will then need to set group read/execute permissions on the virtual environment so that the datavis user can use it in production:
chmod -R g+rx .venv/This is the same as in setting up a development server. See README-devel.md, if postgres is needed for the RAF database backend.
Production settings are set and managed in datavis/settings/production.py. DEBUG should be set to False, as the Django docs warn in several places that using DEBUG = True on a production server exposed to the WWW is a security hole.
Create and set permissions on LOG_DIR, VAR_RUN_DIR and VAR_LIB_DIR, per their values set in datavis/settings/production.py:
sudo mkdir /var/log/django
sudo chgrp datavis /var/log/django
sudo chmod g+sw /var/log/django
sudo mkdir /run/django
sudo chgrp apache /run/django
sudo chmod g+sw /run/django
sudo mkdir /var/lib/django
sudo chgrp datavis /var/lib/django
sudo chmod g+sw /var/lib/djangoConfigure the DATABASES in datavis/settings/default.py as discussed in README-devel.md.
A Django SECRET_KEY must be assigned via the EOL_DATAVIS_SECRET_KEY environment variable. To generate a new SECRET_KEY:
key=$(python3 -c 'import random; import string; print("".join([random.SystemRandom().choice(string.digits + string.ascii_letters + string.punctuation) for i in range(100)]))')
export EOL_DATAVIS_SECRET_KEY=$keyThe key can be passed to gunicorn from systemd by adding a .conf service file to /etc/systemd/system/gunicorn.service.d/, e.g. datavis-secret-key.conf:
[Service]
Environment="EOL_DATAVIS_SECRET_KEY=abc-123-CHANGE-ME"
After updating the .conf service file, systemd will need to have its daemon reloaded and gunicorn will need to be restarted:
sudo systemctl daemon-reload
sudo systemctl restart gunicornThis also runs the django migration command, which should also handle the situation when one of the models changes, or is added or deleted:
cd $DJROOT/ncharts
./create_sqlitedb.shYou will be prompted to enter an administrator's user name, email and password. You can use your own user name and email address. If the server will be exposed to the internet, you should enter a secure password, which should not match other passwords.
Migrations in django are a bit complicated. If the above script fails you may have to reset the migration history for ncharts:
rm db.sqlite3
rm -rf ncharts/migrationsThen run the create script again.
If using a postgres databse, you will need to run
create_pgdb.shinstead ofcreate_sqlitedb.shto create a database, and rundelete_pgdb.shinstead of deleting the sqlite file.
./load_db.shTo fetch the static files of the supporting software used by ncharts, such as jquery, bootstrap and highcharts do:
./get_static_files.shThe files will be written to $DJROOT/ncharts/static/ncharts.
Then on a production server, execute the static.sh shell script:
./static.shThis shell script executes the django collectstatic command to find the static files in the ncharts directory, as well as static files in python site-packages, and copies them to BASE_DIR/static.
On a production server, the root files go in BASE_DIR/static, which is the same as $DJROOT/static. See datavis/settings/default.py:
STATIC_ROOT = os.path.join(BASE_DIR,'static')On a production server, static.sh must be run every time ncharts/static/ncharts/jslib/ncharts.js is changed on the server.
To see what static files are needed for ncharts, see the <script> tags in ncharts/templates/ncharts/base.html.
The memory caching in django has been configured to use the memcached daemon, and a unix socket. The location of the unix socket is specified as CACHES['LOCATION'] in datavis/settings/production.py:
'LOCATION': 'unix:' + os.path.join(VAR_RUN_DIR,'django_memcached.sock'),See above for creating and setting permissions on VAR_RUN_DIR. To setup memcached, do:
# Configure system to create /run/django on each boot
sudo cp usr/lib/tmpfiles.d/django.conf /usr/lib/tmpfiles.d
systemd-tmpfiles --create /usr/lib/tmpfiles.d/django.conf
sudo cp etc/[datavis-dev or datavis]/systemd/system/memcached_django.service /etc/systemd/system
sudo systemctl daemon-reload
sudo systemctl enable memcached_django.service
sudo systemctl start memcached_django.serviceWe are using gunicorn to serve ncharts, so the Apache server will just forward requests to gunicorn. Gunicorn has been installed into the virtual environment when it was created. Add the service file to systemd and start the service:
sudo cp etc/[datavis-dev or datavis]/systemd/system/gunicorn.service /etc/systemd/system
sudo systemctl daemon-reload
sudo systemctl enable gunicorn.service
sudo systemctl start gunicorn.serviceYou will have to add the secret key .conf file to /etc/systemd/system/gunicorn.service.d as described above.
Install the httpd configuration files:
sudo cp /etc/httpd /etc/httpd.orig
sudo cp -r etc/[datavis or datavis-dev]/httpd /etcThe httpd configuration file that sets up the vhost for datavis is etc/datavis/httpd/conf/vhosts/datavis.conf, which is installed to /etc/httpd/conf/vhosts. The same configuration file exists for datavis-dev.
Tweak the umask of the systemd service, so that apache group members can read/write the log files:
sudo mkdir /etc/systemd/system/httpd.service.d
cat << EOD > /tmp/umask.conf
[Service]
UMask=0007
EOD
sudo cp /tmp/umask.conf /etc/systemd/system/httpd.service.d
sudo systemctl daemon-reloadSee above for creating and setting permissions on LOG_DIR.
Enable and start httpd:
sudo systemctl enable httpd.service
sudo systemctl start httpd.serviceReplace the base index.html of the server with a page that uses an HTML redirect to ncharts.
sudo cp var/[eol-datavis-9 or eol-datavis-dev-9]/www/html/index.html /var/www/htmlInstall crontab.datavis as the datavis user on the server:
sudo su - datavis -c "crontab /var/django/ncharts/crontab.datavis"This script clears expired sessions and unattached ClientState objects from the ncharts database once a week.