当前位置:   article > 正文

总结Nginx的安装、配置与设置开机自启_nginx开机自启

nginx开机自启

在Ubuntu下安装Nginx有以下方法,但是如果想要安装最新版本的就必须下载源码包编译安装。

一、Nginx安装


1、基于APT源安装

sudo apt-get install nginx

安装好的文件位置:

/usr/sbin/nginx:主程序

/etc/nginx:存放配置文件

/usr/share/nginx:存放静态文件

/var/log/nginx:存放日志

从上面的根目录文件夹可以知道:

Linux系统的配置文件一般放在/etc,日志一般放在/var/log,运行的程序一般放在/usr/sbin或者/usr/bin。

当然,如果要更清楚Nginx的配置项放在什么地方,可以打开/etc/nginx/nginx.conf

Nginx如果指定默认加载/etc/nginx/nginx.conf的配置文件。如果要查看加载的是哪个配置文件,可以用这个命令sudo nginx -t(检查语法是否正确)或者ps -ef | grep nginx

然后通过这种方式安装的,会自动创建服务,会自动在/etc/init.d/nginx新建服务脚本,然后就可以使用

sudo nginx -s {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}

命令执行相关操作。

2、通过源码包编译安装

这种方式可以自定安装指定的模块以及最新的版本。方式更灵活。

官方下载页面:nginx: download

configure配置文件详解:Building nginx from Sources

安装gcc g++的依赖库

  1. sudo apt-get install build-essential
  2. sudo apt-get install libtool

