Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Tutorials»Running a PHP Script as Systemd Service in Linux

    Running a PHP Script as Systemd Service in Linux

    By RahulApril 13, 20233 Mins Read

    Systemd is a system and service manager for Linux operating systems. It is widely used to manage services, network interfaces, and other system resources on Linux-based servers. In this tutorial, we’ll explain how to run a PHP script as a systemd service on Linux.

    Advertisement

    To create a systemd service for a PHP script, you’ll need to create a unit file that describes the service’s configuration. The unit file should be saved in the /etc/systemd/system directory and should have a .service file extension. Here are the steps to create a systemd service for a PHP script:

    Step 1: Create the PHP Script

    First, create the PHP script that you want to run as a systemd service. For example, let’s create a script called myscript.php that simply outputs “Hello, world!” when run:

    1
    2
    3
    <?php
       echo "Hello world!";
    ?>

    Save this file in a directory of your choice, such as “/var/www/html”.

    Step 2: Create the Systemd Unit File

    Next, create the systemd unit file for the PHP script. This file will define the service’s configuration, such as its name, description, and how it should be started and stopped. Create a file called “myscript.service” in the “/etc/systemd/system” directory:

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

    Add the following content to the file:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [Unit]
    Description=My PHP Script
     
    [Service]
    Type=simple
    ExecStart=/usr/bin/php /var/www/html/myscript.php
    Restart=always
     
    [Install]
    WantedBy=multi-user.target

    This unit file tells systemd to create a service called myscript that runs the myscript.php script using the PHP interpreter. It also specifies that the service should be restarted automatically if it fails for any reason.

    Step 3: Reload the Systemd Configuration

    Once you’ve created the unit file, you need to reload the systemd configuration to make it aware of the new service. You can do this by running the following command:

    sudo systemctl daemon-reload 
    

    Step 4: Start and Enable the Service

    Finally, you can start the “myscript” service by running the following command:

    sudo systemctl start myscript 
    

    You can also enable the service to start automatically at boot time by running the following command:

    sudo systemctl enable myscript 
    

    To check the status of the service, you can run the following command:

    sudo systemctl status myscript 
    

    This will show you whether the service is running, whether it has failed, and any recent log output.

    Conclusion

    In this tutorial, we’ve shown you how to create a systemd service for a PHP script on Linux. By following these steps, you can run a PHP script as a systemd service and ensure that it starts automatically at boot time, restarts if it fails, and is easily manageable with the systemctl command. This can be useful for running PHP scripts that need to run continuously in the background, such as daemons, cron jobs, or long-running processes.

    PHP systemd
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    What are the difference between SH and BASH

    What are the difference between SH and BASH?

    Understanding the Zombie Processes in Linux

    How to Schedule a Cron Job for PHP: A Step-by-Step Guide

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Setting Up Angular on Ubuntu: Step-by-Step Guide
    • Converting UTC Date and Time to Local Time in Linux
    • Git Restore: Functionality and Practical Examples
    • Git Switch: Functionality and Practical Examples
    • Git Switch vs. Checkout: A Detailed Comparison with Examples
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.