编译安装nginx时配置开机自启
发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,详细编译安装nginx请参考【Nginx目录结构与配置文件详解】以及【Nginx安装部署】,在这里就进行简单安装安装Nginx环境介绍操作系统:[root@localhost ~]# cat /etc
千家信息网最后更新 2025年02月01日编译安装nginx时配置开机自启
详细编译安装nginx请参考【Nginx目录结构与配置文件详解】以及【Nginx安装部署】,在这里就进行简单安装
安装Nginx
环境介绍
操作系统:
[root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) [root@localhost ~]# uname -aLinux localhost.localdomain 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
nginx软件版本: nginx-1.17.6.tar.gz
安装依赖
注意:编译安装一定要安装开发工具,否则无法进行安装或安装报错
[root@localhost opt]# yum -y install openssl openssl-devel zlib zlib-devel pcre pcre-devel make gcc gcc-c++
安装nginx
[root@localhost ~]# cd /opt/[root@localhost opt]# wget http://nginx.org/download/nginx-1.17.6.tar.gz[root@localhost opt]# tar zxf nginx-1.17.6.tar.gz [root@localhost opt]# cd nginx-1.17.6/[root@localhost opt]# cd nginx-1.17.6/[root@localhost nginx-1.17.6]# [root@localhost nginx-1.17.6]# lsauto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src[root@localhost nginx-1.17.6]# ./configure --prefix=/usr/local/nginx && make && make install
启动测试nginx
[root@localhost nginx-1.17.6]# cd /usr/local/nginx/[root@localhost nginx]# lsconf html logs sbin[root@localhost nginx]# cd sbin/[root@localhost sbin]# ./nginx [root@localhost sbin]# netstat -anpl | grep nginx //查看端口tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11881/nginx: master unix 3 [ ] STREAM CONNECTED 53405 11881/nginx: master unix 3 [ ] STREAM CONNECTED 53404 11881/nginx: master [root@localhost sbin]# ps aux | grep nginx //查看进程jia 5496 0.0 0.0 302400 852 ? Sl 10:58 0:00 /usr/libexec/ibus-engine-simpleroot 11881 0.0 0.0 20560 620 ? Ss 11:23 0:00 nginx: master process ./nginxnobody 11882 0.0 0.1 23080 1632 ? S 11:23 0:00 nginx: worker processroot 11896 0.0 0.1 112728 988 pts/0 S+ 11:24 0:00 grep --color=auto ngin
设置为系统命令
[root@localhost sbin]# ln nginx /usr/local/sbin/[root@localhost ~]# nginx -t //检查nginx语法nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@localhost ~]# nginx -s stop //停止nginx[root@localhost ~]# which nginx //查看启动程序位置/usr/local/sbin/nginx
方法一利用rc.local脚本
rc.local是启动加载文件,在linux中要把一个程序加入开机启动,一般可以通过修改rc.local来完成,这个文件时开机就要加载的文件,所以我们就可以利用linux这个文件设置nginx开机自启动
[root@localhost ~]# cat /etc/rc.local //文件存放在/etc目录下下面时rc.local的文件内容:#!/bin/bash# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES## It is highly advisable to create own systemd services or udev rules# to run scripts during boot instead of using this file.## In contrast to previous versions due to parallel execution during boot# this script will NOT be run after all other services.## Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure# that this script will be executed during boot.touch /var/lock/subsys/local
利用这个文件可以设置自己想在开机时启动的命令,直接把自己想执行的命令写到rc.local中就可以了
我们把nginx启动命令加入此文件中
[root@localhost ~]# echo sh /usr/local/nginx/sbin/nginx >> /etc/rc.local [root@localhost ~]# cat /etc/rc.local #!/bin/bash# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES## It is highly advisable to create own systemd services or udev rules# to run scripts during boot instead of using this file.## In contrast to previous versions due to parallel execution during boot# this script will NOT be run after all other services.## Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure# that this script will be executed during boot.touch /var/lock/subsys/local/usr/local/nginx/sbin/nginx如果你上面把nginx设置为系统命令那你就可以直接写命令就好了nginx
然后让我们重启系统再次查看端口和进程
[root@localhost ~]# reboot重启后发现nginx自动启动了[root@localhost ~]# netstat -anpl | grep nginxtcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4847/nginx: master unix 3 [ ] STREAM CONNECTED 39265 4847/nginx: master unix 3 [ ] STREAM CONNECTED 39264 4847/nginx: master [root@localhost ~]# ps aux | grep nginxroot 4847 0.0 0.0 20560 612 ? Ss 11:45 0:00 nginx: master process /usr/local/nginx/sbin/nginxnobody 4848 0.0 0.1 23080 1388 ? S 11:45 0:00 nginx: worker processroot 4860 0.0 0.1 112724 988 pts/0 S+ 11:45 0:00 grep --color=auto nginx[root@localhost ~]#
方法二设置系统服务
推荐设置开机自启
配置启动生成pid文件
pid文件是进程文件里面存放的是程序运行的进程ID也就是进程号
nginx生成pid文件需要修改配置文件
修改内容如下:
默认配置文件有这一条,如果没有请在nginx.conf中找到这一条然后将前面注释删除就可以了pid logs/nginx.pid;
在/usr/lib/systemd/system路径下添加nginx.service文件
/usr/lib/systemd/system 此目录是用来存放一些系统服务的
nginx文件内容:
[root@localhost system]# cat nginx.service [Unit]Description=nginx //描述After=syslog.target network.target remote-fs.target nss-lookup.target \\描述服务类别[Service]Type=forking //设置运行方式,后台运行PIDFile=/usr/local/nginx/logs/nginx.pid //设置PID文件ExecStart=/usr/local/nginx/sbin/nginx //启动命令ExecReload=/bin/kill -s HUP $MAINPID //重启命令ExecStop=/bin/kill -s QUIT $MAINPID //关闭命令PrivateTmp=true //分配独立的临时空间*注意命令需要写绝对路径[Install] ///服务安装的相关设置,可设置为多用户WantedBy=multi-user.target
注意:此文件需要754的权限
测试启动关闭
[root@localhost ~]# systemctl start nginx //启动服务[root@localhost ~]# netstat -anpl | grep nginxtcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5249/nginx: master unix 3 [ ] STREAM CONNECTED 42458 5249/nginx: master unix 3 [ ] STREAM CONNECTED 42459 5249/nginx: master [root@localhost ~]# systemctl stop nginx //关闭服务[root@localhost ~]# netstat -anpl | grep nginx[root@localhost ~]# systemctl restart nginx //重新启动服务[root@localhost ~]# netstat -anpl | grep nginxtcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5289/nginx: master unix 3 [ ] STREAM CONNECTED 45346 5289/nginx: master unix 3 [ ] STREAM CONNECTED 45347 5289/nginx: master
需要注意的是使用之前执行脚本来启动服务的,无法使用此方法关闭服务
设置开机自启动
[root@localhost ~]# systemctl enable nginxCreated symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.[root@localhost ~]# systemctl enable nginx
重启看效果
[root@localhost ~]# netstat -anpl | grep nginxtcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4081/nginx: master unix 3 [ ] STREAM CONNECTED 32429 4081/nginx: master unix 3 [ ] STREAM CONNECTED 32428 4081/nginx: master
init.d设置开机启动
init启动方式在centos7系统版本已经不推荐使用了
在/etc/init.d目录中创建启动文件nginx
文件内容如下:
#!/bin/bash# chkconfig: 345 80 20 //启动顺序# description: start the nginx deamon //说明# Source function library. /etc/rc.d/init.d/functionsprog=nginx# 根据自己的路径改写CATALANA_HOMECATALANA_HOME=/usr/local/nginxexport CATALINA_HOMEcase "$1" instart) echo "Starting nginx..." $CATALANA_HOME/sbin/nginx ;;stop) echo "Stopping nginx..." $CATALANA_HOME/sbin/nginx -s stop ;;restart) echo "Stopping nginx..." $CATALANA_HOME/sbin/nginx -s stop sleep 2 echo echo "Starting nginx..." $CATALANA_HOME/sbin/nginx ;;*) echo "Usage: $prog {start|stop|restart}" ;;esacexit 0
设置权限
[root@localhost ~]# chmod +x /etc/init.d/nginx //设置执行权限
测试启动
[root@localhost init.d]# service nginx start //启动nginxStarting nginx (via systemctl): [ 确定 ][root@localhost init.d]# netstat -anpl | grep nginxtcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4081/nginx: master unix 3 [ ] STREAM CONNECTED 38534 4081/nginx: master unix 3 [ ] STREAM CONNECTED 38535 4081/nginx: master [root@localhost init.d]# service nginx stop //关闭nginxStopping nginx (via systemctl): [ 确定 ][root@localhost init.d]# netstat -anpl | grep nginx[root@localhost init.d]# service nginx restart //重新启动nginxRestarting nginx (via systemctl): [ 确定 ][root@localhost init.d]# netstat -anpl | grep nginxtcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5304/nginx: master unix 3 [ ] STREAM CONNECTED 43218 5304/nginx: master unix 3 [ ] STREA
在centos7中init.d中的服务默认也会在system目录中
文件
命令
服务
系统
目录
进程
配置
内容
权限
程序
路径
测试
运行
编译
方式
方法
版本
端口
脚本
推荐
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
智能控制是网络技术吗
如何与服务器建立安全
网络安全绘画文案励志
城市网络安全治理
阿里云轻量级服务器安全组
网络安全二级和三级
数据库设计说明书外部设计
重庆智慧景区软件开发
html如何关联到数据库
上海媒体软件开发服务报价表
服务器显示请求异常
ct数据库处理技术
戴尔服务器的性能
教学软件开发公司开发出适用于
数据库系统开发基础
数据库关系怎么建
服务器管理云平台
数据库图像特征比对
病历本软件开发
监控服务器地址是多少
山东大专软件开发哪个学校好
凤翔开展网络安全宣传
抖音服务器一年维护费需要多少钱
网络安全审计排名
宝山区管理软件开发售后服务
基层网络安全卫士
重庆惠普服务器虚拟化安装服务器
阿塔拉斯服务器
数据库要关闭防火墙吗
网络安全认证协议