目录

  • 1.添加nginx系统服务
  • 1.1 方法一:添加使用service工具进行管理
  • 1.2 方法二:使用systemctl工具进行管理
  • 1.2.1 启动服务失败排错:


1.添加nginx系统服务

今天安装了nginx软件,这是跟Apache软件旗鼓相当的一个软件,为了更方便的使用nginx服务,所以要把它加入到系统服务里面,使用systemctl 命令可以控制,但是发现加入nginx系统服务里面然后启动不了,具体的配置方法有两个:

1.1 方法一:添加使用service工具进行管理

编辑vim /etc/init.d/nginx文件,编写空白文件

[root@server nginx-1.12.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@localhost ~]# vim /etc/init.d/nginx 

#!/bin/bash
# chkconfig: - 99 20                                                    
# description: Nginx Service Control Script
COM="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
   $COM
   ;;
stop)
   kill -s QUIT $(cat $PID)
   ;;
restart)
   $0 stop
   $0 start
   ;;
reload)
   kill -s HUP $(cat $PID)
   ;;
*)
       echo "Usage: $0 {start|stop|restart|reload}"
       exit 1
esac
exit 0

#添加为系统服务
[root@server nginx-1.12.2]# chmod +x /etc/init.d/nginx 
[root@server nginx-1.12.2]# chkconfig --add nginx
#可在目录里面查看编译的文件
[root@localhost ~]# cd /etc/rc.d/init.d
[root@localhost init.d]# ls
functions  netconsole  network  nginx  README
#停止服务,开启服务
[root@server nginx-1.12.2]# systemctl stop nginx
[root@server nginx-1.12.2]# systemctl start nginx

这个时候已经可以正常的访问网页了

docker宝塔面板NGINX启动失败 宝塔nginx启动不了_linux

1.2 方法二:使用systemctl工具进行管理

编辑配置文件 vim /usr/lib/systemd/system/nginx.service,这是一个空白文件,进入文件进行编译:

#添加使用systemctl工具进行管理
[root@localhost ~]# vim /usr/lib/systemd/system/nginx.service 

[Unit] 
Description=nginx    #描述
After=network.target  #描述服务类别
[Service]
Type=forking   #后台运行形势
PIDFile =/usr/local/nginx/logs/nginx.pid  #PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx  #启动服务
ExecReload=/bin/kill -s HUP $MAINPID  #根据PID重载配置
ExecStop=/bin/kill -s QUIT $MAINPID   #根据PID终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target

[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service
#设置754权限是一种安全优化
[root@localhost ~]# systemctl start nginx.service
#输入到这一步的时候发生了报错,无法使用systemctl命令开启nginx服务
[root@localhost ~]# systemctl status firewalld

1.2.1 启动服务失败排错:

第一步,查看了一下我的防火墙状态,发现我的防火墙是关闭的状态

[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

第二步。查看配置文件有无错误,确认无误之后,查看端口信息
然后发现80端口被占用了,因为nginx用的就是80端口,怪不到无法systemctl 无法调用起来,这里把端口进程使用Kill杀掉

这里安利一个小tips:如果httpd服务里面出了问题,例如是端口被占用而且
kill -9 杀不死 这类属于是僵尸进程,而且 status 服务处于运行running的状态
1.最简单的粗暴的是reboot重启
2.检活点:使用curl 一个静态页面去访问来检查服务是否正常
这边我两个都试一下:

[root@server system]# netstat -natp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      15007/nginx: master 
tcp        0      0 192.168.152.130:34294   117.18.237.29:80        ESTABLISHED 15175/firefox        
[root@server system]# kill -9 15007
[root@server system]# kill -9 15175


[root@server system]# reboot  
#杀完端口进程再进行一下系统重启然后再次尝试开启服务

[root@localhost ~]# chkconfig --add nginx
#再一次吧nginx服务加入系统服务

[root@localhost ~]# systemctl start nginx
#验证一下服务是否正常开启,访问本地静态页面
[root@localhost ~]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

#再一次查看端口信息,发现还是被占用,这就很令人烦躁了,继续吧占用端口的进程杀掉,然后查看一下服务状态
[root@localhost ~]# netstat -natp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7497/nginx: master  
tcp        0      0 127.0.0.1:46040         127.0.0.1:80            TIME_WAIT   -                   
[root@localhost ~]# kill -9 7497
[root@localhost ~]# netstat -natp | grep 80
tcp        0      0 127.0.0.1:46040         127.0.0.1:80            TIME_WAIT   -                   
[root@localhost ~]# netstat -natp | grep 80
# 验证配置文件有无语法错误
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

查看状态是开启的状态:

docker宝塔面板NGINX启动失败 宝塔nginx启动不了_docker宝塔面板NGINX启动失败_02


切换到虚拟机可以进行正常的访问,然后也可以使用systemctl 命令开启nginx服务

docker宝塔面板NGINX启动失败 宝塔nginx启动不了_linux_03