Linux配置服务开机自启,Naocs、Redis、RocketMq、ElasticSearch、Nginx
在Linux系统中,可以使用systemd
来配置服务的开机自启。以下是针对Naocs
、Redis
、RocketMQ
、ElasticSearch
和Nginx
的自启配置示例:
- Nacos
创建一个名为nacos.service
的systemd服务文件:
[Unit]
Description=Nacos Server
After=network.target
[Service]
Type=simple
ExecStart=/your/path/to/nacos/bin/startup.sh -m standalone
ExecStop=/your/path/to/nacos/bin/shutdown.sh
User=nacos
Restart=on-failure
[Install]
WantedBy=multi-user.target
将/your/path/to/nacos/bin/startup.sh
替换为实际的启动脚本路径。
启动并启用Nacos服务:
sudo systemctl daemon-reload
sudo systemctl start nacos.service
sudo systemctl enable nacos.service
- Redis
Redis通常作为守护进程自启动,但可以通过systemd配置:
[Unit]
Description=Redis In-Memory Data Store
After=network.target
[Service]
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always
[Install]
WantedBy=multi-user.target
启动并启用Redis服务:
sudo systemctl daemon-reload
sudo systemctl start redis.service
sudo systemctl enable redis.service
- RocketMQ
RocketMQ通常需要使用名为mqnamesrv
和mqbroker
的两个脚本,配置方法类似:
创建rocketmq.service
:
[Unit]
Description=Apache RocketMQ
After=network.target
[Service]
Type=simple
ExecStart=/your/path/to/rocketmq/bin/mqnamesrv
ExecStop=/your/path/to/rocketmq/bin/mqshutdown namesrv
User=rocketmq
Restart=on-failure
[Install]
WantedBy=multi-user.target
启动并启用Namesrv服务:
sudo systemctl daemon-reload
sudo systemctl start rocketmq.service
sudo systemctl enable rocketmq.service
对于Broker,创建一个新的服务文件,替换路径和用户名。
- Elasticsearch
Elasticsearch可以使用systemd直接启动,但通常建议通过elasticsearch-systemd-script来配置:
sudo /your/path/to/elasticsearch/bin/elasticsearch-systemd-setup
sudo systemctl daemon-reload
sudo systemctl start elasticsearch.service
sudo systemctl enable elasticsearch.service
- Nginx
对于Nginx,通常已经有一个systemd服务文件:
sudo systemctl daemon-reload
sudo systemctl start nginx.service
sudo systemctl enable nginx.service
如果没有,可以创建一个:
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/us
评论已关闭