Skip to content

Running Your Instance Properly

Andrew Grant edited this page Feb 12, 2026 · 7 revisions

The previous steps demonstrate how to run the openbench using the django webserver, on localhost, with sqlite3 as the database. In practice, django is not intended to run the web-server, and instead something like Gunicorn, sitting behind a nginx reverse-proxy, is. Additionally, sqlite3 does not support concurrent database access, and is really not adequate. We prefer to use MySQL.

Step #1: Preparing a MySQL Database

# Get the python package. You can try simply pip3 install mysqlclient, but that likely will fail
sudo apt-get install python3-dev default-libmysqlclient-dev
sudo MYSQLCLIENT_CFLAGS="-I/usr/include/mysql" MYSQLCLIENT_LDFLAGS="-L/usr/lib/x86_64-linux-gnu -lmysqlclient" pip3 install mysqlclient

# When prompted, you should disable non-localhost access to the database
sudo apt install mysql-server
sudo mysql_secure_installation
sudo systemctl start mysql
sudo systemctl enable mysql

# Set a root password, if you were not prompted during mysql_secure_installation
sudo mysql
> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '<password>';
> FLUSH PRIVILEGES;
> exit;

# Finally, create the Database
mysql -u root -p
> CREATE DATABASE openbench;
> exit;

Next, you need to modify the contents of OpenSite/settings.py. You do not want to commit these changes. If you want to be able to commit the changes, you might consider an ENV file that contains this information, that you load in the python code.

DATABASES = {
    'default': {
        'ENGINE'  : 'django.db.backends.mysql',
        'NAME'    : 'openbench',
        'USER'    : 'root',
        'PASSWORD': '<password>',
        'HOST'    : 'localhost',  # Change to your MySQL server's host, if you modified it
        'PORT'    : '3306',       # Change to your MySQL server's port, if you modified it
    }
}
# Fully prepare the database, using the migrations in the Repo
# You should never be running makemigrations as a general OpenBench instance runner

python3 manage.py migrate
python3 manage.py migrate --run-syncdb

Step 2: Setting up Nginx as a reverse Proxy

sudo apt install nginx
sudo vi /etc/nginx/sites-available/openbench

Note that this is on port 80 (http) for now. This will get updated for us after we create our certs.

server {
    listen 80;
    server_name example.com; # <--- Your domain here

    client_max_body_size 250M; # Allow uploading 250Mb Network files

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
# Enable the site; test the config; reload nginx
sudo ln -s /etc/nginx/sites-available/openbench /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Step 3: Running with Gunicorn, and Obtaining Certificates

# Run the server; and keep it running for the next steps
pip3 install gunicorn
gunicorn OpenSite.wsgi:application --bind 127.0.0.1:8000 --workers 3
# This will automatically update your nginx config for OpenBench
sudo certbot --nginx -d example.com

At this point, your nginx config should look something like the one below. You should now be able to access the site over https. At this point, the only thing left that you might need to do is open ports 80 or 443.

server {
    server_name example.com;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    client_max_body_size 250M;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name example.com;
    return 404; # managed by Certbot
}

Step 4: Going forward

At this point, you should have the server running whenever you fire up the Gunicorn command. And your clients should be connecting over HTTPs, only utilizing the HTTP redirect for simple web browsing. At some point, your certs will expire, but the certbot steps will have created a cron-job to renew them. Hopefully that all works.

As a general user now, you might just continue to merge AndyGrant/OpenBench/master into your own repo now and again. That is annoying because the Config/config.json will change, and you'll have to manually handle that. Aside from that, the only steps you should need to do are a python3 manage.py migrate everytime you sync with master, in case new database migrations have been created. You'll want to take the server offline to do this. It is a good idea to backup your database every now and again. That can be done very easily, by dumping the contents to a .json file via python3 manage.py dumpdata > backup.json.

Also, some general maintenance, there are a number of models in the database that don't actually matter. You are free to delete all Result, Machine, LogEvent, and PGN objects whenever you like. That can be done with the following code snippet. You can follow this up by deleting all event* files in Media/.

python3 manage.py shell
>>> from OpenBench.models import *
>>> Result.objects.all().delete()
>>> Machine.objects.all().delete()
>>> LogEvent.objects.all().delete()
>>> PGN.objects.all().delete()

Clone this wiki locally