Skip to content

Installation

Marcos Bomfim edited this page Jan 8, 2022 · 34 revisions

Installation

The python version for install nanobrok-server is high python>=3.7 (I recommand this version). If you just want to use the tool, you can install Nanobrok-server in a few different ways according to what you find easiest.

Python

The python version is python3.7 or high.

sudo apt-get install python3.7 python3.7-dev git

Virtualenv

The nanobrok-server can be to install easily using the virtualenv, you should be install the virtualenv your python version.

Installing virtualenv

Installing a virtualenv is done using pip, Python's package manager. follow a google tutorial how to install pip your Operational system (OS).

$ pip3 install virtualenv

Close the nanobrok-server

now, the first thing to do is clone the project from github repository.

git clone https://github.com/P0cL4bs/Nanobrok.git

Next step is create your local enverioment with command bellow:

$ virtualenv -p python3.7 venv

Activating a virtualenv

After creating a virtualenv, we need to activate it so that we can install the necessary project packages. For this, we use the following command:

  • source venv/bin/activate (Linux ou macOS)
  • venv/Scripts/Activate (Windows)

Installing packages

With virtualenv enabled, we can install the necessary packages using the PIP itself:

$ pip install -r requirements.txt

if you got this message error from psycopg2:

ERROR: Could not find a version that satisfies the requirement psycopg2>=2.8.5 (from versions: 2.0.10, 2.0.11, 2.0.12, 2.0.13, 2.0.14, 2.2.0, 2.2.1, 2.2.2, 2.3.0, 2.3.1, 2.3.2, 2.4, 2.4.1, 2.4.2, 2.4.3, 2.4.4, 2.4.5, 2.4.6, 2.5, 2.5.1, 2.5.2, 2.5.3, 2.5.4, 2.5.5, 2.6, 2.6.1, 2.6.2, 2.7, 2.7.1, 2.7.2, 2.7.3, 2.7.3.1, 2.7.3.2, 2.7.4, 2.7.5, 2.7.6, 2.7.6.1, 2.7.7, 2.8, 2.8.1, 2.8.2, 2.8.3, 2.8.4, 2.8.5, 2.8.6, 2.9, 2.9.1)
ERROR: No matching distribution found for psycopg2>=2.8.5

Debian/Ubuntu

Install the libpg and python3.7 dev :

sudo apt install libpq-dev python3.7-dev

The dependency libpq-dev on Ubuntu 20.04.3 LTS require another package, you will see a log like this:

mh4x0f@:~$ sudo apt-get install libpq-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 libpq-dev : Depends: libpq5 (= 12.4-0ubuntu0.20.04.1) but 12.4-1.pgdg20.04+1 is to be installed
E: Unable to correct problems, you have held broken packages.
lhmendes@lhmendes-GA-78LMT-S2P:~$ sudo apt-get install libpq5
Reading package lists... Done
Building dependency tree       
Reading state information... Done
libpq5 is already the newest version (12.4-1.pgdg20.04+1).
0 upgraded, 0 newly installed, 0 to remove and 4 not upgraded.

for resolve this problem you need to download the version libpq-dev with support the package name 12.4-1.pgdg20.04+1, for make this download the package from UbuntuUpdates libpq-dev and follow the command bellow.

sudo dpkg -i libpq-dev_14.1-2.pgdg20.04+1_amd64.deb

After that you can follow the tutorial again.

You might need to install python3.8-dev or similar for e.g. Python 3.8.

Try this and repeat the command pip above again, try to solve the problem with psycopg2:

sudo apt-get install postgresql
pip install psycopg2-binary

Settings Nanobrok-server

Database settings

By default nanobrok use the dababase of postgresql to save all data, but you can set your database manager in settings.toml, checkout how to config connection URI with flask.

Using sqlite

The sqlite is a great way to use for test nanabrok in your localhost, it more easy to setup:

1 - Open the settings.toml file and find the SQLALCHEMY_DATABASE_URI in section [development].

2 - Now, you should be configure or set your .db file like this 'sqlite:///development.db' the value of SQLALCHEMY_DATABASE_URI is a string with path of file sqlite.

Settings file .env

The evenrioment is a great way to settings your server with more secure, in this file you should be add somes information about your env, and somes flag of flask, you need to create this file on current root folder of the project. see the example my file .env:

FLASK_ENV=development
FLASK_DEBUG=1
FLASK_APP=nanobrok.app:create_app()

look, the variable FLASK_ENV is a name your section in settings.toml if you want to run with localhost settings your should be change this option on file .env.

Create Database and populate user

After create your .env file, now you can to settings,build and populate your database.

