I have a python project that I need running on a screen using uWSGI, if I run the script:
screen -d -m uwsgi --emperor "/home/rrcms/wsgi.ini" --socket :8000 --chdir /home/rrcms/ --wsgi-file /home/rrcms/wsgi.py
It will create a new screen and run the script properly, but when I try to add it to crontab it won't even create the screen, this is the script I'm using:
#reboot screen -d -m uwsgi --emperor "/home/rrcms/wsgi.ini" --socket :8000 --chdir /home/rrcms/ --wsgi-file /home/rrcms/wsgi.py
You should replace uwsgi with its absolute path:
(get absolute path from terminal: which uwsgi)
#reboot screen -d -m /home/user/venv/bin/uwsgi --emperor "/home/rrcms/wsgi.ini" --socket :8000 --chdir /home/rrcms/ --wsgi-file /home/rrcms/wsgi.py
Related
Im trying to publish my Django project with NGINX and gunicorn, but I get permission denied from gunicorn.
My last steps what I did.
mkdir /home/sama/websites
cd /home/sama/websites
mkdir test_project
cd test_project
create Virtualenvirement for this project
virtualenv --python=/usr/bin/python3.8 venv
Virtualenvirement activate
source venv/bin/activate
Gunicorn & Django install
pip3 install gunicorn
pip3 install django
create project
cd ..
django-admin startproject config test_project
allow firewall to port 8000
sudo ufw allow 8000
run first test
python manage.py runserver 0.0.0.0:8000
open my server ip on webbrowser
myIP:8000
I can see Django basic page
stop server
CTRL + C
testGunicorn
gunicorn --bind 0.0.0.0:8000 config.wsgi
testagain on webbrowser
Again I can see Djangos page
Server stop
CTRL + C
exit virtualenvirement
deactivate
config Gunicorn
create 2 files in
/etc/systemd/system/
gunicorn.socket and gunicorn.service
edit this files
sudo nano /etc/systemd/system/gunicorn_test_project.socket
/etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn daemon
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
/etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=sama
Group=www-data
WorkingDirectory=/home/sama/websites/test_project
ExecStart=/home/sama/websites/test_project/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock config.wsgi:application
[Install]
WantedBy=multi-user.target
test socket
sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socket
systemlink created, check it
file /run/gunicorn.sock
Output: /run/gunicorn.sock: socket
curl --unix-socket /run/gunicorn.sock localhost
And now I get permission denied
I already set permission for the folder test_project to user sama and group www-data, but without any effects. What is wrong?
I see no good reason to use systemd socket activation here.
Just use a service file and have Gunicorn bind to HTTP.
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=sama
Group=www-data
WorkingDirectory=/home/sama/websites/test_project
ExecStart=/home/sama/websites/test_project/venv/bin/gunicorn --access-logfile - --workers 3 --bind 0.0.0.0:8000 config.wsgi:application
[Install]
WantedBy=multi-user.target
Ok, I'm new to Python/Flask deployment and was following this tutorial.
This is my system file:
[Unit]
Description=Gunicorn instance to serve myapp
After=network.target
[Service]
User=deployer
Group=www-data
WorkingDirectory=/home/deployer/myapp
Environment="PATH=/home/deployer/myapp/myapp_env/bin"
ExecStart=/home/deployer/myapp/myapp_env/bin/gunicorn --workers 3 --bind unix:myapp.sock -m 007 appserver:gunicorn_app
[Install]
WantedBy=multi-user.target
But it doesn't work. I get Main process exited, code=exited, status=203/EXEC or /root/myapp/myapp_env/bin/python3: bad interpreter: Permission denied
If I cd into my myapp directory and issue the gunicorn command like so:
gunicorn --workers 3 --bind unix:smarrttrader_api.sock -m 007 appserver:gunicorn_app
Everything works fine. If I do a which gunicorn from my app directory I get /usr/local/bin/gunicorn and try to run ()from elsewhere in the server like so:
/usr/local/bin/gunicorn --workers 3 --bind unix:smarrttrader_api.sock -m 007 appserver:gunicorn_app
It doesn't work and I get the following error: ImportError: No module named 'appserver', so how can I get it to work?
I'm deploying a django application and it works fine when I run it manually. I'm trying to use supervisor but when I run sudo supervisorctl status botApp the log file says:
Starting botApp as ubuntu
/home/ubuntu/gunicorn_start.bash: line 28: exec: gunicorn: not found
My gunicorn_start.bash is the following one:
#!/bin/bash
NAME="botApp" # Name of the application
DJANGODIR=/home/ubuntu/chimpy # Django project directory
SOCKFILE=/home/ubuntu/django_env/run/gunicorn.sock # we will communicte using this unix socket
USER=ubuntu # the user to run as
GROUP=ubuntu # the group to run as
NUM_WORKERS=3 # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=botApp.settings # which settings file should Django use
DJANGO_WSGI_MODULE=botApp.wsgi # WSGI module name
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $DJANGODIR
source /home/ubuntu/django_env/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--bind=unix:$SOCKFILE \
--log-level=debug \
--log-file=-
And my configuration file in /etc/supervisor/conf.d/botApp.conf is:
[program:botApp]
command = /home/ubuntu/gunicorn_start.bash;
user = ubuntu;
stdout_logfile = /home/ubuntu/logs/gunicorn_supervisor.log;
redirect_stderr = true;
environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8;
Is something wrong in my gunicorn bash? Many thanks
To check virtual environment is active or not. Just write the simple script
#!bin/bash
source /var/www/html/project_env/bin/activate
Then run command
-> sudo bash file_name.sh
If you will not get any error that mean venv is activated
Note - You will not get any venv activate mark on terminal or shell.
I'm trying to start the swagger server using gunicorn on ec2 instance by using the following code:
I tried :
gunicorn -w 4 -b 0.0.0.0:8080 -p pidfile -D swagger_server:app
and this:
gunicorn -w 4 -b 0.0.0.0:8080 -p pidfile -D "python3 -m swagger_server":app
and even this :
gunicorn -w 4 -b 0.0.0.0:8080 -p pidfile -D __main__:app
How can I get it to work?
RAW python code which works : python3 -m swagger_server
What you are trying to do is equivalent to:
from swagger_server.__main__ import main
For this to work with gunicorn, try:
gunicorn "swagger_server.__main__:main" -w 4 -b 0.0.0.0:8080`
In case you have the error:
ImportError: No module named swagger_server
add the PYTHONPATH to gunicorn command:
gunicorn "swagger_server.__main__:main" -w 4 -b 0.0.0.0:8080 --pythonpath path_to_swagger_server
gunicorn -b 0.0.0.0:8080 main:app --reload
This should be the correct syntax, obviously make sure you're in the correct directory and source your virtualenv.
isn't your application looking for a configuration file with a section like [app:main]?
This one worked for me:
gunicorn "swagger_server.__main__:app" -w 4 -b 0.0.0.0:8080
I am not able to access server when starting gunicorn via a .bash file I made. It works when I do it manually with this command
$ gunicorn project.wsgi:application --bind 192.168.1.130:8000
Created a gunicorn.bash file from tutorials. I looks like this and runs without fault.
#!/bin/bash
NAME="project" # Name of the application
DJANGODIR=/home/username/projects/project # Django project directory
SOCKFILE=/home/username/.venvs/project/run/gunicorn.sock # We will communicate using this unix socket
USER=username # the user to run as
GROUP=username # the group to run as
NUM_WORKERS=1 # how many worker processes shoul Gunicorn spawn
DJANGO_SETTINGS_MODULE=project.settings.production # which settings file should Django use
DJANGO_WSGI_MODULE=project.wsgi # WSGI module name
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $DJANGODIR
source /home/username/.venvs/project/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# Create the run directory if it doesn't exsist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Start yout Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use daemon)
exec gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--bind=unix:$SOCKFILE \
--log-level=debug \
--log-file=-
I don't know how to troubleshoot this? Maybe some command to see what differs in running settings from manually starting gunicorn and from the .bash file?
$ gunicorn project.wsgi:application --bind 192.168.1.130:8000
Above you use --bind with host:port but below:
exec gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--bind=unix:$SOCKFILE \
--log-level=debug \
--log-file=-
you specify unix:file which will make your gunicorn listen on unix socket file instead of on your network interface:port, so just replace the unix:$SOCKFILE with 192.168.1.130:8000 and it should be accessible as expected
Additionally you can try and connect to the current config with a curl (curl --unix-socket /path/to/socket http:/some/resurce) or other tool of your choice to verify that it actually runs