Linux: install Linux services as system services

catalogue

Installation steps

configuration parameter

Nginx common commands

Nginx as a system service

script

After adjusting the script according to your actual situation, copy it to the specified directory

Abnormal problem

env: /etc/init.d/nginx: there is no such file or directory

PID file /var/run/nginx.pid not readable (yet?) after start.

reference resources:

Installation steps

# Download installation package
wget http://nginx.org/download/nginx-1.18.0.tar.gz
# Installation dependency
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
# decompression 
tar -zxvf linux-nginx-1.18.0.tar.gz
cd nginx-1.18.0/
# Execute configuration
./configure
# Compile and install (installed in / usr / local / nginx by default)
make
make install

Many parameters can be specified for execution configuration

./configure --with-http_ssl_module

configuration parameter

--prefix=PATH: Specifies the installation directory of nginx
--Conf path = path: specify nginx Conf configuration file path
--user=NAME: user of nginx worker process
--With PCRE: turn on PCRE regular expression support
--with-http_ssl_module: enables SSL support
--with-http_stub_status_module: used to monitor the status of Nginx
--with-http-realip_module: allows you to change the client IP address in the client request header
--With File AIO: enable File AIO
--Add module = path: add a third-party external module

Nginx common commands

  • Nginx default installation directory: / usr/local/nginx
  • Nginx main configuration file: / usr / local / nginx / conf / nginx conf
  • Nginx log file: / usr / local / nginx / logs / access log
  • Start Nginx: / usr/local/nginx/sbin/nginx

Test configuration file: / usr/local/nginx/sbin/nginx -t
Start command: / usr/local/nginx/sbin/nginx
Stop command: / usr/local/nginx/sbin/nginx -s stop/quit
Restart command: / usr/local/nginx/sbin/nginx -s reload
View process command: ps -ef | grep nginx
Smooth restart: kill -HUP [Nginx main process number (i.e. PID found by ps command)]

Nginx as a system service

The official website of nginx has provided us with a script, which is also clearly written on the official website. Save the script information in a file named nginx and put it in the system / etc / init D directory
https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/

script

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  NGINX is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid

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

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -n "$user" ]; then
      if [ -z "`grep $user /etc/passwd`" ]; then
         useradd -M -s /bin/nologin $user
      fi
      options=`$nginx -V 2>&1 | grep 'configure arguments:'`
      for opt in $options; do
          if [ `echo $opt | grep '.*-temp-path'` ]; then
              value=`echo $opt | cut -d "=" -f 2`
              if [ ! -d "$value" ]; then
                  # echo "creating" $value
                  mkdir -p $value && chown -R $user $value
              fi
          fi
       done
    fi
}

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    sleep 1
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $prog -HUP
    retval=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

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

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

The script command is as above. There are two configuration items that need to be adjusted according to the actual situation.

The path of nginx execution file has been adjusted to the path in this example
nginx="/usr/local/nginx/sbin/nginx"

nginx configuration file, adjusted to the path in this example
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

After adjusting the script according to your actual situation, copy it to the specified directory

cp nginx /etc/init.d/
# Change / etc / init D / nginx permissions
chmod 755 /etc/init.d/nginx
# Add nginx service as system service
chkconfig --add nginx

After adding successfully, you can perform service nginx start, stop, restart, status and other operations.

service nginx start # start

service nginx stop # stop

service nginx status # service status

service nginx restart # restart

Abnormal problem

env: /etc/init.d/nginx: there is no such file or directory

Because the file format is doc, Linux cannot recognize it, which leads to the problem.

  • Solution 1. Copy the contents of the file, and then copy the previous / etc / init Delete D / nginx with VI / etc / init D / nginx build another one and copy the content
  • Solution 2. dos2unix is a practical command to convert Windows format files into Unix and Linux formats. The newline character of Windows format file is \ r\n, while the newline character of Unix & Linux file is \ n. The dos2unix command is actually to convert the \ r\n in the file to \ n.

PID file /var/run/nginx.pid not readable (yet?) after start.

Since the pid file cannot be read, I can adjust the path of pid in the configuration file to this directory.

Or put nginx. In your logs directory Just copy the PID file to the / var/run / directory

reference resources:

https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/

Keywords: Linux Nginx

Added by Canman2005 on Sat, 22 Jan 2022 00:13:22 +0200