Nginx 与 ngx_pagespeed (Ubuntu)

Nginx 与 ngx_pagespeed (Ubuntu)

我需要使用 ngx_pagespeed 安装 nginx。我使用这个指令: https://github.com/pagespeed/ngx_pagespeed#how-to-build

但是当我完成安装后,nginx 文件在这里:

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

我这里没有 nginx 启动脚本:

/etc/init.d

我无法像这样运行 nginx:

service nginx start

并且没有 nginx 自动运行。

如果我像这样安装 nginx:

sudo apt-get install nginx

Nginx 位置:

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

我可以这样启动 nginx:

service nginx start

服务器重启后 nginx 自动启动,并且 nginx 进程所有者 www-data。

我的问题。是否可以使用 ngx_pagespeed 但像标准配置一样安装 nginx?:

  1. 位置:/usr/sbin/、/etc/nginx/、/run/
  2. 启动“服务 nginx 启动/重新启动/停止”
  3. 服务器重启后自动加载过程
  4. 使用进程所有者 www-data

答案1

nginx 本身不包含 init 脚本。因为 init 脚本因操作系统和 nginx 构建参数而异。该脚本由存储库贡献者指定。

如果你从源码安装nginxhttps://github.com/pagespeed/ngx_pagespeed#how-to-build,那么你必须编写自己的初始化脚本

$ sudo nano /etc/init.d/nginx

粘贴你的初始化脚本取决于你的操作系统版本,nginx构建参数...

然后:

$ 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

适合您的初始化脚本示例(假设您不更改安装目录):

#! /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

仍想更改目录位置吗?

编译源码https://github.com/pagespeed/ngx_pagespeed#how-to-build,

但是您必须将以下所有参数添加到您的 ./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

然后编写自己的初始化脚本

#!/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

答案2

我想将 PageSpeed 添加到您的存储库安装的 NGINX 中,并将其所有集成到您的包和启动管理器中,为什么不考虑将动态 PageSpeed 模块加载到已安装的 NGINX

systemd这是一个可以执行此操作的Bash 脚本(在 Ubuntu 18.04 LTS 上使用):

#!/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

有关配置和设置,请查看https://www.majlovesreg.one/adding-pagespeed-to-a-running-nginx-instance

相关内容