gunicorn and Django project - django

I just followed instructions from Digital Ocean. After:
sudo gunicorn --bind 0.0.0.0:8000 nameofmyproject.wsgi:application
bind Gunicorn my site is not available.I tried to change port from 8000 to 80 (i type mysite.com:8000 and is not working...), and then site is reachable, but without any static files like css and images. Don't know why this happens.
sudo ss -naptu state listening | grep :80
Output is:
tcp 0 128 *:8000 *:* users:(("gunicorn",pid=18461,fd=5),("gunicorn",pid=18455,fd=5))
What can I do? It's my first deploy so I would be very grateful for really simple instructions...

When you bind to 8000, the site will be available on port 8000, e.g. http://server_domain_or_IP:8000 (just like it was when you tried manage.py runserver earlier in the tutorial.
then site is reachable, but without any static files like css and images
The tutorial explains that gunicorn is not handling static files. That will be done by nginx later. You are seeing the expected behaviour, so you can carry on with the tutorial.
Next, the tutorial changes gunicorn to use a socket file instead of port 8000, then finally it configures Nginx to proxy pass to gunicorn using the socket file. At this point you will see your site, including the static files.

manage.py runserver
It's only for development, if you want to run it on production, you have to use something like nginx and gunicorn
/etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=vbaddict
Group=vbaddict
WorkingDirectory=/...../
ExecStart=/...../bin/gunicorn --bind=127.0.0.1 --timeout 120 --workers 1 --bind unix:/.../application.sock project.wsgi:application
[Install]
WantedBy=multi-user.target
/etc/nginx/conf.d/site.conf
server {
listen 80;
server_name carius.vbaddict.net;
access_log /var/log/.../.log;
location / {
include proxy_params;
proxy_pass http://unix:/home/..../application.sock;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
}
}
It's a small example
And it you need a static file, you just need to run:
python manage.py collectstatic

Related

502 Bad Gateway (13 permission denied) Nginx + Gunicorn

I am trying to deploy a simple hello-world Django site to EC2 (Ubuntu 22.04) using Gunicorn and Nginx. Nginx and Gunicorn services are both reporting as running successfully, and the .sock file has been created seemingly without issue.
Gunicorn configuration file:
[Unit]
Description=gunicorn daemon
After=nextwork.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/sites/mysite-main
ExecStart=/home/ubuntu/.local/share/virtualenvs/mysite-main-_EzVOJAm/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/ubuntu/sites/mysite-main/mysite-main.sock myapp.wsgi:application
[Install]
WantedBy=multi-user.target
Nginx configuration:
server {
listen 80;
server_name <domainname>.com <ipaddress>;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/sites/mysite-main;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/sites/mysite-main/mysite-main.sock;
}
}
Permissions on the .sock file, as well as both the "sites" and "mysite-main" directories are ubuntu:www-data. I attempted to try moving it out of the ubuntu user's home directory into a more "common" location in case it was permissions on the home directory stopping it, but was unable to even get the .sock file to generate in that case, probably due to my unfamliarity with how this works.
It looks like this is one of the most queried problems of all time, but I've tried every solution I could find to no avail. Any help would be greatly appreciated. Thanks in advance!

django nginx gunicorn application showing apache2 default page - only on ip request not domain name

