How to Install InvoiceNinja on Ubuntu 22.04 Server with Apache/Nginx

This tutorial will be showing you how to install InvoiceNinja on Ubuntu 22.04 with Apache or Nginx web server. InvoiceNinja is an open source, self-hosted invoice software, a low-cost alternative to commercial online invoice platforms such as Freshbooks. InvoiceNinja provides hosted invoice service, but if you like to self host the software, you can follow the instructions below.

InvoiceNinja Features

  • With InvoiceNinja, you can send invoices to your clients by using your own domain name and brand.
  • Manage invoicing for multiple businesses all under one account.
  • Save time by automatically billing long-term clients with recurring invoices.
  • Easily create and send beautiful proposals to your customers.
  • Attach 3rd Party Files to Invoices.
  • Create Project Tasks & Track Time.
  • Organize and plan your client work with a visual project management tool.
  • Allow your clients to see all their transactions with you in one glance.
  • Zapier automation allows you to transfer data between your invoicing account and popular apps including Gmail, Google Sheets, QuickBooks Online, Slack, Pipeline, MailChimp, and hundreds more.
  • Request deposits & partial payments using the same invoice again and again.
  • Use a pre-written auto-reminder email sequence to remind clients your invoice needs to be paid.
  • Receive notifications when a client views and pays your invoice.
  • And many more

Requirements

To follow this tutorial, you need an Ubuntu 22.04 OS running on a remote server. If you are looking for a virtual private server (VPS), I recommend Kamatera VPS, which features:

  • 30 days free trial.
  • Starts at $4/month (1GB RAM)
  • High-performance KVM-based VPS
  • 9 data centers around the world, including the United States, Canada, UK, Germany, The Netherlands, Hong Kong, and Isreal.

Follow the tutorial linked below to create your Linux VPS server at Kamatera.

InvoiceNinja is written in PHP and relies on MySQL/MariaDB database server. So you need to set up a LAMP stack or LEMP stack before installing InvoiceNinja.

And you also need a domain name, so your clients can see the invoice via your domain name. I registered my domain name at NameCheap because the price is low and they give whois privacy protection free for life.

It’s very important that your server can send emails, so your clients can receive your invoice. Follow the tutorial linked below to set up SMTP relay on Ubuntu.

Now let’s install InvoiceNinja.

Step 1: Download InvoiceNinja Install Zip File on Ubuntu 22.04 Server

Login to your Ubuntu 22.04 server via SSH. Then run the following command to download the latest version of the InvoiceNinja zip file onto your server.

wget https://github.com/invoiceninja/invoiceninja/releases/download/v5.3.84/invoiceninja.zip

Once downloaded, extract the archive with unzip.

sudo apt install unzip

sudo mkdir -p /var/www/invoiceninja/

sudo unzip invoiceninja.zip -d /var/www/invoiceninja/

The -d option specifies the target directory. InvoiceNinja web files will be extracted to /var/www/invoiceninja.

Then we need to change the owner of this directory to www-data so that the web server can write to this directory.

sudo chown www-data:www-data /var/www/invoiceninja/ -R

Step 2: Create a Database and User in MariaDB

Log into MariaDB database server with the following command.

sudo mysql

Then create a database for Invoice Ninja. This tutorial names the database invoiceninja. You can use whatever name you like.

create database invoiceninja;

Create the database user. Again, you can use your preferred name for this user. Replace your-password with your preferred password.

create user ninja@localhost identified by 'ninja_password';

Grant this user all privileges on the invoiceninja database.

grant all privileges on invoiceninja.* to ninja@localhost;

Flush privileges and exit.

flush privileges;

exit;

Step 3: Configure InvoiceNinja

Go to the InvoiceNinja web root.

cd /var/www/invoiceninja/

Copy the example .env file.

sudo cp .env.example .env

Edit this file with a command-line text editor like Nano.

sudo nano .env

Find the following line.

APP_URL=http://localhost

Change the URL to something like https://invoice.yourdomain.com, so you will be able to access InvoiceNinja using this sub-domain.

Then edit the database connection details. Enter the database name, database user, and password created in step 2.

DB_HOST=localhost
DB_DATABASE=invoiceninja
DB_USERNAME=ninja
DB_PASSWORD=ninja_password
DB_PORT=3306

Save and close the file. Change the owner to www-data.

sudo chown www-data:www-data /var/www/invoiceninja/.env

Next, run the following command to generate a unique app key for your InvoiceNinja installation.

sudo php8.1 /var/www/invoiceninja/artisan key:generate

And migrate the database.

sudo php8.1 /var/www/invoiceninja/artisan migrate:fresh --seed

Step 4: Setting Up Web Server

We can use Apache or Nginx web server.

Apache

If you prefer Apache, create a virtual host file for Invoice Ninja.

