am trying to configure uwsgi and in the process it says on a tutorial that I must run
uwsgi -s /tmp/uwsgi.sock -w myapp:app
the problem is -w is an invalid option. Can anyone help me point out why or what should I do?
Thanks
maybe you are using debian-supplied packages. They are fully modular so you need to install/load the required plugins:
http://projects.unbit.it/uwsgi/wiki/Quickstart
My uwsgi app configuration looks like that
/etc/uwsgi/apps-enabled/mysite.ini
[uwsgi]
socket=/tmp/uwsgi_mysite.sock
chmod-socket=666
abstract-socket=false
master=true
workers=2
uid=altera
gid=altera
chdir=/home/altera/www/mysite ; Current dir
pp=/home/altera/www/mysite ; Python Path (to your application)
pyhome=/home/altera/vpy/mysite ; Path to virtual environment
plugins=python3
module=main ; *.py file name application starting from
post-buffering=8192
/etc/nginx/sites-available/mysite
server {
server_name mysite;
root /home/altera/www/mysite;
location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi_mysite.sock;
}
}
Related
I am newbie to Django recently I created a Django app and I uploaded to the server. I assigned a domain name to it. each time I run the server I need to type xyz.com:8000 to see my website. Is there any way to resolve this issue? Also, I have doubt. Do I need to type python manage.py runserver 0:8000 to launch the website or it's just run automatically like PHP.
You should set up a web server!
The web server is required for any site to work. Currently the most popular are Apache and NGINX. It is the web server that responds to user requests. We need to ensure the interaction of the web server and the python application. Most popular solutions:
uwsgi
gunicorn
Consider an example with Nginx and Gunicorn:
Let's start by installing the Gunicorn module in a virtual environment:
pip install gunicorn
Configure the gunicorn service settings for our project:
sudo nano /etc/systemd/system/gunicorn.service
/etc/systemd/system/gunicorn.service:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=my_user
Group=www-data
WorkingDirectory=/home/project_dir/project
ExecStart=/home/project_dir/project/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/project_dir/project/project.sock project.wsgi
[Install]
WantedBy=multi-user.target
We enable and run the gunicorn service, check its status:
sudo systemctl enable gunicorn
sudo systemctl start gunicorn
sudo systemctl status gunicorn
If all is well, install the nginx web server:
sudo apt install nginx
Configure the project site parameters:
sudo nano /etc/nginx/sites-available/project
/etc/nginx/sites-available/project:
server {
listen 80;
server_name <server IP or domain name>;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/project_dir/project;
}
location /media/ {
root /home/project_dir/project;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/project_dir/project/project.sock;
}
}
We use Nginx as a proxy for the gunicorn Python server:
proxy_pass http://unix:/home/project_dir/project/project.sock;
Create a link in the allowed sites folder “/etc/nginx/sites-enabled”:
sudo ln -s /etc/nginx/sites-available/project/etc/nginx/sites-enabled
We restart the Nginx service and add permissions to the firewall:
sudo systemctl restart nginx
sudo ufw allow 'Nginx Full'
Done! You can check the operation of our site by typing the IP address of the server in the browser.
P.S. Sorry for my english! If you see an error in the text or code, please edit me.
I am new to Nix and trying to implement a service which passes Python Flask web services though Nginx proxy_pass. This is what I have tried so far.
with import <nixpkgs> {};
let
buildInputs = [
nginx
python35Packages.python
python35Packages.flask
python35Packages.pyyaml
];
installPhase = ''
mkdir -p $out/pynix
cp -rv src config.yml $out/pynix
cd $out/pynix && nohup python src/main.py &> log.txt
'';
in {
network.description = "Local machine";
webserver = {
deployment = {
targetEnv = "virtualbox";
virtualbox.memorySize = 1024;
};
services = {
nginx = {
enable = true;
config = '';
http {
include ${nginx}/conf/mime.types;
server_name localhost;
location / {
proxy_pass http://localhost:5000;
}
}
'';
};
};
};
}
src/main.py is a Python Flask service running on port 5000. How can I achieve this web service up and running when I do nixops deploy -d DEPLOYMENT_NAME? Please help.
I think you've confused a package and a service. The package is the static output of the build while the service manages the run time activation of the package. I think your configuration currently attempts to describe a python app that is run at build time without any service to activate it at run time. This is pretty much the opposite of what you want! Especially as with nixops your are likely running your service in a different environment to where it was built.
You should be able to get an idea of what I mean by looking at the nix expressions for the nginx package and the nginx service -
specifically the section services.systemd.nginx. From here you can see that the nginx service manages the running of the nginx package. I think you will want to write similar expressions for your python app. You could also see if there are expressions specifically for python based NixOS services that you could use as a base, but the nginx expressions should be a sufficient guide too.
I am trying to setup uwsgi and Django on Nginx but showing page not found error and error logs are empty.
I cannot identify the error because the error logs are empty.
Error log /var/log/nginx/error.log:
-rw-r--r-- 1 www-data root 0 Feb 26 12:31 error.log
uswgi is running properly because I tested this on following method:
uwsgi --http :8080 --home /home/flybegins/python/django/venv/ --chdir
/home/flybegins/python/django/sample -w sample.wsgi
virtual host
server {
listen 80;
server_name test.aaaaaaa.com;
error_log /var/log/nginx/error.log
location /static/ {
root /home/flybegins/python/django/sample/
}
location / {
include uwsgi_params;
uwsgi_pass unix:/home/flybegins/python/django/sample/sample.sock;
} }
Virtual host permission:
-rw-r--r-- 1 root root 333 Feb 27 08:54 test.aaaa.com
Thanks in advance!
You need to install python plugin for uwsgi
sudo apt-get install uwsgi-plugin-python
or for python 3
sudo apt-get install uwsgi-plugin-python3
You are running your project using port 8080 with this code:
uwsgi --http :8080 --home /home/flybegins/python/django/venv/ --chdir /home/flybegins/python/django/sample -w sample.wsgi
And you are trying to bind NGINX to a socket file that doesn't exist using this configuration:
location / {
include uwsgi_params;
uwsgi_pass unix:/home/flybegins/python/django/sample/sample.sock;
}
That why it doesn't work.
I did two mistakes One is nginx virtual host configuration and another one is socket permission error
uWSGI Configuration
[uwsgi]
project = prd
base = /home/flybegins/python/django
chdir = %(base)/%(project)
home = %(base)/venv
module = %(project).wsgi:application
master = true
processes = 5
gid = www-data
uid = www-data
socket = /var/uwsgi/%(project).sock
chmod-socket = 664
vacuum = true
To create the space for the socket to exist, you just have to pick a persistent directory (e.g. not /run or /tmp) and make www-data (the user nginx runs as) the owner of it, as such:
$ sudo mkdir /var/uwsgi
$ sudo chown www-data:www-data /var/uwsgi
My nginx virtual host configuration
server {
listen 80;
server_name testserver1.com;
access_log /home/flybegins/log/python/testserver1.com/access.log;
error_log /home/flybegins/log/python/testserver1.com/error.log error;
location /static {
alias /home/flybegins/python/django/prd/static_files/;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/var/uwsgi/prd.sock;
}
}
I just set up Gunicorn with Nginx (reverse proxy) for a Django web app. The combo seems to be firing up correctly as per gunicorn.log. Note that I'm not using supervisor.
But curiously, my environment variables (set in .profile) aren't being picked up at all! printenv shows they exist. Some things I've tried are putting the environment variables in /etc/default/nginx and restarting nginx, in etc/environment, in .profile, in nginx.conf, in gunicorn.conf, etc. It just doesn't work!
By the way, it worked perfectly before installing and configuring nginx, i.e. when I was simply running: gunicorn --bind 0.0.0.0:8080 --env DJANGO_SETTINGS_MODULE=myproject.settings myproject.wsgi:application.
Now it seems that nginx:
removes all environment variables inherited from its parent process
except the TZ variable
Source: http://nginx.org/en/docs/ngx_core_module.html#env Could this be the reason why nothing I'm trying has come close to working? But if so, these variables added to nginx.conf ought to have been picked up I suppose. Nevertheless, using echo $envvar yields the correct value on the command line, which tells me that perhaps the variables are set, but being bypassed or overlooked. Note that the USER env variable shows up as None too, whereas print TERM prints linux.
wsgi.py:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())
gunicorn.conf:
description "Gunicorn application server handling myproject"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid myuser
setgid www-data
chdir /home/myuser/directory/myproject/
exec /home/myuser/.virtualenvs/myvirtualenv/bin/gunicorn --chdir=/home/myuser/directory/ --workers 3 --bind unix:/home/myuser/directory/myproject/myproject.sock --env DJANGO_SETTINGS_MODULE=myproject.settings myproject.wsgi:application
/etc/nginx/sites-available/myproject:
server {
listen 80;
server_name myapp.cloudapp.net;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/myuser/directory/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/myuser/directory/myproject/myproject.sock;
}
}
Note: ask me for more information in case you need it.
So an air-tight (hopefully) way of setting environment variables that gunicorn would have no trouble seeing is editing gunicorn.conf as follows:
exec /home/myuser/.virtualenvs/myvirtualenv/bin/gunicorn --chdir=/home/myuser/directory/ --workers 3 --bind unix:/home/myuser/directory/myproject/myproject.sock -e var1=value1 -e var2=value2 myproject.wsgi:application
This has served me well so far. If I run into any issues, I'll update this answer.
my nginx conf:
location / {
include uwsgi_params;
uwsgi_param UWSGI_PYHOME /usr;
uwsgi_pass unix:/var/run/uwsgi-python/uwsgi/socket;
uwsgi_param UWSGI_CHDIR /var/www/my_site;
uwsgi_param UWSGI_SCRIPT my_site:app;
uwsgi_param SERVER_NAME my_site;
uwsgi_param UWSGI_SETENV DEPLOY_VERSION=Development;
}
my uwsgi para:
/usr/local/bin/uwsgi --master --processes 2 --logdate --chmod-socket=666 --uid www --gid www --limit-as 512 --harakiri 60 --max-requests 1000 --no-orphans —-reload-os-env --daemonize /var/log/uwsgi-python/uwsgi.log --pidfile /var/run/uwsgi-python/uwsgi/pid --socket /var/run/uwsgi-python/uwsgi/socket --xmlconfig /etc/uwsgi-python/apps-enabled/uwsgi.xml
uwsgi xml:
<uwsgi>
<master/>
<vhost/>
<memory-report/>
<no-site/>
</uwsgi>
In my flask app
print os.environ.get('DEPLOY_VERSION', 'NONE') #pring NONE
How I can get the env_vars?
Maybe I can not get the env_vars setting by UWSGI_SETENV in <vhost/><no-site/> mode?
btw: How you deploy multi version(Development/Beta/Release) of app in one machine without virtual env?
Instead of:
uwsgi_param UWSGI_SETENV DEPLOY_VERSION=Development;
You could set it as a per request variable in nginx: uwsgi_param DEPLOY_VERSION 'Development';
And then within Flask, access the variable via request.environ: request.environ['DEPLOY_VERSION']
(I had a similar problem and was pointed to the above solution on the uwsgi mailing list)
I had a similar issue defining environment configurations for a django Mezzanine CMS deployment.
As the DEPLOY_VERSION seems to target the underlying application and not the uWSGI service, I think the correct place to put it to be the uWSGI configuration file instead of the Nginx one.
Try changing the .xml file to:
<uwsgi>
<master/>
<vhost/>
<memory-report/>
<env>DEPLOY_VERSION=Development</env> <!-- this -->
<no-site/>
</uwsgi>