• Get In Touch
March 19, 2017

How to Install Invoice Ninja on CentOS 7

Want your very own server? Get our 1GB memory, Xeon V4, 25GB SSD VPS for £10.00 / month.
Get a Cloud Server

Invoice Ninja is a free and open source web based application for invoicing, time tracking and accepting payments. Invoice Ninja is based on PHP and built with the Laravel Framework. You can create Invoices online in just seconds using Invoice Ninja. Invoice Ninja can show you live invoices as PDF files and you can setup your own company logo and use custom invoice templates. Some of its features are listed below:

  • Create work tasks & track time
  • Create invoices online in seconds
  • Email invoices & get paid online
  • Mobile responsive design
  • Integrate 45+ payment gateways
  • View live invoice .PDF creation
  • Add your company logo to invoices
  • Quotations convert to invoices
  • Auto-billing & recurring invoices
  • Multiple tax settings
  • Client Portal to View Invoices
  • Alerts when invoices are paid
  • Set invoice payment due dates

Prerequisites

You’ll need a CentOS 7 server and root privileges over it to follow this guide. You can switch between non root user to root user using the sudo -i command. It is recommended to install Invoice Ninja on a freshly updated server so run below given command and it’ll do the job for you.

yum -y update

Installing Nginx

You’ll have to install nginx web server along with MariaDB and PHP with some required extensions. So first of all install nginx web server and for that you must have to install epel repository to your centos system.

yum -y install epel-release

Now we can install nginx with yum command from epel repository.

yum -y install nginx

Next start the nginx and enable it to start at boot time using following commands.

systemctl start nginx
systemctl enable nginx

We will need to install MariaDB for database purposes for Invoice Ninja. MariaDB 5.5 is shipped in the default CentOS 7 repository, so just run this command to install MariaDB.

yum -y install mariadb-server

Now you’ll have to start the MariaDB service and enable it to start at the boot time like we have done before for nginx web server, to do so please run following command.

systemctl start mariadb.service systemctl enable mariadb.service

You can check status of mariaDB using this below given command and you should see following output.

systemctl status mariadb.service

[root@Sajid ~]# systemctl status mariadb.service
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2017-02-08 14:47:40 UTC; 22s ago
 Main PID: 18035 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─18035 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─18192 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/ma...Feb 08 14:47:38 ip-172-31-22-142 mariadb-prepare-db-dir[17956]: The latest information about MariaDB is available at http://mariadb.org/.
Feb 08 14:47:38 ip-172-31-22-142 mariadb-prepare-db-dir[17956]: You can find additional information about the MySQL part at:
Feb 08 14:47:38 ip-172-31-22-142 mariadb-prepare-db-dir[17956]: http://dev.mysql.com
Feb 08 14:47:38 ip-172-31-22-142 mariadb-prepare-db-dir[17956]: Support MariaDB development by buying support/new features from MariaDB
Feb 08 14:47:38 ip-172-31-22-142 mariadb-prepare-db-dir[17956]: Corporation Ab. You can contact us about this at sales@mariadb.com.
Feb 08 14:47:38 ip-172-31-22-142 mariadb-prepare-db-dir[17956]: Alternatively consider joining our community based development effort:
Feb 08 14:47:38 ip-172-31-22-142 mariadb-prepare-db-dir[17956]: http://mariadb.com/kb/en/contributing-to-the-mariadb-project/
Feb 08 14:47:39 ip-172-31-22-142 mysqld_safe[18035]: 170208 14:47:39 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
Feb 08 14:47:39 ip-172-31-22-142 mysqld_safe[18035]: 170208 14:47:39 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
Feb 08 14:47:40 ip-172-31-22-142 systemd[1]: Started MariaDB database server.

We recommend you make sure that this installation is secure and to do so run following command:

mysql_secure_installation

You’ll be asked to provide root password so enter appropriate password and answer yes to all questions by pressing Y.

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

Now you’ll have to create a database for Invoice Ninja so please follow the instructions.

mysql -u root -p

You’ll be asked to enter password so simply enter a password and now execute the following queries to create a new database.

CREATE DATABASE ninja_data;

