Django deploy Gunicorn and NginX - django

While trying to finally deploy my NEW rover web app a problem occured. I already configured Gunicorn and bind it to 0.0.0.0:80 (with wsgi). Website loads, but without any images, css, javascript files (even "admin" site is without any style). Postgres is installed and running fine. I already did
python manage.py collectstatic
python manage.py migrate
I watched tons of tutorials how to do it. But still after I try to run:
systemctl status nginx.service
i get an error:
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.
#systemctl status nginx.service output:
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since śro 2018-11-28 02:57:12 CET; 1min 13s ago
Process: 14721 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=2)
Process: 25481 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)
Process: 25477 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 10279 (code=exited, status=0/SUCCESS)
nginx[25481]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx[25481]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx[25481]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx[25481]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx[25481]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx[25481]: nginx: [emerg] still could not bind()
systemd[1]: nginx.service: Control process exited, code=exited status=1
systemd[1]: Failed to start A high performance web server and a reverse proxy server.
systemd[1]: nginx.service: Unit entered failed state.
systemd[1]: nginx.service: Failed with result 'exit-code'.
#journalctl -xe output:
sshd[5669]: Received disconnect from 181.15.216.20 port 42140:11: Bye Bye [preauth]
sshd[5669]: Disconnected from 181.15.216.20 port 42140 [preauth]
sudo[5773]: djangosu : TTY=pts/5 ; PWD=/home/djangosu ; USER=root ; COMMAND=/bin/systemctl restart nginx
sudo[5773]: pam_unix(sudo:session): session opened for user root by djangosu(uid=0)
systemd[1]: Stopped A high performance web server and a reverse proxy server.
-- Subject: Unit nginx.service has finished shutting down
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has finished shutting down.
systemd[1]: Starting A high performance web server and a reverse proxy server...
-- Subject: Unit nginx.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has begun starting up.
nginx[5957]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx[5957]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address aginx[5957]: nginx: [emerg] still could not bind()
systemd[1]: nginx.service: Control process exited, code=exited status=1
systemd[1]: Failed to start A high performance web server and a reverse proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has failed.
--
-- The result is failed.
systemd[1]: nginx.service: Unit entered failed state.
systemd[1]: nginx.service: Failed with result 'exit-code'.
sudo[5773]: pam_unix(sudo:session): session closed for user root
sudo[6331]: djangosu : TTY=pts/5 ; PWD=/home/djangosu ; USER=root ; COMMAND=/bin/journalctl -xe
sudo[6331]: pam_unix(sudo:session): session opened for user root by djangosu(uid=0)lready in use)
nginx[5957]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx[5957]: nginx: [emerg] still could not bind()
systemd[1]: nginx.service: Control process exited, code=exited status=1
systemd[1]: Failed to start A high performance web server and a reverse proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has failed.
--
-- The result is failed.
systemd[1]: nginx.service: Unit entered failed state.
systemd[1]: nginx.service: Failed with result 'exit-code'.
sudo[5773]: pam_unix(sudo:session): session closed for user root
sudo[6331]: djangosu : TTY=pts/5 ; PWD=/home/djangosu ; USER=root ; COMMAND=/bin/journalctl -xe
sudo[6331]: pam_unix(sudo:session): session opened for user root by djangosu(uid=0)
/etc/systemd/system/gunicorn.socket file:
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
/etc/systemd/system/gunicorn.service file:
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=djangosu
Group=djangosu
WorkingDirectory=/home/djangosu/website/rover-Project
ExecStart=/home/djangosu/website/rover-env/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
rover.wsgi:application
[Install]
WantedBy=multi-user.target
And "rover" file (the same as name of project) in sites-available (I already linked it to sites-enabled):
server {
listen 80;
server_name :333 ;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/.../website/rover/rover;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
gunicorn.sock exists in /run/ dir.... Any suggestions ?
Release note:
Distributor ID: Ubuntu
Description: Ubuntu 16.04.5 LTS
Release: 16.04
Codename: xenial

nginx is telling you, it cannot bind to port 80 as some other process is already listening on this port. You can see this in your log output here:
nginx[5957]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
To check which process is listening on this port you could use netstat or better ss:
~# ss -naptu state listening | grep :80
tcp 0 128 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=6824,fd=4),("nginx",pid=6823,fd=4))
~# netstat -tulpen | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 0 136280382 6823/nginx: master
The last column will show the process name and the pid of the process listening on the port.

Related

Nginx on EC2: Unable to load the default web page but the service is active

