Serving Odoo 17 on Ubuntu 24.04 Using Nginx Proxy

How to run Odoo 17 on Ubuntu 24.04 behind Nginx proxy server
July 8, 2024 by
Serving Odoo 17 on Ubuntu 24.04 Using Nginx Proxy
Hamed Mohammadi
| No comments yet

In this blog post we discuss how to run Odoo 17 on Ubuntu 24.04 behind Nginx proxy server. We need a running Ubuntu 24.04 server and Nginx server. We need Certbot to get Letsencript certification. 

We are going the whole process of Odoo deployment, but if you only need to understand how to run Odoo 17 behind Nginx proxy, start with Obtaining Let's Encrypt certificate and then configuring Nginx section below. Another post discusses this step only (Deploying Odoo 17 using Nginx on Ubuntu).

Installing Ubuntu 24.04 Minimal Server

Here's a walkthrough of installing Ubuntu 24.04 minimal server:

Preparation:

  1. Download the ISO: Head to the official Ubuntu download page https://ubuntu.com/download/server and grab the latest LTS (Long Term Support) server image for your system architecture (most likely 64-bit).

  2. Create a bootable USB drive: Use a tool like BalenaEtcher https://etcher.balena.io/ to create a bootable USB drive from the downloaded ISO file.

Note: if you buy a VPS from a supplier you don't need to do this process. You usually select Ubuntu 24.04 as your operating system during selecting your VPS.

Installation Process:

  1. Boot from the USB: Boot your system from the prepared USB drive. Choose the boot option that mentions "Boot from USB" or something similar in your BIOS/UEFI settings.

  2. Welcome and Language Selection: The installer will greet you with a welcome message. Select your preferred language and keyboard layout from the on-screen options.

  3. Network Configuration: The installer will attempt to detect your network connection automatically. If successful, you can proceed. Otherwise, you'll need to configure your network settings manually.

  4. Storage Configuration: Here, you'll choose how you want to partition your disks for the Ubuntu installation. If you're new to Linux, it's recommended to use the guided partitioning tool and choose "Use entire disk" for a simple setup. Make sure to back up any important data on the disk before proceeding.

  5. Proxy Configuration (Optional): If you're behind a proxy server, you can configure it at this stage. Otherwise, leave it blank.

  6. User Creation: Create a new user for your system and set a strong password. Remember this password, as you'll need it to log in after the installation.

  7. Package Selection: This is crucial. Here, you'll choose which packages to install. For a minimal server setup, deselect everything except the base packages and OpenSSH server. This will give you a basic command-line system with secure remote access.

  8. Installation: The installer will download and install the selected packages onto your system. This might take a while depending on your internet speed.

  9. Update and Upgrade: Once the installation is complete, the system will update the package lists and upgrade any pre-installed packages.

  10. Reboot: Finally, you'll be prompted to reboot the system.

After rebooting, you should be able to log in to your new Ubuntu 24.04 minimal server with the username and password you created during installation.

Note: if you buy your VPS from a vendor you don't need to do this process. Usually they install the Ubuntu 24.04 on your VPS and provide you with a ssh login user and password. A root ssh access to server is all we need.


Installing Postgresql Database

1. Update System Packages:

Before installing new software, it's good practice to update your system's package lists to ensure you get the latest versions. Open a terminal window and run:

$ sudo apt update

$ sudo apt upgrade


2. Install PostgreSQL:

There are two main ways to install PostgreSQL:

This method installs the version included with Ubuntu 24.04 by default. Run the following command:

Using the default Ubuntu repositories:

$ sudo apt install postgresql -y postgresql-contrib


3. Verify PostgreSQL Installation:

Once the installation is complete, verify it's running by checking the service status:

sudo systemctl status postgresql


4. Create Odoo User

To connect odoo to PostgreSQL create a user:

$ sudo su - postgres -c "createuser -s odoo17"




Installing Odoo 17 on Ubuntu 24.04 with Source Code

Here's how to install Odoo 17 on Ubuntu 24.04 by cloning the source code from GitHub:

1. Update and Upgrade System:

Before proceeding, ensure your system is up-to-date:

$ sudo apt update && sudo apt upgrade -y


2. Install Dependencies:

Odoo has various dependencies. Install them with:

$ sudo apt install python3.10-dev python3-pip libpq-dev build-essential libxml2-dev libssl-dev libjpeg-dev libtiff5-dev libwebkitgtk-dev wkhtmltopdf unzip


3. Create a System User for Odoo:

It's recommended to run Odoo as a dedicated user. Create a new user named odoo with:

$ sudo adduser --system --group --home /opt/odoo odoo


4. Clone Odoo Repository:

Install git if you don't have it:

$ sudo apt install git -y

Now, clone the Odoo repository (replace 17.0 with the specific version if needed):

