How to Install Invoice Ninja on Ubuntu 16.04

Published on: Wed, Aug 30, 2017 at 11:26 am EST
Linux Guides Ubuntu Web Servers

Introduction

Invoice Ninja is a free and open source web-based application that can be used for invoicing, payments, time tracking and much more. It is the best solution for invoicing and billing customers. You can easily create and send invoices online in seconds. Invoice Ninja allows you to create custom invoices and show live invoices as a PDF file.

In this tutorial, I will explain how to install Invoice Ninja on Ubuntu 16.04 server.

Prerequisites

  • A Ubuntu 16.04 server instance with 2GB RAM installed.
  • A sudo user with root privileges.
  • You will need to know the main IP of your server instance. In this tutorial, I will use 192.168.0.227.

Step 1: Update the system

Before installing any packages on Ubuntu server instance, it is recommended to update the system. Login to your server via SSH as your sudo user and run the following command:

sudo apt-get update -y
sudo apt-get upgrade -y
sudo shutdown -r now

Step 2: Install LEMP stack

Before starting, you will need to configure a LEMP (Nginx, MariaDB and PHP) stack on your server.

First, install Nginx and MariaDB with the following command:

sudo apt-get install nginx mariadb-server -y

Once the installation is complete, start the Nginx and MariaDB services and enable them to start on boot:

sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl start mysql
sudo systemctl enable mysql

Next, you will need PHP 7 and PHP-FPM for the Invoice Ninja installation.

You can install PHP-FPM and the other required PHP extensions with the following command:

sudo apt-get install php7.0-fpm php7.0-gd php7.0-xml php7.0-mysql php7.0-zip php7.0-curl php7.0-gmp php7.0-mbstring php7.0-mcrypt

Once the installation has finished, you will need to modify the php.ini configuration file:

sudo nano /etc/php/7.0/fpm/php.ini

Change the following line:

cgi.fix_pathinfo=0

Save and close the file.

Step 3: Configure database

By default, the MariaDB installation has not been secured. You will need to secure it. You can do this by running mysql_secure_installation script.

sudo mysql_secure_installation

Answer all of the questions as shown below:

Set root password? [Y/n] Y
New password:
Re-enter new password:
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

When all is done, connect using the MySQL shell with the following command:

mysql -u root -p

Enter your root password and press "enter", you will see the MySQL (MariaDB) shell.

Create a new database and a new user for Invoice Ninja.

MariaDB [(none)]> CREATE DATABASE ninja_db;
MariaDB [(none)]> GRANT ALL PRIVILEGES ON ninja_db.* TO 'ninja'@'localhost' IDENTIFIED BY 'password';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> \q

Step 4: Configure PHP-FPM

Configure the PHP-FPM pool for the Nginx user:

sudo nano /etc/php/7.0/fpm/pool.d/www-data.conf

Add the following lines:

[www-data]
user = www-data
group = www-data
listen = /var/run/php-fpm-www-data.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0666
pm = ondemand
pm.max_children = 5
pm.process_idle_timeout = 10s
pm.max_requests = 200
chdir = /

Save and close the file when you are finished, then restart PHP-FPM to apply these changes.

systemctl restart php7.0-fpm

Step 5: Download and configure Invoice Ninja

You can download the latest stable version of Invoice Ninja from the GitHub repository with the following command:

cd /var/www/html/
sudo git clone https://github.com/hillelcoren/invoice-ninja.git ninja

You will also need to install Composer, a dependency manager for PHP. You can install it with the following command:

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/bin --filename=composer

Next, install all of the Invoice Ninja dependencies using the composer command as shown below:

cd /var/www/html/ninja
sudo composer install --no-dev -o

Once the installation is done, rename the .env file and make some changes.

sudo mv .env.example .env
sudo nano .env

Change the lines as shown below:

DB_DATABASE=ninja_db
DB_USERNAME=ninja
DB_PASSWORD=password

Save the file when you are finished, then run the following command to prepare the database:

sudo php artisan migrate

You will be prompted to run the command, type "yes" and press "enter".

Next, seed the database with records as shown below:

sudo php artisan db:seed

Type "yes" and press "enter".

Next, change ownership of the /var/www/html/ninja directory:

sudo chown -R www-data:www-data /var/www/html/ninja/

Step 6: Configure Nginx for Invoice Ninja

Next, you will need to create an SSL certificate and create a new virtual host configuration for Invoice Ninja.

First, create a directory for SSL:

sudo mkdir -p /etc/nginx/cert/

Next, generate an SSL certificate with the following command:

sudo openssl req -new -x509 -days 365 -nodes -out /etc/nginx/cert/ninja.crt -keyout /etc/nginx/cert/ninja.key

Give proper permissions to the certificate file.

sudo chmod 600 /etc/nginx/cert/*

Create a new virtual host configuration file inside of the /etc/nginx/ directory.

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

Add the following lines:

server {
    listen  80;
    server_name 192.168.0.227;
    add_header Strict-Transport-Security max-age=2592000;
    rewrite ^ https://$server_name$request_uri? permanent;
}

server {
    listen  443 default;
    server_name 192.168.0.227;
    ssl on;
    ssl_certificate     /etc/nginx/cert/ninja.crt;
    ssl_certificate_key /etc/nginx/cert/ninja.key;
    ssl_session_timeout 5m;
    ssl_ciphers  'AES128+EECDH:AES128+EDH:!aNULL';
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    root /var/www/html/ninja/public;
    index index.html index.htm index.php;
    charset utf-8;
    location / {
    try_files $uri $uri/ /index.php?$query_string;
       }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    # Access and Error Log for Invoice Ninja
    access_log  /var/log/nginx/ininja.access.log;
    error_log   /var/log/nginx/ininja.error.log;

    sendfile off;

    # Handle PHP Applications
    location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php-fpm-www-data.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;
       }
    }

Save and close the file. Then enable the virtual host with the following command.

sudo ln -s /etc/nginx/sites-available/ninja /etc/nginx/sites-enabled/

Finally, restart the Nginx web server.

sudo systemctl restart nginx

Step 7: Access Invoice Ninja

Before accessing Invoice Ninja web interface, you will need to allow HTTP and HTTPS services through the UFW firewall. Run the following commands to open these ports:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

Finally, open your web browser and access the URL https://192.168.0.227. You can then complete the required steps to finish the installation.

Congratulations! We have successfully installed Invoice Ninja with Nginx and MariaDB on Ubuntu 16.04 server.

Want to contribute ?

You could earn up to $300 by adding new articles