This command bellow is used for create the tables into database file or manager.

flask create-db

great, now need should be configure the default username and password, change the default username and password in file nanobrok/ext/commands.py. I will change this with command nano but you can use another text editor.

The commands populate-db and create-db is made for run after on setup your remote server, follow the steps for create the all tables in database and create the frist username and password of nanobrok-server:

$ flask populate-db
``

after that, the only thing you should do is run the service of nanobrok applicaiton, look the param `--host` is used when your is running in localhost:

``` console
$ flask run --host 0.0.0.0 --port 5000

now, open the browser type http://localhost:50000 if your running in localhost.

Docker

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. the Nanobrok is full compatible to run on docker container. let’s go:

on Ubuntu, type this commmand above.

sudo apt-get install docker-compose docker.io docker containerd runc

With docker installed on your SO, next step is settings docker files. On file docker-compose change the variable to your config.

    environment:
      - POSTGRES_USER=postgres_user
      - POSTGRES_PASSWORD=postgres_password
      - POSTGRES_DB=postgres_db

Open the file settings.toml go to section docker and change this line above with your settings environment. after that, close and save the file.

SQLALCHEMY_DATABASE_URI = 'postgresql://postgres_user:postgres_password@db/postgres_db'

The evenrioment is a great way to settings your server with more secure, in this file you should be add somes information about your env, and some flag of flask, you need to create this file on current root folder of the project. see the example my file .env:

FLASK_ENV=docker
FLASK_DEBUG=1
FLASK_APP=nanobrok.app:create_app()

Look, the variable FLASK_ENV is a name your section in settings.toml if you want to run with localhost settings your should be change this option on file .env.

Now need should be configure the default username and password, change the default username and password in file nanobrok/ext/commands.py. I will change this with command nano but you can use another text editor.

Build the image:

docker-compose build

Once the image is built, run the container:

docker-compose up -d

Navigate to http://localhost:5000/ to again view the hello world sanity check. Check for errors in the logs if this doesn't work via docker-compose logs -f

how to stop the docker container

sudo docker-compose down -v 

Heroku

Create App Heroku

The Heroku is a container-based cloud Platform as a Service (PaaS). Developers use Heroku to deploy, manage, and scale modern apps.

Nanobrok-Server is very simple to deploy on heroku, the first step is create an acount on heroku web service and create your app name.

heroku dashboard

Ater the click on create new app you should be see this page:

create app

choise your app name and click on create app, now you can see your app dashboard like this:

heroku app dashboard

Now, on the tab deploy you can see the step for clone your project with git. open the terminal, install the heroku cli and follow this steps, checkout!

how to clone app

my step terminal

As you can see, the heroku app step has been done for me, now go back the folder nanobrok-server-demo and clone the nanobrok-server for your folder.

git clone https://github.com/P0cL4bs/Nanobrok.git

checkout my console log:

nanobrok-clone

Settings Database URI

If you create the database using the sqlite, you not will persist data after doze machine on heroku. for solve this, we need to use the postgres database, this is a plugin (add-ons) of heroku app. Go back the browser and open your dashboard app, the next step is click on Resources tab and click on button Find more add-ons as you can see bellow.

Install the postgresql database into app. After click on button Install Heroku Postgres the next step is apply the postgres on your app, for make this put the name of app and on Add-on plan choise the free version.

after that, click on button Submit Order Form. now, you postgres database is done for and you will see this page bellow:

click on Heroku posgres action bellow and will redirect you for database postgres settings. checkout

The last step on heroku is open the Settings tab and click on view credentials. The information most important is URI that we need to insert on settings.toml file on nanobrok-server project.

Settings Nanobrok-server on Heroku

The most important here is configure the settings.toml for better security, the variable that we need to change is SQLALCHEMY_DATABASE_URI and SECRET_KEY. so, go check the nanobrok clone folder and open the settings.toml into some text editor on section heroku.

1 - set the SECRET_KEY for random string, type this on terminal:

tr -dc A-Za-z0-9 </dev/urandom | head -c 20 ; echo ''

2 - change the SQLALCHEMY_DATABASE_URI value to URI that I finded on heroku postgres on browser example. look, copy only the URI credentials format:

SQLALCHEMY_DATABASE_URI = 'postgresql://ytczhhugwlmmhe:71975bc3d14c771d2249069869eabcddcaa878ea8a97b007e55107c48f63c7a3@ec2-34-233-114-40.compute-1.amazonaws.com:5432/damgr97gmbva7d'

now, save the settings.toml. great, now we need to copy all file in nanobrok folder to nanobrok-server-demo, you can do this on terminal if you have same configuration folder tree. checkout !

λ mh4x0f [~/Projects/nanobrok-heroku] → ls   
 Nanobrok  nanobrok-server-demo
cp -avr Nanobrok/* nanobrok-server-demo

okay, now we need to create the .env file in folder nanobrok-server-demo.

FLASK_ENV=heroku
FLASK_DEBUG=1
FLASK_APP=nanobrok.app:create_app()

Settings Credentials of Nanobrok-Server

great, now need should be configure the default username and password, change the default username and password in file nanobrok/ext/commands.py. I will change this with command nano but you can use aonther text editor.

after that, we need to create the most important file for heroku application is the Procfile , the content this file is:

web flask create-db; flask populate-db; gunicorn "nanobrok.app:create_app()" -w 1 --threads 100 --preload
worker flask create-db; flask populate-db;

look, the folder files:

λ mh4x0f [Projects/nanobrok-heroku/nanobrok-server-demo] at  master ?
→ ls -lha    
total 96K
drwxrwxr-x 8 mh4x0f mh4x0f 4,0K jul 28 10:29 .
drwxrwxr-x 4 mh4x0f mh4x0f 4,0K jul 28 09:28 ..
drwxrwxr-x 3 mh4x0f mh4x0f 4,0K mar  8 21:28 backup
-rw-rw-r-- 1 mh4x0f mh4x0f  268 jun  6 22:20 CHANGELOG.md
-rwxrwxr-x 1 mh4x0f mh4x0f 1,8K jul 25 19:38 create-cert-nanobrok.sh
drwxrwxr-x 2 mh4x0f mh4x0f 4,0K jul 27 17:35 docs
-rw-rw-r-- 1 mh4x0f mh4x0f   67 jul 28 10:25 .env
drwxrwxr-x 7 mh4x0f mh4x0f 4,0K jul 28 10:29 .git
-rw-rw-r-- 1 mh4x0f mh4x0f  12K jul 28 09:28 LICENSE
-rw-rw-r-- 1 mh4x0f mh4x0f  910 jun 13 09:32 Makefile
drwxrwxr-x 4 mh4x0f mh4x0f 4,0K abr  3 20:39 migrations
drwxrwxr-x 8 mh4x0f mh4x0f 4,0K abr  3 20:19 nanobrok
-rw-rw-r-- 1 mh4x0f mh4x0f 5,5K jul 28 09:28 privacy_policy.md
-rw-rw-r-- 1 mh4x0f mh4x0f  149 jul 28 10:29 Procfile
-rw-rw-r-- 1 mh4x0f mh4x0f   26 jul 28 09:28 README.md
-rw-rw-r-- 1 mh4x0f mh4x0f   24 jun 14  2020 requirements_dev.txt
-rw-rw-r-- 1 mh4x0f mh4x0f   13 jun  9  2020 requirements_test.txt
-rw-rw-r-- 1 mh4x0f mh4x0f  427 jul 11 21:54 requirements.txt
-rw-rw-r-- 1 mh4x0f mh4x0f 6,5K jul 28 10:18 settings.toml

great, if you have this files on your folder project, now will follow the last step for deploy the applicaiton on heroku.

Deploy your application Commit your code to the repository and deploy it to Heroku using Git.

$ git add .
$ git commit -am "make it nanobrok-server"
$ git push heroku master

on my case i will run sudo git push heroku master because i install with root. and BoOM now wait the heroku deploy your app on server.

code-6.1 six-1.16.0 toml-0.10.2
remote: -----> Discovering process types
remote:        Procfile declares types -> web, worker
remote: 
remote: -----> Compressing...
remote:        Done: 65.7M
remote: -----> Launching...
remote:        Released v5
remote:        https://nanobrok-server-demo.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.
To https://git.heroku.com/nanobrok-server-demo.git
 * [new branch]      master -> master

deployed to Heroku, open the link https://nanobrok-server-demo.herokuapp.com you see the beatiful and awesome **Nanobrok-Server **.

Install on VPS

if you have a VPS (virtual private server), it great! because you can to install nanobrok-server easily. le't go...

Ubuntu 18.04 or higher

The first steps is install somes dependency for build your nanobrok-server.

sudo apt-get install python3.7 python3.7-dev
sudo apt install gunicorn3
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
sudo pip3 install psycopg2-binary

ok, the first command is for python3.7, after that we need to install gunicorn3 for build your server in background process and change the default python for python3.7.

now, we need to clone the nanobrok-server, and install the all depedency of project.

git clone https://github.com/P0cL4bs/Nanobrok-server
cd Nanobrok-server
python3.7 -m pip install -r requirements.txt

now, we need to create the database and initial settings. for make this, open the file settings.tmol go to section development and change this:

1 - set the SECRET_KEY for random string, type this on terminal:

tr -dc A-Za-z0-9 </dev/urandom | head -c 20 ; echo ''

2 - change the SQLALCHEMY_DATABASE_URI like this, we should be use the sqlite for build us database, but you can use the postgresql if your want.

SQLALCHEMY_DATABASE_URI = 'sqlite:///development.db'

3 - The evenrioment is a great way to settings your server with more secure, in this file you should be add somes information about your env, and somes flags of flask, you need to create this file on current root folder of the project. see the example:

FLASK_ENV=development
FLASK_DEBUG=1
FLASK_APP=nanobrok.app:create_app()

ok, everything is great with settings.

How to use self-signed certificates (HTTPS)

This focuses on generating the certificates for improves security with your connection with your device. This settings is great if you not have a domain to create a certificate valid for your domain. let' go...

Install OpenSSL for ubuntu ( OpenSSL is a Library that’ll help us implement secure websites using SSL and TLS proticols ) and then check if OpenSSL is installed properly, by typing the command below.

$ openssl version

The next step is build a Domain name certificate, create a new file for server configuration, domains.ext. Here we can give the available private IPs (both IPv4 and v6) for the server. 192.168.101.150 is your public IP address.

domains.ext <---This is file name, not part of the file
subjectAltName = @alt_names
extendedKeyUsage = serverAuth

[alt_names]
IP.1 = 192.168.101.150
IP.2 = 192.168.100.150

Type the commands below to generate both CA certificates+keys and Server certificates+keys. CA — will be used by the Client’s browser and Server — for server, obviously. In the folder project nanobrok-server have a script called create-cert-nanobrok.sh, the argument is domains.ext file that we created above.

./create-cert-nanobrok.sh domains.ext 

if it goes well, your will see this message:

Info:  The self-signed certificates has been generated successfully.

How to install a CA

Now, we have a NanobrokCA.crt this file should be used for allow to access your remote server with your browser. At this point, the site would load with a warning about self-signed certificates. In order to get a green lock, your new local CA has to be added to the trusted Root Certificate Authorities.

Windows 10: Chrome, IE11 & Edge

Windows 10 recognizes .crt files, so you can right-click on NanobrokCA.crt > Install to open the import dialog.

Make sure to select "Trusted Root Certification Authorities" and confirm.

You should now get a green lock in Chrome, IE11 and Edge.

Windows 10: Firefox

There are two ways to get the CA trusted in Firefox.

The simplest is to make Firefox use the Windows trusted Root CAs by going to about:config, and setting security.enterprise_roots.enabled to true.

The other way is to import the certificate by going to about:preferences#privacy > Certificats > Import > NanobrokCA.pem > Confirm for websites.

Android

You need to install the NanobrokCA.crt into android device phone for everything work fine. follow the steps

Method 1

1- Open your phone's Settings app.

2- Tap Security Advanced. Encryption & credentials.

3- Under "Credential storage," tap Install a certificate. Wi-Fi certificate.

3- In the top left, tap Menu .

4- Under "Open from," tap where you saved the certificate NanobrokCA.crt.

5- Tap the file. ...

6- Enter a name for the certificate.

7 - Tap OK.

Method 2

Send the NanobrokCA.crt for your android device and click for install.

Create nanobrok Service

the last step is create a service from systemctl for better control the server. use the command nano to create a file in system service.

nano /etc/systemd/system/nanobrok.service

Past the code bellow with your settings folder path in /root/nanobrok-server and change the root for your username:

[Unit]
Description=Gunicorn service
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/root/nanobrok-server
ExecStart=/usr/bin/gunicorn3 -b 0.0.0.0:443 --certfile /root/nanobrok-server/serverNB.crt --keyfile /root/nanobrok-server/serverNB.key --workers 3 -w 1 --threads 100 -m 007 "nanobrok.app:create_app()" --preload --env FLASK_ENV=development 

now,reload the systemctl service

systemctl daemon-reload

great, now we should be create the tables and create the default username and password for do login in nanobrok-server, find the file nanobrok/ext/commands.py and change the variable bellow.

    """Populate db with sample data"""
    user_data = {
        "username": "yourusername",
        "password": "yourpassword",
    }

now, after that, run the command for create the table and populate the default user in system.

$ flask create-db
$ flask populate-db

so, the finish is run the service in background, checkout !

sudo systemctl start nanobrok.service

do not forget to open the port into VPS settings. keep doing, hack the planet!

Fonts VPS: