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
Related
I have a problem with gunicorn. I followed this guide to run website hosting on my raspberry on debian. But I got this error:
gunicorn.service: Failed at step EXEC spawning /var/www/loveIt/env/bin/gunicorn: No such file or directory
I don't understand why it don't create gunicorn. I watched a lot of guides but no one helped me. Here is my gunicorn.service file:
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=anton
Group=www-data
WorkingDirectory=/var/www/loveIt
ExecStart=/var/www/loveIt/env/bin/gunicorn \
--access-logfile - \
--workers 5 \
--bind unix:/run/gunicorn.sock \
lulu.wsgi:application
[Install]
WantedBy=multi-user.target
And this is my gunicorn.socket file:
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
What I need to do to solve this problem?
I am following this How To Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu 18.04 guide.
I have created the following file .socket
sudo nano /etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
I have created the following file .service
sudo nano /etc/systemd/system/gunicorn.service
Original RECOMENDED_FORMATTING-s in the guide
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=sammyRECOMENDED_FORMATTING
Group=www-data
WorkingDirectory=/home/sammyRECOMENDED_FORMATTING/myprojectdirRECOMENDED_FORMATTING
ExecStart=/home/sammyRECOMENDED_FORMATTING/myprojectdirRECOMENDED_FORMATTING/myprojectenvRECOMENDED_FORMATTING/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
myprojectRECOMENDED_FORMATTING.wsgi:application
[Install]
WantedBy=multi-user.target
How I have formatted my own version I had my virtual environment outside of the project folder on the server
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=SERVER_USER
Group=www-data
WorkingDirectory=/home/SERVER_USER/MAIN_PROJECT_FOLDER
ExecStart=/home/SERVER_USER/ven/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/home/SERVER_USER/MAIN_PROJECT_FOLDER/MAINAPPLICATION_FOLDER.sock \
MAINAPPLICATION_FOLDER.wsgi:application
[Install]
WantedBy=multi-user.target
I have also tried leaving these as originally recommended
--bind unix:/run/gunicorn.sock \
Than I have tried to execute the following code
sudo systemctl start gunicorn
error message 1
Failed to start gunicorn.service: Unit gunicorn.service not found.
and I have also tried with 2
sudo systemctl start gunicorn.socket
Error message 2
Failed to start gunicorn.socket: Unit gunicorn.socket is not loaded properly: Invalid argument.
See system logs and 'systemctl status gunicorn.socket' for details.
To solve this, I have tried
Failed to start gunicorn.service: Unit gunicorn.service not found This points back to the exact same guide that I am doing except with an older version of linux.
this is not the same code and not answered https://askubuntu.com/questions/748836/unit-gunicorn-service-failed-to-load-no-such-file-or-directory
Run: systemctl status gunicorn.socket Result: Warning: The unit file, source configuration file or drop-ins of gunicorn.socket changed on disk
● gunicorn.socket - gunicorn daemon
Loaded: error (Reason: Invalid argument)
Active: inactive (dead)
Run: systemctl is-enabled gunicorn.socket Result: enabled
Run: systemctl is-enabled gunicorn.service Result: Failed to get unit file state for gunicorn.service: No such file or directory
It cost me about three hours ... Fxxx
Don't do this:
sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socket
Do this:
sudo systemctl enable gunicorn.socket
sudo systemctl start gunicorn.socket
and:
sudo systemctl status gunicorn.socket
it works fine now !
This fixed it:
sudo systemctl enable gunicorn.service
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 am trying to start Gunicorn from systemd service file, but I get import module error. How can I fix it?
My gunicorn service file:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=sammy
Group=www-data
WorkingDirectory=/home/sammy/myproject
ExecStart=/home/sammy/myproject/venv/bin/gunicorn --workers 3 --bind unix:/home/sammy/myproject/myproject.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
When I execute gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application, there is no error.
But when I use
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
I get this error:
ImportError: No module named 'myproject'
What is wrong?
It is probably a directory permission problem. Make sure the user sammy is in the www-data group and set the home directory permissions with a chmod 710.
Got one django site running with gunicorn & nginx need to setup another site also with the same. Nginx settings is straight forward, how to rework below to add another site /home/ubuntu/webapps/uganda_buzz/
relevant settings are /etc/init/gunicorn.conf
description "Gunicorn application server handling all projects"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid user
setgid www-data
chdir /home/ubuntu/webapps/kenyabuzz
exec /home/ubuntu/webapps/djangoenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/kenyabuzz/kb.sock kb.wsgi:application
and /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=nginx
WorkingDirectory=/home/ubuntu/webapps/kenyabuzz
ExecStart=/home/ubuntu/webapps/djangoenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/kenyabuzz/kb.sock kb.wsgi:application
[Install]
WantedBy=multi-user.target
Try How To Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu 14.04 LTS
or Configuring multiple domains and subdomains. DNS & Nginx issues abound!