Published on

Install AskBot on Ubuntu 20.04 (from source code)

AskBot is an open-source question-and-answer forum written in Django and Python. It provides features similar to StackOverflow, including a karma-based system, voting, and content moderation.

In this guide, you’ll install AskBot and deploy with NGINX as a web server, MySQL as a database server, Gunicorn as a Python WSGI HTTP Server on your Ubuntu 20.04.

I assume that you installed LEMP stack on your server, If not see how to Install Linux, Nginx, MySQL, PHP (LEMP stack) on Ubuntu 20.04

Install Dependencies

sudo apt install python3-pip python3-venv libmysqlclient-dev

Create a Database

Log in to MySQL as the root user:

sudo mysql -u root -p

Create a new MySQL user and database. In the example below, askbotdb is the database name, askb0t is the database user, and @skb0iPassword is the database user’s password.

CREATE DATABASE askbotdb CHARACTER SET UTF8;
CREATE USER askb0t@localhost IDENTIFIED BY '@skb0tPassword';
GRANT ALL PRIVILEGES ON askbotdb.* TO askb0t@localhost;
FLUSH PRIVILEGES;
exit

Install AskBot from source code

We need to create a new user for AskBot installation, as we will not be using root user for it. Create a new user named askbot and give that user a new password.

sudo useradd -m -s /bin/bash askbot
sudo passwd askbot
sudo usermod -a -G sudo askbot

Now, log in as the askbot user with the su command, and then install AskBot.

su - askbot

Create a directory to deploy AskBot

cd /home/askbot

Clone source code and install

git clone https://github.com/ASKBOT/askbot-devel.git src
cd src
python3 -m venv pyvenv
source pyvenv/bin/activate
python -m pip install --upgrade pip
pip install mysqlclient gunicorn wheel
pip install -r askbot_requirements.txt
pip uninstall django-appconf==1.0.4
pip install django-appconf==1.0.3
python setup.py install

Setup and Config AskBot

Create folder contains assets and run setup command

cd /home/askbot
mkdir www
cd /home/askbot/www
askbot-setup --force

The terminal will be show, you can do following my config:

Deploying Askbot - Django Q&A forum application
Problems installing? -> please email support@askbot.com

To CANCEL - press Ctr-C.

Enter the Root directory path (relative or absolute).
This directory will contain the Django project's manage.py file.
Press ENTER to use ./askbot_site.
> .
Enter the Project directory name.
Will be a subdirectory within the Root for the settings.py, urls.py files.
Press ENTER to use www.
> .
Select the database engine: 1 - postgresql, 2 - sqlite, 3 - mysql, 4 - oracle.
Type 1/2/3/4, press ENTER to select sqlite
> 3
Enter the database host name
Press ENTER to use empty string (default value).
>
Enter database name. (required)
> askbotdb
Enter database password. (required)
> @skb0tPassword
Enter database user name. (required)
> askb0t
Enter the database port
Press ENTER to use empty string (default value).
>
Enter email of the site admin (required)
> nvtienanh@local.com
Enter name of the site admin (required)
> nvtienanh
Enter the default from email
Press ENTER to use nvtienanh@local.com.
>
Enter the server email
Press ENTER to use nvtienanh@local.com.
>

After run askbot-setup it will generate setting:

root_dir=/home/askbot/www
proj_dir=/home/askbot/www
media_root_dir=/home/askbot/www/upfiles
static_root_dir=/home/askbot/www/static
log_file_path=/home/askbot/www/log/askbot_app.log
logging_settings=None
database_engine=django.db.backends.mysql
database_host=
database_name=askbotdb
database_password=@skb0tPassword
database_settings=None
database_user=askb0t
database_port=
admin_email=nvtienanh@local.com
admin_name=nvtienanh
admin_settings=None
domain_name=None
extra_settings=None
language_code=en
language_name=English
language_settings=None
timezone=UTC
default_from_email=nvtienanh@local.com
server_email=nvtienanh@local.com
email_backend=django.core.mail.backends.smtp.EmailBackend
email_subject_prefix=
email_host_user=
email_host_password=
email_host=
email_port=
email_use_tls=False
caching_settings=None
email_settings=None
secret_key=05501a467809ad145274485a7edcea68694adc3c699eb5d2a15d0e1c853f5e6f

You can see that database_host and database_port are not yet configurated, we will update by modified settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'askbotdb',                      # Or path to database file if using sqlite3.
        'USER': 'askbot',                      # Not used with sqlite3.
        'PASSWORD': '@skb0tPassword',                  # Not used with sqlite3.
        'HOST': 'localhost',
        'PORT': '3306',
        'TEST': {
            'CHARSET': 'utf8mb4_general_ci', # Setting the character set and collation to utf-8
            'COLLATION': '', # is necessary for MySQL tests to work properly.
        }
    }
}

Modify manage.py line 6 from os.environ.setdefault("DJANGO_SETTINGS_MODULE", "www.settings") to os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

Run some initial command for django project:

# Make source python venv is activated in this terminal
python manage.py collectstatic --noinput
python manage.py makemigrations
python manage.py migrate

Turn off the Debug mode in settings.py yto run AskBot in the production environment:

sed -i "s|DEBUG = True|DEBUG = False|" /home/askbot/www/settings.py

Change the URL of the static files from /m/ to /static/

sed -i "s|STATIC_URL = '/m/'|STATIC_URL = '/static/'|" /home/askbot/www/settings.py

Edit /home/askbot/www/wsgi.py

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Create a systemd service for Gunicorn so that it can run as a service. Create the following gunicorn.service file

sudo nano /etc/systemd/system/gunicorn.service

Paste following content to that file:

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=askbot
Group=www-data
WorkingDirectory=/home/askbot/www
Environment="PATH=/home/askbot/src/pyvenv/bin"
ExecStart=/home/askbot/src/pyvenv/bin/gunicorn --workers 3 --bind unix:askbot.sock wsgi:application

[Install]
WantedBy=multi-user.target

Enable and start the Gunicorn service:

sudo systemctl start gunicorn
sudo systemctl enable gunicorn

Restart NGINX and reload the daemon:

sudo systemctl daemon-reload
sudo systemctl restart nginx

Config NGINX

Add new askbot NGINX Server Blocks (Virtual Host) to run AskBot in the production environment (change nvtienanh.local to your domain):

sudo nano /etc/nginx/sites-available/nvtienanh.local

Paste following config:

server {
    listen 80;
    server_name nvtienanh.local;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
		root /home/askbot/www;
    }

    location /upfiles/ {
        root /home/askbot/www;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/askbot/www/askbot.sock;
    }
}

Restart nginx

sudo systemctl restart nginx

The www-data group must have access to AskBot installation directory so that NGINX can serve static files, media files, and access the socket files. Add the askbot to www-data group so that it has the necessary permissions:

sudo usermod -aG www-data askbot

Now you can access askbot at http://nvtienanh.local

Video tutorial