搜索
首页运维NginxCentos7中Nginx开机自启动问题怎么解决

关于在centos7中设置nginx开机自启动,我们可以通过编写开机自启动shell脚本来解决。

测试环境

操作系统:centos7 64位 1611

nginx版本: 1.11.10

本机nginx安装时的配置参数

./configure \
--prefix=/usr/local/nginx \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

编写脚本

[root@localhost]# vim /etc/init.d/nginx

以下是脚本内容

#!/bin/bash
# nginx startup script for the nginx http server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: nginx is a high-performance web and proxy server.
#       it has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /usr/local/nginx/logs/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
retval=0
prog="nginx"
# source function library.
. /etc/rc.d/init.d/functions
# source networking configuration.
. /etc/sysconfig/network
# check that networking is up.
[ "${networking}" = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
  echo "nginx already running...."
  exit 1
fi
  echo -n $"starting $prog: "
  daemon $nginxd -c ${nginx_config}
  retval=$?
  echo
  [ $retval = 0 ] && touch /var/lock/subsys/nginx
  return $retval
}
# stop nginx daemons functions.
stop() {
    echo -n $"stopping $prog: "
    killproc $nginxd
    retval=$?
    echo
    [ $retval = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
}
# reload nginx service functions.
reload() {
  echo -n $"reloading $prog: "
  #kill -hup `cat ${nginx_pid}`
  killproc $nginxd -hup
  retval=$?
  echo
}
# see how we were called.
case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
reload)
    reload
    ;;
restart)
    stop
    start
    ;;
status)
    status $prog
    retval=$?
    ;;
*)
    echo $"usage: $prog {start|stop|restart|reload|status|help}"
    exit 1
esac
exit $retval
:wq 保存并退出

*对于shell脚本中的部分文件路径请修改成你主机上nginx的相应路径,例如:  nginxd=/usr/local/nginx/sbin/nginx  nginx_config=/usr/local/nginx/conf/nginx.conf  nginx_pid=/usr/local/nginx/logs/nginx.pid  以上都是本测试机nginx的相应路径  还有nginx的pid默认路径是nginx安装目录的logs/nginx.pid里。

设置文件的访问权限

[root@localhost]# chmod a+x /etc/init.d/nginx

(a+x ==> all user can execute  所有用户可执行)

这样在控制台就很容易的操作nginx了:查看nginx当前状态、启动nginx、停止nginx、重启nginx…

usage : nginx {start|stop|restart|reload|status|help}

如果修改了nginx的配置文件nginx.conf,也可以使用上面的命令重新加载新的配置文件并运行,可以将此命令加入到rc.local文件中,这样开机的时候nginx就默认启动了

加入到rc.local文件中

[root@localhost]# vi /etc/rc.local

加入一行  /etc/init.d/nginx start    保存并退出,下次重启会生效。

注意

如果开机后发现自启动脚本没有执行,你要去确认一下rc.local这个文件的访问权限是否是可执行的,因为rc.local默认是不可执行的。

修改rc.local访问权限,增加可执行权限

[root@localhost]# chmod +x /etc/rc.d/rc.local

现在重启后,自启动脚本就能正常执行了。

可以通过以下命令来查看nginx进行的运行情况

[root@localhost]# ps aux | grep nginx

以上是Centos7中Nginx开机自启动问题怎么解决的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:亿速云。如有侵权,请联系admin@php.cn删除
nginx怎么启动nginx怎么启动Apr 14, 2025 pm 01:06 PM

问题:如何启动 Nginx?答案:安装 Nginx启动 Nginx验证 Nginx 是否已启动探索其他启动选项自动启动 Nginx

怎么查看nginx是否启动怎么查看nginx是否启动Apr 14, 2025 pm 01:03 PM

确认 Nginx 是否启动的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 检查端口 80 是否开放;3. 查看系统日志中 Nginx 启动消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

nginx怎么关闭nginx怎么关闭Apr 14, 2025 pm 01:00 PM

要关闭 Nginx 服务,请按以下步骤操作:确定安装类型:Red Hat/CentOS(systemctl status nginx)或 Debian/Ubuntu(service nginx status)停止服务:Red Hat/CentOS(systemctl stop nginx)或 Debian/Ubuntu(service nginx stop)禁用自动启动(可选):Red Hat/CentOS(systemctl disable nginx)或 Debian/Ubuntu(syst

nginx在windows中怎么配置nginx在windows中怎么配置Apr 14, 2025 pm 12:57 PM

如何在 Windows 中配置 Nginx?安装 Nginx 并创建虚拟主机配置。修改主配置文件并包含虚拟主机配置。启动或重新加载 Nginx。测试配置并查看网站。选择性启用 SSL 并配置 SSL 证书。选择性设置防火墙允许 80 和 443 端口流量。

nginx403错误怎么解决nginx403错误怎么解决Apr 14, 2025 pm 12:54 PM

服务器无权访问所请求的资源,导致 nginx 403 错误。解决方法包括:检查文件权限。检查 .htaccess 配置。检查 nginx 配置。配置 SELinux 权限。检查防火墙规则。排除其他原因,如浏览器问题、服务器故障或其他可能的错误。

linux怎么启动nginxlinux怎么启动nginxApr 14, 2025 pm 12:51 PM

在 Linux 中启动 Nginx 的步骤:检查 Nginx 是否已安装。使用 systemctl start nginx 启动 Nginx 服务。使用 systemctl enable nginx 启用在系统启动时自动启动 Nginx。使用 systemctl status nginx 验证启动是否成功。在 Web 浏览器中访问 http://localhost 查看默认欢迎页面。

linux怎么查看nginx是否启动linux怎么查看nginx是否启动Apr 14, 2025 pm 12:48 PM

在 Linux 中,使用以下命令检查 Nginx 是否已启动:systemctl status nginx根据命令输出进行判断:如果显示 "Active: active (running)",则 Nginx 已启动。如果显示 "Active: inactive (dead)",则 Nginx 已停止。

nginx304错误怎么解决nginx304错误怎么解决Apr 14, 2025 pm 12:45 PM

问题的答案:304 Not Modified 错误表示浏览器已缓存客户端请求的最新资源版本。解决方案:1. 清除浏览器缓存;2. 禁用浏览器缓存;3. 配置 Nginx 允许客户端缓存;4. 检查文件权限;5. 检查文件哈希;6. 禁用 CDN 或反向代理缓存;7. 重启 Nginx。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具