iEFdev

Code, Computers & Random Junk

Custom Script to Start/stop PHP-FPM

I am going to write a little bit more of my PHP/Apache setup later. But here is the script (fpmctl) I use to start/stop PHP-FPM. It’s pretty much based off apachectl on OS X. The usage is simular … and eq. simple.

fpmctl [start|stop|restart|graceful etc ...]

I’m not sure if it would work in a Linux environment, but I guess it could be tweaked quite easy. I haven’t tested that yet. Anyway… On OS X there are 2 parts. One LaunchDaemon and this script.

fpmctl

Let’s start with the script. Save this as fpmctl in the same folder as php-fpm. For me that is:

/usr/local/php56/sbin/fpmctl

That will keep the script within PATHassuming your PHP isnstallation is listed there.

fpmctl
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/sh
#
# fpmctl
#
# Description: Custom script based on the OS X version of `apachectl`
# to (un)load php-fpm when using launchtl
#
ARGV="$@"
#
# |||||||||||||||||||| START CONFIGURATION SECTION  ||||||||||||||||||||
# --------------------                              --------------------
#
# the path to your httpd binary, including options if necessary
PHPFPM='/usr/local/php56/sbin/php-fpm'
#
# Config file (if needed)
PHPFPMCONF='/etc/php56/php-fpm.conf'
#
# Using Links or Lynx?
LYNX="links -dump"
#
# Using `open` here (for now). It'll use the default browser
# To specify - add "-a browser" (eg. open -a Firefox)
BROWSER="open"
#
# the URL to php-fpm's status page.  If you do not
# have one, then status and fullstatus will not work.
# Add your settings to httpd-info.conf
STATUSURL="http://localhost/p56/status"
#
# --------------------                              --------------------
# ||||||||||||||||||||   END CONFIGURATION SECTION  ||||||||||||||||||||


# Program and path to LaunchDaemon
LAUNCHCTL="/bin/launchctl"
LAUNCHD_JOB="/System/Library/LaunchDaemons/net.php.php-fpm.plist"

run_launchctl() {
    if [ $UID != 0 ]; then
        echo This operation requires root.
        exit 1
    fi

    $LAUNCHCTL $@
}

ERROR=0

if [ "x$ARGV" = "x" ] ; then
    ARGV="-h"
fi

# Modified from apachectl in OS X
case $ARGV in
start)
    run_launchctl load -w $LAUNCHD_JOB
    ERROR=$?
    ;;

stop)
    run_launchctl unload -w $LAUNCHD_JOB
    ERROR=$?
    ;;

restart|graceful)
    run_launchctl unload -w $LAUNCHD_JOB 2> /dev/null
    run_launchctl load -w $LAUNCHD_JOB
    ERROR=$?
    ;;

# Test FPM configuration and exit
configtest)
    $PHPFPM -y $PHPFPMCONF -t
    ERROR=$?
    ;;

# PHP information
info)
    $PHPFPM -i
    ERROR=$?
    ;;

status-cli)
    $LYNX $STATUSURL
    ;;

status-web)
  $BROWSER $STATUSURL
  ;;

*)
    $PHPFPM "$@"
    ERROR=$?
esac

exit $ERROR

For this you need Links installed and to setup the FPM status page properly. Adjust the values/path/programs to match your installation/setup.

Make sure to set owner and permissions:

sudo chown root:wheel /usr/local/php56/sbin/fpmctl
sudo chmod +x /usr/local/php56/sbin/fpmctl

LaunchDaemon

The LaunchDaemon I use is placed in:

/System/Library/LaunchDaemons/net.php.php-fpm.plist
net.php.php-fpm.plist
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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>KeepAlive</key>
    <false/>
    <key>Label</key>
    <string>net.php.php-fpm</string>
    <key>LaunchOnlyOnce</key>
    <true/>
    <!--<key>NetworkState</key>
    <true/>-->
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/php56/sbin/php-fpm</string>
        <string>--fpm-config</string>
        <string>/etc/php56/php-fpm.conf</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>Debug</key>
    <false/>
    <!--<key>UserName</key>
    <string>_www</string>-->
    <key>ServiceDescription</key>
    <string>PHP 5.6.7 :: FPM/FastCGI</string>
    <key>StandardErrorPath</key>
    <string>/var/php56/log/php-fpm.log</string>
</dict>
</plist>

There are bits and pieces that are leftovers… I’ve kept them there for now.

To load/unload it…

sudo launchctl load -w /System/Library/LaunchDaemons/net.php.php-fpm.plist
sudo launchctl unload -w /System/Library/LaunchDaemons/net.php.php-fpm.plist

Like before. Adjust the values/path/programs to match your installation/setup. Paths to program(s), logs etc. You also need to edit your php-fpm.conf to match status, ping, logs etc…

Locations

If you wonder about the names/locations of the different files/scripts… I’ll write a post on my setup later on, but for all status pages I have adjusted the default pattern to include ap24 and p56. Just to make it reasier to remember and to use.

It looks a little bit like this:

# Apache 2.4.12
{address}/ap24          # Apache 2.4 Manual (local)
{address}/ap24/status   # Apache Server Status for {address} (via 127.0.0.1)
{address}/ap24/info     # Apache Server Information

# PHP 5.6.7
{address}/p56/          # PHP-FPM real-time status page
{address}/p56/status    # PHP-FPM status page (text-only)
{address}/p56/ping      # pong

So, to avoid name conflicts I use /ap24 and /p56. The other version of PHP (5.4.*) is an Apache module (mpd_php).


Perhaps I should put these scripts in a repo on Github?

Comments