How to Install Invoice Ninja on CentOS 7

Invoice Ninja is free and open source web-based software for invoicing, payments, time tracking and much more. You can create Invoices online in seconds, integrate with payments gateways like a stripe, PayPal, and WePay. Invoice Ninja can show you live invoice as PDF file, you can setup your own company logo and use custom invoice templates. Invoice Ninja is based on PHP, build with Laravel Framework and can be installed on Linux, Windows and Mac.

In this tutorial, I will show you how to install and configure Invoice Ninja on a CentOS 7 server. I will use Nginx as the web server and MariaDB as Database system.

Prerequisite

  • CentOS 7 Server
  • Root privileges

Step 1 - Install Nginx

In this step, we will install the Nginx web server. Connect to your server with your ssh rot account.

ssh [email protected]

Before installing Nginx, you must install the Epel repository on your CentOS system.

yum -y install epel-release

Now you can install Nginx with the yum command below from the epel repository:

yum -y install nginx

When the installation is done, start Nginx and enable it to start at the boot time.

systemctl start nginx
systemctl enable nginx

Make sure Nginx is running by checking the port used by Nginx (port 80).

netstat -plntu

Install Nginx on CentOS 7

Note:

If you the netstat command is not found, you can install net-tools like this:

yum -y install net-tools

Step 2 - Install and Configure MariaDB Server

After installing Nginx, we need to install the mariadb-server on the system. It's available in CentOS repository. Install mariadb-server and all packages needed for MariaDB with the command below.

yum install -y mariadb-server

Start the mariadb service and enable it to start at boot time with the systemctl command.

systemctl start mariadb
systemctl enable mariadb

MariaDB is started, now you can set the root password for MariaDB with the 'mysql_secure_installation' command.

mysql_secure_installation

Set your MariaDB root password.

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

If all is done, you can try to connect with MySQL shell and root user.

mysql -u root -p
TYPE YOUR ROOT PASSWORD

You will see the MySQL shell.

Next, we need to create a new database and a new user for Invoice Ninja in the MySQL shell that we opened. We will create a new database named 'ninjadb', a new user 'ninja' with password 'aqwe123'. Please choose a different secure password for yur installation.

Create everything with the MySQL queries below: create a new database, create a new user and password, grant database access to the new user with password.

create database ninjadb;
create user ninja@localhost identified by 'aqwe123';
grant all privileges on ninjadb.* to ninja@localhost identified by 'aqwe123';
flush privileges;

Configure MySQL for Invoice Ninja on CentOS 7

MariaDB has been installed and a new database and user for Invoice Ninja have been created.

Step 3 - Install and Configure PHP7.0-FPM

Invoice Ninja is based on PHP, so we need to install it on the system. I will use PHP7.0-FPM for the Invoice Ninja installation.

There is no PHP7.0 in the CentOS default repositiry, so we need to add a new PHP7.0 repository to the system. I will use the webtatic repository.

Add the PHP7.0 repository to the system.

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

Now you can install PHP7.0-FPM and other PHP extensions needed by Invoice Ninja from the webtatic repository.

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

When the installation is finished, edit the php.ini configuration file with vim.

vim /etc/php.ini

Uncomment the cgi.fix_pathinfo line below and change the value to 0.

cgi.fix_pathinfo=0

Save the file and exit vim.

Then edit the PHP-FPM configuration file.

vim /etc/php-fpm.d/www.conf

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

user = nginx
group = nginx

Instead of a port, we will use a socket file for php-fpm. Change the listen line to '/var/run/php/php-fpm.sock'.

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

Uncomment the socket file configuration, owner, group, and permission.

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

Uncomment the php-fpm environment variables in line 366-370.

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 the editor.

Now we need to create a new PHP session directory and change the owner to the nginx user and group.

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

Create a new directory for the socket file and change permission to the nginx user and group.

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

Next, start PHP7.0-FPM and add it to start at boot time.

systemctl start php-fpm
systemctl enable php-fpm

Install and Configure PHP-FPM 7 on CentOS 7

PHP-FPM has been started. Now you can check it with the command below and you will see the PHP socket file.

netstat -pl

PHP7.0-FPM and all extensions needed by Invoice Ninja are installed.

Step 4 - Install and Configure Invoice Ninja

In this step, we will download and configure Invoice Ninja. First, install unzip on your system.

yum -y install unzip

Create a new directory for the Invoice Ninja webroot files.

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

Download Invoice Ninja with the wget command.

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

Extract the Invoice Ninja zip file and go to the 'ninja' directory.

unzip ninja-v3.1.0.zip
cd ninja/

For the Laravel project, we need to install composer, a Dependency Manager for PHP.

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

Install Composer on CentOS 7

Now you can use the composer command.

Next, install the Invoice Ninja dependencies with the composer command below.

composer install --no-dev -o
  • --no-dev: Disables the installation of require-dev packages.
  • -o: Optimize autoloader during autoloader dump.

