zoukankan      html  css  js  c++  java
  • Centos7下的开机自启动

      记录服务器每次重启之后启动一些服务.

    systemd

    systemd简介

    CentOS7已不再使用chkconfig管理启动项,而是使用systemd。关于systemd的衍生和发展,可以参见《CentOS7/RHEL7 systemd详解》《CentOS7进程管理systemd详解》。简单介绍如下:

    Linux系统从启动到提供服务的过程是这样,先是机器加电,然后通过MBR或者UEFI加载GRUB,再启动内核,内核启动服务,然后开始对外服务。

    SysV init、UpStart、systemd主要是解决服务引导管理的问题。

    SysV init是最早的解决方案,依靠划分不同的运行级别,启动不同的服务集,服务依靠脚本控制,并且是顺序执行的。在CentOS5中使用,配置文件为/etc/inittab。
    SysV init方案的优点是:原理简单,易于理解;依靠shell脚本控制,编写服务脚本门槛比较低。
    缺点是:服务顺序启动,启动过程比较慢;不能做到根据需要来启动服务,比如通常希望插入U盘的时候,再启动USB控制的服务,这样可以更好的节省系统资源。

    为了解决系统服务的即插即用,UpStart应运而生,在CentOS6系统中,SysV init和UpStart是并存的,UpStart主要解决了服务的即插即用。服务顺序启动慢的问题,UpStart的解决办法是把相关的服务分组,组内的服务是顺序启动,组之间是并行启动。在CentOS6系统中,配置文件为/etc/inittab和/etc/init/*.conf。

    但是随着移动互联网的到来,SysV init服务启动慢的问题显得越来越突出,许多移动设备都是基于Linux内核,比如安卓。移动设备启动比较频繁,每次启动都要等待服务顺序启动,显然难以接受,systemd就是为了解决这个问题诞生的。在CentOS7中使用,其配置文件为/usr/lib/systemd/system/ 和 /etc/systemd/system/ 中的文件。
    systemd的设计思路是:尽可能的快速启动服务;尽可能的减少系统资源占用。

    systemd使用

    在CentOS7中,systemctl命令主要负责控制systemd系统和服务管理器。基本取代了service和chkconfig命令,虽然service和chkconfig命令依然保留,但是据说已经被阉割过。

    参考《Centos7下的systemctl命令与service和chkconfig》,整理常用命令如下:

    • systemctl --version,查看版本。
    • whereis systemctl,查看位置。
    • systemctl list-unit-files,列出所有可用单元(服务)。
    • systemctl list-units,列出所有运行中的单元。
    • systemctl --failed,列出所有失败的单元。
    • systemctl list-unit-files | grep enable,查看自启动的软件。
    • systemctl is-enabled mysqld.service,查看某个单元是否开机启动。
    • systemctl status mysqld.service,查看某个单元的状态。
    • systemctl start mysqld.service,启动某个单元。
    • systemctl restart mysqld.service,重启某个单元。
    • systemctl stop mysqld.service,停止某个单元。
    • systemctl daemon-reload,修改了某个单元的配置文件后,重载配置文件。
    • systemctl reload mysqld.service,重载某个单元。
    • systemctl enable mysqld.service,设置开机自启动。
    • systemctl disable mysqld.service,关闭开机自启动。
    • systemctl kill mysqld,杀死单元。

    nginx

      1. yum 安装nginx
        1) 安装nginx存储库
    yum  install epel-release

        2) 安装nginx

    sudo yum install nginx

        3)启动nginx

    systemctl start nginx

    防火墙处理:

     

    sudo firewall-cmd --permanent --zone=public --add-service=http 
    sudo firewall-cmd --permanent --zone=public --add-service=https
    sudo firewall-cmd --reload

        4) 开机启动

    sudo  systemctl enable nginx

        默认安装路径:

    (1) Nginx配置路径:/etc/nginx/
    (2) PID目录:/var/run/nginx.pid
    (3) 错误日志:/var/log/nginx/error.log
    (4) 访问日志:/var/log/nginx/access.log
    (5) 默认站点目录:/usr/share/nginx/html

      2. 手动安装nginx

        1) 编译安装

    1、安装gcc等编译环境
    yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
    
    2、下载nginx1.12.0并解压
    wget http://nginx.org/download/nginx-1.12.0.tar.gz
    
    tar -xzvf nginx-1.12.0.tar.gz
    
    cd nginx-1.12.0
    
    3. 创建目录
    mkdir -p /var/temp
    mkdir -p /var/temp/nginx
    mkdir -p /var/temp/run/nginx
    chmod a+wrx -R temp
    
    4. 配置编译选项
    ./configure 
    --prefix=/usr/local/nginx 
    --pid-path=/var/temp/run/nginx/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
    
    5. 编译安装
    make && make install

      

    浏览器如果不能访问,就打开防火墙或者开端口。
    关闭防火墙,systemctl stop firewalld.service
    开放端口,firewall-cmd --zone=public --add-port=80/tcp --permanent,firewall-cmd --reload

        2) 设置开机启动

          a) 在系统服务目录中创建nginx.service文件

    vi /usr/lib/systemd/system/nginx.service

          b)加入内容:

    [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


          c)开机启动设置
    systemctl  enable  nginx.service
    
    #查看状态
    systemctl status nginx.service
    
    #启动服务
    systemctl start nginx.service

      



  • 相关阅读:
    自增自减
    字符串处理函数
    指针总结指向const的指针、const指针、指向const指针的const指针
    Jenkins基础篇 系列之-—04 节点配置
    soapUI系列之—-07 调用JIRA Rest API接口【例】
    测试人员转正考核方向
    linux系列之-—03 常见问题
    Jenkins基础篇 系列之-—03 配置用户和权限控制
    linux系列之-—01 shell编程笔记
    Jenkins基础篇 系列之-—02 修改Jenkins用户的密码
  • 原文地址:https://www.cnblogs.com/xingxia/p/linux_boot_auto.html
Copyright © 2011-2022 走看看