How to deploy Odoo 12 on Ubuntu 18.04

s2 0

Odoo is the world’s most popular all-in-one business software. It offers a range of business applications such as CRM, website, e-commerce, billing, accounting, manufacturing, warehouse, project management, inventory and much more, all of which are seamlessly integrated.

Odoo
can be installed in several ways. The easiest and fastest way to install Odoo is to use the official APT repositories.

If you want more flexibility, e.g. For example, if you want to run several Odoo versions on the same system, you can either use Docker and Docker Compose or install Odoo in a virtual environment.

This tutorial covers the steps required to install and configure Odoo 12 for production with Git source code and the Python virtual environment on an Ubuntu 18.04 system.

before you start

Log into your Ubuntu computer as a sudo user and update the system to the latest packages:

sudo apt update && sudo apt upgrade

Install Git, Pip, Node.js and the tools needed to create Odoo dependencies:

sudo apt install git python3-pip build-essential wget python3-dev python3-venv python3-wheel libxslt-dev libzip-dev libldap2-dev libsasl2-dev python3-setuptools node-less

Create Odoo user

Create a new system user for Odoo named odoo12 with home directory /opt/odoo12 with the following command:

sudo useradd -m -d /opt/odoo12 -U -r -s /bin/bash odoo12
You can use whatever name you want for your Odoo user as long as you create a PostgreSQL user with the same name.

Install and configure PostgreSQL

Install the PostgreSQL package from Ubuntu’s standard repositories:

sudo apt install postgresql

After the installation is complete, create a PostgreSQL user with the same name as the system user created earlier, so in our case odoo12:

sudo su - postgres -c "createuser -s odoo12"

Install Wkhtmltopdf

the wkhtmltox Package provides a number of open source command line tools that can render HTML to PDF and various image formats. To print PDF reports, you need the wkhtmltopdf Tool. The recommended version for Odoo is 0.12.x which is not available in the official Ubuntu 18.04 repositories.

Download the package with the following wget command:

wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.5/wkhtmltox_0.12.5-1.bionic_amd64.deb

Once the download is complete, install the package by typing:

sudo apt install ./wkhtmltox_0.12.5-1.bionic_amd64.deb

Install and configure Odoo

We are going to install Odoo from the GitHub repository in an isolated virtual Python environment.

Before starting the installation, switch to the “odoo12” user:

sudo su - odoo12

First, clone the Odoo 12 source code from the Odoo GitHub repository:

git clone https://www.github.com/odoo/odoo --depth 1 --branch 12.0 /opt/odoo12/odoo

After the source code has been downloaded, create a new Python virtual environment for the Odoo 12 installation:

cd /opt/odoo12python3 -m venv odoo-venv

Next, enable the environment with the following command:

source odoo-venv/bin/activate

Install any required Python modules with pip3:

pip3 install wheelpip3 install -r odoo/requirements.txt
If you encounter compilation errors during the installation, make sure you have installed all of the required dependencies from the list Before you begin Section.

Disable the environment with the following command:

deactivate

Create a new directory for the custom addons:

mkdir /opt/odoo12/odoo-custom-addons

Switch back to your sudo user:

exit

Next, create a configuration file by copying the sample configuration file provided:

sudo cp /opt/odoo12/odoo/debian/odoo.conf /etc/odoo12.conf

Open the file and edit it as follows:

sudo nano /etc/odoo12.conf
/etc/odoo12.conf
[options]
; This is the password that allows database operations:
admin_passwd = my_admin_passwd
db_host = False
db_port = False
db_user = odoo12
db_password = False
addons_path = /opt/odoo12/odoo/addons,/opt/odoo12/odoo-custom-addons
Don’t forget to change them my_admin_passwd to something safer.

Create a systemd unit file

To run Odoo as a service, we need to create a service unit file in the /etc/systemd/system/ Directory.

Open your text editor and paste the following configuration:

