Symfony2 web server daemon script for CentOS6.3


Symfony2 has a [built-in webserver] works in PHP 5.4 for development environment. The server can be started by running command: `./app/console server:run`.

Today I wrote a daemon script for that built-in webserver. This script works Linux distributions; especially I tested CentOS 6.3 on [vagrant]. It will be helpful for developers who develop Symfony2 web applications on Linux virtual machines and feel un-convenience of starting the built-in webserver whenever the machine reboots. 

NOTICE: This daemon script is for development environments. You may not use it for your products or production environments.

## Installation

(1) Copy and paste the following shell script code into `/etc/init.d/symfony-server` in your Lunix.

```sh:/etc/init.d/symfony-server
#!/bin/sh

# symfony-server        init file for starting up the symfony2 server daemon
#
# chkconfig:   - 20 80
# description: Starts and stops the symfony-server daemon.

# Source function library.
. /etc/init.d/functions

name="symfony-server"
prog="symfony-server"
exec="app/console"
pidfile=/var/run/symfony-server/symfony-server.pid
lockfile=/var/lock/subsys/symfony-server

SYMFONY_DIR="/vagrant"
SYMFONY_SERVER_USER="root"
SYMFONY_SERVER_ARGUMENTS="server:run 0.0.0.0:80 --env=dev"

start() {
    [ -d "$SYMFONY_DIR" ] || exit 7
    cd "$SYMFONY_DIR"
    [ -x "$exec" ] || exit 5
    echo -n $"Starting $name: "
    daemon --pidfile=$pidfile --user $SYMFONY_SERVER_USER "$exec $SYMFONY_SERVER_ARGUMENTS >> /dev/null 2>&1 &"

    retval=$?
    echo
    if [ $retval -ne 0 ]; then
        return $retval
    fi

    touch "$lockfile"

    sleep 3

    pid=`pidof php`

    if [ -n "$pid" ]; then
        echo $pid > $pidfile
        return 0
    else
        return 1
    fi
}

stop() {
    echo -n $"Stopping $name: "
    killproc -p $pidfile $name

    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

reload() {
    false
}

rh_status() {
    status -p $pidfile $name
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        start
        ;;
    stop)
        rh_status_q || exit 0
        stop
        ;;
    restart)
        restart
        ;;
    reload)
        rh_status_q || exit 7
        reload
        ;;
    status)
        rh_status
        ;;
    condrestart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart}"
        exit 2
esac
exit $?
```

(2) If you need, change the path of `SYMFONY_DIR` to fit your Symfony2.


(3) Add execution permission to `/etc/init.d/symfony-server`.

```
sudo chmod +x /etc/init.d/symfony-server
```

(4) Add it to the servicies automatically start on booting

```
sudo chkconfig --add symfony-server
sudo chkconfig symfony-server on
```

(5) Finally, start symfony-server service

```
sudo service symfony-server start
```

Now, enjoy it :)



## By the way, what is Qiita?

Visit [Qiita and Kobito - Great Duet to manage your knowledge.](http://qiita.com/items/392a2ff258a3c26adbfe) ;)

[vagrant]: http://www.vagrantup.com/
[built-in webserver]: https://github.com/symfony/symfony/pull/3465