nginx fails while the website somehow still works - django

Description
I have built my Django3, gunicorn, nginx, Ubuntu 18.04, Digital Ocean project based on this guide. I only had 1 problem that it does not shows the CSS and all other static files like images. Before during the whole guide nginx have given the correct answers as the guide says and also currently the html site still online and running
To solve this I was in the process of using this another guide to make my static files displayed on my site.
I have done all the steps what the creator recommended but at the
What I have tried
After each step of the following [1.2.3...] commands I have executed the following commands to refresh:
python3 manage.py collectstatic
sudo nginx -t && sudo systemctl restart nginx
sudo systemctl restart gunicorn
1.RUN: sudo ln -s /etc/nginx/sites-available/ch-project /etc/nginx/sites-enabled
1.RESULT: ln: failed to create symbolic link '/etc/nginx/sites-enabled/ch-project': File exists
2.RUN: /etc/nginx/sites-enabled/my-project
2.RESULT: -bash: /etc/nginx/sites-enabled/my-project: Permission denied
3.RUN: systemctl status nginx.service
3.RESULT:
● 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 Thu 2020-03-26 13:27:08 UTC; 13s ago
Docs: man:nginx(8)
Process: 11111 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
Process: 11111 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)
Main PID: 11111 (code=exited, status=0/SUCCESS)
4.RUN: sudo nginx -t
4.RESULT:
nginx: [emerg] open() "/etc/nginx/sites-enabled/myproject" failed (2: No such file or directory) in /etc/nginx/nginx.conf:62
nginx: configuration file /etc/nginx/nginx.conf test failed
Nginex should be Ok otherwise because the html on the website loads and works perfectly. This stack overflow post says I should maybe do something with the security of the nginx.conf but in that case they talk about a worldpres site so I don't know how to implement that here.
I have tried this stack overflow post's answer previously, bellow the answer it has a subpost to further configure RUN: sudo nginx -c /etc/nginx/sites-enabled/default -t
6.RESULT:
nginx: [emerg] "server" directive is not allowed here in /etc/nginx/sites-enabled/default:21
nginx: configuration file /etc/nginx/sites-enabled/default test failed

The static files still don't load but nginx has been fixed with the following commands.
deleting accidentally created myproject file from /etc/nginx/sites-enabled/myproject what file name is in the official guide but the_actual_myproject has different name RUN:
cd /etc/nginx/sites-enabled
sudo rm myproject
RUN:
namei -l /run/gunicorn.sock
sudo systemctl restart gunicorn
sudo systemctl daemon-reload
sudo systemctl restart gunicorn.socket gunicorn.service
sudo nginx -t && sudo systemctl restart nginx
RESULT:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
RUN: systemctl status nginx.service
RESULT:
● 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 Thu 2020-03-26 16:32:09 UTC; 7min ago
Docs: man:nginx(8)
Process: 11111 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, sta
Process: 11111 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=ex
Main PID: 11111 (nginx)
Tasks: 2 (limit: 1152)
CGroup: /system.slice/nginx.service

Related

restarting gunicorn and nginx doesn't reflect changes after pull request

Recently I hosted a Django website on digitalocean. I then edited the content of website locally and pushed them to GitHub then pulled it to server and restarted nginx and gunicorn:
sudo systemctl restart nginx
sudo systemctl restart gunicorn
Then changes didn't reflect back to live website, Just to check if I setup gnuicorn and nginx properly, I ran sudo systemctl status gunicorn.socket I get Failed to dump process list, ignoring: No such file or directory
(venv) leptitoxadmin#ubuntu1:~/pyapps/leptx$ sudo systemctl status gunicorn.socket
[sudo] password for leptxadmin:
Failed to dump process list, ignoring: No such file or directory
● gunicorn.socket - gunicorn socket
Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor preset: enabled)
Active: active (running) since Wed 2020-02-12 05:57:58 UTC; 2h 42min ago
Listen: /run/gunicorn.sock (Stream)
CGroup: /system.slice/gunicorn.socket
Feb 12 05:57:58 ubuntu1 systemd[1]: Listening on gunicorn socket.
is this the reason, why the changes don't reflect to live website? How do I solve it? thanks
I think you should also consider reloading daemon as well
sudo systemctl daemon-reload
and then
sudo systemctl restart gunicorn
i have same problem and now i solved!!
./manage.py collectstatic
sudo systemctl restart gunicorn
that code can solve problem!

