How to set nginx service and uwsgi service to start automatically at boot

Last time I learned how to deploy a Django project under a cloud server. The nginx service and the uwsgi service were used. You need to manually start the commands for these two services.

Now consider how to set up auto-start at boot. Why should you consider this? Because if there is a problem with the server and it restarts unexpectedly, won't the Django project we deployed hang up? Will I still have to start it manually?

So it is best to configure auto-start at boot just in case.

I checked a lot of self-starting configurations on the Internet, and finally chose: modify the system file /etc/rc.d/rc.local and add a startup command script.

During the testing process, I found that it is easier to start the nginx service automatically, just add it directly. However, I found that the uwsgi service has several ways to configure the service to start automatically after rebooting, causing the cloud server to hang up and unable to log in. It can only be accessed by restarting through the cloud service official website, so operate with caution! ! !

After many tests and online experience summarization, a feasible solution was determined:

1. Under the /etc/init.d/ path, create the uwsgi.sh startup command script. Note "Be sure to put it under the /etc/init.d/ path!!!". Other paths can easily cause the cloud server to hang. I've tried it many times.

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

Add the following content: Note "The file to execute the command must use an absolute path!!!"

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

esc to exit editing, :wq to save changes

Modify /etc/init.d/uwsgi.sh to be executable

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

3. Add the boot auto-start script to /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 to exit editing, :wq to save changes

Modify rc.local to be executable

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

4. Reboot restart test results

[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

Guess you like

Origin blog.csdn.net/xionghui2007/article/details/132754423