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!
Related
I have a linux server that run multiple different website on multiple different domain which are working fine.
But I created a sub domain for one of the domain which is demo.mywebsitedomain.com I did the configuration like it was a different domain which mean I created a specific nginx socket / service / nginx config file for this subdomain
But when I go the my link demo.mywebsitedomain.com it run the website application of an other django application folder, it not the one who I have pointed in the socket and service file. I am verry confuse.
It should run the django application whos in the testdemo folder but instead it run a different folder application. Those are the file I created for the subdomain
sudo vim /etc/systemd/system/testdemo.socket
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/testdemo.sock
Environment="PATH=/usr/bin:/home/tiber/testdemo/env/bin"
[Install]
WantedBy=sockets.target
sudo vim /etc/systemd/system/testdemo.service
[Unit]
Description=gunicorn daemon
Requires=testdemo.socket
After=network.target
[Service]
User=tiber
Group=www-data
WorkingDirectory=/home/tiber/testdemo
ExecStart=/home/tiber/testdemo/env/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/testdemo.sock \
mysite.wsgi:application
[Install]
WantedBy=multi-user.target
sudo vim /etc/nginx/sites-available/testdemo
server {
listen 80;
server_name demo.mywebsitedomain.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /var/www/testdemo;
}
location /media/ {
root /var/www/testdemo;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/testdemo.sock;
}
}
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!
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
I have DjangoServer1 and DjangoServer2 running a virtualenv, where gunicorn is installed. nginx is installed under user in Ubuntu.
I make DjangoServer1 running under nginx, gunicorn.
Server IP: 12.12.12.12
Web site domain for DjangoServer1 is mydomain1.com
Web site domain for DjangoServer2 is mydomain2.com
This is nginx server config for DjangoServer1.
/etc/nginx/sites-available/DjangoServer1
server {
listen 0.0.0.0:80;
server_name 127.0.0.1;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/user/develop/DjangoServer1;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/user/develop/DjangoServer1/DjangoServer1.sock;
}
}
I start the DjangoServer1:
1) Under virtualenv, run gunicorn command to start DjangoServer1
gunicorn --daemon --workers 3 --bind unix:/home/user/develop/DjangoServer1/DjangoServer1.sock DjangoServer1.wsgi
2) Then, run:
sudo service nginx restart
3) In router, I portforward port 80, 8000, to server 12.12.12.12
4) In browser, enter: 12.12.12.12. DjangoServer1 works. Enter: mydomain1.com, DjangoServer1 works.
Now, I want to run DjangoServer2 under same server: 12.12.12.12
Question: How to configure DjangoServer1 and DjangoServer2 to different port?
How to run gunicorn command to use different port? Following command uses port 8000? Why?
gunicorn --daemon --workers 3 --bind unix:/home/user/develop/DjangoServer1/DjangoServer1.sock DjangoServer1.wsgi
How to configure nginx file?
Change your Gunicorn command to run the servers on the specified port.
gunicorn --daemon --workers 3 --bind :8080 DjangoServer1.wsgi
Now change your NGINX conf file to forward it to the Application Server.
upstream django-server-1 {
server 0.0.0.0:8080;
}
server {
listen 0.0.0.0:80;
server_name 127.0.0.1;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/user/develop/DjangoServer1;
}
location / {
include proxy_params;
proxy_pass http://django-server-1;
proxy_next_upstream off;
}
}
Restart your NGINX service.
This will forward all the requests coming to 80 port to your application server DjangoServer1.
If you explicitly want to forward requests coming to 8080 to your application server, change the server block in the NGINX configuration or have a new server block with your rules.
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.