I'm new to stack overflow, Django, gunicorn, and so many things... so sorry in advance for any possible violation of the rules.
I'm trying to make gunicorn (running on Django) service automatically restart after rebooting the server (ubuntu 18.04).
When I reboot the computer, gunicorn is enabled and running, but the problem is that the port 8000 is already in use, as described in here:
Django Server Error: port is already in use
I can manually kill the process and restart the gunicorn (as suggested in the solution of the above link), but I want a solution to automate this process. Any suggestions would be appreciated. Thank you.
I guess I need to change some config files, but not sure what to do. I didn't develop the Django web server. Somebody created for me and I'm just using it, so I have no clue how to solve this issue.
EDIT: (Thanks for comment by Code-Apprentice to clarify the settings)
I'm running a website using Django, Nginx, and Gunicorn. Up to my understanding, nginx and gunicorn services are enabled. I mean, they are set to start once the ubuntu reboots.
When I reboot the ubuntu and type
sudo systemctl status nginx
it returns the following messages:
● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2023-03-14 13:10:19 KST; 36s ago
Docs: man:nginx(8) Process: 2741 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 2622 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 2751 (nginx)
Tasks: 73 (limit: 11059) CGroup: /system.slice/nginx.service
├─2751 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
├─2754 nginx: worker process
├─2756 nginx: worker process
├─2758 nginx: worker process
...
When monitoring the status of gunicorn, it returns:
● gunicorn.service - gunicorn daemon
Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Tue 2023-03-14 13:10:28 KST; 14min ago
Process: 2605 ExecStart=/data/webmaster/anaconda3/bin/gunicorn --workers 3 --bind 0.0.0.0:8000 cmlab_site.wsgi:application (code=exited, status=1/FAILURE)
Main PID: 2605 (code=exited, status=1/FAILURE)
3월 14 13:10:24 ubuntu gunicorn[2605]: [2023-03-14 13:10:24 +0900] [2605] [ERROR] Retrying in 1 second.
3월 14 13:10:25 ubuntu gunicorn[2605]: [2023-03-14 13:10:25 +0900] [2605] [ERROR] Connection in use: ('0.0.0.0', 8000)
3월 14 13:10:25 ubuntu gunicorn[2605]: [2023-03-14 13:10:25 +0900] [2605] [ERROR] Retrying in 1 second.
3월 14 13:10:26 ubuntu gunicorn[2605]: [2023-03-14 13:10:26 +0900] [2605] [ERROR] Connection in use: ('0.0.0.0', 8000)
3월 14 13:10:26 ubuntu gunicorn[2605]: [2023-03-14 13:10:26 +0900] [2605] [ERROR] Retrying in 1 second.
3월 14 13:10:27 ubuntu gunicorn[2605]: [2023-03-14 13:10:27 +0900] [2605] [ERROR] Connection in use: ('0.0.0.0', 8000)
3월 14 13:10:27 ubuntu gunicorn[2605]: [2023-03-14 13:10:27 +0900] [2605] [ERROR] Retrying in 1 second.
3월 14 13:10:28 ubuntu gunicorn[2605]: [2023-03-14 13:10:28 +0900] [2605] [ERROR] Can't connect to ('0.0.0.0', 8000)
3월 14 13:10:28 ubuntu systemd[1]: gunicorn.service: Main process exited, code=exited, status=1/FAILURE
3월 14 13:10:28 ubuntu systemd[1]: gunicorn.service: Failed with result 'exit-code'.
To figure out what process is using the port 8000, I checked
sudo lsof -i :8000
and got:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
python3 1914 root 8u IPv4 69539 0t0 TCP url_of_my_website:46512->ubuntu:8000 (SYN_SENT)
node 2730 root 18u IPv4 67787 0t0 TCP *:8000 (LISTEN)