sudo nano /etc/apache2/sites-available/invoice-ninja.conf

Put the following text into the file. Replace the red-colored text with your actual data. Don’t forget to set A record for the domain name. (Note that the web root is set to /var/www/invoice-ninja/public/, not /var/www/invoice-ninja/)

<VirtualHost *:80>
    ServerName invoice.yourdomain.com
    DocumentRoot /var/www/invoice-ninja/public

    <Directory /var/www/invoice-ninja/public>
       DirectoryIndex index.php
       Options +FollowSymLinks
       AllowOverride All
       Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/invoice-ninja.error.log
    CustomLog ${APACHE_LOG_DIR}/invoice-ninja.access.log combined

</VirtualHost>

Save and close the file. Then enable this virtual host.

sudo a2ensite invoice-ninja.conf

We need to enable the rewrite module.

sudo a2enmod rewrite

Restart Apache for the changes to take effect.

sudo systemctl restart apache2

Now you can access the Invoice Ninja setup wizard page (invoice.yourdomain.com/setup). If you see the default Apache page instead of the setup wizard, then you need to disable the default virtual host.

sudo a2dissite 000-default.conf

And restart Apache.

Before entering any information in the setup wizard, we need to enable HTTPS.

Nginx

If you prefer Nginx, create a invoiceninja.conf file in /etc/nginx/conf.d/ directory.

sudo nano /etc/nginx/conf.d/invoiceninja.conf

Put the following text into the file. Replace the red-colored text with your actual data. Don’t forget to create DNS A record for this sub-domain. (Note that the web root is set to /var/www/invoiceninja/public/, not /var/www/invoiceninja/)

server {
    listen   80;
    listen   [::]:80;
    server_name invoice.yourdomain.com;

    root /var/www/invoiceninja/public/;
    index index.php index.html index.htm;
    charset utf-8;
    client_max_body_size 20M;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    if (!-e $request_filename) {
       rewrite ^(.+)$ /index.php?q= last;
    }

    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/invoiceninja.access.log;
    error_log   /var/log/nginx/invoiceninja.error.log;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php8.1-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;
    }

    sendfile off;
}

Save and close the file. Then test Nginx configuration.

sudo nginx -t

If the test is successful, reload Nginx for the changes to take effect.

sudo systemctl reload nginx

Now you can access the Invoice Ninja setup wizard page (invoice.yourdomain.com/setup). Before entering any information in the setup wizard, we need to enable HTTPS.

Step 5: Enabling HTTPS

To encrypt the HTTP traffic, we can enable HTTPS by installing a free TLS certificate issued from Let’s Encrypt. Run the following command to install Let’s Encrypt client (certbot) on Ubuntu 22.04 server.

sudo apt install certbot

If you use Nginx, then you also need to install the Certbot Nginx plugin.

sudo apt install python3-certbot-nginx

Next, run the following command to obtain and install TLS certificate.

sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email [email protected] -d invoice.yourdomain.com

If you use Apache, install the Certbot Apache plugin.

sudo apt install python3-certbot-apache

And run this command to obtain and install TLS certificate.

sudo certbot --apache --agree-tos --redirect --hsts --staple-ocsp --email [email protected] -d invoice.yourdomain.com

Where

  • --nginx: Use the nginx plugin.
  • --apache: Use the Apache plugin.
  • --agree-tos: Agree to terms of service.
  • --redirect: Force HTTPS by 301 redirect.
  • --hsts: Add the Strict-Transport-Security header to every HTTP response. Forcing browser to always use TLS for the domain. Defends against SSL/TLS Stripping.
  • --staple-ocsp: Enables OCSP Stapling. A valid OCSP response is stapled to the certificate that the server offers during TLS.

The certificate should now be obtained and automatically installed.

invoice ninja ubuntu 18.04

Step 6: Install PHP Modules

Run the following commands to install PHP modules required or recommended by InvoiceNinja.

sudo apt install php-imagick php8.1-common php8.1-gd php8.1-curl php8.1-zip php8.1-xml php8.1-mbstring php8.1-bz2 php8.1-intl php8.1-gmp

Then restart Apache. (If you use Nginx, you don’t need to restart Nginx.)

sudo systemctl restart apache2

If these modules are not installed, you will see the following error after login.

Whoops, looks like something went wrong.

Step 7: Finish Installation with the Setup Wizard

