# GNU Health/Installation
## Requirements

The latest stable GNU Health Federation ecosystem uses these main resources:
* Operating system: GNU/Linux or FreeBSD for the server.
* RDBMS Database: PostgreSQL >= 10.x
* Document-oriented Database for Health Information System / Person Master Index: PostgreSQL :>= 10.x
* Python: >= 3.6
* uwsgi : >=2.0
* Flask : 1.0
* Tryton 6.0
* Bash shell
* PIP for Python 3, verify through:pip --version You should see python3, as in: pip x.x.x from /usr/local/lib/python3.6/site-packages (python 3.6) If you see python2.x then stop and get pip for Python 3.

## Errata

Before you continue, please read the Errata chapter for the latest issues involved the installation or upgrade procedure.

## Installing GNU Health on GNU/Linux and FreeBSD
### Operating System requirements

The following table contains the instructions to setup your operating system for a standard GNU Health installation. The operating systems and their version shown in the list have been tested using the instructions for each OS.
The installation instructions for the different operating systems and distributions have been done on a fresh installation. For simplicity's sake, the server environment was installed without a GUI. No firewall was configured (we will cover this on the security section), and OpenSSH server was installed.
The instructions – written here – have been applied and verified with the following operating systems as shown below.
| Operating System | Version | Link |
| openSUSE | Leap 15.4 | openSUSE setup |
| FreeBSD | FreeBSD 12.1 | FreeBSD setup |
| CentOS | 7.8 | CentOS setup |
| Ubuntu | 20.04 | Ubuntu setup |
| Armbian | 20.05 | Armbian setup |
| Debian | 10.1 | Debian setup |

### Setting up Network Time Protocol (NTP)

In order to properly run GNU Health, you need to make sure that the time on both the server (database and central instance) and clients are properly set and in sync. The best way to do this is to keep your clock synchronized with a NTP Server .
This is a critical step, not only for the smooth functioning of GNU Health, but also because many documents will have a timestamp associated with them that can have legal value.

### Creating the Operating System User

The following steps will create the GNU Health operating system user. Please note that many operating systems give you the option to create a regular user at installation time. If you already created the "gnuhealth" operating system user, you can skip this section, otherwise, create it now.
Run the following command as root:
Note: If your Operating System doesn't include the adduser command, you can use the useradd command:

### Verify PostgreSQL authentication method

Note: You can skip this section if you made a standard installation on FreeBSD
PostgreSQL uses different authentication methods (MD5, ident, trust ... ). Depending the Operating System, the postgreSQL server authentication method will vary.
The standard GNU Health installation uses the trust authentication method, so you need to check the postgreSQL authentication file configuration.
Locate the pg_hba.conf file and verify that the trust method is set. The location of this configuration file varies across operating systems; under UNIX/Linux, the full pathname of the file can be obtained with the following command, to be executed as root:
You may need to start the postgres server at least one time as this file may be created during first startup. Usually this file is located at /etc/postgresql/10/main or /var/lib/pgsql/data.
An example configuration file entry specifying use of the trust method is given in the following line:
The following example in particular may address issues with establishing a working database connection as reported in the context of the creation of the GNU Health database upon first use of the Tryton client (see further down; Symptom: the "Create" button is not displayed):
Make sure you edit the file as user 'postgres', not root. Otherwise, postgres may have trouble reading the changed file. After any changes to the file, the postgreSQL server needs to be restarted.
Many authentication errors (e.g., database connection errors) arise because of not having correctly configured this file. Of course, you can use other authentication methods, and you can adapt the tryton / GNU Health configuration file to each of them. For the sake of simplicity, we based the documentation and sample files in this book on one specific method (trust).
Make sure you restart your postgresql server:

### Creating the Database User

The following command switches to the postgres administration user and gives permissions to your newly created gnuhealth administrator:
Execute as root:

### Downloading and Installing GNU Health
#### Running the GNU Health Installer
##### Verify the package signature

