1

I need to install nginx with ngx_pagespeed. I use this instruction: https://github.com/pagespeed/ngx_pagespeed#how-to-build

But when I finish installation, nginx file here:

/usr/local/nginx/sbin/nginx
/usr/local/nginx/conf/nginx.conf
/usr/local/nginx/logs/nginx.pid

And I've no nginx start script here:

/etc/init.d

And I can't run nginx like this:

service nginx start

And no nginx autorun.

If I install nginx like this:

sudo apt-get install nginx

Nginx location:

/usr/sbin/nginx
/etc/nginx/nginx.conf
/run/nginx.pid

I can start nginx like this:

service nginx start

And nginx autostart after server rebooting and nginx process owner www-data.

My question. Is it possible to install nginx with ngx_pagespeed but like standard configuration?:

  1. Locations: /usr/sbin/, /etc/nginx/, /run/
  2. Starting "service nginx start/restart/stop"
  3. Autoload process after server rebooting
  4. Use process owner www-data
2

nginx itself does not include init script. Because init script are different by OS and by nginx build parametters. That script is specify by repository contributers.

If you install nginx from source code https://github.com/pagespeed/ngx_pagespeed#how-to-build, then you must write your own init script

$ sudo nano /etc/init.d/nginx

paste your init script depend on your os version, nginx build param ...

then:

$ sudo chmod +x /etc/init.d/nginx
$ sudo /usr/sbin/update-rc.d -f nginx defaults
$ # start service
$ sudo service nginx start
$ # make it autostart
$ sudo chkconfig nginx on

Example of init script work for you (assume you do not change install dir):

#! /bin/sh

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/nginx
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
    . /etc/default/nginx
fi

set -e

. /lib/lsb/init-functions

case "$1" in
  start)
    echo -n "Starting $DESC: "
    start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
        --exec $DAEMON -- $DAEMON_OPTS || true
    echo "$NAME."
    ;;
  stop)
    echo -n "Stopping $DESC: "
    start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
        --exec $DAEMON || true
    echo "$NAME."
    ;;
  restart|force-reload)
    echo -n "Restarting $DESC: "
    start-stop-daemon --stop --quiet --pidfile \
        /usr/local/nginx/logs/$NAME.pid --exec $DAEMON || true
    sleep 1
    start-stop-daemon --start --quiet --pidfile \
        /usr/local/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
    echo "$NAME."
    ;;
  reload)
      echo -n "Reloading $DESC configuration: "
      start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
          --exec $DAEMON || true
      echo "$NAME."
      ;;
  status)
      status_of_proc -p /usr/local/nginx/logs/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
      ;;
  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
    exit 1
    ;;
esac

exit 0

Still want to change directory location?

Complie source code https://github.com/pagespeed/ngx_pagespeed#how-to-build,

But you MUST add all following params to your ./configure

--prefix=/etc/nginx
--conf-path=/etc/nginx/nginx.conf
--error-log-path=/var/log/nginx/error.log
--http-client-body-temp-path=/var/lib/nginx/body
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi
--http-log-path=/var/log/nginx/access.log
--http-proxy-temp-path=/var/lib/nginx/proxy
--http-scgi-temp-path=/var/lib/nginx/scgi
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi
--lock-path=/var/lock/nginx.lock
--pid-path=/var/run/nginx.pid

then write your own init script

#!/bin/sh

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx

# Include nginx defaults if available
if [ -f /etc/default/nginx ]; then
    . /etc/default/nginx
fi

test -x $DAEMON || exit 0

set -e

. /lib/lsb/init-functions

test_nginx_config() {
    if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1; then
        return 0
    else
        $DAEMON -t $DAEMON_OPTS
        return $?
    fi
}

