nginx正常启动
/usr/local/sbin/nginx -s reload 加载配置文件
service nginx start 或
service nginx restart 或者找到nginx文件
执行:./nginx
有时执行会返回如下:
Redirecting to /bin/systemctl start nginx.service
这时也可以用此命令启动:
/bin/systemctl start nginx.service
启动异常情况
[error] invalid PID number “” in “/run/nginx.pid”
Failed to start start.service: Unit not found. 等类问题。
可以先检查自己的nginx.conf配置文件是否正确,检查命令:
/usr/local/sbin/nginx -t
执行正常会返回
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
一般报错往往会是配置文件错误导致启动错误,
如果以上都不能解决问题,可以试试执行如下命令重新指定下配置文件:
/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
后面的路径是配置的nginx.conf的路径,在执行nginx -t 后会返回此路径,执行完后再重新启动。
查看nginx是否启动:
ps -ef |grep nginx 或 ps -aux |grep nginx
查看进程id
ps -C nginx -o pid
查看端口
netstat -anp | grep :80
设置nginx开机自动启动
设置nginx自动启动首先需确保系统中有nginx.service服务,默认用 yum install 命令安装nginx会自动创建nginx.service。如果是下载包安装,则需要在系统中创建nginx.service文件
vi /lib/systemd/system/nginx.service
-
添加内容
[Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target
-
参数说明
[Unit]:服务的说明 Description:描述服务 After:描述服务类别 [Service]服务运行参数的设置 Type=forking是后台运行的形式 ExecStart为服务的具体运行命令 ExecReload为重启命令 ExecStop为停止命令 PrivateTmp=True表示给服务分配独立的临时空间 注意:[Service]的启动、重启、停止命令全部要求使用绝对路径 [Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
设置开机自动启动
systemctl enable nginx.service
关闭开机自启
systemctl disable nginx.service