3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Symfony2 web server daemon script for CentOS6.3

Last updated at Posted at 2013-01-18

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.

/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. ;)

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?