Can't start gunicorn.service on VirtualBox with CentOS 8, Nginx and Django-Rest-Framework

Trying to deploy django application using gunicorn and nginx on CentOS. Following DigitalOcean tutorial:
https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-centos-7
But I have CentOS 8.
I can run my application locally from virtualenv using:
python manage.py runserver
gunicorn --bind 0.0.0.0:8000 app.wsgi:application
but then I try to run gunicorn.service I have status - failed.
Inside of systemctl status gunicorn.service I have
started gunicorn deamon
gunicorn.service: main process exited, code=exited, status=203/EXEC
gunicorn.service: failed with result 'exit-code'
Without this file I can't bind app.sock file as it not being created.
My gunicorn.service looks like this
app - is fictional application name :)
admin - is a real user of this system
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=admin
Group=nginx
WorkingDirectory=/home/admin/app/portal/app
ExecStart=/home/admin/app/env/bin/gunicorn --workers 3 --bind unix:/home/admin/app/portal/app/app.sock app.wsgi:application
[Install]
WantedBy=multi-user.target
There is a tree of my project:
app
- env
- portal
-- client
-- app
--- documents
--- fixtures
--- images
--- app
---- __init__.py
---- settings.py
---- urls.py
---- wsgi.py
--- app_project
---- ...
--- manage.py
--- requirements.txt
What can be done to make it work and that can I check to find more clues why it doesn't work?
Any input is welcome.
Thanks
in journalctl -xe
I've noticed
SELinux is preventing file/path from execute access on the file
so I changed
SELINUX=permissive
in /etc/selinux/config
rebooted and now I have different error in systemctl status gunicorn.service:
gunicorn: error: unrecognized arguments: app.wsgi:application
oh, that was because I added additional flag to ExecStart. Removed it and it works now.
So issue was with SELinux.
Will keep it here in case if it would be useful to anyone

gunicorn [/etc/systemd/system/gunicorn.socket:6] Unknown section 'Service'. Ignoring

I'm setting up django on digitalocean, they have a setup rule that works this time though gunicorn won't work.
First step is setting up the gunicorn service using this command sudo nano /etc/systemd/system/gunicorn.service
and the content
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=sammy
Group=www-data
WorkingDirectory=/home/sammy/webapp/prestige
ExecStart=/home/sammy/webapp/envs/prestige/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
prestige.wsgi:application
[Install]
WantedBy=multi-user.target
and the socket file sudo nano /etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
note that this directory
/home/sammy/webapp/prestige
is the main folder directory of django with manage.py so basically if I cd into it I can run python manage.py runserver and it works.
this is the folder for the virtualenv
/home/sammy/webapp/envs/prestige
now when I run the start gunicorn command I get this
(prestige) sammy#prestige:~/webapp$ sudo systemctl start gunicorn.socket
Failed to start gunicorn.socket: Unit gunicorn.socket is not loaded properly: Invalid argument.
See system logs and 'systemctl status gunicorn.socket' for details.
if you check the status
(prestige) sammy#prestige:~/webapp$ sudo systemctl status gunicorn.socket
● gunicorn.socket - gunicorn daemon
Loaded: error (Reason: Invalid argument)
Active: inactive (dead)
May 17 07:25:18 prestige systemd[1]: [/etc/systemd/system/gunicorn.socket:6] Unknown section 'Service'. Ignoring.
May 17 07:25:18 prestige systemd[1]: gunicorn.socket: Unit lacks Listen setting. Refusing.
May 17 07:33:32 prestige systemd[1]: [/etc/systemd/system/gunicorn.socket:6] Unknown section 'Service'. Ignoring.
May 17 07:33:32 prestige systemd[1]: gunicorn.socket: Unit lacks Listen setting. Refusing.
May 17 07:34:09 prestige systemd[1]: [/etc/systemd/system/gunicorn.socket:6] Unknown section 'Service'. Ignoring.
May 17 07:34:09 prestige systemd[1]: gunicorn.socket: Unit lacks Listen setting. Refusing.
May 17 07:53:41 prestige systemd[1]: [/etc/systemd/system/gunicorn.socket:6] Unknown section 'Service'. Ignoring.
May 17 07:53:41 prestige systemd[1]: gunicorn.socket: Unit lacks Listen setting. Refusing.
May 17 07:56:59 prestige systemd[1]: [/etc/systemd/system/gunicorn.socket:6] Unknown section 'Service'. Ignoring.
May 17 07:56:59 prestige systemd[1]: gunicorn.socket: Unit lacks Listen setting. Refusing.
No idea what's going on.
Found out the hard way that if you put the wrong code for the service or socket for gunicorn it bugs out. After just deleting the droplet and starting again it went on smoothly.
ENABLE gunicorn.socket after start it
sudo systemctl ENABLE gunicorn.socket
and make sure you link socket with gunicorn.service

