nginx服务和uwsgi服务如何设置开机自启动

上次学到了在云服务器下如何部署Django项目,用到了nginx服务和uwsgi服务,需要手工启动这2个服务的命令。

现在考虑如何设置开机自启动,为什么要这样考虑?因为服务器万一出问题,意外重启了,那我们部署的Django项目不就挂了吗?到时候还是要人工去启动吗?

所以最好还是配置开机自启动以防万一。

网上查了很多开机自启动配置,最后选择:修改系统文件 /etc/rc.d/rc.local ,添加启动命令脚本。

测试过程中,发现nginx服务自启动比较容易,直接添加就行。但是发现 uwsgi 服务,配置该服务开机自启几种方式,reboot后,导致云服务器挂掉,无法再登录,只能通过云服务官网重启才能进入,所以谨慎操作!!!

经过多次测试和网上经验总结,确定了一个可行方案:

1. 在 /etc/init.d/ 路径下面,建立 uwsgi.sh 启动命令脚本,注意“一定要放到 /etc/init.d/ 路径下面!!!”,其他路径容易导致云服务器挂掉,我试过多次了。

2.  vim /etc/init.d/uwsgi.sh

添加如下内容:注意“执行命令的文件一定要用绝对路径!!!”

#!/bin/bash
/usr/python3/bin/uwsgi --ini /home/django_pro/mysite/uwsgi.ini;

esc 退出编辑,:wq 保存修改

将 /etc/init.d/uwsgi.sh修改为可执行

chmod 777 /etc/init.d/uwsgi.sh

3. 添加开机自启动脚本 到  /etc/rc.d/rc.local 上。

vim /etc/rc.d/rc.local

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

#开机自启动 nginx 服务
/usr/sbin/nginx
#开机自启动 uwsgi 服务,一定要用绝对路径执行该sh
/etc/init.d/uwsgi.sh

esc 退出编辑,:wq 保存修改

将rc.local修改为可执行

chmod 777 /etc/rc.d/rc.local

4. reboot 重启检验结果

[root@~]# ps -ef|grep uwsgi
root      1056     1  0 16:23 ?        00:00:00 /usr/python3/bin/uwsgi --ini /home/django_pro/mysite/uwsgi.ini
root      1408  1056  0 16:23 ?        00:00:00 /usr/python3/bin/uwsgi --ini /home/django_pro/mysite/uwsgi.ini
root      1572  1473  0 16:48 pts/0    00:00:00 grep --color=auto uwsgi
[root@~]# ps -ef|grep nginx
root      1017     1  0 16:23 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx     1023  1017  0 16:23 ?        00:00:00 nginx: worker process
root      1574  1473  0 16:49 pts/0    00:00:00 grep --color=auto nginx

猜你喜欢

转载自blog.csdn.net/xionghui2007/article/details/132754423