systemd设置nginx开机自启动

网友投稿 942 2022-11-06

本站部分文章、图片属于网络上可搜索到的公开信息,均用于学习和交流用途,不能代表睿象云的观点、立场或意见。我们接受网民的监督,如发现任何违法内容或侵犯了您的权益,请第一时间联系小编邮箱jiasou666@gmail.com 处理。

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

[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、常用命令

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等编译环境

[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并解压

[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创建目录

[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配置编译选项

[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编译安装

[root@localhost nginx-1.18.0]# make && make install

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

4.6测试启动

[root@localhost ~]# usr/local/nginx/sbin/nginx -c usr/local/nginx/conf/nginx.conf

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

4.7测试访问

[root@localhost ~]# curl localhostWelcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.

For online documentation and support please refer tonginx.org.
Commercial support is available atnginx.com.

Thank you for using nginx.

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

关闭防火墙

[root@localhost ~]# systemctl stop firewalld.service

开放端口

[root@localhost ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent[root@localhost ~]# firewall-cmd --reload

5、设置开机启动

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

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

[root@localhost ~]# 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设置开机自启动

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

5.4查看nginx状态

[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

[root@localhost ~]# pkill -9 nginx[root@localhost ~]# ps -aux|grep nginxroot      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重启服务器

[root@localhost ~]# reboot

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

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

# 查看启动耗时[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。

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

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

[root@localhost ~]# systemctl list-dependencies --all nginx.service

上一篇:软件测试培训之测试人员容易遗漏一些隐藏的缺陷
下一篇:软件测试培训之谈谈软件兼容性测试
相关文章

 发表评论

暂时没有评论,来抢沙发吧~