安装pcre依赖库(http://www.pcre.org/

  1. sudo apt-get update
  2. sudo apt-get install libpcre3 libpcre3-dev
安装zlib依赖库(http://www.zlib.net)
sudo apt-get install zlib1g-dev

安装SSL依赖库

sudo apt-get install openssl

安装Nginx

  1. #下载最新版本:
  2. wget http://nginx.org/download/nginx-1.13.6.tar.gz
  3. #解压:
  4. tar -zxvf nginx-1.13.6.tar.gz
  5. #进入解压目录:
  6. cd nginx-1.13.6
  7. #配置:
  8. ./configure --prefix=/usr/local/nginx
  9. #编译:
  10. make
  11. #安装:
  12. sudo make install
  13. #启动:
  14. sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  15. 注意:-c 指定配置文件的路径,不加的话,nginx会自动加载默认路径的配置文件,可以通过-h查看帮助命令。
  16. #查看进程:
  17. ps -ef | grep nginx

配置软链接

sudo ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

现在就可以不用路径直接输入nginx启动。

二、配置开机启动服务

1、方法一:

在/etc/init.d/下创建nginx文件,sudo vim /etc/init.d/nginx,内容如下:

  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: nginx
  4. # Required-Start: $local_fs $remote_fs $network $syslog $named
  5. # Required-Stop: $local_fs $remote_fs $network $syslog $named
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: starts the nginx web server
  9. # Description: starts nginx using start-stop-daemon
  10. ### END INIT INFO
  11. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  12. DAEMON=/usr/local/nginx/sbin/nginx
  13. NAME=nginx
  14. DESC=nginx
  15. # Include nginx defaults if available
  16. if [ -r /etc/default/nginx ]; then
  17. . /etc/default/nginx
  18. fi
  19. STOP_SCHEDULE="${STOP_SCHEDULE:-QUIT/5/TERM/5/KILL/5}"
  20. test -x $DAEMON || exit 0
  21. . /lib/init/vars.sh
  22. . /lib/lsb/init-functions
  23. # Try to extract nginx pidfile
  24. PID=$(cat /usr/local/nginx/conf/nginx.conf | grep -Ev '^\s*#' | awk 'BEGIN { RS="[;{}]" } { if ($1 == "pid") print $2 }' | head -n1)
  25. if [ -z "$PID" ]; then
  26. PID=/run/nginx.pid
  27. fi
  28. if [ -n "$ULIMIT" ]; then
  29. # Set ulimit if it is set in /etc/default/nginx
  30. ulimit $ULIMIT
  31. fi
  32. start_nginx() {
  33. # Start the daemon/service
  34. #
  35. # Returns:
  36. # 0 if daemon has been started
  37. # 1 if daemon was already running
  38. # 2 if daemon could not be started
  39. start-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON --test > /dev/null \
  40. || return 1
  41. start-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON -- \
  42. $DAEMON_OPTS 2>/dev/null \
  43. || return 2
  44. }
  45. test_config() {
  46. # Test the nginx configuration
  47. $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1
  48. }
  49. stop_nginx() {
  50. # Stops the daemon/service
  51. #
  52. # Return
  53. # 0 if daemon has been stopped
  54. # 1 if daemon was already stopped
  55. # 2 if daemon could not be stopped
  56. # other if a failure occurred
  57. start-stop-daemon --stop --quiet --retry=$STOP_SCHEDULE --pidfile $PID --name $NAME
  58. RETVAL="$?"
  59. sleep 1
  60. return "$RETVAL"
  61. }
  62. reload_nginx() {
  63. # Function that sends a SIGHUP to the daemon/service
  64. start-stop-daemon --stop --signal HUP --quiet --pidfile $PID --name $NAME
  65. return 0
  66. }
  67. rotate_logs() {
  68. # Rotate log files
  69. start-stop-daemon --stop --signal USR1 --quiet --pidfile $PID --name $NAME
  70. return 0
  71. }
  72. upgrade_nginx() {
  73. # Online upgrade nginx executable
  74. # http://nginx.org/en/docs/control.html
  75. #
  76. # Return
  77. # 0 if nginx has been successfully upgraded
  78. # 1 if nginx is not running
  79. # 2 if the pid files were not created on time
  80. # 3 if the old master could not be killed
  81. if start-stop-daemon --stop --signal USR2 --quiet --pidfile $PID --name $NAME; then
  82. # Wait for both old and new master to write their pid file
  83. while [ ! -s "${PID}.oldbin" ] || [ ! -s "${PID}" ]; do
  84. cnt=`expr $cnt + 1`
  85. if [ $cnt -gt 10 ]; then
  86. return 2
  87. fi
  88. sleep 1
  89. done
  90. # Everything is ready, gracefully stop the old master
  91. if start-stop-daemon --stop --signal QUIT --quiet --pidfile "${PID}.oldbin" --name $NAME; then
  92. return 0
  93. else
  94. return 3
  95. fi
  96. else
  97. return 1
  98. fi
  99. }
  100. case "$1" in
  101. start)
  102. log_daemon_msg "Starting $DESC" "$NAME"
  103. start_nginx
  104. case "$?" in
  105. 0|1) log_end_msg 0 ;;
  106. 2) log_end_msg 1 ;;
  107. esac
  108. ;;
  109. stop)
  110. log_daemon_msg "Stopping $DESC" "$NAME"
  111. stop_nginx
  112. case "$?" in
  113. 0|1) log_end_msg 0 ;;
  114. 2) log_end_msg 1 ;;
  115. esac
  116. ;;
  117. restart)
  118. log_daemon_msg "Restarting $DESC" "$NAME"
  119. # Check configuration before stopping nginx
  120. if ! test_config; then
  121. log_end_msg 1 # Configuration error
  122. exit $?
  123. fi
  124. stop_nginx
  125. case "$?" in
  126. 0|1)
  127. start_nginx
  128. case "$?" in
  129. 0) log_end_msg 0 ;;
  130. 1) log_end_msg 1 ;; # Old process is still running
  131. *) log_end_msg 1 ;; # Failed to start
  132. esac
  133. ;;
  134. *)
  135. # Failed to stop
  136. log_end_msg 1
  137. ;;
  138. esac
  139. ;;
  140. reload|force-reload)
  141. log_daemon_msg "Reloading $DESC configuration" "$NAME"
  142. # Check configuration before stopping nginx
  143. #
  144. # This is not entirely correct since the on-disk nginx binary
  145. # may differ from the in-memory one, but that's not common.
  146. # We prefer to check the configuration and return an error
  147. # to the administrator.
  148. if ! test_config; then
  149. log_end_msg 1 # Configuration error
  150. exit $?
  151. fi
  152. reload_nginx
  153. log_end_msg $?
  154. ;;
  155. configtest|testconfig)
  156. log_daemon_msg "Testing $DESC configuration"
  157. test_config
  158. log_end_msg $?
  159. ;;
  160. status)
  161. status_of_proc -p $PID "$DAEMON" "$NAME" && exit 0 || exit $?
  162. ;;
  163. upgrade)
  164. log_daemon_msg "Upgrading binary" "$NAME"
  165. upgrade_nginx
  166. log_end_msg $?
  167. ;;
  168. rotate)
  169. log_daemon_msg "Re-opening $DESC log files" "$NAME"
  170. rotate_logs
  171. log_end_msg $?
  172. ;;
  173. *)
  174. echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}" >&2
  175. exit 3
  176. ;;
  177. esac
  1. #设置服务脚本有执行权限
  2. sudo chmod +x /etc/init.d/nginx
  3. #注册服务
  4. cd /etc/init.d/
  5. sudo update-rc.d nginx defaults

2、方法二:

在/usr/lib/systemd/system目录下新建nginx.service文件,内容如下

  1. [Unit]
  2. Description=nginx - high performance web server
  3. After=network.target remote-fs.target nss-lookup.target
  4. [Service]
  5. Type=forking
  6. ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  7. ExecReload=/usr/local/nginx/sbin/nginx -s reload
  8. ExecStop=/usr/local/nginx/sbin/nginx -s stop
  9. [Install]
  10. WantedBy=multi-user.target

脚本解释:

