How to get Uwsgi working with wagtail (django) - python

I am trying to get up to speed with wagtail. I am running it on a remote server. I have installed a virtual environment, then I switched to the virtual environment and installed wagtail as per the steps here: http://docs.wagtail.io/en/v1.9/getting_started/tutorial.html
pip install wagtail
wagtail start rocker
cd rocker
pip install -r requirements.txt
python manage.py migrate
python manage.py createsuperuser
The next step in the guide is to test the installation has worked by running:
python manage.py runserver
I can't do this as working with a remote server, and also there is already a Django app running on the server (using uwsgi).
So I am now trying to connect to this wagtail app, via uwsgi.
Using the string that starts the existing app as a template, I have modified it to bind a socket to the wagtail app: -
uwsgi --chdir=/opt/rocker/rocker --module=rocker.wsgi:application --env DJANGO_SETTINGS_MODULE=rocker.settings --master --pidfile=/tmp/rocker.pid --socket=/opt/rocker/core.sock --processes=5 --uid=www-data --gid=www-data --harakiri=20 --max-requests=5000 --vacuum --home=/opt/rocker --daemonize=/var/log/uwsgi/rocker.logroot#caspium:/etc/init.d#
However the app isn't starting... the error in the uwsgi log says the following: -
*** Operational MODE: preforking ***
Traceback (most recent call last):
File "./rocker/wsgi.py", line 18, in <module>
application = get_wsgi_application()
File "/opt/rocker/local/lib/python2.7/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
django.setup(set_prefix=False)
File "/opt/rocker/local/lib/python2.7/site-packages/django/__init__.py", line 22, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/opt/rocker/local/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
File "/opt/rocker/local/lib/python2.7/site-packages/django/conf/__init__.py", line 41, in _setup
self._wrapped = Settings(settings_module)
File "/opt/rocker/local/lib/python2.7/site-packages/django/conf/__init__.py", line 116, in __init__
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
**django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.**
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 4617)
spawned uWSGI worker 1 (pid: 4622, cores: 1)
spawned uWSGI worker 2 (pid: 4623, cores: 1)
spawned uWSGI worker 3 (pid: 4624, cores: 1)
spawned uWSGI worker 4 (pid: 4625, cores: 1)
spawned uWSGI worker 5 (pid: 4626, cores: 1)
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting
must not be empty.
I researched this and found this Django: ImproperlyConfigured: The SECRET_KEY setting must not be empty which suggests adding
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings.local")
in manage.py which I did. This did not fix anything.
I also found this django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty which says you need a secret key specified in settings.py
However in the wagtail project there is no settings.py
I created one and added a secret key, and am still getting the error.
Can someone advise how I can fix this so I can run uwsgi to connect to the wagtail, and test if it's working.
Thanks

In the Wagtail base project structure, settings.py is split into several files in the settings directory - see http://docs.wagtail.io/en/v1.9/reference/project_template.html#django-settings for details. The best place to put SECRET_KEY is settings/local.py, since you want to keep it out of version control.

The problem was in the following file:
/opt/rocker/local/lib/python2.7/site-packages/django/conf/global_settings.py
There is a Secret Key = '' specified here.
I added a secret key, and the app ran ok with Uwsgi

What worked for me after so many tries, was to delete the whole settings/ folder and create a proper settings.py file instead. If you guys from #Wagtail want to separate settings... you can do this https://simpleisbetterthancomplex.com/tips/2016/11/01/django-tip-19-protecting-sensitive-information.html

Related

ImportError: No module named django.core.wsgi for wsgi server setting

