Marcos Aguayo
Marcos Aguayo

Reputation: 7170

PHP program create installable package

I have the following PHP script, and I'd like to create a package that people can install easy, something similar to this:

sudo dpkg -i packagename.deb

Any ideas on how can I do this?

/home/script.sh

#!/bin/bash
echo "Starting NAME service"
sudo -u root php -f /home/script.php &

/home/script.php

<?php

// The worker will execute every X seconds:
$seconds = 2;

// We work out the micro seconds ready to be used by the 'usleep' function.
$micro = $seconds * 1000000;

while(true){
 // This is the code you want to loop during the service...
 $myFile = "/home/filephp.txt";
 $fh = fopen($myFile, 'a') or die("Can't open file");
 $stringData = "File updated at: " . time(). "\n";
 fwrite($fh, $stringData);
 fclose($fh);

 // Now before we 'cycle' again, we'll sleep for a bit...
 usleep($micro);
}

/etc/systemd/system/name.service

[Unit]
Description=Crawler cache Service
After=network.target

[Service]
User=root
Restart=always
Type=forking
ExecStart=/home/script.sh

[Install]
WantedBy=multi-user.target

Upvotes: 3

Views: 147

Answers (1)

Marcos Aguayo
Marcos Aguayo

Reputation: 7170

Solved creating the following folder tree:

> package_name/
>> DEBIAN/
>>> control
>> usr/
>>> share/
>>>> package_name/
>>>>> script.sh
>>>>> script.php
>> etc/
>>> systemd/
>>>> system/
>>>>> name.service

In the "control" file you have to set some parameters, like the version number or dependencies.

Package: package_name
Version: 0.0.1
Section: base
Priority: optional
Architecture: all
Depends: php, php-curl
Maintainer: Marcos Aguayo <[email protected]>
Description: Package description

After that you only have to type the following command:

sudo dpkg-deb --build package_name/

When you have the .deb package, if you want to install it you can use this command:

sudo dpkg -i package_name.deb # --> Install .deb
systemctl start package_name  # --> Start the service

Upvotes: 1

Related Questions