天天看点

cmd 顺序启动服务_systemd设置nginx开机自启动Welcome to nginx!

1、简介

服务器每次重启,都需要手动启动一些服务,这不是一个程序员可以忍受的,难怪大家都喜欢写脚本。CentOS7之后已不再使用chkconfig管理启动项,而是使用systemd。

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

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

2、systemd使用

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

历史上,linux的启动一直采用init进程,比如

[[email protected] ~]# sudo /etc/init.d/apache start# 或者[[email protected] ~]# service apache start
           

优点

  • 原理简单,易于理解;依靠shell脚本控制,编写服务脚本门槛比较低。

这种方法有两个缺点。

  • 一是启动时间长。

    init

    进程是串行启动,只有前一个进程启动完,才会启动下一个进程。
  • 二是启动脚本复杂。

    init

    进程只是执行启动脚本,不管其他事情。脚本需要自己处理各种情况,这往往使得脚本变得很长。

Systemd 就是为了解决这些问题而诞生的。它的设计目标是,为系统的启动和管理提供一套完整的解决方案。根据 Linux 惯例,字母

d

是守护进程(daemon)的缩写。Systemd 这个名字的含义,就是它要守护整个系统。使用了 Systemd,就不需要再用

init

了。Systemd 取代了

initd

,成为系统的第一个进程(PID 等于 1),其他进程都是它的子进程。

3、常用命令

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 # 杀死单元。
           

4、手动安装nginx

4.1安装gcc等编译环境

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

4.2下载nginx1.12.0并解压

