登录
首页 >  文章 >  php教程

关闭宝塔闲置服务,节省资源更高效

时间:2026-04-12 15:39:42 173浏览 收藏

宝塔面板中大量闲置服务长期运行不仅会无谓消耗服务器资源,还可能带来潜在安全风险,本文系统梳理了四种高效关闭闲置服务的实用方法:既可通过宝塔内置bt命令安全停止核心服务,也能运行定制化Shell脚本一键关停Nginx、MySQL、Redis及多版本PHP-FPM等常见服务;对不熟悉命令行的用户,任务管理器提供直观可视化操作;而systemctl命令则支持精准识别并停用非宝塔依赖的冗余系统服务,兼顾安全性与可控性——无论你是运维新手还是资深管理员,都能快速找到最适合自己的优化方案。

如何在宝塔面板中一键关闭所有不必要的闲置系统服务?

如果您在宝塔面板中发现大量后台服务处于运行状态但实际未被使用,可能造成资源占用过高或安全风险。以下是多种可执行的一键关闭闲置系统服务的方法:

一、使用宝塔内置命令行工具停止面板及关联服务

该方法通过宝塔官方提供的命令行接口统一控制核心服务启停,操作安全且兼容性强,适用于大多数Linux发行版。

1、通过SSH连接服务器,执行以下命令进入宝塔命令行工具:
bt

2、在交互式菜单中选择编号2(停止面板服务)

3、确认执行后,面板自身服务(btPanel)将立即停止

4、若需同时停止Web服务,再依次执行:
/etc/init.d/nginx stop
/etc/init.d/mysqld stop
/etc/init.d/php-fpm-74 stop(请根据您实际安装的PHP版本替换数字)

二、执行自定义Shell脚本批量关停服务

该方法依据宝塔官方停服逻辑封装为可复用脚本,能识别并关停Nginx、Apache、MySQL、Pure-FTPd、Redis、Memcached、Tomcat及各PHP-FPM实例等常见服务,避免遗漏。

1、复制以下完整脚本内容:
/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH stop_service(){ /etc/init.d/bt stop if [ -f "/etc/init.d/nginx" ]; then /etc/init.d/nginx stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/httpd" ]; then /etc/init.d/httpd stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/mysqld" ]; then /etc/init.d/mysqld stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/pure-ftpd" ]; then /etc/init.d/pure-ftpd stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/tomcat" ]; then /etc/init.d/tomcat stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/redis" ]; then /etc/init.d/redis stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/memcached" ]; then /etc/init.d/memcached stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-52" ]; then /etc/init.d/php-fpm-52 stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-53" ]; then /etc/init.d/php-fpm-53 stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-54" ]; then /etc/init.d/php-fpm-54 stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-55" ]; then /etc/init.d/php-fpm-55 stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-56" ]; then /etc/init.d/php-fpm-56 stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-70" ]; then /etc/init.d/php-fpm-70 stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-71" ]; then /etc/init.d/php-fpm-71 stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-72" ]; then /etc/init.d/php-fpm-72 stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-73" ]; then /etc/init.d/php-fpm-73 stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-74" ]; then /etc/init.d/php-fpm-74 stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-80" ]; then /etc/init.d/php-fpm-80 stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-81" ]; then /etc/init.d/php-fpm-81 stop > /dev/null 2>&1 fi swapoff /www/swap } stop_service

2、将脚本保存为stop-all-services.sh文件

3、赋予执行权限:
chmod +x stop-all-services.sh

4、运行脚本:
./stop-all-services.sh

三、通过宝塔任务管理器手动筛选并关停服务

该方法提供可视化界面操作,适合不熟悉命令行的用户,可逐项确认服务状态与用途后再决定是否关闭。

1、登录宝塔面板,在左侧菜单栏点击【宝塔任务管理器】

2、切换至【服务】标签页

3、浏览服务列表,识别名称中含“backup”、“test”、“demo”、“sample”、“dev”等标识的服务

4、对确认闲置的服务,点击其右侧的【停止】按钮

5、对已停止但仍在进程列表中残留的服务,切换至【会话】页,执行ps aux | grep 定位PID,再使用kill -9 强制终止

四、使用systemctl命令精准关停非宝塔核心服务

该方法基于systemd服务管理机制,可列出所有启用状态的服务并筛选出非必要项,避免误停宝塔依赖服务。

1、列出所有正在运行的服务:
systemctl list-units --type=service --state=running

2、过滤出非宝塔相关服务:
systemctl list-units --type=service --state=running | grep -E -v "(bt|nginx|httpd|mysql|mysqld|php|redis|memcached|pure-ftpd|tomcat)"

3、对输出结果中的每个服务名(如cups.service、avahi-daemon.service),执行:
systemctl stop
systemctl disable

4、特别注意保留以下服务:bt、nginx、mysqld、php-fpm-*、pure-ftpd

到这里,我们也就讲完了《关闭宝塔闲置服务,节省资源更高效》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

资料下载
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>