The above query will create a database named ninja_data. For the database you can use any name you prefer in the place of ninja_data. Make sure that you use a semicolon at the end of each query as a query always ends with a semicolon. Once the database is created you can create a new user and grant the required permissions to the user for the database.

CREATE USER 'ninja_user'@'localhost' IDENTIFIED BY 'StrongPassword';
The above query will create a user with username ninja_user. You can use any preferred username instead of ninja_user. Replace StrongPassword with a strong password.

Now provide the appropriate privileges to your database user over the database you have created. Run the following query to do so.

GRANT ALL PRIVILEGES ON ninja_data.* TO 'ninja_user'@'localhost';

Now run the following query to immediately apply the changes on the database privileges.

FLUSH PRIVILEGES;

Now you can exit from MariaDB prompt using following command.

exit.

Next you’ll need to install PHP because Invoice Ninja is based on PHP so you’ll have to install PHP. Here we are installing PHP 7, run following commands to install PHP and some required extension.

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

yum -y install php70w-fpm php70w-cli php70w-pear php70w-gd php70w-xml php70w-mysql php70w-zip php70w-mbstring php70w-mcrypt php70w-curl php70w-gmp php70w-pdo

Once the PHP installation is finished next you’ll need to edit your php.ini configuration file to meet Invoice Ninja requirements. Here we are using nano text editor, you can also install it if you want using this command.

yum -y install nano

nano /etc/php.ini

Find the line “cgi.fix_pathinfo”, uncomment it and change the value to 0 like this. cgi.fix_pathinfo=0

Save the file and exit from the text editor.

Next, you’ll have edit PHP-FPM configuration file using any text editor.
nano /etc/php-fpm.d/www.conf

Change the user and group on line 8 and 10 to the ‘nginx’ group.

user = nginx
group = nginx

Change the listen line to ‘/var/run/php/php-fpm.sock’.

listen = /var/run/php/php-fpm.sock

Find the following lines and uncomment them like this.

listen.owner = nginx
listen.group = nginx
listen.mode = 0660

Next uncomment the php-fpm environment variables in line 366-370 like this.

env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

Save the file and exit from the text editor.

Execute following commands one by one for creating a new PHP session directory and set proper ownership to the nginx group and user.

mkdir -p /var/lib/php/session

chown -R nginx:nginx /var/lib/php/session/

mkdir -p /var/run/php/

chown -R nginx:nginx /var/run/php/

Next, start PHP7.0-FPM and enable it to start at boot time using follwing commands.

systemctl start php-fpm
systemctl enable php-fpm

Installing Invoice Ninja

We have successfully installed all the dependencies required by Invoice Ninja now we are ready to download and install Invoice Ninja but first install wget and unzip package on your system if you already don’t have them installed.

yum -y install wget unzip

Next create a new directory for web root files of Invoice Ninja and then go to that directory and download the Invoice Ninja using wget package.

mkdir -p /var/www/
cd /var/www/

wget https://download.invoiceninja.com/ninja-v3.1.0.zip

Extract the Invoice Ninja zip file using unzip package and then go to the ‘ninja’ directory, simply run following commands they’ll do the job for you.

unzip ninja-v3.1.0.zip
cd ninja/

Next, you need to install composer for the laravel project so use following command to install it.

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

Next, run following commands to install dependencies required by Invoice Ninja.

composer install --no-dev -o
cp .env.example .env

Next, edit the .env file using any text editor here we are using nano text editor.

nano .env

Change the database values according to your database.

DB_DATABASE=ninja_data
DB_USERNAME=ninja_user
DB_PASSWORD=StrongPassword

Save the file and exit from the text editor. Next, edit the database configuration in the config directory.