Very odd behavior from my Ubuntu 18.04 LTS server
I have followed this tutorial here (twice) and it is all working properly except a couple odd things
firstly, when I use my browser to visit the IP of my VPS, the django default application page shows up throughout the tutorial however accessing it through the domain name results in a time out error
secondly, now that I have completed the tutorial and configured nginx to proxy pass to gunicorn, the apache2 ubuntu default page is now displaying instead of the django default page on a visit to the ip address and still no response from the domain name, even though there is no installation of apache2 on this server...
$ whereis apache2
apache2:
here is my gunicorn.socket file
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
here is my gunicorn.service file
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=trends
Group=www-data
WorkingDirectory=/trends_dir
ExecStart=/trends_dir/trendsvenv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
trends.wsgi:application
[Install]
WantedBy=multi-user.target
here is my /etc/nginx/sites-available config file for the site, which has been properly symlinked to /etc/nginx/sites-enabled
server {
listen 80;
server_name www.trendsontheblock.com trendsontheblock.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /trends_dir;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
like i said, no errors from the ip address and cannot get a response from the domain name, any help would be greatly appreciated, thank you
As pointed out by Mohamed in the comments section the error was due to incorrect ip-domain mapping caused by an inconsistent A record value in my domain name hosting service (godaddy)
This was because I had switched from shared hosting to VPS and the domain name's name-server values had therefore become inconsistent
The domain-name server needed to be reset to the default host values and it's A record was updated to the new VPS server's IP this has solved my issue

Nginx unable to proxy django application through gunicorn

I'm trying to deploy a Django app in a Ubuntu Server 18.04 using Nginx and Gunicorn. Every tool seems to work properly, at least, from logs and status points of view.
The point is that if I log into my server using SSH and try to use curl, gunicorn is able to see the request and handle it. However, if I write directly my IP, I get simply the typical Welcome to nginx home page and the request is completely invisible to gunicorn, so it seems nginx is unable to locate and pass the request to the gunicorn socket.
I'm using nginx 1.14.0, Django 2.2.1, Python 3.6.7, gunicorn 19.9.0 and PostgreSQL 10.8.
This is my nginx config
server {
listen 80;
server_name localhost;
location /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /home/django/myproject/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
And these are my gunicorn.sock
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
and gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=django
Group=www-data
WorkingDirectory=/home/django/myproject/myproject
ExecStart=/home/django/myproject/myproject/myproject/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
MyProject.wsgi:application
[Install]
WantedBy=multi-user.target
I've been following this guide (https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04), where most of all has worked as expected, but with the difference that my project is not a completely new one as in the tutorial, but cloned from my git repo (however, it's tested and the code works properly)
I was expecting the Django admin to be accessible from my browser already with this config, but it's not. I try to access my IP from my browser and I get Welcome to nginx but also 404 if I visit /admin. In addition, the gunicorn logs shows no requests.
In the other hand, if I log through SSH into my server and I execute curl --unix-socket /run/gunicorn.sock localhost, I can see in the gunicorn logs the request done by curl.
Any help is welcome.. I've been here for hours and I'm not able to get even 1 request from outside the server.
PD: it's also not something related to the ports in the server, since when I access the root of my IP, I receive the nginx answer. It just seems like Nginx has no config at all.
in your nginx config, you should use your proper server_name instead of localhost
server_name mydomain.com;
If not, you will fall back to the default nginx server, which returns the "welcome to nginx" message. You can change which virtual server is default by changing the order of servers, removing the nginx default, or using the default_server parameter. You can also listen to multiple server names.
listen 80 default_server;
server_name mydomain.com localhost;
If the Host header field does not match a server name, NGINX Plus routes the request to the default server for the port on which the request arrived. The default server is the first one listed in the nginx.conf file, unless you include the default_server parameter to the listen directive to explicitly designate a server as the default.
https://docs.nginx.com/nginx/admin-guide/web-server/web-server/#setting-up-virtual-servers
Remember that you have to reload the nginx config after making changes. sudo nginx -s reload for example.
Finally, I've got it working properly. You were right about the config of nginx, although my real problem was not to delete/modify default config file for nginx in sites_enabled folder. Thus, when I was setting listen 80 default_server I got the following error
[emerg] 10619#0: a duplicate default server for 0.0.0.0:80 in
/etc/nginx/sites-enabled/mysite.com:4
Anyway, I had a problem with the static files which I still not knowing why it works like that. I needed to set DEBUG = True to be able to see static files of the admin module.
I'll keep on investigating the proper way of serving static files in production for the admin panel.
Thank you so much for the help!

deploying django app with gunicorn and nginx

Okay so I have been working on this for awhile now and I know many people have had similar problems to myself but I can't seem to find the right solution.
Anyway I have followed the tutorial exactly as laid out on Digital Ocean
https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04
everything works fine when I test gunicorn with
gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
however when I create a systemd file and try to create a proxy pass with nginx I receive a 502 bad gateway error.
when I run:
sudo tail -f /var/log/nginx/error.log
I get error message:
15:18:39 [crit] 10558#10558: *1 connect() to unix:/home/bmhinformatics/Desktop/ProvCaRe/ProvCaRe.sock failed (2: No such file or directory) while connecting to upstream
my gunicorn systemd file looks like:
/etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=sammy
Group=www-data
WorkingDirectory=/home/usr/myproject
ExecStart=/home/usr/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:/home/usr/myproject/myproject.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
and my nginx proxypass file looks like:
/etc/nginx/sites-available/myproject
server {
listen 80;
server_name server_domain_or_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/usr/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/sammy/myproject/myproject.sock;
}
}
I can't seem to identify the error. Any help is appreciated. Thanks!

Communication between Gunicorn and Nginx

I have been trying to run my django production server using Gunicorn as my application server and Nginx as a reverse proxy.
Below is my nginx conf file:
server {
listen 80;
server_name myproject.com;
location /static/ {
alias /var/www/myproject/static/;
}
location /media/ {
alias /var/www/myproject/media/;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/user/myproject/myproject.sock;
}
}
Below is my gunicorn.conf file:
description "Gunicorn application server handling my project file"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid user
setgid www-data
chdir /home/user/myproject/
exec gunicorn --workers 3 --bind unix:/home/user/myproject/myproject.sock myproject.wsgi:application
Below is the code for myproject.sock file:
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn/socket
ListenStream=0.0.0.0:9000
ListenStream=[::]:8000
[Install]
WantedBy=sockets.target
When I try to run with these settings. I get the following error:
connect() to unix:/home/user/myproject/myproject.sock failed (111: Connection refused) while connecting to upstream
It would be great if anyone can help me understand what i am doing wrong. Thanks
Suppose you are run Ubuntu 14.04:
Step1:
First you can install and run django with gunicorn in virtualenv.Here, it doesn't matter with nginx, but you can get the feedback from Django and gunicorn altogether.
Step2:
Then it's time to do with nginx for a robust way.
Make sure step 1 is on green light then jump to step 2, so you can isolate any trouble in different stage.