Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

20.2.2 Configure the Nginx service


May 24, 2021 That's what Linux should learn



Nginx is a fairly good lightweight service program for deploying dynamic websites, originally developed for the Russian portal site and trusted by users for its stability, feature-richness, low memory footprint, and ability to be complex. At present, such as Sina, NetEase, Tencent and other portal sites have used this service.

The stability of the Nginx service program stems from the use of phased resource allocation technology, which reduces CPU and memory usage, so the dynamic web environment deployed using the Nginx program is not only stable and efficient, but also consumes very few system resources. I n addition, Nginx has almost the same number of modules as Apache, and now fully supports common modules such as proxy, rewrite, mod_fcgi, ssl, and vhosts. What's more, Nginx also supports hot-deployment technology, providing uninterrupted service at 7×24 and upgrading the Nginx service program directly without suspending service.

20.2.2 Configure the Nginx service Figure 20-2 Nginx and Apache famous LOGO

Frankly speaking, although the code quality of the Nginx program is very high, the code is very standardized, the technology is mature, the module extension is also very easy, but there are still a lot of problems, such as being developed by the Russians, so the data documentation is not perfect, the quality of Chinese data is a mix of fish and dragon. But Nginx service programs have grown rapidly in recent years and are expected to have a good future in the lightweight Web server market.

Before we can formally install the Nginx service program, we also need to address the relevant software dependencies, such as the package pcre for providing a perl language-compatible regular expression library, which is an essential dependency package for the Nginx service program to implement pseudo-static functionality. Here's how to unzim, compile, build, and install the source files for the Nginx service program:

