I'm trying to run my django project on subdomain, my nginx configuration is,
server {
listen 80;
server_name subdomain.example.me www.subdomain.example.me;
location /static/ {
root /home/gagan/webmash/blog;
}
location /media/ {
root /home/gagan/webmash/blog;
}
location / {
include proxy_params;
proxy_pass http://my_ip:9000;
}
}
While my supervisor configuration is,
[program:webmash]
command=/home/gagan/webmash/env/bin/gunicorn --workers 3 --bind unix:/home/gagan/webmash /blog/blog.sock blog.wsgi --env DJANGO_SETTINGS_MODULE=blog.settings.production
directory=/home/gagan/webmash/blog
autostart=true
autorestart=true
stderr_logfile=/var/log/saporawebapp.err.log
stdout_logfile=/var/log/saporawebapp.out.log
when i run supervisor using,
sudo supervisorctl restart webmash
It doesn't show any error. On restarting nginx, it too doesn't show any error.But my project is not runing either at https://subdomain.example.com or my_ip:9000.
What can be the possible causes for such behaviour
I recommend Nginx with systemd over supervisor.
Here's a tutorial to leave Django running with Nginx + Gunicorn + Systemd + AnaConda.
Maybe my github here will help you:
Githhub/Nginx
Related
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!
I deployed my Django project on Digital Ocean using this guide (https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04), I completed the steps shown in the guide and all of them seem to run without any error on the terminal still when I type the IP address of the website the Nginx welcome page shows up.
The website was working fine up to this command (gunicorn --bind 0.0.0.0:8000 NGOsamyak.wsgi) after configuring Ngnix the problem occurred.
/etc/nginx/sites-available/NGOsamyak
server {
listen 80;
server_name 165.22.216.110;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/samyakbvs/NGOsamyak-project;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/samyakbvs/NGOsamyak-project/NGOsamyak.sock;
}
}
Picture of the terminal log :
Picture of the welcome page :
It looks like you have missed a space in the command sudo ln
sudo ln -s
/etc/nginx/sites-available/NGOsamyak/etc/nginx/sites-enabled
That should be
sudo ln -s /etc/nginx/sites-available/NGOsamyak /etc/nginx/sites-enabled
Run sudo nginx -t to verify everything is correct.
Then finally run sudo systemctl restart nginx
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 am trying to run project on Django with Gunicorn and Nginx. On DigitalOcean OneClick install image my project works fine with no virtualenv and with global Django installation. But when I created virtual environment for different Django version I couldn't get it to work. So kindly someone please provide me some help with the multi site hosting on Ubuntu using virtual environment. Follwing is my Gunicorn settings for virtual environment:
description "Gunicorn daemon for Django project"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]
# If the process quits unexpectadly trigger a respawn
respawn
setuid django
setgid django
chdir /home/django
exec gunicorn \
--name=myproject2\
--pythonpath=myproject2\
--bind=127.0.0.1:9500 \
--config /etc/gunicorn.d/gunicorn.py \
myproject2.wsgi:application
My Nginx settings for the second project are:
upstream ashyanaa_server {
server 127.0.0.1:9500 fail_timeout=0;
}
server {
listen 80;
listen [::]:80;
root /home/django/myproject2;
index index.html index.htm;
client_max_body_size 4G;
server_name www.myproject2.com;
keepalive_timeout 5;
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2|woff|ttf)$ {
expires 365d;
}
# Your Django project's media files - amend as required
location /media {
alias /home/django/myproject2/media/;
}
# your Django project's static files - amend as required
location static/static-only {
alias /home/django/myproject2/static-only/;
}
# Django static images
location /static/myproject2/images {
alias /home/django/myproject2/static-only/images/;
}
# Proxy the static assests for the Django Admin panel
location /static/admin {
alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://myproject2_server;
}
Only thing different in my first project settings from the second are that I am using virtual environment for the second project and obviously I had to use different port for new project.
'Bad Gateway' indicates that Nginx is having trouble connecting the the Gunicorn process.
Double check that the service that starts Gunicorn (the one defined by the upstart script you posted) is actually running
What happens when you do curl http://127.0.0.1:9500/? Do you get a response from Gunicorn?
This is due to lack of understanding about Nginx. I added www.mydomain.com in Nginx but I have habit of typing domain name without www in browser. I simply added "mydomain.com" and "www.mydomain.com". So now both working without error. For others to follow if you have all the settings correct and still getting 502 that means the address you are looking for is not listed in Nginx. It could be one of the reason. Thanks for help though guys.
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.