$ sudo -H -u odoo git clone https://github.com/odoo/odoo /opt/odoo --depth 1 --branch 17.0 --single-branch


5. Create an Odoo Configuration File:

A configuration file defines how Odoo runs. Create one named odoo.conf inside the Odoo directory with:

$ sudo -H -u odoo nano /opt/odoo/odoo.conf

Inside the file, add the following lines (replace <db_password> with your desired password):

[options]
; This list contains the databases that Odoo will manage.
db_name = odoo
; This is the password for the Odoo user that will be created in the database.
db_password = false
; This is the Odoo admin password used to access the web interface. This password is **strongly** recommended to be different from the database password.
admin_password = <admin_password>
# This option defines the Odoo server address.
# By default, Odoo listens on all interfaces on port 8069.
# Here we specify a specific interface and port (change as needed).
# server_host = 127.0.0.1
# server_port = 8069

# Additional configuration options can be found in the Odoo documentation:
# https://www.odoo.com/documentation/server/deploy.html


6. Save the changes and exit the editor (Ctrl+O, then Ctrl+X).

7. Set File Permissions:

$ sudo chown -R odoo:odoo /opt/odoo


8. Create a Virtual Environment:

This step isolates Odoo's Python dependencies and avoids conflicts with other system packages.

Install python3-venv:

$ sudo apt install python3-venv -y

Create a virtual environment directory:

$ sudo -H -u odoo python3 -m venv /opt/odoo/env

Activate the virtual environment:

$ source /opt/odoo/env/bin/activate


9. Install Python Dependencies:

Within the activated virtual environment, install Odoo's Python dependencies:

$ sudo -H -u odoo pip3 install -r /opt/odoo/requirements.txt


10. Create Log Directory (Optional):

It's helpful to have a dedicated directory for Odoo logs:

$ sudo mkdir /opt/odoo/log

$ sudo chown odoo:odoo /opt/odoo/log


11. Start Odoo:

With the virtual environment activated (if used), start Odoo in the background:

sudo -H -u odoo nohup /opt/odoo/env/bin/python3 /opt/odoo/odoo-bin -c /opt/odoo/odoo.conf &


12. Verify Odoo Installation:

Open a web browser and navigate to http://localhost:8069 (or the server address and port specified in odoo.conf). If everything is set up correctly, you should see the Odoo web interface. You can then log in with the admin password you defined in the configuration file.

Run Odoo As a System Service

Consider creating a system service for running Odoo.



Install Nginx

Here's the process of installing Nginx server on Ubuntu 24.04 using the apt package manager:

Use the following command to install Nginx and some common modules:

$ sudo apt install nginx

You can verify if Nginx is running by checking its service status:

$ sudo systemctl status nginx

The output should show the service as "active (running)".



Install Certbot

Certbot comes with a client and a plugin for your web server (like Nginx or Apache). Here's how to install it:

$ sudo apt install certbot python3-certbot-nginx

Use steps in this blog post to obtain a certificate for your domain: Obtaining an SSL Certificate for Your Site for Nginx on UbuntuYou can use certificate that you obtained in this section in next step.


Configuring Odoo Behind Nginx Proxy


Change the configuration file for your website to use nginx proxy for odoo. This is the configuration file for odoo website:

# Odoo servers
upstream odoo {
server 127.0.0.1:8069;
}

upstream odoochat {
server 127.0.0.1:8072;
}

map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}

# HTTP -> HTTPS
server {
listen 80;
server_name https://www.example.com/ https://example.co;/

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

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

ssl_certificate /etc/letsencrypt/live/https://example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/https://example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/https://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 https://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/https://example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/https://example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/https://example.com/chain.pem;
include snippets/ssl.conf;
include snippets/letsencrypt.conf;

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

# Redirect websocket requests to odoo gevent port
location /websocket {
proxy_pass http://odoochat;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header X-Forwarded-Host $http_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;
}

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

# Redirect requests to odoo backend server
location / {
# Add Headers for odoo proxy mode
proxy_set_header X-Forwarded-Host $http_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;
proxy_redirect off;
proxy_pass http://odoo;

# Enable HSTS
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
# requires nginx 1.19.8
#proxy_cookie_flags session_id samesite=lax secure;
}

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

# Gzip
gzip_types text/css text/less text/plain text/xml application/xml application/json application/javascript;
gzip on;
}

Restart Nginx to apply the new configuration:

$ sudo systemctl restart nginx

Serving Odoo 17 on Ubuntu 24.04 Using Nginx Proxy
Hamed Mohammadi July 8, 2024
Share this post
Archive

Please visit our blog at:

https://zehabsd.com/blog

A platform for Flash Stories:

https://readflashy.com

A platform for Persian Literature Lovers:

https://sarayesokhan.com

Sign in to leave a comment