(root@linuxprobe. root@linuxprobe [email protected] root@linuxprobe . The s/usr/local/pcre (root@linuxprobe pcre-8.35) s make(root@linuxprobe pcre-8.35) s make opensl package is a program file used to provide a website encryption certificate service, and the installation directory of the service program is required to install the program to make them more controllable when you call them later.

(root@linuxprobe pcre-8.35) s cd /usr/local/src (root@linuxprobe src) tar xzv openssl-1.0.1h.tar.gz (root@linuxprobe src) s cd openssl-1.0.1h (root@linuxprobe openssl-1.0.1h ./config--prefix/usr/local/openssl(root@linuxprobe openssl-1.0.1h) make(root@linuxprobe openssl-1.0.1h) s make installation openssl package will provide a lot of available commands in the /usr/local/opensl/bin directory by default. We need to add this directory to the PATH environment variable, as we did earlier, and write it to the configuration file, and finally execute the source command so that the contents of the new PATH environment variable can take effect immediately:

[root@linuxprobe pcre-8.35]# vim /etc/profile .................. O mit some of the output information... 6 4 65 for i in /etc/profile.d/ .sh ; d o 66 if [ -r "$i" ]; t hen 67 if [ "${-# i}" != "$-" ]; t hen 68 . " $i" 69 else 70 . " $i":/dev/null 71 fi 72 fi 73 done 74 export PATH=$PATH:/usr/local/mysql/bin:/usr/local/local/Opensl/bin 75 unset i 76 unset -f pathmunge (root@linuxprobe pcre-8.35) source /etc/profile zlib package is a library file for providing compression. In fact, the Nginx service program calls these service programs do not need to know more, as long as a general understanding of its role is enough:

(root@linuxprobe pcre-8.35) s cd /usr/local/src (root@linuxprobe src) tar xzvf zlib-1.2.8.tar.gz (root@linuxprobe src) slib-1.2.8 (root@linuxprobe zlib-1.8 2.8' ./configure--prefix=/usr/local/zlib (root@linuxprobe zlib-1.2.8) s make(root@linuxprobe zlib-1.2.8) s make install to create an account for executing the Nginx service program after installing and deploying the dependent package. Account names can be customized, but don't forget, because subsequent calls are required:

[root@linuxprobe zlib-1.2.8]# cd .. r oot@linuxprobe When compiling the Nginx service program with commands, you need to set a particularly large number of parameters, where the ,--prefix parameter is used to define where the service program will be installed later,--user and -group parameters are used to specify the user name and user group that execute the Nginx service program. When calling openssl, zlib, and pcre packages with parameters, write out the decompression path for the software source package, not the installation path for the program:

[root@linuxprobe src]# tar xzvf nginx-1.6.0.tar.gz [root@linuxprobe src]# cd nginx-1.6.0/ [root@linuxprobe nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/local/src/openssl-1.0.1h -- w ith-zlib=/usr/local/src/zlib-1.2.8 --with-pcre=/usr/local/src/pcre-8.35 (root@linuxprobe ngin) a script file is also required to start the root@linuxprobe Nginx service program and add it to the boot item. Unfortunately, after installing the Nginx package, the default does not provide the user with a script file, so Mr. Liu Wei prepared a available startup script file for the reader, we only need to create the script file in the /etc/rc.d/init.d directory and copy the script content below directly (I believe that readers after mastering the contents of Chapter 4, you should be able to understand the script file smoothly).

[root@linuxprobe nginx-1.6.0]# vim /etc/rc.d/init.d/nginx

!/bin/bash

  1. # nginx - this script starts and stops the nginx daemon
  2. # chkconfig: - 85 15
  3. # description: Nginx is an HTTP(S) server, HTTP(S) reverse \
  4. # proxy and IMAP/POP3 proxy server
  5. # processname: nginx
  6. # config: /etc/nginx/nginx.conf
  7. # config: /usr/local/nginx/conf/nginx.conf
  8. # pidfile: /usr/local/nginx/logs/nginx.pid
  9. # Source function library.
  10. . /etc/rc.d/init.d/functions
  11. # Source networking configuration.
  12. . /etc/sysconfig/network
  13. # Check that networking is up.
  14. [ "$NETWORKING" = "no" ] && exit 0
  15. nginx="/usr/local/nginx/sbin/nginx"
  16. prog=$(basename $nginx)
  17. NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
  18. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
  19. lockfile=/var/lock/subsys/nginx
  20. make_dirs() {
  21. # make required directories
  22. user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
  23. if [ -z "`grep $user /etc/passwd`" ]; then
  24. useradd -M -s /bin/nologin $user
  25. fi
  26. options=`$nginx -V 2>&1 | grep 'configure arguments:'`
  27. for opt in $options; do
  28. if [ `echo $opt | grep '.*-temp-path'` ]; then
  29. value=`echo $opt | cut -d "=" -f 2`
  30. if [ ! -d "$value" ]; then
  31. # echo "creating" $value
  32. mkdir -p $value && chown -R $user $value
  33. fi
  34. fi
  35. done
  36. }
  37. start() {
  38. [ -x $nginx ] || exit 5
  39. [ -f $NGINX_CONF_FILE ] || exit 6
  40. make_dirs
  41. echo -n $"Starting $prog: "
  42. daemon $nginx -c $NGINX_CONF_FILE
  43. retval=$?
  44. echo
  45. [ $retval -eq 0 ] && touch $lockfile
  46. return $retval
  47. }
  48. stop() {
  49. echo -n $"Stopping $prog: "
  50. killproc $prog -QUIT
  51. retval=$?
  52. echo
  53. [ $retval -eq 0 ] && rm -f $lockfile
  54. return $retval
  55. }
  56. restart() {
  57. #configtest || return $?
  58. stop
  59. sleep 1
  60. start
  61. }
  62. reload() {
  63. #configtest || return $?
  64. echo -n $"Reloading $prog: "
  65. killproc $nginx -HUP
  66. RETVAL=$?
  67. echo
  68. }
  69. force_reload() {
  70. restart
  71. }
  72. configtest() {
  73. $nginx -t -c $NGINX_CONF_FILE
  74. }
  75. rh_status() {
  76. status $prog
  77. }
  78. rh_status_q() {
  79. rh_status >/dev/null 2>&1
  80. }
  81. case "$1" in
  82. start)
  83. rh_status_q && exit 0
  84. $1
  85. ;;
  86. stop)
  87. rh_status_q || exit 0
  88. $1
  89. ;;
  90. restart|configtest)
  91. $1
  92. ;;
  93. reload)
  94. rh_status_q || exit 7
  95. $1
  96. ;;
  97. force-reload)
  98. force_reload
  99. ;;
  100. status)
  101. rh_status
  102. ;;
  103. condrestart|try-restart)
  104. rh_status_q || exit 0
  105. ;;
  106. *)
  107. echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
  108. exit 2
  109. esac

After you save the script file, remember to give it 755 permissions so that you can execute the script. T he script is then executed in an absolute path, the Nginx service program is restarted with the restart parameter, and finally the Nginx service program is added to the boot item using the chkconfig command. Home and dry!

(root@linuxprobe nginx-1.6.0) s chmod 755 /etc/rc.d/init.d/nginx (root@linuxprobe nginx-1.6.0) /etc/rc.d/init .d/nginx restart restarting nginx (via systemctl): the default web page can be viewed by entering the IP address of the server in the browser after the start-up of the service program . . . root@linuxprobe . The default page of the Nginx service program is more concise than the red default page of the Apache service program, as shown in Figure 20-2.

20.2.2 Configure the Nginx service

Figure 20-3 The default page for the Nginx service program