If you are self-hosting, installing on Ubuntu is the best possible method, and highly suggested. Running Ubuntu as a host allows you to install with the least amount of effort, fewer dependencies, and auto-update the application through the admin portal user interface. The 'invoiceninja.zip' package that is compiled by the Invoice NInja team every release has many dependency packages bundled into it, but it is only compatible with Ubuntu, and maybe other distros more similar to Ubuntu. Because this package is used for the auto updates as well, auto updates only work on Ubuntu.
Invoice Ninja on Ubuntu production servers depends on PHP 7.4 and multiple PHP 7.4 extensions that aren't yet available on PHP 8.0, mariadb for SQL, and because it is a laravel application we will install composer. I don't believe you need composer to install, or update Invoice Ninja on Ubuntu. You may consider removing composer, but I would suggest keeping it installed anyways, because it is generally reccomended on production servers running laravel applications.
# apt update
# apt dist-upgrade -y
# apt install php7.4 php7.4-{fpm,bcmath,ctype,fileinfo,json,mbstring,pdo,tokenizer,xml,curl,zip,gmp,gd,mysqli} mariadb-server mariadb-client curl git nginx vim composer -y
Done installing things. Let's configure them.
Debian Users
You have different paths for php-fpm's unix socket than Ubuntu users. This guide assumes you spawn /run/php/php-fpm.sock for a socket, but you do not. Other distros based on Debian or Ubuntu should pay attention to php-fpm.sock mentions in the nginx config example below, and change them to match the php-fpm path on your distro.
Check where php-fpm runs its socket with:
# systemctl status php7.4-fpm
Start, and enable services for mariadb, the program/service that manages your SQL database and the incoming and outgoing communication it has with other applications like Invoice Ninja.
# systemctl start mariadb
# systemctl enable mariadb
This command will take you through a guided wizard to initialize the SQL database.
# mysql_secure_installation
Enter current password for root (enter for none):
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y
These commands will directly access the SQL database through the services provided by mariadb. We will create a database with any arbitrary name 'ninjadb' in this example, and create arbitrary username and password combination 'ninja' and 'ninjapass'. The database name will be used by InvoiceNinja during the server setup after installation is complete, as well as the username and password you specify here, in order for InvoiceNinja to login to the SQL database with read/write permission.
# mysql -u root -p
Enter Password: ******
MariaDB .. > create database ninjadb;
MariaDB .. > create user 'ninja'@'localhost' identified by 'ninjapass';
MariaDB .. > grant all privileges on ninjadb.* to 'ninja'@'localhost';
MariaDB .. > flush privileges;
MariaDB .. > exit
You should not need to do this. I am about to show you NGINX configuration file that points to an example based openssl cert. Most of you would be using letsencrypt, or some other CA. I am not going to provide a guide to setup letsencrypt. So for the purposes of this guide, the NGINX configuration will use an instant OpenSSL certificate that is not particularly trusted on the Internet.
# mkdir -p /etc/nginx/cert
# openssl req -new -x509 -days 365 -nodes -out /etc/nginx/cert/ninja.crt -keyout /etc/nginx/cert/ninja.key
The default NGINX install on Ubuntu has a pesky default website located at /etc/nginx/sites-enabled/default - and for our cases, we do not want this default website hosted by nginx. When left unconfigured, this page presents some security loopholes. It can also cause conflicts sometimes. Lets remove it.
# rm /etc/nginx/sites-enabled/default
Create a text file with the '.conf' ending in the /etc/nginx/conf.d/ directory, and any code in it will be included and run with the settings under /etc/nginx/nginx.conf.
# vim /etc/nginx/conf.d/invoiceninja.conf
Press 'i' to enter insert mode, and paste this server configuration.
You cannot copy paste this entire document, you will need to edit appropriate sections to accomodate your own environment. I will try to """indicate""" when specific strings of text need your attention.
server {
# NOTE That the 'default_server' option is only necessary if this is your primary domain application.
# If you run multiple subdomains from the same host already, remove the 'default_server' option.
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name """invoices.example.ca""";
client_max_body_size 20M;
# Here, enter the path to your invoiceninja directory, in the public dir. VERY IMPORTANT
# DO NOT point the root directly at your invoiceninja directory, it MUST point at the public folder
# This is for security reasons.
root /usr/share/nginx/"""invoiceninja"""/public;
gzip on;
gzip_types application/javascript application/x-javascript text/javascript text/plain application/xml application/json;
gzip_proxied no-cache no-store private expired auth;
gzip_min_length 1000;
index index.php index.html index.htm;
# Enter the path to your existing ssl certificate file, and certificate private key file
# If you don’t have one yet, you can configure one with openssl in the next step.
ssl_certificate "/etc/nginx/cert/ninja.crt";
ssl_certificate_key "/etc/nginx/cert/ninja.key";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers 'AES128+EECDH:AES128+EDH:!aNULL';
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
charset utf-8;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q= last;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Here we pass php requests to the php7.4-fpm listen socket.
# PHP errors are often because this value is not correct.
# Verify your php7.4-fpm.sock socket file exists at the below directory
# and that the php7.4-fpm service is running.
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ /\.ht {
deny all;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log /var/log/nginx/ininja.access.log;
error_log /var/log/nginx/ininja.error.log;
sendfile off;
}
server {
listen 80;
server_name """invoices.example.ca""";
add_header Strict-Transport-Security max-age=2592000;
rewrite ^ https://$server_name$request_uri? permanent;
}
That's great! Hope you remembered to check root, server_name, and SSL certificate paths. Next Steps.
For Ubuntu 20.04, I had to disable apache2 in order to enable nginx to run on ports 80 and 443 without conflict. You might prefer to use apache2, but I am only supporting one web server conf file, and am already using nginx.
$ sudo systemctl stop apache2
$ sudo systemctl disable apache2
$ sudo systemctl start nginx
$ sudo systemctl enable nginx
Please visit https://github.com/invoiceninja/invoiceninja/releases to get the latest github release of InvoiceNinja from the team. Look closely at what you are downloading, the list also includes recent updates to v4.
$ cd /usr/share/nginx
$ sudo mkdir invoiceninja && cd invoiceninja
$ sudo wget <latest zip url>
$ sudo unzip invoiceninja.zip
You have an automatically generated .env file with preset encryption key and ready to initialize the setup page.
Strongly reccomend you to, populate the .env file with a new genuine encryption key. Then BACKUP this file, because it is your key to your invoicing data. Do not lose this forever by accidentally deleting or overwriting it one day.
# php7.4 artisan key:generate
Often when you are hit with a 500 or page not working problem, you just have to run one of these two commands to verify permissions and reoptimize the software.
Set permissions for the directory and all its contents to allow web server permission to view and edit files.
# chown -R www-data:www-data /usr/share/nginx/invoiceninja
Run auto configure process, something you must do again if you ever change the values of .env or other files within the invoiceninja directory.
# php7.4 artisan optimize
Now, there is need to enable cron job that will run some sort of regular maintenance, or you get a nasty red exclamation mark error in InvoiceNinja after logging in. See here for more: https://invoiceninja.github.io/selfhost.html#installing-invoice-ninja
$ sudo -u www-data crontab -e
Then copy paste the following into the bottom of your cron file, which will be run for something to do with laravel which InvoiceNinja depends on.
This will run, as user www-data, with the explicitly correct php version, the artisan schedule command in order to provide Invoice Ninja application with the backend services it needs to work properly. Edit the path according to your environment
* * * * * php7.4 /usr/share/nginx/"""invoiceninja"""/artisan schedule:run >> /dev/null 2>&1
Done. Enjoy.
I won't provide a step by step for how to backup your files, maybe not at this time yet, but ensure you have backed up your .env file, the public/storage directory, and the SQL database. As an admin, when you attach files to a client or invoice, to be viewed on the client portal for example, those files are stored in the public/storage directory. The SQL database contains all your important information directly. The .env file is precious, because it contains the encryption key for your SQL database, and you can get locked out without it.