I am setting with djano and uwsgi
$uwsgi --ini uwsgi.ini
My Django root is here /var/www/html/myapp/current
It must be quite simple setting however I am not sure the yet.
I have these two files
/var/www/html/myapp/current/myapp/settings.py
/var/www/html/myapp/current/myapp/wsgi.py
[uwsgi]
chdir=/var/www/html/myapp/current #it success
module=myapp.wsgi:application #it success
env DJANGO_SETTINGS_MODULE=myapp.settings # it success
http-socket = 0.0.0.0:8008
processes = 1
threasds = 1
master = 1
max-requests = 100000
The error is below, but I can't dig the detailed logs.
spawned uWSGI worker 1 (pid: 27353, cores: 1)
--- no python application found, check your startup logs for errors ---
[pid: 27353|app: -1|req: -1/1] 172.17.1.143 () {28 vars in 334 bytes} [Thu Mar 26 17:37:01 2020] GET / => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (0 switches on core 0)
And this error occurs.
*** Operational MODE: single process ***
Traceback (most recent call last):
File "./myapp/wsgi.py", line 13, in <module>
from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 1705)
spawned uWSGI worker 1 (pid: 1706, cores: 1)
spawned uWSGI http 1 (pid: 1707)
error happens here
from django.core.wsgi import get_wsgi_application
Also I am using anaconda3
$conda activate py37 then
start this command
$uwsgi --ini uwsgi.ini
I solved the problem , the problem was uwsgi itself.
My setting file was ok.
install uwsgi by conda
conda install -c conda-forge libiconv
conda install -c conda-forge uwsgi
then start uwsgi /home/ubuntu/anaconda3/envs/py37/bin/uwsgi --ini uwsgi.ini

uwsgi + django + nginx: Python application not loading

