编译安装nginx时配置开机自启
详细编译安装nginx请参考【Nginx目录结构与配置文件详解】以及【Nginx安装部署】,在这里就进行简单安装
安装Nginx
环境介绍
操作系统:
[root@localhost ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@localhost ~]# uname -a Linux localhost.localdomain -.el7.x86_64 # SMP Thu Nov :: UTC 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.gz [root@localhost opt]# cd nginx-/ [root@localhost opt]# cd nginx-/ [root@localhost nginx-]# [root@localhost nginx-]# ls auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src [root@localhost nginx-]# ./configure --prefix=/usr/local/nginx && make && make install
启动测试nginx
[root@localhost nginx-]# cd /usr/local/nginx/ [root@localhost nginx]# ls conf html logs sbin [root@localhost nginx]# cd sbin/ [root@localhost sbin]# ./nginx [root@localhost sbin]# netstat -anpl | grep nginx //查看端口 tcp /nginx: master unix [ ] STREAM CONNECTED /nginx: master unix [ ] STREAM CONNECTED /nginx: master [root@localhost sbin]# ps aux | grep nginx //查看进程 jia ? Sl : : /usr/libexec/ibus-engine-simple root ? Ss : : nginx: master process ./nginx nobody ? S : : nginx: worker process root pts/ S+ : : 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 ok nginx: 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 nginx tcp /nginx: master unix [ ] STREAM CONNECTED /nginx: master unix [ ] STREAM CONNECTED /nginx: master [root@localhost ~]# ps aux | grep nginx root ? Ss : : nginx: master process /usr/local/nginx/sbin/nginx nobody ? S : : nginx: worker process root pts/ S+ : : 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 nginx tcp /nginx: master unix [ ] STREAM CONNECTED /nginx: master unix [ ] STREAM CONNECTED /nginx: master [root@localhost ~]# systemctl stop nginx //关闭服务 [root@localhost ~]# netstat -anpl | grep nginx [root@localhost ~]# systemctl restart nginx //重新启动服务 [root@localhost ~]# netstat -anpl | grep nginx tcp /nginx: master unix [ ] STREAM CONNECTED /nginx: master unix [ ] STREAM CONNECTED /nginx: master
需要注意的是使用之前执行脚本来启动服务的,无法使用此方法关闭服务
设置开机自启动
[root@localhost ~]# systemctl enable nginx Created 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 nginx tcp /nginx: master unix [ ] STREAM CONNECTED /nginx: master unix [ ] STREAM CONNECTED /nginx: master
init.d设置开机启动
init启动方式在centos7系统版本已经不推荐使用了
在/etc/init.d目录中创建启动文件nginx
文件内容如下:
#!/bin/bash # chkconfig: //启动顺序 # description: start the nginx deamon //说明 # Source function library . /etc/rc.d/init.d/functions prog=nginx # 根据自己的路径改写CATALANA_HOME CATALANA_HOME=/usr/local/nginx export CATALINA_HOME case "$1" in start) 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 echo echo "Starting nginx..." $CATALANA_HOME/sbin/nginx ;; *) echo "Usage: $prog {start|stop|restart}" ;; esac exit
设置权限
[root@localhost ~]# chmod +x /etc/init.d/nginx //设置执行权限
测试启动
[root@localhost init.d]# service nginx start //启动nginx Starting nginx (via systemctl): [ 确定 ] [root@localhost init.d]# netstat -anpl | grep nginx tcp /nginx: master unix [ ] STREAM CONNECTED /nginx: master unix [ ] STREAM CONNECTED /nginx: master [root@localhost init.d]# service nginx stop //关闭nginx Stopping nginx (via systemctl): [ 确定 ] [root@localhost init.d]# netstat -anpl | grep nginx [root@localhost init.d]# service nginx restart //重新启动nginx Restarting nginx (via systemctl): [ 确定 ] [root@localhost init.d]# netstat -anpl | grep nginx tcp /nginx: master unix [ ] STREAM CONNECTED /nginx: master unix [ ] STREA
在centos7中init.d中的服务默认也会在system目录中
编译安装nginx时配置开机自启的更多相关文章
- 【转】linux 编译安装nginx,配置自启动脚本
linux 编译安装nginx,配置自启动脚本 本文章来给各位同学介绍一篇关于linux 编译安装nginx,配置自启动脚本教程,希望有需要了解的朋友可一起来学习学习哦. 在公司的suse服务器装ng ...
- linux 编译安装nginx,配置自启动脚本
本文章来给各位同学介绍一篇关于linux 编译安装nginx,配置自启动脚本教程,希望有需要了解的朋友可一起来学习学习哦. 在公司的suse服务器装nginx,记录下安装过程: 参照这篇文章:Linu ...
- Ubuntu编译安装nginx以及配置自动启动
本文主要介绍ubuntu如何编译安装nginx以及遇到的问题 和 配置系统自动启动服务 查看操作系统版本 cat /etc/issue Ubuntu 18.04.3 LTS \n \l 更改镜 ...
- 源码编译安装nginx及设置开机启动项
1.上传nginx文档:解压到/data目录下,并安装依赖包tar xf nginx-1.20.1.tar.gz -C /data/cd /data/nginx-1.20.1/ && ...
- 【转】解决编译安装NGINX时make报错
编译参数:--[root@localhostnginx-1.4.6]#./configure--user=nginx--group=nginx--prefix=/usr/local/nginx--wi ...
- nginx 编译安装以及简单配置
前言 Nginx的大名如雷贯耳,资料太多了,网上一搜一大把,所以这里就不阐述nginx的工作原理了,只是简单的编译安装nginx,然后呢,简单配置一下下. 下载Nginx.安装 下载地址:http:/ ...
- Ubuntu 1604 安装配置 kafka,并配置开机自启(systemctl)
安装 kafka 需要先安装 jdk.一.下载官网:http://kafka.apache.org/downloads.html 二.安装 安装参考:https://segmentfault.com/ ...
- Centos7编译安装Nginx+keepalived
一.安装环境.主机信息及软件版本 Nginx:1.12.2keepalived:2.0.12时间同步(同步后确认各服务器时间是否一致,不一致需要修改一下时区) 关闭防火墙 二.编译安装Nginx 1. ...
- Centos7 编译安装 Nginx PHP Mariadb Memcached 扩展 ZendOpcache扩展 (实测 笔记 Centos 7.3 + Mariadb 10.1.20 + Nginx 1.10.2 + PHP 7.1.0 + Laravel 5.3 )
环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7-x86_64-Minimal-1611.iso 安装步骤: 1.准备 1.0 查看硬 ...
随机推荐
- Angular前端优化思路
简单总结接下我这边angular前端优化步骤都是满满的干货,各位客官有好的改进欢迎留言~ 1. 动静分离 项目里面前端比较占用带宽的一般都是加载静态资源,请求后台接口一般占用带宽都是1kb左右,但是在 ...
- oracle基础(基本介绍)
数据库 磁盘上存储的数据的集合 在物理上表现为数据文件.日志文件和控制文件等 在逻辑上以表空间形式存在 必须首先创建数据库,然后才能使用Oracle 数据库实例 每个启动的数据库都对应一个数据库实例, ...
- centos7 openssh 7.9.1 升级
由于项目构建时间比较长,近期安全检查发现openssh有漏洞.所以要升级openssh到7.9p1版本.由于ssh用于远程连接,所以要谨慎操作. 1. 依赖安装 OpenSSL版本:目前OpenSSH ...
- Python3.7.1(四) Print如何在输出中插入变量
# 如果想在打印的字符串中的任意地方加入任意的变量,可以使用python的格式化输出.## 用例代码如下:s = 'Hello'x = len(s)print("The length of ...
- 朱辉(茶水): Linux Kernel iowait 时间的代码原理
本文系转载,著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 作者: 朱辉(茶水) 来源: 微信公众号linux阅码场(id: linuxdev) 作者介绍 朱辉,个人主页 htt ...
- 目录(cd mkdir rmdir rm pwd ls) 文件(ln touch mv rm cat more head rail) 文件权限(chmod chown chgrp) 文件通配符(* ? [])
记住Linux目录树的结构是一个称职Linux系统管理员的必备素质! 目录漫游cd cd - 目录显示pwd 目录管理 mkdir -p a/b/c/1 parent创建多层目录 -m 700 ...
- Java基础知识总结之类的集合
Java集合概述 1.集合类也叫作容器类.它的功能相当于一个容器.可以存储数量不确定的数据,以及保存具有映射关系的数据(也被称为关联数组). 2.Java的集合(容器),它是用来”装对象的“(实际上是 ...
- RNN-LSTM讲解-基于tensorflow实现
cnn卷积神经网络在前面已经有所了解了,目前博主也使用它进行了一个图像分类问题,基于kaggle里面的food-101进行的图像识别,识别率有点感人,基于数据集的关系,大致来说还可行.下面我就继续学习 ...
- Photoshop CS2软件下载与安装教程
Photoshop CS2精简版下载地址: 链接:https://pan.baidu.com/s/1ryJPLuKG_MixWjGJgLebOg提取码:nzz9 软件介绍: Photoshop主要处理 ...
- vCenter Server Appliance(VCSA )6.7部署指南
目录 简介 环境准备 开始安装 第一阶段安装 第二阶段安装 使用 简介 早期的VCSA支持 SUSE 和 Windows,不太懂SUSE,也不想用Windows 而在2018年4月17日VCSA 6. ...