Linux 使用 systemd 配置 MySQL、Nacos、Redis、Nginx 开机自启
前言
最近把服务器上的几个常用服务统一改成了 systemd 管理,包括:
- MySQL 5.7
- Nacos
- Redis 6.2
- Nginx
这样做的好处:
- 支持开机自动启动
- 统一管理服务
- 支持自动重启
- 支持 systemctl 管理
- 查看日志方便
服务器环境:
- Linux:CentOS 7
- systemd:219
- MySQL:5.7.44
- Redis:6.2.6
- Nginx:源码安装
- Nacos:Standalone 模式
确认系统支持 systemd
执行:1
bash systemctl --version
如果能看到版本号,例如:1
bash systemd 219
说明支持。
MySQL 开机自启
由于 MySQL 是 tar 包手动安装,并没有自带 systemd 服务。
MySQL 安装目录:1
/usr/mysql/mysql-5.7.44
1. 创建 mysqld.service
创建文件:1
vim /etc/systemd/system/mysqld.service
写入:
1 | [Unit] |
2. 生效并启动
1 | systemctl daemon-reload |
3. 查看状态
1 | systemctl status mysqld |
Nacos 开机自启
Nacos 启动命令:1
cd /usr/local/nacos/bin sh startup.sh -m standalone
1. 创建 nacos.service
1 | vim /etc/systemd/system/nacos.service |
写入:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23[Unit]
Description=Nacos Server
After=network.target mysqld.service redis.service
[Service]
Type=forking
WorkingDirectory=/usr/local/nacos
Environment="JAVA_HOME=/usr/java/jdk1.8.0_311"
Environment="PATH=/usr/java/jdk1.8.0_311/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
ExecStart=/bin/bash /usr/local/nacos/bin/startup.sh -m standalone
ExecStop=/bin/bash /usr/local/nacos/bin/shutdown.sh
User=root
Group=root
Restart=always
RestartSec=10
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
2. 生效并启动
1 | systemctl daemon-reload |
3. 查看状态
1 | systemctl status nacos |
4. Nacos 常见问题
JAVA_HOME 未配置
报错:1
ERROR: Please set the JAVA_HOME variable in your environment
解决:1
Environment="JAVA_HOME=/usr/java/jdk1.8.0_311"
Redis 开机自启
Redis 安装目录:1
/usr/local/redis/redis-6.2.6
原启动命令:1
cd /usr/local/redis/redis-6.2.6/src ./redis-server /usr/local/redis/redis-6.2.6/redis.conf
1. 修改 redis.conf
必须开启后台运行:1
vim /usr/local/redis/redis-6.2.6/redis.conf
修改:1
daemonize yes
2. 创建 redis.service
1 | vim /etc/systemd/system/redis.service |
写入:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21[Unit]
Description=Redis
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/redis/redis-6.2.6/src/redis-server /usr/local/redis/redis-6.2.6/redis.conf
ExecStop=/usr/local/redis/redis-6.2.6/src/redis-cli shutdown
ExecReload=/bin/kill -s HUP $MAINPID
Restart=always
RestartSec=5
User=root
Group=root
LimitNOFILE=10032
[Install]
WantedBy=multi-user.target
3. 生效并启动
1 | systemctl daemon-reload systemctl enable redis systemctl start redis |
4. 查看状态
1 | systemctl status redis |
Nginx 开机自启
Nginx 安装目录:1
/usr/local/nginx
原启动命令:1
/usr/local/nginx/sbin/nginx
1. 创建 nginx.service
1 | vim /etc/systemd/system/nginx.service |
写入:
1 | [Unit] |
2. 生效并启动
1 | systemctl daemon-reload |
3. 查看状态
1 | systemctl status nginx |
常见问题
Nginx 启动失败问题
启动时报错:1
cannot load certificate
原因:
SSL 证书文件不存在。
例如:1
/usr/local/nginx/html/cert/www.renyuxin.cn.pem
解决方式1
- 方案一:临时关闭 HTTPS
编辑:删除/注释:1
vim /usr/local/nginx/conf/nginx.conf
或删除/注释整个:1
2ssl_certificate xxx.pem;
ssl_certificate_key xxx.key;1
2
3
4server {
listen 443 ssl;
......
} - 方案二:上传正确证书
将证书上传到对应目录。1
2xxxxx.pem
xxxxx.key解决方式2
直接创建一个全新空白的基础nginx.conf1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
}
常用 systemctl 命令
启动服务
1 | systemctl start nginx |
停止服务
1 | systemctl stop nginx |
重启服务
1 | systemctl restart nginx |
查看状态
1 | systemctl status nginx |
查看是否开机自启
1 | systemctl is-enabled nginx |
查看日志
1 | journalctl -u nginx -f |
最终验证
重启服务器:1
reboot
验证:1
2
3
4
5
6
7systemctl status mysqld
systemctl status nacos
systemctl status redis
systemctl status nginx
查看端口:1
ss -tunlp | grep -E "80|3306|6379|8848"
总结
通过 systemd 管理服务后:
- 所有服务统一管理
- 支持开机自启
- 自动重启
- 查看日志方便
- 更适合生产环境
相比手动启动:1
2
3sh startup.sh
./redis-server
nginx
systemd 更稳定,也更规范。