“nano config/database.php`

We are using MariaDB as database so go to the MySQL settings at line 55 and change following lines.

'database'  => env('DB_DATABASE', 'ninja_data'),
'username'  => env('DB_USERNAME', 'ninja_user'),
'password'  => env('DB_PASSWORD', 'StrongPassword'),

Save the file and exit from the text editor and prepare the database with following command.

php artisan migrate

You’ll be asked to run this command so type yes and press Enter.

Next, seed the database with all records using following command php artisan db:seed type yes and hit the Enter button.

Run following command to generate the application key and note this application key.

php artisan key:generate

Next, edit the app.php file with nano text editor.

nano config/app.php

Go to the APP_KEY on line 85 and paste the generated key above.

'key' => env('APP_KEY', 'yourGenerated Key'),

Change proper ownership to the ninja directory using following command.

cd /var/www/
chown -R nginx:nginx ninja/

Configure SSL and the Virtual Host

Now we will generate a SSL certificate file with the openssl command. So run following commands one by one to generate SSL certificates.

mkdir -p /etc/nginx/cert/

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

chmod 600 /etc/nginx/cert/*

We have generated an SSL certificate now we will create a virtual host for Invoice Ninja using following commands.

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

Add following content to the virtual host configuration file.

server {
    # Your Domain Name - YourDomain
    listen      80;
    server_name ninja.co YourServerName;

    # Rewrite redirect to https
    add_header Strict-Transport-Security max-age=2592000;
    rewrite ^ https://$server_name$request_uri? permanent;
}

server {
    # Your Domain Name - yourDomain
    listen      443 default;
    server_name ninja.co YourServerName;

    # Enable SSL for Invoice Ninja
    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;

    # Invoice Ninja web root files
    root /var/www/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/php-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;
    }
}

Save the file and exit from text editor. Restart your nginx web server using this command:

systemctl restart nginx

Finally temporarily disable SELinux using following command.

setenforce 0

If in case you don’t have firewalld already installed on your system then you can install it using yum -y install firewalld and then start and enable it to start at boot time using following command.

systemctl start firewalld
systemctl enable firewalld

Now you’ll have to change some firewalld rules using following commands:

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

Finally open up your favorite web browser and visit your domain that you used before and finish the installation process via web GUI and provide the necessary details like database details, your name,email,password and other.

Conclusion

In this tutorial you’ve learned how to install and configure Invoice Ninja on your CentOS 7 server. You also learned how to create Invoice online within seconds. We hope now you have enough knowledge to work with Invoice Ninja.

Want your very own server? Get our 1GB memory, Xeon V4, 25GB SSD VPS for £10.00 / month.
Get a Cloud Server

Share this Article!

Related Posts

Node.js Authentication – A Complete Guide with Passport and JWT

Node.js Authentication – A Complete Guide with Passport and JWT

Truth be told, it’s difficult for a web application that doesn’t have some kind of identification, even if you don’t see it as a security measure in and of itself. The Internet is a kind of lawless land, and even on free services like Google’s, authentication ensures that abuses will be avoided or at least […]

Node.js and MongoDB: How to Connect MongoDB With Node

Node.js and MongoDB: How to Connect MongoDB With Node

MongoDB is a document-oriented NoSQL database, which was born in 2007 in California as a service to be used within a larger project, but which soon became an independent and open-source product. It stores documents in JSON, a format based on JavaScript and simpler than XML, but still with good expressiveness. It is the dominant […]

Using MySQL with Node.js: A Complete Tutorial

Using MySQL with Node.js: A Complete Tutorial

Although data persistence is almost always a fundamental element of applications, Node.js has no native integration with databases. Everything is delegated to third-party libraries to be included manually, in addition to the standard APIs. Although MongoDB and other non-relational databases are the most common choice with Node because if you need to scale an application, […]

Node.Js Vs Django: Which Is the Best for Your Project

Node.Js Vs Django: Which Is the Best for Your Project

Django and NodeJs are two powerful technologies for web development, both have great functionality, versatile applications, and a great user interface. Both are open source and can be used for free. But which one fits your project best? NodeJs is based on JavaScript, while Django is written in Python. These are two equally popular technologies […]

Nodejs Vs PHP:  Which Works Best?

Nodejs Vs PHP: Which Works Best?

Before getting into the “battle” between Node.js and PHP we need to understand why the issue is still ongoing. It all started with the increased demand for smartphone applications, their success forcing developers to adapt to new back-end technologies that could handle a multitude of simultaneous requests. JavaScript has always been identified as a client-side […]