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

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. Prerequisites

Create dir where project files would be located:

1
sudo mkdir /var/www/html/costs

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

1
sudo mkdir ~/site

Copy files from local machine to the server:

1
local$ rsync -chavzP --stats site_backup_2018_05_14/ reheda.pro:~/site

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

1
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Costs
server {
listen 80;
server_name costs.reheda.pro;
return 301 https://$host$request_uri;
}

server {
listen 127.0.0.1:443 ssl;
include snippets/ssl-reheda.pro.conf;
include snippets/ssl-params.conf;

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

index index.php;

server_name costs.reheda.pro;

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:

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

Check and reload nginx:

1
2
sudo nginx -t
sudo systemctl reload nginx

 

Step 3. Create service to run JAR file

Copy jar file to the server

1
rsync -chavzP --stats CostMonitorServer-1.0-SNAPSHOT.jar reheda.pro:~/

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

1
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:

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

/etc/systemd/system/costs.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[Unit]
Description=Cost-monitor Java REST Service
After=network.target

[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

[Install]
WantedBy=multi-user.target

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

Next, we apply service changes:

1
sudo systemctl daemon-reload

And enable and start service:

1
2
3
sudo systemctl enable costs.service
sudo systemctl start costs
sudo systemctl status costs

To see logs use this command:

1
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:

1
sudo journalctl -f -n 1000 -u costs

Tail the live log using the -f option:

1
sudo journalctl -f -u costs

Stop the service by using:

1
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

1
psql costsdb -U costs

Populate with populate-data.sql file data:

1
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:

1
sudo apt install curl

Register user

1
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ "name":"Valerii R", "email": "test@gmail.com", "password": "tBAA7NC5T2GxuFxXrgcs" }' 'http://127.0.0.1:9000/api/v1/signup'

Grand amdin role to user:

1
2
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:

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

/etc/nginx/sites-available/api-costs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Api costs
server {
listen 80;
server_name api.costs.reheda.pro;
return 301 https://$host$request_uri;
}
server {

listen 127.0.0.1:443 ssl;
server_name api.costs.reheda.pro;

include snippets/ssl-reheda.pro.conf;
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          http://127.0.0.1:9000;
proxy_read_timeout  90;

proxy_redirect      http://127.0.0.1:9000 https://api.costs.reheda.pro;

}
}

Create symlink

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

Reload nginx

1
sudo systemctl reload nginx