This guide installs Strapi as a systemd-managed Node.js service backed by PostgreSQL.
It supports Debian 10 to latest stable, Ubuntu LTS, and RHEL 9+ compatible systems.
- name: Install Strapi
hosts: strapi
become: true
vars:
strapi_user: strapi
strapi_group: strapi
strapi_home: /opt/strapi
strapi_port: 1337
tasks:
- name: Install dependencies on Debian/Ubuntu
apt:
name:
- nodejs
- npm
- postgresql
- postgresql-contrib
- curl
- git
state: present
update_cache: true
when: ansible_os_family == "Debian"
- name: Install dependencies on RHEL family
dnf:
name:
- nodejs
- npm
- postgresql-server
- postgresql
- curl
- git
state: present
when: ansible_os_family == "RedHat"
- name: Initialize PostgreSQL on RHEL family
command: postgresql-setup --initdb
args:
creates: /var/lib/pgsql/data/PG_VERSION
when: ansible_os_family == "RedHat"
- name: Enable and start PostgreSQL
service:
name: postgresql
state: started
enabled: true
- name: Create Strapi group
group:
name: "{{ strapi_group }}"
state: present
- name: Create Strapi user
user:
name: "{{ strapi_user }}"
group: "{{ strapi_group }}"
home: "{{ strapi_home }}"
shell: /usr/sbin/nologin
create_home: true
- name: Create Strapi app directory
file:
path: "{{ strapi_home }}"
state: directory
owner: "{{ strapi_user }}"
group: "{{ strapi_group }}"
mode: "0755"
- name: Create Strapi app
command: npx create-strapi-app@latest . --quickstart --no-run
args:
chdir: "{{ strapi_home }}"
creates: "{{ strapi_home }}/package.json"
- name: Install production dependencies
command: npm install --omit=dev
args:
chdir: "{{ strapi_home }}"
- name: Create systemd service for Strapi
copy:
dest: /etc/systemd/system/strapi.service
mode: "0644"
content: |
[Unit]
Description=Strapi CMS
After=network.target postgresql.service
[Service]
Type=simple
User={{ strapi_user }}
Group={{ strapi_group }}
WorkingDirectory={{ strapi_home }}
Environment=NODE_ENV=production
Environment=PORT={{ strapi_port }}
ExecStart=/usr/bin/npm run start
Restart=on-failure
[Install]
WantedBy=multi-user.target
- name: Reload systemd
systemd:
daemon_reload: true
- name: Enable and start Strapi
service:
name: strapi
state: started
enabled: true
Beyond this playbook, we offer:
Contact our automation team: office@linux-server-admin.com