First get the signing key if you haven't done so:
The key is issued by Luis Falcon (meanmicio at GNU) <falcon@gnu.org> and its fingerprint is ACBF C80F C891 631C 68AA 8DC8 C015 E1AE 0098 9199. This information can be seen issuing:
Then, verify the signature, using the matching version number for the latest. For instance, if latest GNU Health version is 4.0.4, then
Download the detached signature:
Verify the package using the detached signature:
If the file is correctly validated, the output should be something like:
The important part is the Good signature from "Luis Falcon ....". The WARNING means that, even if the file and signature are OK and validated correctly, you aren't trusting that key; and it's OK. You can read more about this in The GNU Privacy Handbook, Chapter 3. Key Management.

##### Run the GNU Health installer

Debian Family: How do I solve "error: externally-managed-environment" everytime I use pip3?
* OR remove file /usr/lib/python3.x/EXTERNALLY-MANAGED,
* OR use pip's argument --break-system-packages,
* OR add following lines to ~/.config/pip/pip.conf: [global] break-system-packages = true

##### Enable the BASH environment for the GNU Health admin

Finally, enable the BASH environment for the gnuhealth user.

#### Activate Network Devices for the JSON-RPC Protocol

The Tryton GNU Health server listens to localhost at port 8000, not allowing direct connections from other workstations. If necessary, enter the following:
You can edit the parameter listen in the [web] section, to activate the network device so workstations in your net can connect. For example, the following block
will allow to connect to the server in the different devices of your system.

#### Setting up a Local Directory for Attachments

By default, Tryton uses a system-wide directory to store the attachments. It is advisable, in GNUHealth to keep the attachments in the gnuhealth user space.
If necessary, edit the server configuration file trytond.conf and enter the attach directory under the [database] section, for instance:
Since debian systems connect to database over a UNIX socket, add an extra / under the [database] section, for instance:

#### Configuring the log file (optional)

The way the server logs and tracks events is based on a log configuration file, that resides in the config directory "${GNUHEALTH_DIR}"/tryton/server/config/.
A default version is shipped, called gnuhealth_log.conf. If necessary, enter the following into gnuhealth_log.conf:
In this example (and in the standard file) the log file is written in the default logs directory. You can change it to fit your specific installation.
In order to use logging, you need to provide the --logconf option, along with the path to the log configuration file gnuhealth_log.conf as argument, when invoking the Tryton server in the next section (e.g. trytond --logconf "${GNUHEALTH_DIR}"/tryton/server/config/gnuhealth_log.conf).
For more information, check the following resources:
* Python logging facility logging tutorial: https://docs.python.org/3/howto/logging.html#logging-basic-tutorial
* Tryton Server logging documentation: http://trytond.readthedocs.org/en/latest/topics/logs.html

## Initialize the database instance

Create the database
Change to your newly installed system (use the alias cdexe):
and initialize the instance:
You will be asked to provide a password for the "admin" user.
If everything goes well, you are ready to start the GNU Health HMIS node server.
Start the GNU Health HMIS node
You can execute the GNU Health server in the background (using nohup ./start_gnuhealth.sh &) and check the output in the file nohup.out.

## Creating a Systemd service for the GNU Health server

If you use the standard installation method, you can use the following scripts to automate the startup/stop of the GNU Health instance using systemd services.

### GNU Health service unit file

Create the GNU Health Unit file under /usr/lib/systemd/system/gnuhealth.service:
For Ubuntu 18.04 LTS users: /etc/systemd/system/gnuhealth.service:

### Starting and Stopping the GNU Health service

You can issue the commands:
or:

### Enable the service to start at boot time

If you want to automatically start the GNU Health server whenever you start the operating system, you can enable the service with the following command:

## Using a WSGI Server for GNU Health Hospital Management Component

GNU Health HMIS uses by default the werkzeug server. This should be valid only for development scenarios. For production servers, GNU Health HMIS will benefit from a Web Server Gateway Interface (WSGI), such as uWSGI and a web server that supports reverse proxy, as NGINX.

### Your Trytond configuration file

Edit your trytond.conf file to meet the requirements. You can edit this file directly using the alias "editconf" with the gnuhealth user.
This sample enables access both to the GTK and webclient.

### uWSGI configuration file

This is a sample for the gnuhealth uwsgi .ini ("gh.ini") file. Make sure NINGX user has the appropriate permissions to the uwsgi socket.