case "$1" in
    start)
        echo -n "Starting $DESC: "
        test_nginx_config
        # Check if the ULIMIT is set in /etc/default/nginx
        if [ -n "$ULIMIT" ]; then
            # Set the ulimits
            ulimit $ULIMIT
        fi
        start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
            --exec $DAEMON -- $DAEMON_OPTS || true
        echo "$NAME."
        ;;

    stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
            --exec $DAEMON || true
        echo "$NAME."
        ;;

    restart|force-reload)
        echo -n "Restarting $DESC: "
        start-stop-daemon --stop --quiet --pidfile \
            /var/run/$NAME.pid --exec $DAEMON || true
        sleep 1
        test_nginx_config
        # Check if the ULIMIT is set in /etc/default/nginx
        if [ -n "$ULIMIT" ]; then
            # Set the ulimits
            ulimit $ULIMIT
        fi
        start-stop-daemon --start --quiet --pidfile \
            /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
        echo "$NAME."
        ;;

    reload)
        echo -n "Reloading $DESC configuration: "
        test_nginx_config
        start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
            --exec $DAEMON || true
        echo "$NAME."
        ;;

    configtest|testconfig)
        echo -n "Testing $DESC configuration: "
        if test_nginx_config; then
            echo "$NAME."
        else
            exit $?
        fi
        ;;

    status)
        status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
        ;;
    *)
        echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
        exit 1
        ;;
esac

exit 0
  • And create nginx user "useradd -s /bin/false nginx" right? – SPnova Aug 15 '13 at 11:07
  • nginx use 'nobody' by default, no need to add nginx. But you can add user=spnova in nginx.conf, then "useradd -s /bin/false spnova". You can also change default user by add --user=spnova as a param to ./configure – damphat Aug 15 '13 at 11:43
  • Thanks for your config, It is working fine for me. But In my Ubuntu (13.04 Linode) I should use this location (with your configure config) "DAEMON=/etc/nginx/sbin/nginx" – SPnova Aug 15 '13 at 11:59
0

I you wanted to add PageSpeed to your repo-installed NGINX with all of its integrations to your package and startup managers, why not consider loading dynamic PageSpeed modules to your already-installed NGINX?

Here's a Bash script (on Ubuntu 18.04 LTS using systemd) that can do that:

#!/bin/bash
# https://www.majlovesreg.one/tag/code/
# https://www.majlovesreg.one/adding-pagespeed-to-a-running-nginx-instance

# For custom NGINX version, use:
# ngver=1.14.2
# For passing via the command line, use:
# ngver=$1
# For automated detection of installed NGINX, use:
ngver=$(nginx -v 2>&1 | grep -oP '(?<=/).*')

moddir=/usr/share/nginx/modules
builddir=$(mktemp -d)

# Build in tmp directory
cd ${builddir}

# Use script provided by pagespeed
nice -n 19 ionice -c 3 bash <(curl -f -L -sS https://ngxpagespeed.com/install) -n ${ngver} -m -b ${builddir} -a '--with-compat' -y || { echo '!! error with module creation, exiting...'; exit 1; }

# Replace ngx_pagespeed.so if exists, otherwise, copy it
[ -f ${moddir}/ngx_pagespeed.so ] && sudo mv ${moddir}/ngx_pagespeed.so ${moddir}/ngx_pagespeed.so.old
sudo chmod 644 /usr/local/nginx/modules/ngx_pagespeed.so || { echo '!! error with module path, exiting...'; exit 2; }
sudo cp /usr/local/nginx/modules/ngx_pagespeed.so ${moddir}/

# If new module works well, clean up build and install files
sudo nginx -t && { sudo rm -r /usr/local/nginx; rm -r ${builddir}/incubator-pagespeed-ngx-latest-stable; rm -r ${builddir}/nginx-${ngver}; } || { echo '!! nginx conf failed, exiting...'; exit 4; }

# Restart NGINX
systemctl is-active nginx && sudo systemctl restart nginx || sudo systemctl start nginx
echo
systemctl --no-pager status nginx
echo
echo 'Build and install of ngx_pagespeed sucessful!'
echo

For configs and settings, check out https://www.majlovesreg.one/adding-pagespeed-to-a-running-nginx-instance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.