前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >systemd设置nginx开机自启动

systemd设置nginx开机自启动

作者头像
胡齐
发布2020-08-11 17:58:57
6.3K0
发布2020-08-11 17:58:57
举报
文章被收录于专栏:运维猫运维猫

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进程,比如

代码语言:javascript
复制
[root@localhost ~]# sudo /etc/init.d/apache start
# 或者
[root@localhost ~]# service apache start

优点

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

这种方法有两个缺点。

  • 一是启动时间长。init进程是串行启动,只有前一个进程启动完,才会启动下一个进程。
  • 二是启动脚本复杂。init进程只是执行启动脚本,不管其他事情。脚本需要自己处理各种情况,这往往使得脚本变得很长。

Systemd 就是为了解决这些问题而诞生的。它的设计目标是,为系统的启动和管理提供一套完整的解决方案。根据 Linux 惯例,字母d是守护进程(daemon)的缩写。Systemd 这个名字的含义,就是它要守护整个系统。使用了 Systemd,就不需要再用init了。Systemd 取代了initd,成为系统的第一个进程(PID 等于 1),其他进程都是它的子进程。

3、常用命令

代码语言:javascript
复制
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等编译环境
代码语言:javascript
复制
[root@localhost ~]# yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre-devel openssl openssl-devel
4.2下载nginx1.12.0并解压
代码语言:javascript
复制
[root@localhost ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
[root@localhost ~]# tar -xzvf nginx-1.18.0.tar.gz
[root@localhost ~]# cd nginx-1.18.0
[root@localhost nginx-1.8.0]#
4.3创建目录
代码语言:javascript
复制
[root@localhost nginx-1.8.0]# mkdir -p /var/temp
[root@localhost nginx-1.8.0]# mkdir -p /var/temp/nginx
[root@localhost nginx-1.8.0]# mkdir -p /var/temp/run/nginx
[root@localhost nginx-1.8.0]# chmod a+wrx -R temp
[root@localhost nginx-1.8.0]# chmod a+wrx -R /var/temp
4.4配置编译选项
代码语言:javascript
复制
[root@localhost 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编译安装
代码语言:javascript
复制
[root@localhost nginx-1.18.0]# make && make install

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

4.6测试启动
代码语言:javascript
复制
[root@localhost ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

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

4.7测试访问
代码语言:javascript
复制
[root@localhost ~]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
4.8浏览器如果不能访问,就打开防火墙或者开端口

关闭防火墙

代码语言:javascript
复制
[root@localhost ~]# systemctl stop firewalld.service

开放端口

代码语言:javascript
复制
[root@localhost ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
[root@localhost ~]# firewall-cmd --reload

5、设置开机启动

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

5.1在系统服务目录里创建nginx.service文件
代码语言:javascript
复制
[root@localhost ~]# vi /usr/lib/systemd/system/nginx.service # 写入内容如下
[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
5.2解释
代码语言:javascript
复制
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
5.3设置开机自启动
代码语言:javascript
复制
[root@localhost ~]# systemctl enable nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
5.4查看nginx状态
代码语言:javascript
复制
[root@localhost ~]# 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
代码语言:javascript
复制
[root@localhost ~]# pkill -9 nginx
[root@localhost ~]# ps -aux|grep nginx
root      29703  0.0  0.1  12112  1108 pts/0    R+   04:10   0:00 grep --color=auto nginx
[root@localhost ~]# systemctl start nginx

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

5.6重启服务器
代码语言:javascript
复制
[root@localhost ~]# reboot
5.7再次连接后,查看服务状态
代码语言:javascript
复制
[root@localhost ~]# 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 process

Aug 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查看启动耗时

代码语言:javascript
复制
# 查看启动耗时
[root@localhost ~]# systemd-analyze                                                                                      
# 查看每个服务的启动耗时
[root@localhost ~]# systemd-analyze blame

# 显示瀑布状的启动过程流
[root@localhost ~]# systemd-analyze critical-chain

# 显示指定服务的启动流
[root@localhost ~]# critical-chain atd.service

7、依赖关系

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

代码语言:javascript
复制
# 列出一个 Unit 的所有依赖
[root@localhost ~]# systemctl list-dependencies nginx.service

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

代码语言:javascript
复制
[root@localhost ~]# systemctl list-dependencies --all nginx.service

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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-08-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 运维猫 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、简介
  • 2、systemd使用
  • 3、常用命令
  • 4、手动安装nginx
    • 4.1安装gcc等编译环境
      • 4.2下载nginx1.12.0并解压
        • 4.3创建目录
          • 4.4配置编译选项
            • 4.5编译安装
              • 4.6测试启动
                • 4.7测试访问
                  • 4.8浏览器如果不能访问,就打开防火墙或者开端口
                  • 5、设置开机启动
                    • 5.1在系统服务目录里创建nginx.service文件
                      • 5.2解释
                        • 5.3设置开机自启动
                          • 5.4查看nginx状态
                            • 5.5杀死nginx重启nginx
                              • 5.6重启服务器
                                • 5.7再次连接后,查看服务状态
                                • 6、systemd-analyze查看启动耗时
                                • 7、依赖关系
                                领券
                                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档