nginx / php-fpm简单运维命令记录

这里简单记录下 nginx / php-fpm 日常使用到的一些命令和命令背后的故事。先说nginx:
测试下指定配置是否存在错误:
nginx -t -c /usr/local/conf/nginx.conf

测试默认的nginx配置文件
nginx -t

启动nginx:
nginx -c /usr/local/conf/nginx.conf

平滑重启nginx
nginx收到HUP信号之后,重启过程中nginx会检测修改的配置文件是否正确,如果不正确,新的work不会创建,旧的进程将继续工作。配置文件没有问题,就先启动新的work,同时通知旧的work关闭监听套接字,但已经存在的连接继续服务,直到所有旧连接处理完成,旧work就退出了。
nginx -s reload 后者 sudo kill -HUP 'cat /usr/local/var/run/nginx.pid'

停止nginx:

直接干掉    pkill -9  nginx
从容停止    kill -QUIT 主进程号
快速停止    kill -TERM 主进程号

平滑升级nginx:
nginx升级官方建议使用自动升级,其实半自动升级。

[root@tm /data/nginx-1.10.1]# ./configure --prefix=/usr/local/nginx                                                                 
[root@tm /data/nginx-1.10.1]#make  
[root@tm /data/nginx-1.10.1]#cp objs/nginx  /usr/local/nginx/sbin 
[root@tm /data/nginx-1.10.1]#ps -ef | grep nginx
[root@tm ~/nginx/nginx-1.10.1]#ps -ef | grep nginx
root     20634     1  0 18:04 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   20635 20634  0 18:04 ?        00:00:00 nginx: worker process
root     20641 15050  0 18:04 pts/0    00:00:00 grep --color=auto nginx
[root@tm ~/nginx/nginx-1.10.1]#cat Makefile    // 瞅瞅makefie文件
default:        build
clean:
        rm -rf Makefile objs
build:
        $(MAKE) -f objs/Makefile
install:
        $(MAKE) -f objs/Makefile install
upgrade:
        /usr/local/nginx/sbin/nginx -t
        kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
        sleep 1
        test -f /usr/local/nginx/logs/nginx.pid.oldbin
        kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
[root@tm ~/nginx/nginx-1.10.1]#make upgrade   
[root@tm ~/nginx/nginx-1.10.1]#ps -ef | grep nginx  
[root@tm /usr/local/nginx/sbin]#./nginx -V                            
 nginx version: nginx/1.10.1

这里顺便给出一个添加nginxsystemctl服务管理里面,这样上面的启动,重启,停止都可以通过systemctl来管理了,在/lib/systemd/system下面增加一个nginx.service

    [Unit]
        Description=nginx - high performance web server  
        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 /usr/local/nginx/conf/nginx.conf  
        ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
        ExecReload=/bin/kill -s HUP $MAINPID  
        ExecStop=/bin/kill -s QUIT $MAINPID  
        PrivateTmp=true  
       
    [Install] 
        WantedBy=multi-user.target

再来看下php-fpm:
测试配置文件是否正常:

/usr/local/php/sbin/php-fpm -t

启动php-fpm:

systemctl start php-fpm     //服务方式控制
or
/usr/local/php/sbin/php-fpm 

停止php-fpm:

systemctl stop php-fpm
or
kill -QUIT 'cat /usr/local/php/var/run/php-fpm.pid'   // 平滑停止
kill -INT 'cat /usr/local/php/var/run/php-fpm.pid'    // 快速停止

重启php-fpm:

kill -HUP  'cat /usr/local/php/var/run/php-fpm.pid'  // 平滑重启

这里顺便给也出一个添加php-fpmsystemctl服务管理里面,这样上面的启动,重启,停止都可以通过systemctl来管理了,在/lib/systemd/system下面增加一个php-fpm.service

[Unit]
Description=The PHP 5.6 FastCGI Process Manager
After=network.target

[Service]
Type=simple
PIDFile=/usr/local/var/run/php-fpm.pid
ExecStart=/etc/init.d/php-fpm --nodaemonize --fpm-config /usr/local/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target

小记1:设置文件的权限方式, 文字设定和数字设定:chmod (-R) u=r file_name or chmod (-R)400 file_name
小记2:你尝试使用systemctl start docker.service 发现报错如下:

[root@username system]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Tue 2017-10-10 09:49:19 CST; 11min ago
     Docs: http://docs.docker.com
  Process: 29127 ExecStart=/usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec/docker/docker-proxy-current $OPTIONS $DOCKER_STORAGE_OPTIONS $DOCKER_NETWORK_OPTIONS $ADD_REGISTRY $BLOCK_REGISTRY $INSECURE_REGISTRY (code=exited, status=1/FAILURE)
 Main PID: 29127 (code=exited, status=1/FAILURE)

Oct 10 09:49:19 taomin.localdomain systemd[1]: Starting Docker Application Container Engine...
Oct 10 09:49:19 taomin.localdomain dockerd-current[29127]: time="2017-10-10T09:49:19+08:00" level=fatal msg="unable to configure the Docker daemon with file /etc/docker/daemon.... string\n"
Oct 10 09:49:19 taomin.localdomain systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE
Oct 10 09:49:19 taomin.localdomain systemd[1]: Failed to start Docker Application Container Engine.
Oct 10 09:49:19 taomin.localdomain systemd[1]: Unit docker.service entered failed state.
Oct 10 09:49:19 taomin.localdomain systemd[1]: docker.service failed.

这种报错的原因是:/etc/docker/daemon.json这个文件不应该存在,如果存在它应该是一个合法的json文件。解决上面的报错可以将/etc/docker/daemon.json这个删掉即可。

你可能感兴趣的:(nginx / php-fpm简单运维命令记录)