sudo nano /etc/systemd/system/odoo12.service
/etc/systemd/system/odoo12.service
[Unit]
Description=Odoo12
Requires=postgresql.service
After=network.target postgresql.service

[Service]
Type=simple
SyslogIdentifier=odoo12
PermissionsStartOnly=true
User=odoo12
Group=odoo12
ExecStart=/opt/odoo12/odoo-venv/bin/python3 /opt/odoo12/odoo/odoo-bin -c /etc/odoo12.conf
StandardOutput=journal+console

[Install]
WantedBy=multi-user.target

Notify systemd that a new unit file exists and start the Odoo service by running:

sudo systemctl daemon-reloadsudo systemctl start odoo12

Check the service status with the following command:

sudo systemctl status odoo12

The output should look something like this to indicate that the Odoo service is up and running.

* odoo12.service - Odoo12
   Loaded: loaded (/etc/systemd/system/odoo12.service; disabled; vendor preset: enabled)
   Active: active (running) since Tue 2018-10-09 14:15:30 PDT; 3s ago
 Main PID: 24334 (python3)
    Tasks: 4 (limit: 2319)
   CGroup: /system.slice/odoo12.service
           `-24334 /opt/odoo12/odoo-venv/bin/python3 /opt/odoo12/odoo/odoo-bin -c /etc/odoo12.conf

Activate the Odoo service so that it starts automatically when booting:

sudo systemctl enable odoo12

If you want to see the messages logged by the Odoo service, you can use the following command:

sudo journalctl -u odoo12

Test the installation

Open your browser and enter: https://<your_domain_or_IP_address>:8069

If the installation is successful, you will see a screen similar to the following:

Configure Nginx as an SSL termination proxy

Make sure that you meet the following requirements before proceeding with this section:

  • Domain name pointing to your public server IP. In this tutorial we will use example.com.
  • Nginx installed.

  • SSL certificate for your domain. You can install a free Let’s Encrypt SSL certificate.

The standard Odoo web server provides the data traffic via HTTP. To make our Odoo deployment more secure, we will configure Nginx as an SSL termination proxy serving the traffic over HTTPS.

SSL termination proxy is a proxy server that handles SSL encryption / decryption. This means that our termination proxy (Nginx) processes and decrypts incoming TLS connections (HTTPS) and forwards the unencrypted requests to our internal service (Odoo) so that the traffic between Nginx and Odoo is not encrypted (HTTP).

Using a reverse proxy gives you many benefits such as load balancing, SSL termination, caching, compression, serving static content, and more.

In this example we will configure SSL termination, HTTP to HTTPS redirection, WWW to non-WWW redirection, cache the static files and enable GZip compression.

Open your text editor and create the following file:

sudo nano /etc/nginx/sites-enabled/example.com.conf
/etc/nginx/sites-enabled/example.com.conf
# Odoo servers
upstream odoo {
 server 127.0.0.1:8069;
}

upstream odoochat {
 server 127.0.0.1:8072;
}

# HTTP -> HTTPS
server {
    listen 80;
    server_name www.example.com example.com;

    include snippets/letsencrypt.conf;
    return 301 https://example.com$request_uri;
}

# WWW -> NON WWW
server {
    listen 443 ssl http2;
    server_name www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;

    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name example.com;

    proxy_read_timeout 720s;
    proxy_connect_timeout 720s;
    proxy_send_timeout 720s;

    # Proxy headers
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;

    # SSL parameters
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;

    # log files
    access_log /var/log/nginx/odoo.access.log;
    error_log /var/log/nginx/odoo.error.log;

    # Handle longpoll requests
    location /longpolling {
        proxy_pass https://odoochat;
    }

    # Handle / requests
    location / {
       proxy_redirect off;
       proxy_pass https://odoo;
    }

    # Cache static files
    location ~* /web/static/ {
        proxy_cache_valid 200 90m;
        proxy_buffering on;
        expires 864000;
        proxy_pass https://odoo;
    }

    # Gzip
    gzip_types text/css text/less text/plain text/xml application/xml application/json application/javascript;
    gzip on;
}
Don’t forget to replace example.com with your Odoo domain and set the correct path to the SSL certificate files. The snippets used in this configuration are created in this guide.

When done, restart the Nginx service with:

sudo systemctl restart nginx

Next we need to tell Odoo that we will be using a proxy. To do this, open the configuration file and add the following line:

/etc/odoo12.conf
proxy_mode = True

Restart the Odoo service for the changes to take effect:

sudo systemctl restart odoo12

At this point your server is configured and you can access your Odoo instance at: https://example.com

Change the binding interface

This step is optional, but good security practice.

By default, the Odoo server listens on all interfaces on port 8069. If you want to deactivate direct access to your Odoo instance, you can either block the port 8069 for all public interfaces or force Odoo to only listen on the local interface.

In this guide we are going to configure Odoo to just stop 127.0.0.1. Open the configuration and add the following two lines to the end of the file:

/etc/odoo12.conf
xmlrpc_interface = 127.0.0.1
netrpc_interface = 127.0.0.1

Save the configuration file and restart the Odoo server for the changes to take effect:

sudo systemctl restart odoo12

Activate multiprocessing

Odoo works in multithreading mode by default. For production deployments, it is recommended to switch to the multiprocessing server as this increases stability and makes better use of system resources. To enable multiprocessing, we need to edit the Odoo configuration and define a number of non-zero worker processes.

The number of workers is calculated based on the number of CPU cores in the system and the amount of RAM available.

According to the official Odoo documentation
To calculate the number of workers and the amount of RAM memory required, we use the following formulas and assumptions:

Calculation of the number of workers

  • theoretical maximum number of workers = (system_cpus * 2) + 1
  • 1 worker can serve ~ = 6 simultaneous users
  • Cron workers also need CPU

Calculation of the RAM memory size

  • We assume that 20% of all inquiries are heavy inquiries, while 80% are lighter ones. Heavy queries consume around 1 GB of RAM, while the lighter ones consume around 150 MB of RAM
  • Required RAM = number_of_workers * ( (light_worker_ratio * light_worker_ram_estimation) + (heavy_worker_ratio * heavy_worker_ram_estimation) )

If you don’t know how many CPUs you have on your system, you can use the following command:

grep -c ^processor /proc/cpuinfo

Let’s say we have a system with 4 CPU cores, 8 GB of RAM and 30 simultaneous Odoo users.

  • 30 users / 6 = **5** (5 is the theoretical number of workers required)
  • (4 * 2) + 1 = **9** (9 is the theoretical maximum number of workers)

Based on the above calculation, we can use 5 workers + 1 worker for the cron worker, which is a total of 6 workers.

Calculate RAM memory usage based on the number of workers:

  • RAM = 6 * ((0.8*150) + (0.2*1024)) ~= 2 GB of RAM

The above calculation shows us that our Odoo installation requires around 2 GB of RAM.

To switch to multiprocessing mode, open the configuration file and add the following lines:

/etc/odoo12.conf
limit_memory_hard = 2684354560
limit_memory_soft = 2147483648
limit_request = 8192
limit_time_cpu = 600
limit_time_real = 1200
max_cron_threads = 1
workers = 5

Restart the Odoo service for the changes to take effect:

sudo systemctl restart odoo12

The rest of the system resources are used by other services running on this system. In this guide we have installed Odoo together with PostgreSQL and Nginx on the same server and, depending on the setup, other services can also run on your server.

diploma

This tutorial walked you through the installation of Odoo 12 on Ubuntu 18.04 in a virtual Python environment with Nginx as the reverse proxy. You also learned how to enable multiprocessing and optimize Odoo for the production environment.

You can also read our tutorial on creating automatic daily backups of your Odoo databases.

If you have any questions, feel free to leave a comment below.