How to run uWSGI via upstart in Ubuntu 14.04 (Trusty Tahr) - django

I follow Setting up Django and your web server with uWSGI and nginx, and I have ran
sudo uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data
mentioned in the Emperor mode section successfully. Then, I try to run uWSGI like a daemon. Following Running uWSGI via Upstart, I try this routine:
sudo mkdir /etc/init/uwsgi.conf
sudo vim /etc/init/uwsgi.conf
This is the uwsgi.conf file:
description "uWSGI Emperor"
start on runlevel [2345]
stop on runlevel [06]
respawn
exec uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data
I don't know what else I have missed or there are some errors in the configuration file. Anyway, it doesn't work.

Edit /etc/rc.local and add:
/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data --master

Related

uWSGI - chdir(): No such file or directory [core/uwsgi.c line 2629]

I am unable to start a dockerized Django application using uWSGI.
Below is the configuration being used.
Dockerfile
FROM python:3.6
ENV PYTHONUNBUFFERED 1
RUN apt-get update
RUN apt-get install -y swig libssl-dev dpkg-dev netcat unixodbc-dev python-dev libldap2-dev libsasl2-dev
RUN mkdir /code
WORKDIR /code
COPY app/ /code/
RUN pip install -U pip
ENV PIP_TRUSTED_HOST="pypi.python.org pypi.org files.pythonhosted.org"
RUN apt-get install ca-certificates
RUN pip install -Ur requirements.txt
RUN chmod -R 777 /code
CMD ["/code/run.sh"]
run.sh
#!/bin/bash
./manage.py migrate
uwsgi --ini uwsgi.ini
app/uwsgi.ini
[uwsgi]
http-socket = :8000
chdir = /opt/code/
module = app.wsgi:application
master = 1
processes = 2
threads = 2
Error when starting the container
Operations to perform:
Apply all migrations: admin, api, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.17 (64bit) on [Wed May 30 05:35:23 2018] ***
compiled with version: 6.3.0 20170516 on 30 May 2018 05:34:47
os: Linux-4.9.12-moby #1 SMP Tue Feb 28 12:11:36 UTC 2017
nodename: 0a9f49a479c7
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 6
current working directory: /code
detected binary path: /usr/local/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
chdir() to /opt/code/
chdir(): No such file or directory [core/uwsgi.c line 2629]
Can someone help me figure out what seems to be the problem with this configuration?
The following is the project structure -
project
|--app
|--app
|--requirements.txt
|--run.sh
|--uwsgi.ini
|--manage.py
|--Dockerfile

Gunicorn Nginx host two websites - Gunicorn creates a sock file only for one

I have a Ubuntu server on which I'm trying to host two Django applications using Gunicorn and Nginx.
When I have only one website hosted all works fine. Gunicorn creates a .sock file and nginx redirects to it.
The problem I have is that I have a gunicorn config file for the second site and that doesn't create a .sock file therefore I get a bad gateway error.
Here are my files:
Gunicorn.conf
description "David Bien com application"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid ubuntu
setgid www-data
chdir /home/ubuntu/davidbien
exec venv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/davidbien/davidbien.sock davidbiencom.wsgi:application
The above file works fine. The second one:
Gunicorn1.conf
description "Projects"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid ubuntu
setgid www-data
chdir /home/user/dbprojects
exec virtual/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/dbprojects/dbproject.sock dbproject.wsgi:application
This one doesn't create the .sock file. I tried restarting gunicorn and even tried running the second file only by running sudo service gunicorn1 start but I get:
start: Job failed to start
In the logs there's nothing mentioning the second file running.
What am I doing wrong?
Ok, I just noticed a typo in my config file
chdir /home/user/dbprojects
Should be ubuntu no user. This can be closed.

django nginx gunicorn,, start: Job failed to start

every one I am trying to deploying django project using nginx gunicorn,,but when I run
sudo service gunicorn start,
it said,
start: Job failed to start,
here it the working path,,
the gunicorn.conf file
/etc/init/gunicorn.conf
description "Gunicorn application server handling myproject"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid user
setgid www-data
chdir /home/parallels/books/mysite
exec myprojectenv/bin/gunicorn --workers 3 --bind unix:/home/parallels/books/mysite/mysite.sock mysite.wsgi:application
when I run
gunicorn --bind 0.0.0.0:8000 mysite.wsgi:application
I can see mysite running
but when I try
sudo service gunicorn start
fail,,,
I just following the instruction here
any one who can reply thank you very much
I know this is too late, but in any case:
In your gunicorn.conf I see setuid user but there is user parallels in your prompt (it depends of your bash settings, but probably it is name of curent user).
There is chdir /home/parallels/books/mysite and then exec myprojectenv/bin/gunicorn and I guess your gunicorn location is not
/home/parallels/books/mysite/myprojectenv/bin/gunicorn. So you need to use appropriate path to run gunicorn.
Check gunicorn log file to find out problems. It should be here /var/log/upstart/gunicorn.log
You need to update setuid user to your user name and also the path.
Check the exec command and its parameters. If you have any problem in that it will fail
In my case I missed bin after venv (virtual env) path.
ie exec myprojectenv/bin/gunicorn --workers 3 --bind ...