uWSGI is not creating Unix socket at specified path

I've worked with Nginx and uWSGI numerous times, but I've never seen socket not being created even when specified explicitly.
My uWSGI config (/etc/uwsgi/sites/my_site.ini) is quite simple:
[uwsgi]
project = my_site
base = /root
chdir = %(base)/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application
master = true
processes = 5
socket = /run/uwsgi/%(project).sock
chmod-socket = 664
vacuum = true
As visible, uWSGI must create socket with 644 permissions (although some suggest it should be 666).
Nginx server configuration (/etc/nginx/nginx.conf) is quite simple as well:
server {
listen 80;
server_name mysite.com www.mysite.com;
location /static/ {
root /root/my_site;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/my_site.sock;
}
}
uwgsi.service (startup script) (/etc/systemd/system/uwsgi.service on CentOS):
[Unit]
Description=uWSGI Emperor service
After=syslog.target
[Service]
ExecStart=/usr/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
After making these configurations, I executed following commands:
sudo systemctl daemon-reload
sudo systemctl restart uwsgi
sudo systemctl restart nginx
sudo systemctl status uwsgi
sudo systemctl status nginx
Everything was fine according to commands above, but Nginx returned 502 bad gateway) error when visiting website.
Thus I checked /var/log/nginx/error.log/:
2018/08/25 19:33:10 [crit] 14920#0: *3 connect() to unix:/run/uwsgi/my_site.sock failed (2: No such file or directory) while connecting to upstream, client: xx.xxx.xxx.xx, server: my_site.com, request: "GET / HTTP/1.1", upstream: $
As you can see above, uWSGI doesn't create socket at /run/uwsgi/ directory for some reason...
I'm sure that socket is not being created at /run/uwsgi/, since ls /run/uwsgi/ returns nothing.
Permissions:
sudo usermod -a -G root nginx;chmod 710 /root
Main problem:
uWSGI is not creating a unix socket at the specified path in the configuration above, what could be the problem?
Is there any possible cause?

systemctl strange error: Invalid arguments

Here's my service file:
[Unit]
Description=Daphne Interface
[Service]
ExecStartPre=cd /home/git/hsfzmun/server
ExecStart=/bin/bash/ -c "cd /home/git/hsfzmun/server && /home/git/virtualenvs/hsfzmun/bin/daphne -b 0.0.0.0 -p 8001 -v2 config.asgi:channel_layer"
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all
[Install]
WantedBy=multi-user.target
When I execute sudo systemctl start daphnei I get:
Failed to start daphnei.service: Unit daphnei.service is not loaded properly: Invalid argument.
See system logs and 'systemctl status daphnei.service' for details.
And the result of systemctl status daphnei.service:
* daphnei.service - Daphne Interface
Loaded: error (Reason: Invalid argument)
Active: inactive (dead) (Result: exit-code) since Mon 2017-02-13 19:55:10 CST; 13min ago
Main PID: 16667 (code=exited, status=1/FAILURE)
What's wrong? I am using Ubuntu Server 16.04.
Generally, to debug exact cause of "Invalid argument", you can use:
sudo systemd-analyze verify daphnei.service
or in case of user's local service:
sudo systemd-analyze --user verify daphnei.service
Maybe you've figured it out by now, but there's an extra / after /bin/bash in your ExecStart line.
I may have a slightly newer version of systemd -- when I tried it, the output of systemctl status included this message:
Executable path specifies a directory, ignoring: /bin/bash/ -c "cd /home/git/hsfzmun/server && /home/git/virtualenvs/hsfzmun/bin/daphne -b 0.0.0.0 -p 8001 -v2 config.asgi:channel_layer"
Also, you might consider using a WorkingDirectory line in the service, instead of cd &&