How to run service with PHP and Java API on Debian 9

For many projects, there is a need to use several programming languages on the back-end. We will consider creating a Java API service using PHP as the back-end language for retrieving and displaying data for the user.

- Debian 9
- a sudo non root user
- Nginx with SSL
- PostgreSQL


Step 1. Copy files to the server to serve site

Create dir where project files would be located:

sudo mkdir /var/www/html/costs

Then make dir in the home folder to copy files from local machine:

sudo mkdir ~/site

Copy files from local machine to the server:

local$ rsync -chavzP --stats site_backup_2018_05_14/

And move those folder with files to place when it should be:

sudo mv ~/site/ /var/www/html/costs/site


Step 2. Configure Nginx to process site with PHP

Create new Nginx server block file:
sudo nano /etc/nginx/sites-available/costs

# Costs
server {
listen 80;
return 301 https://$host$request_uri;

server { listen ssl; include snippets/; include snippets/ssl-params.conf;

root /var/www/html/costs/site;

index index.php;


location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { log_not_found off; access_log off; allow all; } location / { try_files $uri $uri/ /index.php?q=$uri&$args; rewrite ^/(index)$ /$1.php last; rewrite ^/(login)$ /$1.php last; rewrite ^/(logout)$ /$1.php last; rewrite ^/(crud)$ /$1.php last; rewrite ^/(list)$ /$1.php last; rewrite ^/(analysis)$ /$1.php last; }

location ~* \.(jpg|jpeg|gif|css|png|js|ico|html|woff2)$ { access_log off; expires 30d; }

location ~ /\.ht { deny  all; }

location ~ \.php$ { fastcgi_index index.php; fastcgi_keep_conn on; include /etc/nginx/fastcgi_params; fastcgi_pass unix:/run/php/php7.1-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }


Make symlink:

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

Check and reload nginx:

sudo nginx -t
sudo systemctl reload nginx


Step 3. Create service to run JAR file

Copy jar file to the server

rsync -chavzP --stats CostMonitorServer-1.0-SNAPSHOT.jar

Move jar file to the folder where it should be located:

sudo mv CostMonitorServer-1.0-SNAPSHOT.jar /var/www/html/costs

To be able run your jar file automatically when server was restarted we will create systemd service.

Create service:

sudo nano /etc/systemd/system/costs.service

Description=Cost-monitor Java REST Service

[Service] WorkingDirectory=/var/www/html/costs ExecStart       = /usr/bin/java -Xms256m -Xmx1g -jar CostMonitorServer-1.0-SNAPSHOT.jar \ --COSTMONITOR_DB_URL=jdbc:postgresql://localhost:5432/costsdb \ --COSTMONITOR_DB_USERNAME=costs \ --COSTMONITOR_DB_PASSWORD=costs-pass \ --COSTMONITOR_JWT_SECRET=j2XD6jahUgZqHmpGuA9v \ --COSTMONITOR_PORT=9000 SuccessExitStatus=143 TimeoutStopSec=10 Restart=on-failure RestartSec=5


As you can see, we also specified few program arguments, including port (9000).

Next, we apply service changes:

sudo systemctl daemon-reload

And enable and start service:

sudo systemctl enable costs.service
sudo systemctl start costs
sudo systemctl status costs

To see logs use this command:

sudo journalctl --unit=costs

See real-time logs by using the -f option.

If you want to trim them, use -n <# of lines> to view the specified number of lines of the log:

sudo journalctl -f -n 1000 -u costs

Tail the live log using the -f option:

sudo journalctl -f -u costs

Stop the service by using:

sudo systemctl stop costs


Step 4. Work with PostgreSQL

It's not recommended to use the approach as described below, because the code was written long ago.
That's bad but it works, so as a pretty famous rule: If it works don't touch anything :) You can use Liquibase to populate data instead.

Login to database

psql costsdb -U costs

Populate with populate-data.sql file data:

INSERT INTO public.roles (id, role) VALUES (1, 'ADMIN'); INSERT INTO public.roles (id, role) VALUES (2, 'USER');

Install curl to be able to send request:

sudo apt install curl

Register user

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ "name":"Valerii R", "email": "", "password": "tBAA7NC5T2GxuFxXrgcs" }' ''

Grand amdin role to user:

psql costsdb -U costs
UPDATE users_roles SET role_id=1 WHERE user_id=1;


Step 5. Configure Nginx to process Java API

Create block for api:

cd /etc/nginx/sites-available/
sudo nano api-costs

# Api costs
server {
listen 80;
return 301 https://$host$request_uri;
server {

listen ssl; server_name;

include snippets/; include snippets/ssl-params.conf;

location / {

proxy_set_header        Host $host:$server_port; proxy_set_header        X-Real-IP $remote_addr; proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header        X-Forwarded-Proto $scheme;

proxy_pass; proxy_read_timeout  90;


} }

Create symlink

sudo ln -s /etc/nginx/sites-available/api-costs /etc/nginx/sites-enabled/api-costs

Reload nginx

sudo systemctl reload nginx



  1. Reply
    Situs Domino99 June 27, 2018 at 12:25 am

    My relativws always say that I am killing my time here at
    web, except I know I am getting experience daily by reading thes good articles or reviews.

  2. Reply
    Darren Sparky June 30, 2018 at 4:07 pm

    Thank you for posting this awesome article. I’m a long time reader but I’ve never been compelled to leave a comment.

    I subscribed to your blog and shared this on my Facebook.
    Thanks again for a great article!

  3. Reply
    baca infonya July 22, 2018 at 10:04 am

    I thіnk the admin of this website is actually working hard in favor of his site, for the reason that here every information іs quality basеd stuff.

  4. Reply
    Elly November 28, 2018 at 8:42 pm

    It’s awesome in support of me to have a web page, which is valuable designed for my experience. thanks admin

  5. Reply
    Cialis March 20, 2019 at 10:02 pm

    Very nice post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts.
    After all I’ll be subscribing to your rss feed and I hope you write again very soon!

  6. Reply
    바카라사이트 July 5, 2019 at 9:43 am

    Thank you for the good writeup. It in fact used to be a enjoyment account it.
    Glance complex to far added agreeable from you!
    By the way, how can we communicate?

  7. Reply
    Sulfur Granules January 31, 2020 at 7:17 pm

    I couldn’t resist commenting. Perfectly written!

Leave a reply