1. NGNIX 学习系列-基础
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1. NGNIX 学习系列-基础相关的知识,希望对你有一定的参考价值。
1.Ngnix进程控制
控制正在运行ngnix是通过发送信号来完成,主要有以下一些信号
nginx –s stop :Stops the daemon immediately (using the TERM signal).
nginx –s quit :Stops the daemon gracefully (using the QUIT signal).
nginx –s reopen :Reopens the log files.
nginx –s reload :Reloads the configuration
nginx -t : Test Configuration File
nginx –g "timer_resolution 200ms"; 指定新的配置项
2. 将Nginx作为系统服务运行(System Service)
为了让Ngnix随系统启动,需要将它设置为系统服务
System V
System V中系统服务是由init进程来管理,系统服务分为不同的级别(Runlevel State):
0 | System is halted |
1 | Single-user mode (rescue mode) |
2 | Multiuser mode, without NFS support(Debian and Ubuntu 默认级别) |
3 | Full multiuser mode(Red Hat,Fedora,CentOS6 默认级别) |
4 | Not used |
5 | Graphical interface mode(Red Hat and Fedora 默认级别) |
6 | System reboot |
centos 6 /etc目录中有不同级别服务对应的启动脚本,这些目录中脚本都是软链接到/etc/init.d目录
编写sysv script ,系统服务启动脚本通常在系统启动,service httpd start ,/etc/init.d/httpd start 时被调用
将Nginx做成System V需要3个步骤
1.编写Sysv 脚本: /etc/init.d/nginx
#!/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 # Make sure to properly indicate the full path of your Nginx binary and conf. file here nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" pid_file="/usr/local/nginx/logs/nginx.pid" get_pid(){ cat "$pid_file" } is_running(){ [ -f "$pid_file" ] && ps $(get_pid) > /dev/null 2>&1 } start() { if [ ! -x $nginx ];then echo "$nginx is not executable.." exit 3 fi if [ ! -f $NGINX_CONF_FILE ];then echo "$NGINX_CONF_FILE is not exist" exit 4 fi if is_running;then echo "$prog is Already Running..." else echo -n $"Starting $prog: " $nginx -c $NGINX_CONF_FILE retval=$? if [ $retval -ne 0 ];then echo "Unable to start $prog" exit 5 fi echo "$prog Started" fi } stop() { if is_running;then echo -n $"Stopping $prog: " kill -s QUIT "$(get_pid)" retval=$? if [ $retval -ne 0 ];then echo "Unable to stop $prog" fi echo "$prog Stopped" fi } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } status() { if is_running;then echo "Running" else echo "Stopped" exit 1 fi } case "$1" in start) $1 ;; stop) $1 ;; restart|configtest) $1 ;; reload) $1 ;; status) $1 ;; *) echo $"Usage: $0 {start|stop|status|restart|reload|configtest}" exit 2 esac
2.将nginx加入到系统默认启动级别中
[[email protected] ~]# chkconfig nginx on
3. 检查nginx 是否在对应启动级别中(在对应的rc3.d中有软链接)
[[email protected] ~]# chkconfig --list nginx
Nginx 0:off 1:off 2:on 3:off 4:on 5:on 6:off
Systemd
CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,即:/usr/lib/systemd/system ,/usr/lib/systemd/user
每一个服务以.service结尾,一般会分为3部分:[Unit]、[Service]和[Install],具体内容如下:
[Unit] Description=nginx Documentation=http://nginx.org/en/docs/After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /etc/nginx/nginx.conf ExecStart=/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf ExecReload=/usr/bin/kill -s HUP $MAINPID ExecStop=/usr/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
以上是关于1. NGNIX 学习系列-基础的主要内容,如果未能解决你的问题,请参考以下文章