编译安装nginx时配置开机自启

详细编译安装nginx请参考Nginx目录结构与配置文件详解】以及【Nginx安装部署】,在这里就进行简单安装

安装Nginx

环境介绍

操作系统:

[[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[[email protected] ~]# uname -a
Linux 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

安装依赖

注意:编译安装一定要安装开发工具,否则无法进行安装或安装报错

[[email protected] opt]# yum -y install openssl openssl-devel zlib zlib-devel pcre pcre-devel make gcc gcc-c++

安装nginx

[[email protected] ~]# cd /opt/
[[email protected] opt]# wget http://nginx.org/download/nginx-1.17.6.tar.gz
[[email protected] opt]# tar zxf nginx-1.17.6.tar.gz
[[email protected] opt]# cd nginx-1.17.6/
[[email protected] opt]# cd nginx-1.17.6/
[[email protected] nginx-1.17.6]#
[[email protected] nginx-1.17.6]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[[email protected] nginx-1.17.6]# ./configure --prefix=/usr/local/nginx && make && make install

启动测试nginx

[[email protected] nginx-1.17.6]# cd /usr/local/nginx/
[[email protected] nginx]# ls
conf html logs sbin
[[email protected] nginx]# cd sbin/
[[email protected] sbin]# ./nginx
[[email protected] 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
[[email protected] sbin]# ps aux | grep nginx    //查看进程
jia 5496 0.0 0.0 302400 852 ? Sl 10:58 0:00 /usr/libexec/ibus-engine-simple
root 11881 0.0 0.0 20560 620 ? Ss 11:23 0:00 nginx: master process ./nginx
nobody 11882 0.0 0.1 23080 1632 ? S 11:23 0:00 nginx: worker process
root 11896 0.0 0.1 112728 988 pts/0 S+ 11:24 0:00 grep --color=auto ngin

设置为系统命令

[[email protected] sbin]# ln nginx /usr/local/sbin/
[[email protected] ~]# 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
[[email protected] ~]# nginx -s stop    //停止nginx
[[email protected] ~]# which nginx    //查看启动程序位置
/usr/local/sbin/nginx

方法一利用rc.local脚本

rc.local是启动加载文件,在linux中要把一个程序加入开机启动,一般可以通过修改rc.local来完成,这个文件时开机就要加载的文件,所以我们就可以利用linux这个文件设置nginx开机自启动

[[email protected] ~]# 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启动命令加入此文件中

[[email protected] ~]# echo sh /usr/local/nginx/sbin/nginx >> /etc/rc.local
[[email protected] ~]# 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

然后让我们重启系统再次查看端口和进程

[[email protected] ~]# reboot
重启后发现nginx自动启动了
[[email protected] ~]# netstat -anpl | grep nginx
tcp 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
[[email protected] ~]# ps aux | grep nginx
root 4847 0.0 0.0 20560 612 ? Ss 11:45 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 4848 0.0 0.1 23080 1388 ? S 11:45 0:00 nginx: worker process
root 4860 0.0 0.1 112724 988 pts/0 S+ 11:45 0:00 grep --color=auto nginx
[[email protected] ~]# 

方法二设置系统服务

推荐设置开机自启

置启动生成pid文件

pid文件是进程文件里面存放的是程序运行的进程ID也就是进程号
nginx生成pid文件需要修改配置文件
**修改内容如下:**

默认配置文件有这一条,如果没有请在nginx.conf中找到这一条然后将前面注释删除就可以了
pid logs/nginx.pid;

在/usr/lib/systemd/system路径下添加nginx.service文件

/usr/lib/systemd/system 此目录是用来存放一些系统服务的
nginx文件内容:

[[email protected] 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的权限

测试启动关闭

[[email protected] ~]# systemctl start nginx    //启动服务
[[email protected] ~]# netstat -anpl | grep nginx
tcp 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
[[email protected] ~]# systemctl stop nginx    //关闭服务
[[email protected] ~]# netstat -anpl | grep nginx
[[email protected] ~]# systemctl restart nginx    //重新启动服务
[[email protected] ~]# netstat -anpl | grep nginx
tcp 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 

需要注意的是使用之前执行脚本来启动服务的,无法使用此方法关闭服务

设置开机自启动

[[email protected] ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[[email protected] ~]# systemctl enable nginx

重启看效果

[[email protected] ~]# netstat -anpl | grep nginx
tcp 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/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
sleep 2
echo
echo "Starting nginx..."
$CATALANA_HOME/sbin/nginx
;;
*)
echo "Usage: $prog {start|stop|restart}"
;;
esac
exit 0

设置权限

[[email protected] ~]# chmod +x /etc/init.d/nginx    //设置执行权限

测试启动

[[email protected] init.d]# service nginx start    //启动nginx
Starting nginx (via systemctl): [ 确定 ]
[[email protected] init.d]# netstat -anpl | grep nginx
tcp 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
[[email protected] init.d]# service nginx stop    //关闭nginx
Stopping nginx (via systemctl): [ 确定 ]
[[email protected] init.d]# netstat -anpl | grep nginx
[[email protected] init.d]# service nginx restart //重新启动nginx
Restarting nginx (via systemctl): [ 确定 ]
[[email protected] init.d]# netstat -anpl | grep nginx
tcp 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目录中

原文地址:https://www.cnblogs.com/kuiyajia/p/12038805.html

时间: 2024-10-01 12:42:26

编译安装nginx时配置开机自启的相关文章

【转】解决编译安装NGINX时make报错

编译参数:--[[email protected]]#./configure--user=nginx--group=nginx--prefix=/usr/local/nginx--with-http_stub_status_module--with-http_ssl_mod 编译参数: --[[email protected] nginx-1.4.6]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with

Linux源码安装nginx并配置

linux 编译安装nginx,配置自启动脚本 本文章来给各位同学介绍一篇关于linux 编译安装nginx,配置自启动脚本教程,希望有需要了解的朋友可一起来学习学习哦. 在公司的suse服务器装nginx,记录下安装过程: 参照这篇文章:Linux 从源码编译安装 Nginx: 1.1.准备 pcre 库pere 是为了让 nginx 支持正则表达式.只是准备,并不安装,是为了避免在64位系统中出现错误.  代码如下 复制代码 wget ftp://ftp.csx.cam.ac.uk/pub/

RedHat 7 编译安装Nginx 1.12并配置WEB站点

一.安装环境 1.操作系统版本:Red Hat Enterprise Linux Server release 7.2 (Maipo) 2.Nginx版本:nginx-1.12.2.tar.gz 3.pcre版本:pcre-8.42.tar.gz 4.zlib版本:zlib-1.2.11.tar.gz 5.openssl版本:openssl-1.0.2l.tar.gz 二.安装说明 官网上关于Nginx的依赖包说明如下: 三.安装过程 1.安装gcc:yum install gcc-c++ -y

编译安装Nginx及基本配置

一.编译安装Nginx # yum install pcre-devel openssl-devel zlib-devel gcc gcc-c++ -y        ==>编译前所需要的包 # groupadd -r nginx        ==>新建nginx系统组 # useradd -r -g nginx nginx        ==>nginx系统帐号 # ./configure --prefix=/usr/local/nginx --conf-path=/etc/ngin

Ubuntu编译安装Php,配置时出现:Configure: error: XML configuration could not be found

解决这个问题是需要安装libxml, sudo apt-get install libxml2-dev Ubuntu编译安装Php,配置时出现:Configure: error: XML configuration could not be found,布布扣,bubuko.com

Ubuntu 1604 安装配置 kafka,并配置开机自启(systemctl)

安装 kafka 需要先安装 jdk.一.下载官网:http://kafka.apache.org/downloads.html 二.安装 安装参考:https://segmentfault.com/a/1190000012990954 1. 解压安装(我的安装目录/opt/kafka/) # tar -zvxf kafka_2.11-2.1.0.tgz 2.  修改配置 # vim /opt/kafka/kafka_2.11-2.1.0/config/server.properties 3.

开发人员学Linux(5):CentOS7编译安装Nginx并搭建Tomcat负载均衡环境

1.前言在上一篇讲述了JMeter的使用,在本篇就可以应用得上了.本篇将讲述如何编译安装Nginx并利用前面的介绍搭建一个负载均衡测试环境.2.软件准备Nginx-1.12.0,下载地址:https://nginx.org/download/nginx-1.12.0.tar.gzTomcat8(本系列已介绍过如何下载和安装)JMeter(本系列已介绍过如何下载和使用)注:VirtualBox宿主机IP为"192.168.60.16,虚拟机IP为:192.168.60.198,虚拟机通过桥接方式接

Centos7 编译安装 Nginx、MariaDB、PHP

前言 本文主要大致介绍CentOS 7下编译安装Nginx.MariaDB.PHP.面向有Linux基础且爱好钻研的朋友.技艺不精,疏漏再所难免,还望指正. 环境简介: 系统: CentOS 7,最小化安装 IP: 192.168.170.128 Nginx: 1.6.1 MariaDB: 5.5.39 PHP: 5.5.16 1.准备工作 1.1.系统硬件准备 尽管Linux能最大化发挥硬件资源,但RHEL/CentOS随着版本增加对最低硬件的配置也越来越高[1].RHEL7/CentOS最低

CentOS6.6 32位 Minimal版本纯编译安装Nginx Mysql PHP Memcached

CentOS是红帽发行的免费的稳定Linux版本,在企业服务器应用中都会选用Minimal版本,因为Minimal是CentOS"最纯洁"的服务器系统,因为Minimal版本连vim编辑器都需要自己安装,Minimal组件最少,无桌面,扩展灵活,非常适合做服务器. 1.配置网卡 Minimal版本的CentOS被安装后,网卡驱动默认是down状态,需要手动激活,在连接好网线后需要执行命令: [[email protected] soft]# vi /etc/sysconfig/netw