I have 3 python django application on same server. And I want to run each service using different port.
ex)
80 for end user
8001 for service provider
8002 for service operator
But I have no idea how can I do this.
Now, one uwsgi service is running using systemctl.
This is my uwsgi.service.
# uwsgi.service
[Unit]
Description=uWSGI
After=syslog.target
[Service]
ExecStartPre=/bin/bash -c 'mkdir -p /var/run/uwsgi; chown root:ubuntu
/var/run/uwsgi; chmod g+w /var/run/uwsgi;'
ExecStart=/bin/bash -c 'source /var/www/html/remosys/bin/activate; uwsgi --ini /var/www/html/remosys/uwsgi.ini'
#Restart=always
Restart=on-failure
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
And uwsgi.ini is the following.
[uwsgi]
uid = ubuntu
gid = ubuntu
# Django-related settings
# the base directory (full path)
chdir = /var/www/html/remosys/remoshin
# Django's wsgi file
module = remoshin.wsgi
# the virtualenv (full path)
home = /var/www/html/remosys
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 2
threads = 1
# the socket (use the full path to be safe
socket = /var/run/uwsgi/master.sock
pidfile = /var/run/uwsgi/master.pid
# ... with appropriate permissions - may be needed
chmod-socket = 666
# clear environment on exit
vacuum = true
thunder-lock = true
max-requests = 6000
max-requests-delta = 300
# log
logto = /var/log/uwsgi/uwsgi.log
deamonize = /var/log/uwsgi/uwsgi-#(exec://date +%Y-%m-%d).log
log-reopen = true
And my nginx setting is the following.
# the upstream component nginx needs to connect to
upstream django {
# for a file socket
server unix:///var/run/uwsgi/master.sock;
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
# substitute your machine's IP address or FQDN
server_name localhost
charset utf-8;
location /clinic {
# your Django project's static files - amend as required
alias /home/ubuntu/public_html/clinic;
}
# max upload size
# Django media
location /static {
# your Django project's static files - amend as required
alias /home/ubuntu/remosys/remoshin/apiv1/static;
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
# the uwsgi_params file you installed
include /var/www/html/remosys/uwsgi_params;
}
}
I want to know how can I make settings in order to launch some uwsgi service and how to setting nginx config file.
Would you give me an advice?
Thanks in advance.
uwsgi_p1.ini for my_project1
[uwsgi]
chdir = /root/my_workplace/my_project1
module = my_project1.wsgi
virtualenv = /root/my_workplace/virtual/my_project1
processes = 2
socket = 127.0.0.1:10001 # **pay attention to this port**
vacuum = true
master = true
nginx_p1.conf for my_project1
server {
listen 8888; # use different server_name or listenport
charset utf-8;
client_max_body_size 75M;
server_name project1.mydomain.com; # use different server_name or listenport
location / {
uwsgi_pass 127.0.0.1:10001; # pay attention to here
include uwsgi_params;
}
}
==============================
uwsgi_p2.ini for my_project2
[uwsgi]
chdir = /root/my_workplace/my_project2
module = my_project2.wsgi
virtualenv = /root/my_workplace/virtual/project2
processes = 2
socket = 127.0.0.1:10002
vacuum = true
master = true
nginx_p2.conf for my_project2
server {
listen 8889;
charset utf-8;
client_max_body_size 75M;
server_name project2.mydomain.com;
location / {
uwsgi_pass 127.0.0.1:10002;
include uwsgi_params;
}
}
and so on...
Related
I tried to follow the nginx document here https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html. But stuck at Configure nginx for your site. I restarted nginx and it said "nginx: [emerg] open() "/home/hanys/oligoweb/uwsgi_params" failed (13: Permission denied) in /etc/nginx/sites-enabled/oligoweb.conf:19".
My site.ini:
[uwsgi]
chdir = /home/hanys/oligoweb/
module = oligoweb.wsgi
home = /home/hanys/.virtualenv/oligo-env
master = true
processes = 10
socket = /home/hanys/oligoweb/oligoweb.sock
chmod-socket = 666
vacuum = true
daemonize = /home/hanys/uwsgi-emperor.log
uid = www-data
gid = www-data
my site.conf in /etc/nginx/sites-available/
upstream django {
server unix:///home/hanys/oligoweb/oligoweb.sock;
}
server {
listen 80;
server_name IP address here;
charset utf-8;
# max upload size
client_max_body_size 75M;
# Django media and static files
location /static {
alias /home/hanys/oligoweb/static;
}
# Send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/hanys/oligoweb/uwsgi_params;
}
}
uwsgi_params has 664 permission. I tried chmod 777 uwsgi_params but it didnt work.
Any help will be appreciated!
In general, uwsgi_params is already shipped with your Nginx, so all you need is include uwsgi_params (so it refers to /etc/nginx/uwsgi_params or similar).
If that is not the case, you will likely also need to give Nginx enough permissions to read the directory structure that file is in, not just the file itself.
I'm having issues running 2 vassals in emperor mode (for a main app + websocket) behind an nginx server. Everything seems to be running well, but all the websocket requests return error 502 bad gateway. The websocket app is running django-websocket-redis. Any ideas where I went wrong?
Running from upstart
exec /usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals/ --logto /var/log/uwsgi.log
With /etc/uwsgi/vassals/dashdb_websocket.ini:
# dashdb_websocket.ini file
[uwsgi]
umask = 002
home = /opt/dashdb/venv/dashdb-3.5
plugin = python35
chdir = /opt/dashdb
master = true
no-orphans = true
die-on-term = true
memory-report = true
env = DJANGO_SETTINGS_MODULE=dashdb.settings.opener
socket = /var/run/uwsgi/dashdb_ws.sock
pythonpath = /opt/dashdb
module = dashdb.wsgi_websocket
threads = 1
processes = 1
http-websockets = true
gevent = 1000
# Log file location
daemonize = /var/log/uwsgi/dashdb_websocket.log
With /etc/uwsgi/vassals/dashdb.ini:
# dashdb.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /opt/%n
# Django's wsgi file
module = dashdb.wsgi
# the virtualenv (full path)
home = /opt/dashdb/venv/%n-3.5
plugin = python35
# settings location
env = DJANGO_SETTINGS_MODULE=dashdb.settings.opener
# Process-related settings
# master
master = true
# maximum number of worker processes
processes = 3
# the socket (use the full path to be safe)
socket = /var/run/uwsgi/%n.sock
# ... with appropriate permissions - may be needed
chmod-socket = 666
# clear environment on exit
vacuum = true
# Log file location
daemonize = /var/log/uwsgi/%n.log
and nginx config:
upstream django_dashdb {
server unix:/var/run/uwsgi/dashdb.sock;
}
server {
listen *:80;
server_name MY_DOMAIN;
server_tokens off;
root /opt/dashdb;
charset utf-8;
# Increase this if you want to upload large attachments
# Or if you want to accept large git objects over http
client_max_body_size 250m;
# Individual nginx logs for this GitLab vhost
access_log /var/log/dashdb/dashdb_access.log;
error_log /var/log/dashdb/dashdb_error.log;
# Django media
location /media {
alias /opt/dashdb/media;
}
# Django static
location /static {
alias /opt/dashdb/static;
}
location / {
uwsgi_read_timeout 180;
uwsgi_pass django_dashdb;
include /opt/dashdb/uwsgi_params;
}
location /ws/ {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://unix:/var/run/uwsgi/dashdb_ws.sock;
}
error_page 502 /502.html;
}
Errors in the logs:
2017/03/20 23:27:51 [error] 3586#3586: *2759 upstream prematurely closed connection while reading response header from upstream, client:
10.31.32.22, server: MY_DOMAIN...com, request: "GET /ws/builds?subscribe-broadcast&echo HTTP/1.1", upstream: "http://unix:/var/run/uwsgi/dashdb_ws.sock:/ws/builds?subscribe-broadcast&echo", host: "MY_DOMAIN...com"
I needed to set
http-socket = /var/run/uwsgi/dashdb_ws.sock
instead of
socket = /var/run/uwsgi/dashdb_ws.sock
in the websocket uwsgi config ini
The following setup is only letting me see the default Nginx html page. How can I get to Django?
I've been following Linode's documentation on how to set this up (and numerous other tutorials), but they don't use systemd, so things are a bit different.
https://www.linode.com/docs/websites/nginx/deploy-django-applications-using-uwsgi-and-nginx-on-ubuntu-14-04
I am using Linode with Fedora24. I have installed my virutalenv at
/home/ofey/djangoenv and activated it,
Django is installed using pip at
/home/ofey/qqiProject
Into the virtualenv I've installed uwsgi.
Firstly,
/etc/systemd/system/uwsgi.service
[Unit]
Description=uWSGI Emperor service
After=syslog.target
[Service]
ExecStart=/home/ofey/djangoenv/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
This executes,
/etc/uwsgi/sites/qqiProject.ini
[uwsgi]
project = qqiProject
base = /home/ofey
chdir = %(base)/%(project)
home = %(base)/djangoenv
module = %(project).wsgi:application
master = true
processes = 2
socket = %(base)/%(project)/%(project).sock
chmod-socket = 664
vacuum = true
Also,
/etc/nginx/sites-available/qqiProject
server {
listen 80;
server_name qqiresources.com www.qqiresources.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/django/qqiProject;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/home/django/qqiProject/qqiProject.sock;
}
}
The file /etc/nginx/nginx.conf has not been changed.
The user is ofey, I've used,
$ sudo systemctl daemon-reload
$ sudo systemctl restart nginx
$ sudo systemctl start uwsgi.service
Started Django with,
$ python manage.py runserver
To Django's settings.py I turned off debugging and added a host
DEBUG = False
ALLOWED_HOSTS = ['qqiresources.com']
I have also created a symbolic link,
sudo ln -s /etc/nginx/sites-available/qqiProject /etc/nginx/sites-enabled
Any help would be greatly appreciated,
Thanks
The following is working for me, however I am not sure if this is the correct or most efficient way to do it, especially where the sockets are concerned.
systemd runs the uwsgi.service and can be started with,
$ sudo systemctl start uwsgi.service
Sometimes it is necessary to reload systemd using,
$ sudo systemctl daemon-reload
/etc/sysemd/system/uwsgi.service
[Unit]
Description=uWSGI Emperor service
After=syslog.target
[Service]
ExecStart=/home/ofey/djangoenv/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
This calls the binary inside my django virtual environment directory with,
ExecStart=/home/ofey/djangoenv/bin/uwsgi
and also takes us to /etc/uwsgi/sites where the uwsgi configuration files is called djangoForum.ini
/etc/uwsgi/sites/djangoForum.ini
[uwsgi]
project = djangoForum
base = /home/ofey
chdir = %(base)/%(project)
home = %(base)/djangoenv
module = crudProject.wsgi:application
master = true
processes = 2
socket = 127.0.0.1:3031
chmod-socket = 664
vacuum = true
Django is at /home/ofey/djangoForum and my django project is at /home/ofey/djangoForum/crudProject
/etc/nginx/nginx.conf
events {
worker_connections 1024;
}
http{
upstream django {
# connect to this socket
# server unix:///tmp/uwsgi.sock; # for a file socket
server 127.0.0.1:3031; # for a web port socket
}
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name example.com; # 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/ofey/djangoForum/fileuploader/uploaded_files; # your Django project's media files
}
location /static {
alias /home/ofey/djangoForum/noAppBoundStaticDirectory; # your Django project's static files
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
# uwsgi_pass 127.0.0.1:3031;
include /etc/nginx/uwsgi_params; # or the uwsgi_params you installed manually
}
}
}
nginx can be turned on with,
$ sudo systemctl start nginx.service
'start' can be replaced with, 'restart' or 'stop'.
These configuration files for uwsgi and nginx worked for me.
Useful links:
https://gist.github.com/evildmp/3094281
https://www.linode.com/docs/websites/nginx/deploy-django-applications-using-uwsgi-and-nginx-on-ubuntu-14-04
I have setup Django project on CentOS 6.5 with Nginx and uwsgi.
I am Getting error while accessing static content as below (/var/log/nginx/error.log)-
2015/11/02 19:05:37 [error] 29701#0: *52 open() "/home/amar/workspace/myproj/config/static/rest_framework/js/default.js" failed (13: Permission denied), client: 172.29.100.104, server: myapi.dev, request: "GET /static/rest_framework/js/default.js HTTP/1.1", host: "myapi.dev", referrer: "http://myapi.dev/api/v1/datasets/"
My /etc/nginx/conf.d/virtual.conf is as shown below -
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:///tmp/uwsgi.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
#
#API
#
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name myapi.dev; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
location /static {
autoindex on;
alias /home/amar/workspace/myproj/config/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 /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
}
}
Here is my uwsgi.ini file :
[uwsgi]
chdir = /home/amar/workspace/myproj
#home = %(base)/.virtualenvs/myproj
module = config.wsgi:application
home = /home/amar/.virtualenvs/myproj
master = true
processes = 3
socket = /tmp/uwsgi.sock
chmod-socket = 777
vacuum = true
Could someone point me in the right direction?
It took time but I've fixed the problem myself. Changed the user from amar to root and set static folder permission to 666. Hope it helps someone in future.
Probably related to SELinux. You will need to allow HTTPD scripts and modules to connect to the network.
setsebool httpd_can_network_connect on -P
I am following this tutorial but i am having a problem I'm getting a 502 Nginx error.
tutorial link
Ive check the error logs and it says
uwsgi.log
ImportError: No module named khcards.wsgi
my uwsgi file looks like this
[uwsgi]
# variables
projectdomain = khcardsonline.co.uk www.khcardsonline.co.uk
uid = www-data
god = www-data
chmod = 775
# config
plugins = python
master = true
pidfile=/tmp/project-master.pid
chdir = /var/www/example.com/src/khcards
protocol = uwsgi
env = DJANGO_SETTINGS_MODULE=khcards.settings
home = /var/www/example.com/env
pythonpath = /var/www/example.com/src/khcards
module = khcards.wsgi:application
socket = 127.0.0.1:8889
logto = /var/www/example.com/logs/uwsgi.log
#below line runs it as a daemon in background
daemonize = /var/log/uwsgi/khcards.log
my nginx.conf
server {
listen 80;
server_name domain www.domain.co.uk;
root /var/www/example.com/src/khcards;
access_log /var/www/example.com/logs/access.log;
error_log /var/www/example.com/logs/error.log;
location /static/ { # STATIC_URL
alias /var/www/example.com/src/static/static/; # STATIC_ROOT
expires 30d;
}
location /media/ { # MEDIA_URL
alias /var/www/example.com/src/media/; # MEDIA_ROOT
expires 30d;
}
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8889;
}
}
When i run
sudo service uwsgi restart
the terminal spits out
Restarting app server(s) uwsgi [ OK ]
but when i do
sudo service nginx restart
the terminal spits out
Restarting nginx: nginx.
Does this mean Nginx isn't configured correctly?
Try changing
chdir = /var/www/example.com/src/khcards
to
chdir = /var/www/example.com/src