# rc.d script issues, trying to launch second nginx



## wonslung (May 9, 2009)

ok...i'm very very new to freebsd and the rc.d scripts are killing me =)
i recently switched over to freebsd from ubuntu for a few great reasons....i love it but i'm having trouble..

i need to have a second instance of nginx running as a different web user to accomplish something for my situation.

i was able to do this easily in ubuntu but rc.d has me stumped
i changed what i thought was enough to make it work and then i ended up changing more....but no matter what i do it always uses the default configuration file even though i've told it to use a different one.

first i'll post the original script/config file location then i'll post my new one...
i'm sure it's a simple stupid mistake but if someone can point me right.....


original nginx rc.d script

```
#!/bin/sh
# $FreeBSD: ports/www/nginx/files/nginx.sh.in,v 1.4 2008/10/15 07:36:39 osa Exp $

# PROVIDE: nginx
# REQUIRE: LOGIN cleanvar
# KEYWORD: shutdown

#
# Add the following lines to /etc/rc.conf to enable nginx:
# nginx_enable (bool):          Set to "NO" by default.
#                               Set it to "YES" to enable nginx
# nginx_profiles (str):         Set to "NO" by default.
#                               Unsupported feature, reserved for future releases.
# nginxlimits_enable (bool):    Set to "NO" by default.
#                               Set it to yes to run `limits $limits_args`
#                               just before nginx starts.
# nginx_flags (str):            Set to "" by default.
#                               Extra flags passed to start command.
# nginxlimits_args (str):       Default to "-e -U %%WWWOWN%%"
#                               Arguments of pre-start limits run.

. /etc/rc.subr

name="nginx"
rcvar=`set_rcvar`

start_precmd="nginx_precmd"
restart_precmd="nginx_checkconfig"
reload_precmd="nginx_checkconfig"
configtest_cmd="nginx_checkconfig"
upgrade_precmd="nginx_checkconfig"
upgrade_cmd="nginx_upgrade"
command="/usr/local/sbin/nginx"
_pidprefix="/var/run/nginx"
pidfile="${_pidprefix}.pid"
required_files=/usr/local/etc/nginx/nginx.conf

[ -z "$nginx_enable" ]       && nginx_enable="NO"
[ -z "$nginx_profiles" ]     && nginx_profiles="NO"
[ -z "$nginx_flags" ]        && nginx_flags=""
[ -z "$nginxlimits_enable" ] && nginxlimits_enable="NO"
[ -z "$nginxlimits_args" ]   && nginxlimits_args="-e -U %%WWWOWN%%"

load_rc_config $name

if [ -n "$2" ]; then
        echo "Profiles are unsupported by this version, exit..."
        exit 1
fi

nginx_checkconfig()
{
        echo "Performing sanity check on nginx configuration:"
        eval ${command} ${nginx_flags} -t
}

nginx_upgrade()
{
        echo "Upgrading nginx binary:"

        reload_precmd=""
        sig_reload="USR2"
        run_rc_command ${rc_prefix}reload $rc_extra_args || return 1

        sleep 1

        echo "Stopping old binary:"

        sig_reload="QUIT"
        pidfile="$pidfile.oldbin"
        run_rc_command ${rc_prefix}reload $rc_extra_args || return 1
}

nginx_precmd()
{
        nginx_checkconfig

        if checkyesno nginxlimits_enable
        then
                eval `/usr/bin/limits ${nginxlimits_args}` 2>/dev/null
        else
                return 0
        fi
}

extra_commands="reload configtest upgrade"
run_rc_command "$1"
```

so i made a new config file at /usr/local/etc/nginx/ called nginx2.comnf
changed everything in the script i thought i needed to change
set the proper rc.conf enable="YES" and it won't start with the NEW config
it keeps using the one one
here is my wrongly modified script

```
#!/bin/sh
# $FreeBSD: ports/www/nginx/files/nginx.sh.in,v 1.4 2008/10/15 07:36:39 osa Exp $

# PROVIDE: nginx
# REQUIRE: LOGIN cleanvar
# KEYWORD: shutdown

#
# Add the following lines to /etc/rc.conf to enable nginx:
# nginx_enable (bool):          Set to "NO" by default.
#                               Set it to "YES" to enable nginx
# nginx_profiles (str):         Set to "NO" by default.
#                               Unsupported feature, reserved for future releases.
# nginxlimits_enable (bool):    Set to "NO" by default.
#                               Set it to yes to run `limits $limits_args`
#                               just before nginx starts.
# nginx_flags (str):            Set to "" by default.
#                               Extra flags passed to start command.
# nginxlimits_args (str):       Default to "-e -U %%WWWOWN%%"
#                               Arguments of pre-start limits run.

. /etc/rc.subr

name="nginx2"
rcvar=`set_rcvar`

start_precmd="nginx_precmd"
restart_precmd="nginx_checkconfig"
reload_precmd="nginx_checkconfig"
configtest_cmd="nginx_checkconfig"
upgrade_precmd="nginx_checkconfig"
upgrade_cmd="nginx_upgrade"
command="/usr/local/sbin/nginx2"
_pidprefix="/var/run/nginx2"
pidfile="${_pidprefix}.pid"
required_files=/usr/local/etc/nginx/nginx2.conf

[ -z "$nginx_enable" ]       && nginx_enable="NO"
[ -z "$nginx_profiles" ]     && nginx_profiles="NO"
[ -z "$nginx_flags" ]        && nginx_flags=""
[ -z "$nginxlimits_enable" ] && nginxlimits_enable="NO"
[ -z "$nginxlimits_args" ]   && nginxlimits_args="-e -U %%WWWOWN%%"

load_rc_config $name

if [ -n "$2" ]; then
        echo "Profiles are unsupported by this version, exit..."
        exit 1
fi

nginx_checkconfig()
{
        echo "Performing sanity check on nginx configuration:"
        eval ${command} ${nginx_flags} -t
}

nginx_upgrade()
{
        echo "Upgrading nginx binary:"

        reload_precmd=""
        sig_reload="USR2"
        run_rc_command ${rc_prefix}reload $rc_extra_args || return 1
 
        sleep 1

        echo "Stopping old binary:"

        sig_reload="QUIT"
        pidfile="$pidfile.oldbin"
        run_rc_command ${rc_prefix}reload $rc_extra_args || return 1
}

nginx_precmd()
{
        nginx_checkconfig

        if checkyesno nginxlimits_enable
        then
                eval `/usr/bin/limits ${nginxlimits_args}` 2>/dev/null
        else
                return 0
        fi
}

extra_commands="reload configtest upgrade"
run_rc_command "$1"
```
any help would make me owe you a million


----------



## wonslung (May 10, 2009)

so noone has any advice?


----------



## gordon@ (May 10, 2009)

The reason that nginx is using the old config is because the binary is built with the config filename for the first one. The rc.d script is starting up nginx by just invoking the command ('/usr/local/sbin/nginx2' in this case). You'll need to figure out the correct start command to force nginx to use a different config file and pass that to the startup command in the rc.d script.

You probably need to do something like:

```
command="/usr/local/sbin/nginx2"
command_args"-c /usr/local/etc/nginx/nginx2.conf"
```

Look for the comment block in /etc/rc.subr around line 390.


----------



## wonslung (May 13, 2009)

it's still not working.....this is frustrating...i guess i can try to download the source and compile a new binary


----------



## wonslung (May 13, 2009)

ok, cool, that worked....compiled a new one from the latest stable source and used --prefix=/usr/local/nginx2 --sbin=/usr/local/sbin2

then renamed /usr/local/sbin2/nginx to /usr/local/sbin/nginx2


----------