Unit:服务的启动顺序和依赖关系
Description:对该服务的描述;
After:在b.target服务组启动后,再启动本服务;
Service:服务具体执行的方式
ExecStart,ExecStop,ExecReload等:启动命令组,分别是服务启动时,停止时,重启时,启动前,启动后,停止后执行的命令;
Type:服务启动类型。默认simple表示ExecStart为主进程,notify类似于simple,启动结束后会发出通知信号。另外还有forking,oneshot,dbus,idle等类型;
Install:把服务放在哪个服务组
WantedBy:服务所在的服务组。

更多请参考 systemd的.service服务文件配置

设置开机自启动并启动nginx

设置开机自启动:

systemctl enable nginx.service
查看是否正确启动:

systemctl list-unit-files |grep nginx

看下如下图就成功启动了

启动Nginx:

systemctl start nginx.service

其他常用命令:
开启开机自启动:systemctl enable nginx.service
停止开机自启动 : systemctl disable nginx.service
启动 nginx 服务 : systemctl start nginx.service
停止 nginx 服务 : systemctl stop nginx.service
重启 nginx 服务 : systemctl restart nginx.service
查看服务当前状态 : systemctl status nginx.service
查看所有已启动的服务 : systemctl list-units --type=service

三、需要注意问题:


nginx.conf中,设置以下几个参数,增加超时时间

  1. fastcgi_connect_timeout
  2. fastcgi连接超时时间,默认60
  3. fastcgi_send_timeout
  4. nginx 进程向 fastcgi 进程发送请求过程的超时时间,默认值60
  5. fastcgi_read_timeout
  6. fastcgi 进程向 nginx 进程发送输出过程的超时时间,默认值60
  7. 上传超过1M大的客户端文件无法正常上传,nginx直接报错,上传文件太大,修改了下nginx的配置,就可以了。
  8. client_max_body_size 10M;

四、nginx示例:

  1. user root;
  2. worker_processes auto;
  3. pid /run/nginx.pid;
  4. include /etc/nginx/modules-enabled/*.conf;
  5. events {
  6. worker_connections 768;
  7. # multi_accept on;
  8. }
  9. http {
  10. ##
  11. # Basic Settings
  12. ##
  13. sendfile on;
  14. tcp_nopush on;
  15. tcp_nodelay on;
  16. keepalive_timeout 65;
  17. types_hash_max_size 2048;
  18. # server_tokens off;
  19. # server_names_hash_bucket_size 64;
  20. # server_name_in_redirect off;
  21. include /etc/nginx/mime.types;
  22. default_type application/octet-stream;
  23. client_max_body_size 100m;
  24. ##
  25. # SSL Settings
  26. ##
  27. ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
  28. ssl_prefer_server_ciphers on;
  29. ##
  30. # Logging Settings
  31. ##
  32. access_log /var/log/nginx/access.log;
  33. error_log /var/log/nginx/error.log;
  34. ##
  35. # Gzip Settings
  36. ##
  37. gzip on;
  38. server {
  39. listen 80;
  40. server_name 172.16.xxx.xxx;
  41. fastcgi_buffers 8 128k;
  42. # 将该服务下的所有请求实体的大小限制为1024m
  43. client_max_body_size 1024m;
  44. #charset koi8-r;
  45. #access_log logs/host.access.log main;
  46. charset UTF-8;
  47. #access_log /var/log/nginx/nginx_access.log;
  48. #error_log /var/log/nginx/nginx_error.log;
  49. # 静态资源配置
  50. location /xxxx {
  51. add_header 'Access-Control-Allow-Origin' '*';
  52. alias /root/app/panoramademo/static;
  53. }
  54. # 路由配置
  55. location /xxx_app/{
  56. #limit_req zone=one burst=5;
  57. include uwsgi_params;
  58. uwsgi_pass 172.16.xxx.xxx:8002;
  59. uwsgi_send_timeout 100;
  60. uwsgi_connect_timeout 100;
  61. uwsgi_read_timeout 100;
  62. }
  63. }
  64. # gzip_vary on;
  65. # gzip_proxied any;
  66. # gzip_comp_level 6;
  67. # gzip_buffers 16 8k;
  68. # gzip_http_version 1.1;
  69. # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  70. ##
  71. # Virtual Host Configs
  72. ##
  73. include /etc/nginx/conf.d/*.conf;
  74. include /etc/nginx/sites-enabled/*;
  75. }
  76. #mail {
  77. # # See sample authentication script at:
  78. # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
  79. #
  80. # # auth_http localhost/auth.php;
  81. # # pop3_capabilities "TOP" "USER";
  82. # # imap_capabilities "IMAP4rev1" "UIDPLUS";
  83. #
  84. # server {
  85. # listen localhost:110;
  86. # protocol pop3;
  87. # proxy on;
  88. # }
  89. #
  90. # server {
  91. # listen localhost:143;
  92. # protocol imap;
  93. # proxy on;
  94. # }
  95. #}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/207944?site
推荐阅读
相关标签
  

闽ICP备14008679号