### Configuring NGINX as a reverse proxy for GNU Health HMIS

In this sample, NINGX will listen to 8100 in HTTPS mode, to requests coming from the web clients. It also listens to port 8000 for the native GTK client.

### Putting everything together and booting the GNU Health server

Once you have configured the three elements (Trytond server, uwsgi and NGINX) is time to put in into production
* Make sure your NGINX server is running:
* Start uWSGI with the corresponding gnuhealth .ini file:

## Installation of the GNU Health Client
### Requirements
#### openSUSE

Tested on openSUSE Leap 15.1 and Tumbleweed
* Disable Non-OSS repositoriess
* Desktop with KDE Plasma
* Create user "gnuhealth"
* Login as "gnuhealth" user
* Get the required packages / dependencies

### GNU Health Client installation with pip3

* Update PATH. To make changes permanent, add this line in $HOME/.bashrc
* Update pip3
* Install GNU Health client
The following command will boot your GNU Health client:

### Alternative Methods
#### System Packages

Instead from source as described above, you can install the GNU Health Client from pre-build packages as well. openSUSE offer packages that you can install with your systems package manager. Make sure you get the current gnuhealth-client version 4.0.x

#### Microsoft Windows and macOS

If you use Microsoft Windows or macOS, you can try using the Tryton 6.0 client, which may be compatible with GNU Health 4.0. Keep in mind that the windows client does not have the GNU Health commands, nor the plugins like GNU Health GNUPG crypto or GNU Health Camera and Federation Resource Locator.
Download the Tryton client executable (Windows) and follow the instructions.

## Logging into the Application

Now that you're back at the login screen, you'll notice that the selected profile is the one you've just created. Fill in the login form:
* User name: the one you used previously (usually admin)
* Password: the one entered twice in the previous section
Login credentials for The Demo database: GNU Health/The Demo database#Connection to the GNU Health HMIS and LIMS

## Installing the Default Modules

From this point on, you will use the client for almost every process. Start with the installation of the basic functionality:
* After you've created the database, the system will ask you to create some new users. You can skip this step for now.
* You are then presented with a list of modules that will provide the functionality you desire. If you don't see the Modules window, navigate to it on the left side: Administration → Modules → Modules.
* Select the health_profile module, and click on Mark for installation.
* Click on the Action icon (two cogwweels, previous versions used a blue rotated square) and select Perform Pending Installation/Upgrade: Step 5: Perform pending installation/upgrade after clicking on the Action icon
* Tryton will automatically select all the dependent modules required for the installation:Step 5/6: Packages to be installed, Start upgrade button
* Click on Start Upgrade. This process will take a while, depending on the computer where GNU Health is being installed on. Once it's done, the following message appears. Step 6, system upgrade finished

## Creating a Company

The next thing you need to do is to create the initial company, that will be your health center. You will be presented with a wizard to create it.
Press F3 to create a new company.
Note: At the party form, please make sure you set the institution attribute. You will link this company to your main health institution later on. Please refer to the screenshot provided in this section for details.

## Disabling demo users in production environments

GNU Health comes with a set of pre-defined users for demo purposes. They all have the prefix demo_ (demo_doctor, demo_front_desk, demo_nurse... ).
To deactivate the users:
* Navigate to Administration > Users > Users in the sidebar.
* In filters, choose Login: demo_ and Active: True
* Unset the "active" flag of each of them (untick the "Active" boxes). The demo users are now de-activated in your environment.
Look at the screenshot captioned Deactivation of demo users in production environments for an example (the Active checkboxes haven't been unticked).

## Customizing the GNU Health Client

For GNU/Linux and other free operating systems, the GNU Health GTK client configuration file can be found at:
For example:

### Using a custom greeter / banner

You can customize the login greeter banner to fit your institution.
In the section [client], include the banner parameter with the absolute path of the png file.
Something like:
The default resolution of the banner is 500 x 128 pixels. Adjust yours to approximately this size.

## Completion

Congratulations! You have completed the initial installation of GNU Health. In the next chapter we will discuss how to add functionality by installing additional modules.