I am trying to deploy a simple Django project, but fail all the time. I am following the instruction here.
When I use python manage.py runserver 0.0.0.0:8000 I can see that my site is running:
In the virtualenv I've installed uWSGI:
pip install uwsgi
and wrote the test.py:
# test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#return ["Hello World"] # python2
When running uwsgi --http :8000 --wsgi-file test.py I can see
I then installed en started Nginx:
sudo apt-get install nginx
sudo /etc/init.d/nginx start
I can see
and then wrote mysite_nginx.conf:
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name 10.211.55.21; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
#location /media {
# alias /path/to/your/mysite/media; # your Django project's media files - amend as required
#}
#location /static {
# alias /path/to/your/mysite/static; # your Django project's static files - amend as required
#}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/parallels/books/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
because it is just a simple site I commented out the media and static paths, and uwsgi_params:
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
the directory:
and run:
sudo ln -s ~/home/parallels/books/mysite/mysite_nginx.conf
/etc/nginx/sites-enabled/
I can see mysite_nginx.conf within /etc/nginx/sites-enabled/:
and then run:
sudo /etc/init.d/nginx restart
uwsgi --socket :8001 --wsgi-file test.py
I see nothing in my webbrowser:
I do not know why, whenever I try deploying a Django site the socket part fails.
Finally I made it by using XML to configure uWSGI and deploying Django project successfully. However, this time I do not use a virtualenv.
I created the virtual host configuration /etc/nginx/sites-enabled/mysite:
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
server_name 10.211.55.21;
access_log /var/log/nginx/mysite-access.log ;
error_log /var/log/nginx/mysite-error.log ;
location / {
uwsgi_pass 127.0.0.1:8630;
include uwsgi_params;
}
}
Then created the /home/parallels/books/mysite/django.xml file, note that the name of the file can be anything but should end with *.xml:
<uwsgi>
<socket>127.0.0.1:8630</socket>
<chdir>/home/parallels/books/mysite/mysite</chdir>
<pythonpath>..</pythonpath>
<module>wsgi</module>
</uwsgi>
the <chdir>/home/parallels/books/mysite/mysite</chdir> just point to <module>wsgi</module> because there is a wsgi.py in /home/parallels/books/mysite/mysite/wsgi.py.
I then run the following commands:
$ sudo /etc/init.d/nginx restart
$ uwsgi -x django.xml
now the website is reachable:
But I fail to configure and deploying Django using uwsgi.ini.
Related
I have my application deployed in django web framework running with wsgi + nginx on the production env. Recently, i found some weird behavior from wsgi where it starts executing/repeating the function if the original request does not complete within 4-5 minutes. I increased the read_timeout and write_timeout under nginx config file, however, this issue is still there!! any expert on nginx uwsgi deployment? snippet from nginx.conf file:-
location /netadc/ {
uwsgi_pass django;
include /apps/netadc/netadc/uwsgi_params;# the uwsgi_params file you installed
uwsgi_read_timeout 1800;
uwsgi_send_timeout 1800;
proxy_read_timeout 1800;
}
Basically this feature/script does lot of pre-validations, creates a snapshot of the n/w devices, pushes configs to around 30-40 devices and does post validations. so it takes 2-3 mins minimum to execute. sometimes when it takes more than 5 mins., it restarts the wsgi process, but not sure why... since i already increased the timeout to 30 mins as shown above.
uwsgi_params :-
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
There is no error in nginx error file or access log file that I can locate.
nginx.conf file:-
upstream django {
server unix:///apps/netadc/netadc/uwsgi/tmp/netadc.sock; # for a file socket
#server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}
server {
#listen 443;
listen 443 ssl;
server_name x.x.x.x;
uwsgi_ignore_client_abort on;
#client timeouts
#client_body_timeout 600;
#client_header_timeout 600;
#keepalive_timeout 240;
#send_timeout 600;
#client buffer sizes
#client_body_buffer_size 1m;
#client_max_body_size 32m;
client_body_temp_path /apps/netadc/opt/nginx/root/var/log/nginx/client_body_temp;
ssl_certificate cert_path;
ssl_certificate_key key_path;
location /netadc/static {
alias /apps/netadc/static;
}
location /netadc/ {
uwsgi_pass django;
include /apps/netadc/netadc/uwsgi_params;# the uwsgi_params file you installed
uwsgi_read_timeout 7200;
uwsgi_send_timeout 7200;
proxy_read_timeout 3600;
#return 301 https://$host$request_uri;
#proxy_set_header X-Forwarded-Proto $scheme;
}
server {
listen 80;
server_name x.x.x.x;
uwsgi_ignore_client_abort on;
location /netadc/ {
return 301 https://$host$request_uri;
}
}
The uwsgi configuration file is the netadc_uwsgi.ini file i presume :-
# netadc_uwsgi.ini file
[uwsgi]
#uid = nginx
#gid = nginx
# Django-related settings
env = HTTPS=on
# the base directory (full path)
chdir = /apps/netadc
# Django's wsgi file
module = netadc.wsgi
# the virtualenv (full path)
home = home_path
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
buffer-size = 65536
buffer-size = 32768
# the socket (use the full path to be safe
socket = /apps/netadc/netadc/uwsgi/tmp/netadc.sock
# read post data by default
post-buffering = 1
# ... with appropriate permissions - may be needed
#chmod-socket = 666
# daemonize
daemonize = true
# logging
logger = file:/apps/netadc/netadc/uwsgi/tmp/netadc_uwsgi.log
# clear environment on exit
vacuum = true
Command-line arguments:-
/apps/netadc/.venvs/netadc/bin/uwsgi --ini /apps/netadc/netadc/uwsgi/netadc_uwsgi.ini --enable-threads
Also another weird thing is find is some of the permissions changes after a week and some of my apps do not load.
[root#xxxxxxx ~]# ls -l /opt/rh/nginx16/root/var/lib/nginx/tmp/
total 20
drwxrwxr-x 2 my_username nginx 4096 Aug 20 10:27 client_body
drwxrwxr-x 2 my_username nginx 4096 Jan 21 2015 fastcgi
drwxrwxr-x 2 my_username nginx 4096 Jan 21 2015 proxy
drwxrwxr-x 2 my_username nginx 4096 Jan 21 2015 scgi
drwxrwxr-x 12 my_username nginx 4096 Sep 7 12:31 uwsgi
Until I change the above permissions to 777, the page takes forever to load. And then after a week, it randomly changes the permisions to the above 775. It's very bizarre. No infrastructure team is doing any changes, so I am sure something in nginx+uwsgi is messing this up.
Setting up a django site, I can run it outside the virtualenv with uwsgi --ini kb_uwsgi.ini --http :8000 but when I try to set it up live with nginx brings an error in the logs:
2017/01/30 08:00:26 [crit] 24629#24629: *1 connect() to unix:/tmp/kb.sock failed (2: No such file or directory) while connecting to upstream, client: 197.232.12.165, server: kenyabuzz.nation.news, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/tmp/kb.sock:", host: "kenyabuzz.nation.news"
Here's the nginx file
# kb.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:/tmp/kb.sock; # for a file socket
#server 0.0.0.0:8000; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 0.0.0.0:80;
# the domain name it will serve for
server_name kenyabuzz.nation.news; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/ubuntu/webapps/kenyabuzz/kb/media; # your Django project's media files - amend as required
}
location /static {
alias /home/ubuntu/webapps/kenyabuzz/kb/static; # your Django project's static files - amend as required
}
location /favicon.ico {
alias /home/ubuntu/webapps/kenyabuzz/kb/static/kb/favicon.ico; # favicon
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/ubuntu/webapps/kenyabuzz/uwsgi_params; # the uwsgi_params file you installed
}
}
uwsgi ini
# kb_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /home/ubuntu/webapps/kenyabuzz
# Django's wsgi file
module = kb.wsgi
# the virtualenv (full path)
home = /home/ubuntu/webapps/djangoenv
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /tmp/kb.sock
# ... with appropriate permissions - may be needed
chmod-socket = 664
# clear environment on exit
#vacuum = true
and uwsgi_params
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
The file doesn't exist kb.sock and I've considered tried creating it with proper permissions but that leads to an access error that itself maybe diagnosis a problem I created.
I think you should use unix:// rather than unix:
And, read this post, use /var/run instead of /tmp
I have been following the http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
and went up to the
http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html#running-the-django-application-with-uwsgi-and-nginx
and started the uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664 it is started but when i load my webpage on browser getting 502 Bad Gateway
Django version 1.10.2
error is
error] 25444#25444: *1 connect() failed (111: Connection refused)
while connecting to upstream, client: 127.0.0.1, server: mytest.com,
request: "GET / HTTP/1.1", upstream: "uwsgi://127.0.0.1:8001", host:
"127.0.0.1:8000"
What could be the issue ?
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name mytest.com; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 175M; # adjust to taste
# Django media
location /media {
alias /home/karesh/tutorial/uwsgi-tutorial/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /home/karesh/tutorial/uwsgi-tutorial/mysite/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass unix:/home/karesh/tutorial/uwsgi-tutorial/mysite/mysite.sock;
include /home/karesh/tutorial/uwsgi-tutorial/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
uwsgi_params file inside project directory
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
A few things, that could help you:
1) Add user, that runs uWSGI, to nginx group
2) On CentOS with enabled SE the best way to put socket is a directory under /run
3) Set socket permissions to <your_user>:nginx(in other words - set permission for socket for your user and for nginx group.
These steps normally helps me to figure out with permissions error.
I am trying to set up my little Django project (Django 1.8) with nginx and uwsgi, but it just fails when running:
uwsgi --socket :8001 --wsgi-file test.py
which follows documentation.
I do not know why? I just step by step but when I run above command and check my serverip:8000 it shows nothing. However when I run:
uwsgi --http :8000 --wsgi-file test.py
it works and shows Hello World web site. Running command:
uwsgi --http :8000 --module myproject.wsgi
it works and shows myproject web site.
But when it comes to
uwsgi --socket :8001 --wsgi-file test.py
it just fails..
here is my nginx site config:
upstream django {
#server unix:///home/ubuntu/path/myproject/myproject.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name 123.123.123.123; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/ubuntu/path/myproject/media; # your Django project's media files - amend as required
}
location /static {
alias /home/ubuntu/path/myproject/myapp/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/ubuntu/path/myproject/uwsgi_params; # the uwsgi_params file you installed
}
}
the uwsgi_params
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
Log from (not working) uWSGI command:
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 7862
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address :8001 fd 3
Python version: 2.7.6 (default, Jun 22 2015, 18:01:27) [GCC 4.8.2]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1ad5ec0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72768 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1ad5ec0 pid: 13959 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 13959, cores: 1)
In the following i am trying to configure from http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html
The following is my conf file below placed in my project directory and have linked the same in the sites-enabled of the nginx directory.
Now when i access my http://127.0.0.1:8000/app1/files?path=/ i end up with 502 Bad Gateway and the nginx error log says
2015/09/21 14:07:41 [error] 8023#0: *7 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET / HTTP/1.1", upstream: "uwsgi://127.0.0.1:8001", host: "127.0.0.1:8000"
but i am able to access this link http://127.0.0.1:8000/media/a.jpg what am i doing wrong here and how can access my django application
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name 127.0.0.1; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/rajeev/django-test/mysite/media; # your Django project's media files - amend as required
}
location /static {
#alias /path/to/your/mysite/static; # your Django project's static files - amend as required
alias /home/rajeev/django-test/mysite/static/; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/rajeev/django-test/mysite/conf/uwsgi_params; # the uwsgi_params file you installed
}
}
Edit1: uwsgi_params file
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
You've missed one point in this tutorial. Configuration of uWSGI for nginx is different than configuration for checking in browser if uWSGI is working not only by used port, but also by protocol.
When deploying uWSGI for nginx, --http should be replaced by --socket. By that change, nginx will understand properly output from your uWSGI server, but your browser won't.
Configuration for browser (to check if uWSGI is working) can work for nginx also, but you must use proxy_pass instead of uwsgi_pass in nginx, and using it is not recommended, uwsgi protocol is faster and better for communication between web server and uWSGI server.