I have installed the Nginx in my ec2 machine
The ubuntu version is
Distributor ID:Ubuntu
Description:Ubuntu 20.04.3 LTS
Release:20.04
Codename:focal
I have installed using the following command
sudo apt install Nginx
After that, I can able to see that the Nginx service is up and running
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 Sun 2022-06-19 07:27:28 UTC; 9min ago
Docs: man:nginx(8)
Process: 4938 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 4940 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 4941 (nginx)
Tasks: 3 (limit: 1116)
Memory: 4.9M
CGroup: /system.slice/nginx.service
├─4941 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
├─5003 nginx: worker process
└─5004 nginx: worker process
Jun 19 07:27:28 ip-172-31-42-178 systemd[1]: Starting A high performance web server and a reverse proxy server...
Jun 19 07:27:28 ip-172-31-42-178 systemd[1]: Started A high performance web server and a reverse proxy server.
But when i access the ip of the instance i m getting the error site can't be reached
Security rule groups is added for port 80 and 443.
SSH is active on port 22.
Checked the var/log/nginx but seems all files are empty.
UPDATE
When I check the ufw status using the command sudo ufw status i can see that Status: inactive
But not sure if I enabled the ufw via sudo ufw allow 'Nginx HTTP' it will impact the current security rule group settings.

Nginx running but not serving on port 80 | Ubuntu 20.04 | EC2 | EFS

I am running Ubuntu 20.04 on an EC2 instance in AWS. For some reason when the server is rebooted, Nginx starts but there is no service listing on port 80. When using SSH I can run sudo service Nginx reload and everything starts running. Ideas, help and suggestions are appreciated.
See output after a fresh reboot.
ubuntu#ip-xxx-xxx-xxx-xxx:~$ ps aux | grep nginx
root 504 0.0 0.1 57184 1460 ? Ss 10:21 0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 508 0.0 0.5 57944 5368 ? S 10:21 0:00 nginx: worker process
ubuntu 1039 0.0 0.2 8160 2572 pts/0 S+ 10:44 0:00 grep --color=auto nginx
ubuntu#ip-xxx-xxx-xxx-xxx:~$ sudo service nginx status
● 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 2021-02-09 10:21:59 UTC; 8min ago
Docs: man:nginx(8)
Process: 460 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 503 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 504 (nginx)
Tasks: 2 (limit: 1164)
Memory: 9.2M
CGroup: /system.slice/nginx.service
├─504 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─508 nginx: worker process
Feb 09 10:21:59 ip-xxx-xxx-xxx-xxx systemd[1]: Starting A high performance web server and a reverse proxy server...
Feb 09 10:21:59 ip-xxx-xxx-xxx-xxx systemd[1]: Started A high performance web server and a reverse proxy server.
ubuntu#ip-xxx-xxx-xxx-xxx:~$ sudo netstat -tanpl|grep nginx
ubuntu#ip-xxx-xxx-xxx-xxx:~$ sudo netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 397/systemd-resolve
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 628/sshd: /usr/sbin
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/init
tcp6 0 0 :::22 :::* LISTEN 628/sshd: /usr/sbin
tcp6 0 0 :::111 :::* LISTEN 1/init
udp 0 0 xxx.xxx.xxx.xxx 0.0.0.0:* 397/systemd-resolve
udp 0 0 xxx.xxx.xxx.xxx:68 0.0.0.0:* 394/systemd-network
udp 0 0 0.0.0.0:111 0.0.0.0:* 1/init
udp6 0 0 :::111 :::* 1/init
Update 1
It looks like this is an issue with EFS. Nginx configs were saved on an EFS volume and Nginx is loading before the EFS volume has been mounted. I will continue to investigate and read, updates to follow.
Nginx loads services based on configs set in /lib/systemd/system/ (Path may change depending on the OS version).
It would seem Nginx is starting before Ubuntu mounts the network drives on startup. Because Nginx was told to load all configs from a directory on the EFS volume, it could not load the configs when launched as they do not exist until the filesystems finish mounting.
This can be solved by telling Nginx to wait for the specific file system to be mounted before starting.
Edit the service file by running:
Sudo nano /lib/systemd/system/nginx.service
Add the following line under the [Service] section, amending your mount target as needed:
After=network.target var-www\x2defs.mount
If you do not know the mount target you can do the following to find out, replace dir with the dir you mount the drive to.
systemctl list-units --type=mount | grep /var/www
Article / Post where I found the information to solve the issue: https://unix.stackexchange.com/questions/246935/set-systemd-service-to-execute-after-fstab-mount

