Create a Linux Service
Preface
This note provides some background and explanation for Creating a Linux service with systemd which illustrates establishing a simple service in a Linux environment.
Prerequisites
Access with root authority to an updated Linux server.
The referenced document develops its service using PHP, a server-side scripting language typically used for developing web sites and web applications. Your Linux server will need to have PHP installed:
# check if php intalled
php -version
# install php
sudo apt install php
The netcat (nc) Command
The referenced document uses the netcat (or nc) to connect to the application and service developed. Review 5 Examples of the netcat (nc) Command in Linux for some background. Note that instead of using the IP address 127.0.0.1 as the referenced document does, localhost could be used instead. As an example if your Linux Server has Apache running (i.e., listening on port 80) then
nc -C localhost 80
will open a connection to Apache and once connected the http method
GET /
should return (as html) the default page for the server (i.e., the page at the web site's root /). Press enter again or use Ctrl+C to exit the connection port 80 connection.
Development Comments
The referenced document names the php as server.php but rot13.php might be a better choice (e.g., we may have several different servers created in php so this name better identifies the service's function). The exact name doesn't matter but it's important to remember it exactly -- including upper / lower case -- and know precisely what directory it is saved in.
In using the netcat command, the -u flag indicates to connect with the UDP (user data protocol) and localhost can be used instead of the IP address.
After creating and starting the service check that is has started and if not, check syslog for guidance on why starting may have failed:
systemctl status rot13.service
tail /var/log/syslog
Once it is established as a service there is no need to open a new terminal to run it; the service is now running in the system background, not as part of a user's login session.