Following a bunch of tutorials, I wrote my first django app and then decided to deploy it on a linode server. Following their tutorial, I only got so far. The best I could tell, that was based on an earlier version of Ubunto and I tried some other things, including the uwsgi quickstart tutorial.
I got far enough that the command 'python manage.py runserver' would serve up my site and wanted to move on to using uwsgi to do the same.
It seems as though there is some environment variable missing. When I try to start uwsgi from the command line with:
uwsgi --http :8000 --module dynamicefl.wsgi
I get the following:
*** Starting uWSGI 2.0.15 (64bit) on [Fri Aug 11 19:37:04 2017] ***
compiled with version: 6.3.0 20170406 on 10 August 2017 23:41:13
os: Linux-4.9.36-x86_64-linode85 #1 SMP Thu Jul 6 15:31:23 UTC 2017
nodename: roosevelt
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /home/django/worksheets/dynamic-efl
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 3941
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :8000 fd 4
spawned uWSGI http 1 (pid: 5998)
uwsgi socket 0 bound to TCP address 127.0.0.1:43637 (port auto-assigned) fd 3
Python version: 2.7.13 (default, Jan 19 2017, 14:48:08) [GCC 6.3.0 20170118]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x562bdad6fda0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72760 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
Traceback (most recent call last):
File "./dynamicefl/wsgi.py", line 12, in <module>
from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 5997, cores: 1)
A lot of the tutorials emphasize putting the path to the virtual environment in the .ini file, and I did (double checking with 'env' that I had the virtual environment right.) Here is the .ini file I'm working with
[uwsgi]
home = /home/django/Env/worksheets
chdir = /home/django/worksheets/dynamic-efl/dynamicefl
module = dynamicefl.wsgi::application
uid = www-data
gid = www-data
socket = localhost:9191
chmod-socket = 644
chown-socket = www-data
processes = 4
threads = 2
logto = /var/log/uwsgi.log
I have experimented with changing the chdir command some, as I'm not sure exactly where it should be pointing, but regarless of where it points that results in a similar line in uwsgi.log:
-- no python application found, check your startup logs for errors ---
(Fun fact: I haven't been able to find reference to uwsgi startup logs)
If I go to the site in my web browser, I get an 'Internal Server Error' message.
I don't think that anything is getting as far an nginx, but I can include the .conf file and error logs, too, if that's helpful.
I know that there are a log of similar questions, but I've looked at them and they're not helping me. (Which is not to say that they don't contain the answer.)
The django docs helped me out here. It turns out that my reference to the module was apparently wrong. I had a colon too many in the reference to the module. The corrected .ini reads:
module = dynamicefl.wsgi:application
What helped was resolving to battle it out until I could serve the site purely from uwsgi, so that I could stop restarting services.

uWSGI + Django + Python: no module named uwsgi

I am trying to setup uwsgi for my Django project.
It runs fine with
./manage.py runserver 0.0.0.0:9010
But when I try
uwsgi --http :9010 --chdir /home/user/appname --module wsgi --wsgi-file /home/user/appname/appname/wsgi.py
I get
ImportError: No module named wsgi
What I am doing wrong ?
Here is the full log:
uWSGI http bound on :9010 fd 4
spawned uWSGI http 1 (pid: 1900)
uwsgi socket 0 bound to TCP address 127.0.0.1:42684 (port auto-assigned) fd 3
Python version: 2.7.9 (default, Mar 1 2015, 13:01:26) [GCC 4.9.2]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1c17310
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72760 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
ImportError: No module named wsgi
unable to load app 0 (mountpoint='') (callable not found or import error)
Traceback (most recent call last):
File "/home/robert/surmaroute/surmaroute/wsgi.py", line 13, in <module>
from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 1899, cores: 1)
The issue is that your app does not have a module named uwsgi. You change the directory to /home/user/appname but it looks like the actual module would be appname.uwsgi since the uwsgi.py file lives within /home/user/appname/appname/uwsgi.py.
Typically though, you don't need to specify both --wsgi-file and --module so I would either do
uwsgi --http :9010 --chdir /home/user/appname --wsgi-file /home/user/appname/appname/wsgi.py
or
uwsgi --http :9010 --chdir /home/user/appname --module appname.uwsgi
I personally prefer the second.

Problems when deploying a django application using nginx and uwsgi?

I'm having problems when I'm trying to deploy my Django application using nginx and uwsgi . I will start by putting here the config files:
tutorial_nginx.conf
upstream django {
server unix:///home/mrgrj/site/blog/tutorial.sock;
}
server {
listen 8000;
server_name xx.xx.xxx.xx;
location /media {
alias /home/mrgrj/site/blog/polls/templates;
}
location /static/ {
root /home/mrgrj/site/blog/polls/templates/polls;
}
location / {
uwsgi_pass django;
include /home/mrgrj/site/blog/uwsgi_params;
}
}
tutorial_uwsgi.ini
[uwsgi]
chdir = /home/mrgrj/site/blog
module = wusgi.py
home = /home/mrgrj/site
master = true
processes = 10
socket = /home/mrgrj/site/blog/tutorial.sock
vacuum = true
I followed this tutorial from Official Django Documentation and unfortunately I didn't succeed to finish it as I encountered some issues.
The nginx server is set up correctly as it displays me the right page when I access the ip from the web browser. The django application is working when I am running python manage.py runserver 0.0.0.0:8000. The issues that I have, regarding that tutorial:
uwsgi --http :8000 --module mysite.wsgi . I don't have such a file, but I have something simillar which is called wsgy.py:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "blog.settings")
application = get_wsgi_application()
When I am trying tu run that command using wsgi.py it says Internal server error on my browser ( I am accessing it via ip:8000 ). Traceback:
* Starting uWSGI 2.0.10 (64bit) on [Sun Apr 12 16:42:15 2015] compiled with version: 4.8.2 on 10 April 2015 08:25:41 os:
Linux-3.13.0-43-generic #72-Ubuntu SMP Mon Dec 8 19:35:06 UTC 2014
nodename: alex-test machine: x86_64 clock source: unix detected number
of CPU cores: 1 current working directory: /home/mrgrj/site/blog/blog
detected binary path: /usr/local/bin/uwsgi !!! no internal routing
support, rebuild with pcre support !!!
WARNING: you are running uWSGI without its master process manager your processes number limit is 3750 your memory page size is 4096 bytes detected max file descriptor number: 1024 lock engine: pthread
robust mutexes thunder lock: disabled (you can enable it with
--thunder-lock) uWSGI http bound on :8000 fd 4 spawned uWSGI http 1 (pid: 2169) uwsgi socket 0 bound to TCP address 127.0.0.1:41225 (port
auto-assigned) fd 3 Python version: 2.7.6 (default, Mar 22 2014,
23:03:41) [GCC 4.8.2]
Python threads support is disabled. You can enable it with --enable-threads Python main interpreter initialized at 0x122d0c0 your server socket listen backlog is limited to 100 connections your
mercy for graceful operations on workers is 60 seconds mapped 72760
bytes (71 KB) for 1 cores
Operational MODE: single process Traceback (most recent call last): File "./wsgi.py", line 16, in
application = get_wsgi_application() File "/usr/local/lib/python2.7/dist-packages/django/core/wsgi.py", line 14,
in get_wsgi_application
django.setup() File "/usr/local/lib/python2.7/dist-packages/django/init.py", line 17,
in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) File "/usr/local/lib/python2.7/dist-packages/django/conf/init.py",
line 48, in getattr
self._setup(name) File "/usr/local/lib/python2.7/dist-packages/django/conf/init.py", line
44, in _setup
self._wrapped = Settings(settings_module) File "/usr/local/lib/python2.7/dist-packages/django/conf/init.py", line
92, in init
mod = importlib.import_module(self.SETTINGS_MODULE) File "/usr/lib/python2.7/importlib/init.py", line 37, in import_module
import(name) ImportError: No module named blog.settings unable to load app 0 (mountpoint='') (callable not found or import error)
no app loaded. going in full dynamic mode
uWSGI is running in multiple interpreter mode * spawned uWSGI worker 1 (and the only) (pid: 2168, cores: 1)
Another problem that I have is when I am trying to run uwsgi --socket :8001 --wsgi-file test.py. It gives me a **no data received message** `(Unable to load the webpage because the server sent no data.)'
Can anybody help me out, please ?

How to configure a uWSGI site to use different Python interpreters than what uWSGI was built with? (uWSGI + virtualenv + emperor)

Our app is written for Python 2.7. It has a virtualenv that is configured to use Python 2.7. The system's installed uWSGI was built with Python 2.6. It seems that this is a problem. We could uninstall/reinstall uWSGI to use Python 2.7, but what happens when we eventually have a Python 3 site? Isn't this what virtualenv is for? We could install a separate instance of uWSGI in the virtualenv, but it's not clear how that works -- need to activate it at boot time and how might that work with an emperor? Should we use the uWSGI keyword "unprivileged-binary-patch"?
How do we configure uWSGI so that we can run multiple apps using different Python interpreters?
We are seeing this problem when launching from uWSGI.
Python version: 2.6.9 (unknown, Oct 29 2013, 19:58:13) [GCC 4.6.3 20120306 (Red
Hat 4.6.3-2)]
Set PythonHome to /var/local/virtualenv/myapp
Python main interpreter initialized at 0xf86790
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 655200 bytes (639 KB) for 8 cores
*** Operational MODE: preforking ***
initialized 137 metrics
Traceback (most recent call last):
File "/var/local/myapp/cgi/myapp.wsgi", line 6, in <module>
from myapp import create_app
File "./myapp/__init__.py", line 3, in <module>
from app import create_app
File "./myapp/app.py", line 1, in <module>
import os
ImportError: No module named os
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
gracefully (RE)spawned uWSGI master process (pid: 2500)
spawned uWSGI worker 1 (pid: 2521, cores: 1)
spawned uWSGI worker 2 (pid: 2522, cores: 1)
spawned uWSGI worker 3 (pid: 2523, cores: 1)
spawned uWSGI worker 4 (pid: 2524, cores: 1)
spawned uWSGI worker 5 (pid: 2525, cores: 1)
spawned uWSGI worker 6 (pid: 2526, cores: 1)
spawned uWSGI worker 7 (pid: 2527, cores: 1)
spawned uWSGI worker 8 (pid: 2528, cores: 1)
metrics collector thread started
*** Stats server enabled on 127.0.0.1:9191 fd: 27 ***
spawned uWSGI http 1 (pid: 2530)
The app is written with Flask and is using the create_app pattern to launch it. The app works fine when using virtualenv and the developer web server built into Flask on port 5000.
The ini file looks like this:
[uwsgi]
chdir = /var/local/%n
callable = create_app
virtualenv=/var/local/virtualenv/%n
wsgi-file = /var/local/%n/cgi/%n.wsgi
master = true
daemonize = /var/log/uwsgi/%n.log
pidfile = /var/run/uwsgi/%n.pid
http = :9090
We want to run the site in emperor mode. Here is the emperor ini file:
[uwsgi]
emperor = /etc/uwsgi/vassals
pidfile = /var/run/uwsgi/%n.pid
daemonize = /var/log/uwsgi/%n.log
The best approach would be having a uWSGI plugin for each python version (like the uWSGI packages in your distro do)
(doc on the old site)
http://projects.unbit.it/uwsgi/wiki/MultiPython
You can install an uwsgi inside your other python/virtualenv version. Then you can use the uwsgi emperor in stackable mode:
[uwsgi]
chdir = /var/local/%n
callable = create_app
virtualenv=/var/local/virtualenv/%n
wsgi-file = /var/local/%n/cgi/%n.wsgi
master = true
daemonize = /var/log/uwsgi/%n.log
pidfile = /var/run/uwsgi/%n.pid
http = :9090
unprivileged-binary-patch = /var/local/virtualenv/bin/uwsgi

Categories