Now go to invoice.yourdomain.com/setup to launch the web-based setup wizard. First, you need to designate an URL for your InvoiceNinja installation (https://invoice.yourdomain.com).

invoiceninja web-based setup wizard

 

Then click the Test PDF button. If it’s successful, you will then enter the database connection credentials.

invoiceninja mariadb database connection

Click the Test connection button.

Next, Enter SMTP settings to make sure it can end invoices to your clients via email. If you use the free Sendinblue SMTP relay service, then enter your Sendinblue credentials here.

invoiceninja SMTP settings

 

  • Driver: SMTP
  • Host: smtp-relay.sendinblue.com
  • Username: your sendinblue username
  • Password: your sendinblue password
  • Port: 587
  • Encryption: STARTTLS

Click the Send test email button to check if it’s working.

Finally, create an admin account.

invoiceninja admin account

After creating the admin user, you can log into InvoiceNinja.

 

Set Up Cron Jobs

We need to set up Cron jobs to send recurring invoices and email reminders. Edit the www-data user’s crontab file.

sudo -u www-data crontab -e

Add the following lines at the end of this file.

#InvoiceNinja
0 8 * * * /usr/bin/php8.1 /var/www/invoiceninja/artisan ninja:send-recurring > /dev/null
0 8 * * * /usr/bin/php8.1 /var/www/invoiceninja/artisan ninja:send-reminders > /dev/null

Save and close the file. The two Cron jobs will run at 8 AM every day. You can also manually run a job like below.

sudo /usr/bin/php8.1 /var/www/invoice-ninja/artisan ninja:send-recurring

Removing the Invoice Ninja branding

By default, your invoice will have the Invoice Ninja branding, you can purchase a white label license for $30 per year to remove the Invoice Ninja branding from the invoice and client portal. Go to settings -> Invoice Design, click the white-label link at the bottom to purchase.

How to Upgrade Invoice Ninja

When a new version of InvoiceNinja is out, follow these steps to upgrade.

First, back up the database.

sudo mysqldump -u root invoiceninja > invoiceninja.sql

Then back up the web files.

sudo tar -cpzvf invoiceninaja.tar.gz /var/www/invoiceninja/

Next, download the InvoiceNinja zip file from Github and replace the content in /var/www/invoice-ninja/, and install the dependencies.

cd /var/www/invoice-ninja/

sudo chown www-data:www-data /var/www/invoiceninja/ -R

sudo -u www-data composer install

Remove the .env file.

sudo rm /var/www/invoiceninja/.env

Next, go to https://invoice.yourdomain.com/setup to re-run the setup wizard. The database schema will be upgraded in the process.

Conclusion

I hope this tutorial helped you install Invoice Ninja on Ubuntu 22.04 server. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care 🙂

Rate this tutorial
[Total: 2 Average: 5]

13 Responses to “How to Install InvoiceNinja on Ubuntu 22.04 Server with Apache/Nginx

  • Mysterion
    2 weeks ago

    Thanks for the detailed guide:)

  • Thanks for this – I got there in the end.
    Do I need to install a mail server as well or is that optional?

  • I guess this version of Invoice Ninja cannot support additional modules as it doesn’t use the contain the Composer portions, right?

  • Thank you so much!

    I rarely get Nginx working when I use these kind of directions but I got it working the first time around.

    You are the best. I’ll buy you a cup of coffee after I have closed my first sale.

  • IMRON HS
    3 weeks ago

    Hi XIao, please update:
    Purchase a ONE YEAR white label license for $30 to remove the Invoice Ninja branding from the invoice and client portal.

    Now $30 for remove label inovice ninja branding.

    Yesterday, I have making donation for you. $10
    Thank you

  • hi
    I followed your instructions (including installing LAMP Stack on 22.04) and everything went well right till the end.
    I installed a self-assigned ssl certificate.
    I tested the database connection and email test. both work okay.

    • I would never use a self-signed certificate for an invoicing application, because Web browsers will tell visitors the web page is insecure, and do you expect visitors to pay you on an insecure web page?

      InvoicingNinja is supposed to run on a server with a real domain name and valid SSL certificate.

      • we are not using the server for external use. simply just using it for internal company invoicing and we will not be using any remote login from outside our network.

  • ¡Hi all!

    Just in case someone have had the same issue. If you did well installing your mail server with Linuxbabe tutorials, then you maybe find that using DRIVER: SMTP and ENCRYPTION:TLS , it won’t work. You just have to change DRIVER:SENDMAIL, that’s it. I didn’t probe the other options, but that worked to me.

    • Actually I just did a fresh install InvoiceNinja on a new server, and the SMTP driver works without any problem.

      When using your own email server, there are two drivers to choose from: SMTP and Sendmail.

      • Choose SMTP if your mail server and Invoice Ninja runs on two different hosts.
      • Choose Sendmail if your mail server and Invoice Ninja runs on the same host.

      If you choose SMTP, use settings like below.

      • host: mail.yourdomain.com port: 587 Encryption: STARTTLS. And enter your username and password.

      If you choose Sendmail, use settings like below.

      • host: 127.0.0.1 port: 25 Encryption: none. You don’t need to enter username or password.

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here