When the dependency installation is done, copy the .env file and edit it with vim.

cp .env.example .env
vim .env

Change the value of the database settings below.

DB_DATABASE=ninjadb
DB_USERNAME=ninja
DB_PASSWORD=aqwe123

environment configuration for Invoice Ninja

Save and exit.

Next, edit the database configuration in the config directory.

vim config/database.php

We are using MariaDB/MySQL database, go to the MySQL settings at line 55.

'database'  => env('DB_DATABASE', 'ninjadb'),
'username'  => env('DB_USERNAME', 'ninja'),
'password'  => env('DB_PASSWORD', 'aqwe123'),

Database Configureation Invoice Ninja

Save and exit.

All configuration files are edited, next prepare the database with the command below.

php artisan migrate

You will be asked for a run the command, type 'yes' and press Enter.

Migrate Database Invoice Ninja

Next, seed the database with all records.

php artisan db:seed

Type 'yes' and press Enter to confirm.

Database Seed Invoice Ninja

Generate the application key.

php artisan key:generate

You will see the application key.

Edit the app.php file with vim.

vim config/app.php

Go to the APP_KEY line 85 and paste the generated key (the key below is an example, use the key that you got from the command above instead).

'key' => env('APP_KEY', 'base64:0o5QLWbNeDCNer064+600Hl8oJ20OPCIymadKJQ1RGo='),

Save and exit.

Finally, change the owner of the '/var/www/ninja' directory to the 'nginx' user and group.

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

Generate Application Key for Invoice Ninja

Invoice Ninja has been configured and is ready for the installation.

Step 5 - Configure SSL and the Virtual Host

In this step, we will generate a SSL Certificate file with the openssl command and create a new virtual host configuration for Invoice Ninja. If you are on a live server, you can use a free SSL from Let's Encrypt as well.

Create a new 'cert' directory for the SSL files.

mkdir -p /etc/nginx/cert/

Run the OpenSSL command below to generate the certificate files.

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

Change the permission of the certificate files to '600'.

chmod 600 /etc/nginx/cert/*

Generate New SSL Certificates for Invoice Ninja

Next, go to the Nginx directory and create a new virtual host configuration file named 'ninja.conf'.

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

Paste virtual host configuration below.

server {
    # Your Domain Name - hakase-labs.co
    listen      80;
    server_name ninja.co www.ninja.co;

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

server {
    # Your Domain Name - hakase-labs.co
    listen      443 default;
    server_name ninja.co www.ninja.co;

    # 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 and exit.

Now test the Nginx configuration and make sure there are no errors.

nginx -t

Restart the Nginx web server.

systemctl restart nginx

Configure Nginx Virtual Host for Invoice Ninja

The Invoice Ninja virtual host configuration has been created.

Step 6 - Configure SELinux and Firewalld

If your SELinux is off, you can skip this step and start to configure Firewalld. In this step, we will configure SELinux and Firewalld for Invoice Ninja. Check your SELinux status and make sure firewalld installed.

Check the SELinux status with the command below.

getenforce

If your SELinux is on, you will see the results 'Enforcing' or 'Permissive'.

Now install the SELinux management tools from the repository.

yum -y install policycoreutils-python

And execute the commands below to allow Invoice Ninja running under SELinux in Enforcing mode.

semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/ninja(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/ninja/public(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/ninja/storage(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/ninja/app(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/ninja/bootstrap(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/ninja/config(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/ninja/database(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/ninja/resources(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/ninja/vendor(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/ninja/tests(/.*)?'
restorecon -Rv '/var/www/ninja/'

Next, we need to open the server port so Invoice Ninja can be accessed from outside.

Make sure firewalld is installed on your system, or you can install it with yum command.

yum -y install firewalld

Start firewalld and enable it to be started boot time.

systemctl start firewalld
systemctl enable firewalld

Now we need to open the HTTP and HTTPS ports for Invoice Ninja. Run the commands below to open the ports.

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

SELinux and Firewalld are configured.

Step 7 - Testing

Open your web browser and type in the Invoice Ninja URL, in my case: ninja.co.

You will be redirected to an https connection and the setup page.

Invoice Ninja Installation Configuration

Enter the requestd configuration details, Application Settings, Database Connection, Email Settings, User Details and check the Invoice Ninja TOS.

Database Settings and Application Settings Invoice Ninja

Click on 'Submit' and you will be redirected to the login page of Invoice Ninja.

Install Invoice Ninja on CentOS 7

Type in your email and password, then press 'LOGIN'.

Invoice Ninja Login Page

You will see the Invoice Ninja Dashboard.

Invoice Ninja Dashboard

Invoice Ninja Settings Page.

Invoice Ninja Settings

Invoice Ninja has been installed with Nginx and MariaDB on CentOS 7.

References

Share this page:

5 Comment(s)