Gunicorn / Nginx / Django :( - django

I have deployed my django app on ubunto below status seems ok but website is not running, please guide
Nginx status
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Gunicorn status
● gunicorn.socket - gunicorn socket
Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor preset: enabled)
Active: active (listening) since Tue 2022-09-20 11:31:47 CEST; 1h 16min ago
Triggers: ● gunicorn.service
Listen: /run/gunicorn.sock (Stream)
Tasks: 0 (limit: 19140)
Memory: 0B
CGroup: /system.slice/gunicorn.socket
website showing only
website status

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.

Apache 2.4 Status number of total request are increasing instead of going down

Thanks in advance.
I am having trouble with number of ever increasing Total Requests/child process of Apache.
I am currently using Apache 2.4/ PHP 7.4 on EC2 server with Centos 7.
Also, uses prefork-MPM.
The server log reached the maximum of space in EBS, it couldn't process any more incoming request for 20 minutes until I found out that problem. What I did was restart apache server
service httpd restart
It worked fine for about 1 hour or so.
And then Apahce server again went downtime.
At that point,
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2022-07-04 14:08:42 KST; 2h 56min ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 5899 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Process: 28298 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Main PID: 5949 (httpd)
Status: "Total requests: 54982; Current requests/sec: 0; Current traffic: 0 B/sec"
I think it reached maximum of total requests which was 54,982 requests.
I restarted server again using
service httpd restart
However, total number of requests kept increasing every hour from about one thousand request to now almost 30,000 total requests. Currently I can't figure out what is going on my server and how to fix problem.
Later, while I researched around my server setting, I found the following line in the error.log which recommend me to increase MaxRequestWorker setting; however, I can't find any where in conf directory where I can increase MaxRequestWorker at all.
So, I like to know what is the way to reduce total requests in Apache if there is one. Another question is how to increase MaxRequestWorker in the setting.
the folling is current status of Apache
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2022-07-04 17:07:35 KST; 1h 42min ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 29339 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Process: 28298 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Main PID: 29568 (httpd)
Status: "Total requests: 38027; Current requests/sec: 16.8; Current traffic: 172KB/sec"
CGroup: /system.slice/httpd.service
├─ 1374 /usr/sbin/httpd -DFOREGROUND
├─ 3056 /usr/sbin/httpd -DFOREGROUND
├─ 3213 /usr/sbin/httpd -DFOREGROUND
├─ 3783 /usr/sbin/httpd -DFOREGROUND
Jul 04 17:07:35 ip-.compute.internal systemd[1]: Starting The Apache HTTP Server...
Jul 04 17:07:35 ip-compute.internal httpd[29568]: AH00112: Warning: DocumentRoot [/home/source/tictoccroc/test] does not exist
Jul 04 17:07:35 ip-.compute.internal systemd[1]: Started The Apache HTTP Server.

PGADMIN4 with DJANGO on google cloud 404 error

ok I can see the files. I can use the database. Apparently its running:
(myprojectenv) michael#contra:~/myproject$ sudo systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Sun 2022-01-16 23:51:55 UTC; 2 days ago
Main PID: 22981 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 613)
Memory: 0B
CGroup: /system.slice/postgresql.service
However, when I go to contra.nz/pgadmin4 Django takes over and is looking for a page. How do I access PGADMIN4 without uninstalling DJANGO :(
For some reason this answer isn't written anywhere.
For a 404 issue when trying to get to your django admin, it may be as simple as your missing the / at the end.
I.E
www.domain.com/admin will not work, it does even tell you in the error file.
you need to go to www.domain.com/admin/
:)

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

Django deploy Gunicorn and NginX

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.