Cannot reach nginx on ubuntu AWS EC2 server

I'm trying to launch a Python webapp, doing the same as I usually do (not that I totally know what I'm doing). Usually I see a "Bad Gateway" when I'm running nginx on the server and I go to the IP in my browser. For whatever reason, I'm just getting timed out right now.
edit: im expecting a Bad Gateway, im getting nothing
Thanks in advance,
I haven't done anything with the default config file...
My etc/nginx/sites-available/default looks like this
server {
listen 80;
server_name ec2-52-36-167-131.us-west-2.compute.amazonaws.com;
access_log /var/log/nginx/test.log;
location / {
proxy_pass http://127.0.0.1:8000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
sudo service nginx status looks like this
● 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 Fri 2020-06-19 02:06:09 UTC; 8s ago
Docs: man:nginx(8)
Process: 9149 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
Process: 9161 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 9152 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 9162 (nginx)
Tasks: 2 (limit: 1152)
CGroup: /system.slice/nginx.service
├─9162 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─9163 nginx: worker process
Jun 19 02:06:09 ip-172-31-25-6 systemd[1]: Stopped A high performance web server and a reverse proxy server.
Jun 19 02:06:09 ip-172-31-25-6 systemd[1]: Starting A high performance web server and a reverse proxy server...
Jun 19 02:06:09 ip-172-31-25-6 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Jun 19 02:06:09 ip-172-31-25-6 systemd[1]: Started A high performance web server and a reverse proxy server.
When I curl -I ec2-52-36-167-131.us-west-2.compute.amazonaws.com on the server I get the bad gateway I'm expecting, but on my local machine I get a timeout.
Solved, it was a problem in the AWS security groups.
found it here: nginx website on Ubuntu 18 not loading on public IP

Permission Denied when attempting to start Daphne systemctl process

I'm deploying a website using Django and Django-Channels, with Channel's daphne ASGI server substituting for the typical Gunicorn WSGI setup. Using this Gunicorn WSGI tutorial as a jumping off guide, I attempted to write a systemctl service for my daphne server, when I hit the below error:
CRITICAL Listen failure: [Errno 13] Permission denied: '27646' -> b'/run/daphne.sock.lock'
I was unfortunately unable to find any answers to why permissions would be denied to the .sock file, (in context to Daphne) so I was hoping I could get some hints on where to begin debugging this problem. Below are my daphne.socket and my daphne.service files.
daphne.service
[Unit]
Description=daphne daemon
Requires=daphne.socket
After=network.target
[Service]
User=brianl
Group=www-data
WorkingDirectory=/home/brianl/autoXMD
ExecStart=/home/brianl/autoXMD/env/bin/daphne -u /run/daphne.sock -b 0.0.0.0 -p 8000 autoXMD.asgi:application
[Install]
WantedBy=multi-user.target
daphne.socket
[Unit]
Description=daphne socket
[Socket]
ListenStream=/run/daphne.sock
[Install]
WantedBy=sockets.target
Based off the linked DigitalOcean tutorial, I start my service with sudo systemctl start daphne.socket.
My guess is that there's some kind of discrepancy between setting up systemctl services for Gunicorn and Daphne that I missed, but I don't know for sure.
(If it helps, I'm planning on using Nginx as the main server, but I haven't reached that point yet)
EDIT:
It would help if I also attached the full output systemd gives:
● daphne.service - daphne daemon
Loaded: loaded (/etc/systemd/system/daphne.service; enabled; vendor preset: enabled)
Active: failed (Result: start-limit-hit) since Thu 2019-09-05 22:00:43 UTC; 1min 51s ago
Process: 22041 ExecStart=/home/brianl/autoXMD/env/bin/daphne -u /run/daphne.sock -b 0.0.0.0 -p 8000 autoXMD.asgi:application (code=exited, status=0/SUCCESS)
Main PID: 22041 (code=exited, status=0/SUCCESS)
Sep 05 22:00:43 autoxmd daphne[22041]: warnings.warn('%s. joblib will operate in serial mode' % (e,))
Sep 05 22:00:43 autoxmd daphne[22041]: 2019-09-05 22:00:43,013 INFO Starting server at tcp:port=8000:interface=0.0.0.0, unix:/run/daphne.sock
Sep 05 22:00:43 autoxmd daphne[22041]: 2019-09-05 22:00:43,017 INFO HTTP/2 support not enabled (install the http2 and tls Twisted extras)
Sep 05 22:00:43 autoxmd daphne[22041]: 2019-09-05 22:00:43,020 INFO Configuring endpoint tcp:port=8000:interface=0.0.0.0
Sep 05 22:00:43 autoxmd daphne[22041]: 2019-09-05 22:00:43,022 INFO Listening on TCP address 0.0.0.0:8000
Sep 05 22:00:43 autoxmd daphne[22041]: 2019-09-05 22:00:43,022 INFO Configuring endpoint unix:/run/daphne.sock
Sep 05 22:00:43 autoxmd daphne[22041]: 2019-09-05 22:00:43,022 CRITICAL Listen failure: [Errno 13] Permission denied: '22041' -> b'/run/daphne.sock.lock'
Sep 05 22:00:43 autoxmd systemd[1]: daphne.service: Start request repeated too quickly.
Sep 05 22:00:43 autoxmd systemd[1]: daphne.service: Failed with result 'start-limit-hit'.
Sep 05 22:00:43 autoxmd systemd[1]: Failed to start daphne daemon.
I think this occurred for the permission issue. By default /run directory is owned by root. So the daphne socket failed to create the daphne.sock.lock file in /run directory.
The solution is that create a folder in /run directory and give permission to your user and group.
For example:
sudo mkdir /run/daphne
sudo chown brianl:www-data /run/daphne
Now change the Unix sock path in the service & socket file.
daphne.service
[Unit]
Description=daphne daemon
Requires=daphne.socket
After=network.target
[Service]
User=brianl
Group=www-data
WorkingDirectory=/home/brianl/autoXMD
ExecStart=/home/brianl/autoXMD/env/bin/daphne -u /run/daphne/daphne.sock -b 0.0.0.0 -p 8000 autoXMD.asgi:application
[Install]
WantedBy=multi-user.target
daphne.socket
[Unit]
Description=daphne socket
[Socket]
ListenStream=/run/daphne/daphne.sock
[Install]
WantedBy=sockets.target
Hopefully, this works for you. For further you can go through a similar issue MySQL Daemon Lock issue

How can I run Gunicorn with NGINX (I want to move from Django development to test my production)?

This is for learning purpose. I have done the web application with Django + Celery/RabbitMQ. I tried to follow this tutorial. I got everything set until "That’s all for gunicorn.". In sense, my Gunicors runs like what it is describe in the tutorial.
Now I am confused with the NGINX settings.
I have these configurations in /etc/nginx/nginx.conf in its http block.
upstream awesome_app {
server unix:/home/notalentgeek/Downloads/awesome_app/run/gunicorn.sock fail_timeout=10s;
}
server {
listen 8080;
client_max_body_size 4G;
access_log /home/notalentgeek/Downloads/awesome_app/logs/nginx-access.log;
error_log /home/notalentgeek/Downloads/awesome_app/logs/nginx-error.log warn;
location /static/ {
autoindex on;
alias /home/notalentgeek/Downloads/awesome_app/static/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://awesome_app;
break;
}
}
}
Everything else there stay untouched.
And then what should I do after this? The tutorial does not way anything. How can I see my web application? Moreover how can I set NGINX for Docker ready?
EDIT: With Gunicorn is running or not, starting NGINX with sudo service nginx start gives this error.
-- Subject: Unit nginx.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has begun starting up.
sep 27 17:23:48 notalentgeek-ThinkPad-X220 nginx[28260]: nginx: [emerg] open() "/home/notalentgeek/Downloads/awesome_app/logs/nginx-access.log" failed (2: N
sep 27 17:23:48 notalentgeek-ThinkPad-X220 nginx[28260]: nginx: configuration file /etc/nginx/nginx.conf test failed
sep 27 17:23:48 notalentgeek-ThinkPad-X220 systemd[1]: nginx.service: Control process exited, code=exited status=1
sep 27 17:23:48 notalentgeek-ThinkPad-X220 systemd[1]: Failed to start A high performance web server and a reverse proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has failed.
--
-- The result is failed.
sep 27 17:23:48 notalentgeek-ThinkPad-X220 systemd[1]: nginx.service: Unit entered failed state.
sep 27 17:23:48 notalentgeek-ThinkPad-X220 systemd[1]: nginx.service: Failed with result 'exit-code'.
sep 27 17:23:48 notalentgeek-ThinkPad-X220 sudo[28225]: pam_unix(sudo:session): session closed for user root
The error and access log need to be created before hand. After this my website runs fine.