[[email protected] ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz[[email protected] ~]# tar -xzvf nginx-1.18.0.tar.gz[[email protected] ~]# cd nginx-1.18.0[[email protected] nginx-1.8.0]#
           

4.3创建目录

[[email protected] nginx-1.8.0]# mkdir -p /var/temp[[email protected] nginx-1.8.0]# mkdir -p /var/temp/nginx[[email protected] nginx-1.8.0]# mkdir -p /var/temp/run/nginx[[email protected] nginx-1.8.0]# chmod a+wrx -R temp[[email protected] nginx-1.8.0]# chmod a+wrx -R /var/temp
           

4.4配置编译选项

[[email protected] nginx-1.8.0]#./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# 切记,pid-path不能设置为/var/run/nginx/nginx.pid。因为CentOS每次重启后,都会删除/var/run目录中的自建目录和文件,从而导致nginx自启动失败。
           

4.5编译安装

[[email protected] nginx-1.18.0]# make && make install
           

进入/usr/local/nginx查看文件是否存在conf、sbin、html文件夹,若存在则安装成功

4.6测试启动

[[email protected] ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
           

如果不指定-c,nginx在启动时默认加载conf/nginx.conf文件。

4.7测试访问

[[email protected] ~]# curl localhostWelcome to nginx!    body {        width: 35em;        margin: 0 auto;        font-family: Tahoma, Verdana, Arial, sans-serif;    }
           

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.

For online documentation and support please refer to

href="http://nginx.org/" target="_blank" rel="external nofollow" >nginx.org.Commercial support is available athref="http://nginx.com/" target="_blank" rel="external nofollow" >nginx.com.

Thank you for using nginx.

4.8浏览器如果不能访问,就打开防火墙或者开端口

关闭防火墙

[[email protected] ~]# systemctl stop firewalld.service
           

开放端口

[[email protected] ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent[[email protected] ~]# firewall-cmd --reload
           

5、设置开机启动

手动安装的nginx,该怎样设置开机自启动?

5.1在系统服务目录里创建nginx.service文件

[[email protected] ~]# vi /usr/lib/systemd/system/nginx.service # 写入内容如下[Unit]Description=nginxAfter=network.target  [Service]Type=forkingExecStart=/usr/local/nginx/sbin/nginxExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s quitPrivateTmp=true  [Install]WantedBy=multi-user.target
           

5.2解释

[Unit]:服务的说明Description:描述服务After:描述服务类别[Service]服务运行参数的设置Type=forking是后台运行的形式ExecStart为服务的具体运行命令ExecReload为重启命令ExecStop为停止命令PrivateTmp=True表示给服务分配独立的临时空间注意:[Service]的启动、重启、停止命令全部要求使用绝对路径[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
           

5.3设置开机自启动

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

5.4查看nginx状态

[[email protected] ~]# systemctl status nginx.service● nginx.service - nginx   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor>   Active: inactive (dead)#  很奇怪,明明启动成功了,为什么显示Active: inactive (dead)?
           

5.5杀死nginx重启nginx

[[email protected] ~]# pkill -9 nginx[[email protected] ~]# ps -aux|grep nginxroot      29703  0.0  0.1  12112  1108 pts/0    R+   04:10   0:00 grep --color=auto nginx[[email protected] ~]# systemctl start nginx
           

再次查看状态,变成了active,搞定。

5.6重启服务器

[[email protected] ~]# reboot
           

5.7再次连接后,查看服务状态

[[email protected] ~]# systemctl status nginx● nginx.service - nginx   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor>   Active: active (running) since Mon 2020-08-10 04:11:56 EDT; 1min 17s a>  Process: 905 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status> Main PID: 913 (nginx)    Tasks: 2 (limit: 4879)   Memory: 2.5M   CGroup: /system.slice/nginx.service           ├─913 nginx: master process /usr/local/nginx/sbin/nginx           └─917 nginx: worker processAug 10 04:11:56 localhost.localdomain systemd[1]: Starting nginx...Aug 10 04:11:56 localhost.localdomain systemd[1]: Started nginx.
           

看到nginx已经启动,至此,nginx自启动配置成功。

6、systemd-analyze查看启动耗时

# 查看启动耗时[[email protected] ~]# systemd-analyze                                                                                      # 查看每个服务的启动耗时[[email protected] ~]# systemd-analyze blame# 显示瀑布状的启动过程流[[email protected] ~]# systemd-analyze critical-chain# 显示指定服务的启动流[[email protected] ~]# critical-chain atd.service
           

7、依赖关系

Unit 之间存在依赖关系:A 依赖于 B,就意味着 Systemd 在启动 A 的时候,同时会去启动 B。

# 列出一个 Unit 的所有依赖[[email protected] ~]# systemctl list-dependencies nginx.service
           

上面命令的输出结果之中,有些依赖是 Target 类型,默认不会展开显示。如果要展开 Target,就需要使用

--all

参数。

[[email protected] ~]# systemctl list-dependencies --all nginx.service
           

如果文章有任何错误欢迎不吝赐教,其次大家有任何关于运维的疑难杂问,也欢迎和大家一起交流讨论。关于运维学习、分享、交流,笔者开通了微信公众号【运维猫】,感兴趣的朋友可以关注下,欢迎加入,建立属于我们自己的小圈子,一起学运维知识。群主还经营一家Orchis饰品店,喜欢的小伙伴欢迎👏前来下单。

扫描二维码

获取更多精彩

运维猫公众号

cmd 顺序启动服务_systemd设置nginx开机自启动Welcome to nginx!

有需要技术交流的小伙伴可以加我微信,期待与大家共同成长,本人微信:

扫描二维码

添加私人微信

运维猫博主

cmd 顺序启动服务_systemd设置nginx开机自启动Welcome to nginx!

扫码加微信

最近有一些星友咨询我知识星球的事,我也想继续在星球上发布更优质的内容供大家学习和探讨。运维猫公众号平台致力于为大家提供免费的学习资源,知识星球主要致力于即将入坑或者已经入坑的运维行业的小伙伴。

cmd 顺序启动服务_systemd设置nginx开机自启动Welcome to nginx!
cmd 顺序启动服务_systemd设置nginx开机自启动Welcome to nginx!

点击阅读原文  查看更多精彩内容!!!