convert following systemd into upstart for ubuntu 14.04 -- uwsgi emperor mode did not work as expected

I have the following script that was given to help me turn on uwsgi at emperor mode.
https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-uwsgi-and-nginx-on-centos-7
I have tweaked it slightly for the below:
[Unit]
Description=uWSGI Emperor service
[Service]
ExecStartPre=/bin/bash -c 'mkdir -p /run/uwsgi; chown vagrant:www-data /run/uwsgi'
ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all
[Install]
WantedBy=multi-user.target
I needed to rewrite this for ubuntu 14.04 hence I wrote the below into /etc/init/uwsgi.conf
description "uWSGI Emperor service"
## equivalent to wantedby=multi-user.target
start on runlevel [2345]
# create a directory needed by the daemon
pre-start exec /bin/bash -c 'mkdir -p /run/uwsgi; chown vagrant:www-data /run/uwsgi'
exec /usr/local/bin/uwsgi --emperor /etc/uwsgi/sites
respawn
kill signal SIGQUIT
expect stop
What was expected:
/run/uwsgi will be created with the ownership as vagrant:www-data
it will have 2 .sock files called djangoonpy2.sock and djangoonpy3.sock
What I did see:
/run/uwsgi will be created with the ownership as vagrant:www-data
it only has 1 .sock file called djangoonpy2.sock
I am runnning ubuntu14.04 using vagrant as a virtual machine.
Please advise.

How to start gunicorn via Django manage.py after server reboot

I am starting gunicon by calling python manage.py run_gunicorn (inside a virtualenv).
How can I achieve to restart gunicorn after my Ubuntu 12.04 server rebooted?
You can use supervisor to launch your app on startup and restart on crashes.
Install supervisor and create a config file /etc/supervisor/conf.d/your_app.conf with content like this:
[program:your_app]
directory=/path/to/app/working/dir
command=/path/to/virtualenv_dir/bin/python /path/to/manage_py/manage.py run_gunicorn
user=your_app_user
autostart=true
autorestart=true
Since I'm on Ubuntu and like to work with tools already included in the distro I used Upstart to start gunicorn after booting the machine.
I put the following code into /etc/init/django_sf_flavour.conf :
description "Run Custom Script"
start on runlevel [2345]
stop on runlevel [06]
respawn
respawn limit 10 5
exec /home/USER/bin/start_gunicorn.sh
Which executes this file (/home/USER/bin/start_gunicorn.sh) after booting:
#!/bin/bash
set -e
cd MY_PROJ_ROOT
source venv/bin/activate
test -d $LOGDIR || mkdir -p $LOGDIR
exec python MY_PROJ_PATH/manage.py run_gunicorn
this is based on #j7nn7k answer but with a bit changes
1 - create a .conf file in /etc/init dir to run with upstart
cd /etc/init
nano django_sf_flavour.conf
2 - put below lines in django_sf_flavour file and save it.
description "Run Custom Script"
start on runlevel [2345]
stop on runlevel [06]
respawn
respawn limit 10 5
exec /home/start_gunicorn.sh
3 - create start_gunicorn.sh file in home dir with following lines
cd /home
nano start_gunicorn.sh
4 - put these code in it and save it.
#!/bin/bash
set -e
cd mysite
source myenv/bin/activate
test -d $LOGDIR || mkdir -p $LOGDIR
exec gunicorn —bind 0.0.0.0:80 mysite.wsgi:application
5 - set runable permission to start_gunicorn.sh
cd /home
chmod 775 start_gunicorn.sh
** extra **
check syntax django_sf_flavour.conf with this command
init-checkconf /etc/init/django_sf_flavour.conf
and answer should be like this:
File /etc/init/django_sf_flavour.conf: syntax ok
you can see problems in upstart log file if needed:
cat /var/log/upstart/django_sf_flavour.log
test your django_sf_flavour.conf file like this without reboot
service django_sf_flavour start
test your bash file "start_gunicorn.sh" like this
cd /home
bash start_gunicorn.sh
check state of django_sf_flavour
initctl list | grep django_sf_flavour