Starting and stopping NginX-MySQL-PHP-FCGI on Mac OS X

As these things go, it’s super easy to get a LEMP stack up and running on Mac OS X 10.6 (Snow Leopard).  Although at that point I guess it’s a MNMP stack, but that’s an even more ridiculous mnemonic.

Starting up and shutting down each individual service, however, is a pain in the butt.  Sure, 3 services doesn’t sound like a lot now but wait until your 12th configuration tweak in the course of a few hours.  Sure, every tutorial out there has a copy-and-paste-service-script-that-only-needs-a-few-tweaks-to-get-up-and-running-on-your-system.  Forget that; I just want to sudo port install nginx mysql-server php-fcgi and rock’n’roll, bitch.

I don’t want these things running all the time, so forget setting them as startup items.  And I don’t really have the patience to make or tweak service scripts.

So instead I just whipped up a quick shell script.  I call it “web” (no .sh to save on typing) and I put it in /usr/local/bin/ which was already in my shell path. Don’t forget to make it executable.

UPDATE: I posted a new version of the shell script, including php-fpm support.

# touch /usr/local/bin/web
# chmod 755 /usr/local/bin/web
# vim /usr/local/bin/web

#! /bin/bash
# These are the paths used when installing via MacPorts
MYSQL="/opt/local/share/mysql5/mysql/mysql.server"
NGINX="/opt/local/sbin/nginx"
PHPCGI="/opt/local/bin/php-cgi"
SPAWNFCGI="/opt/local/bin/spawn-fcgi"
PIDPATH="/opt/local/var/run/nginx"if [ $1 = "start" ]; then
    sudo $MYSQL start
    echo "Starting php-cgi ..."
    sudo $SPAWNFCGI -f $PHPCGI -a 127.0.0.1 -p 9000 -P $PIDPATH/php-cgi.fastcgi.pid -u nobody -g nobody
    echo "Starting nginx ..."
    sudo $NGINX
    echo "Done!"
elif [ $1 = "stop" ]; then
    echo "Stopping nginx ..."
    sudo kill `cat $PIDPATH/nginx.pid`
    echo "Stopping php-cgi ..."
    sudo kill `cat $PIDPATH/php-cgi.fastcgi.pid`
    sudo $MYSQL stop
    echo "Done!"
elif [ $1 = "restart" ]; then
    echo "Stopping nginx ..."
    sudo kill `cat $PIDPATH/nginx.pid`
    echo "Stopping php-cgi ..."
    sudo kill `cat $PIDPATH/php-cgi.fastcgi.pid`
    sudo $MYSQL stop
    sudo $MYSQL start
    echo "Starting php-cgi ..."
    sudo $SPAWNFCGI -f $PHPCGI -a 127.0.0.1 -p 9000 -P $PIDPATH/php-cgi.fastcgi.pid -u nobody -g nobody
    echo "Starting nginx ..."
    sudo $NGINX
    echo "Done!"
fi

Note the -u nobody -g nobody under $SPAWNFCGI. That’s the user and group PHP will run under.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s