Create nginx scripts in Linux-start, stop, reload ..., Nginx-start

Source: Internet
Author: User

Create nginx scripts in Linux-start, stop, reload ..., Nginx-start

1. Disable nginx
Use ps-aux | grep nginx to check whether nginx is started. If so, kill and kill.
2. Create the/etc/init. d/nginx file.

root@dnnp:~/software/nginx-1.2.3# vim /etc/init.d/nginx

3. add permissions and start

root@dnnp:~/software/nginx-1.2.3# chmod +x /etc/init.d/nginxroot@dnnp:~/software/nginx-1.2.3# /etc/init.d/nginx startStarting nginx: nginx.root@dnnp:~/software/nginx-1.2.3# ps -aux | grep nginxWarning: bad ps syntax, perhaps a bogus '-'? See http://procps.sf.net/faq.htmlroot   25078 0.0 0.0  4596  700 ?    Ss  14:20  0:00 nginx: master process /usr/local/nginx/sbin/nginxnobody  25079 0.0 0.1  4820 1056 ?    S  14:20  0:00 nginx: worker processroot   25081 0.0 0.0  3304  768 pts/0  S+  14:20  0:00 grep nginxroot@dnnp:~/software/nginx-1.2.3#

Note: The content of the/etc/init. d/nginx file is as follows:

#! /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/binDAEMON=/usr/local/nginx/sbin/nginxNAME=nginxDESC=nginx test -x $DAEMON || exit 0 # Include nginx defaults if availableif [ -f /etc/default/nginx ] ; then  . /etc/default/nginx#    . /usr/local/nginx/conffi 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


How to restart nginx in linux

In earlier versions of nginx, the kill command is used to send a signal to nginx to restart nginx.
However, the-s option is added to nginx to stop and reload nginx.
1. For a smooth restart of nginx, you can use the./nginx-s reload command to achieve a smooth restart of nginx.
2. If it is not a smooth restart, you can stop nginx and then start it:
./Nginx-s stop &./nginx

After we modify the nginx configuration, we want to restart nginx to make nginx take effect. In this case, to ensure that nginx can provide normal services during the restart phase, we usually use a smooth restart (reload) method) restart nginx. In this case, nginx loads the new configuration and fork the new worker process. At the same time, the master process sends a signal to the old worker process to tell the current situation of the old worker process. After the old worker process receives the signal from the master process, if the request is not processed at the time, it will exit. If the request is being processed, the old worker process will finish processing the request and then exit. Nginx reload the new configuration in this way, so that the service can still be provided during the restart process.

Restart nginx. The command is displayed. Enter the following command:

Start service stop Service reload re-reading configuration restart service
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.