uwsgi + python2.7 unable to import anything - django

I'm trying to use uwsgi + python2.7 + django + nginx on debian 6.
I installed uwsgi with the command: pip2.7 install uwsgi so it's running with python 2.7.
I'm running uwsgi in emperor mode with the command:
uwsgi --emperor /etc/uwsgi/vassals/ -d /var/log/uwsgi.log --pidfile /var/run/uwsgi.pid
The vassals folder contains only one app for the moment. Here is it's yaml file:
uwsgi:
socket: /home/uwsgi/uwsgi/uwsgi.sock
virtualenv: /opt/myproj/virt
pythonpath: /home/uwsgi/project/my_proj
pidfile: /home/uwsgi/uwsgi/uwsgi.pid
uid: uwsgi
gid: uwsgi
chmod-socket: 1
module: wsgi_app
daemonize: /home/uwsgi/uwsgi/uwsgi.log
harakiri-verbose: 1
This is the content of the Django project file:
#! /usr/bin/python
SITE_DIR = '/home/uwsgi/project/my_proj'
import site
site.addsitedir(SITE_DIR)
import os
import sys
sys.path.append(SITE_DIR)
sys.path.append('/home/uwsgi/project/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'my_proj.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Now, here is the result in the /home/uwsgi/uwsgi/uwsgi.log file when I try to launch it:
*** Starting uWSGI 1.0.4 (32bit) on [Fri Mar 2 19:00:46 2012] ***
compiled with version: 4.6.2 on 02 March 2012 18:03:07
current working directory: /etc/uwsgi/vassals
writing pidfile to /home/uwsgi/uwsgi/uwsgi.pid
detected binary path: /usr/local/bin/uwsgi
setgid() to 1001
setuid() to 1001
your memory page size is 4096 bytes
chmod() socket to 666 for lazy and brave users
uwsgi socket 0 bound to UNIX address /home/uwsgi/uwsgi/uwsgi.sock fd 3
Python version: 2.7.2+ (default, Dec 1 2011, 02:17:49) [GCC 4.6.2]
Set PythonHome to /opt/myproj/virt
ImportError: No module named site
*** Starting uWSGI 1.0.4 (32bit) on [Fri Mar 2 19:00:47 2012] ***
compiled with version: 4.6.2 on 02 March 2012 18:03:07
current working directory: /etc/uwsgi/vassals
writing pidfile to /home/uwsgi/uwsgi/uwsgi.pid
detected binary path: /usr/local/bin/uwsgi
setgid() to 1001
setuid() to 1001
your memory page size is 4096 bytes
chmod() socket to 666 for lazy and brave users
uwsgi socket 0 bound to UNIX address /home/uwsgi/uwsgi/uwsgi.sock fd 3
Python version: 2.7.2+ (default, Dec 1 2011, 02:17:49) [GCC 4.6.2]
Set PythonHome to /opt/myproj/virt
ImportError: No module named site
As you can see, uwsgi can't import the site module so uwsgi keeps trying to restart the app.
So I tried to add the no-site :1 option in the yaml file. The result is that I can't import anything in my Django project file...
I also know it's not linked to the project because if I run uwsgi with python 2.6, it just works fine... Unfortunally, I have to run it with python2.7...
Do you have any idea what happens ?
Thanks a lot!

Be sure your virtualenv is built for python2.7, otherwise it cannot be used.
It has to contains /opt/myproj/virt/lib/python2.7 directory

Related

Running flask automatically after boot [duplicate]

This question already has answers here:
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
How to enable a virtualenv in a systemd service unit?
(4 answers)
Closed 1 year ago.
I'm trying to run flask when my raspberry pi turns on.
I created a service for my flask API, this is the service file:
[Unit]
Description= Flask API
After=network.target
[Service]
WorkingDirectory=/home/pi/bucketlist/
ExecStart=flask run --host=0.0.0.0
Restart=Always
[Install]
WantedBy=multi-user.target
But when I try sudo systemctl start flask then sudo systemctl status flask I get this error.
Jun 10 09:56:40 raspberrypi flask[4769]: Usage: flask run [OPTIONS]
Jun 10 09:56:40 raspberrypi flask[4769]: Error: While importing "app", an ImportError was raised:
Jun 10 09:56:40 raspberrypi flask[4769]: Traceback (most recent call last):
Jun 10 09:56:40 raspberrypi flask[4769]: File "/usr/lib/python3/dist-packages/flask/cli.py", line 235, in locate_app
Jun 10 09:56:40 raspberrypi flask[4769]: __import__(module_name)
Jun 10 09:56:40 raspberrypi flask[4769]: File "/home/pi/bucketlist/app/__init__.py", line 5, in <module>
Jun 10 09:56:40 raspberrypi flask[4769]: from flask_api import FlaskAPI
Jun 10 09:56:40 raspberrypi flask[4769]: ModuleNotFoundError: No module named 'flask_api'
Jun 10 09:56:40 raspberrypi systemd[1]: flask_api.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Jun 10 09:56:40 raspberrypi systemd[1]: flask_api.service: Failed with result 'exit-code'.
When I type flask run --host=0.0.0.0 manually it works fine, I don't know why the service gives this error.
More info:
I also tried python /home/pi/bucketlist/run.py instead of flask run --host=0.0.0.0 in ExecStart.
I have a virtual environment set-up in the bucketlist directory.
Python 2.7.16
Flask 1.1.4
Werkzeug 1.0.1

Manually start uwsgi with python3.6 and python2 and django 2

I just started to use uWSGI and Django. I want to invoke it manually.
I'm using python36, Django 2 and if I install uWSGI (pip install uwsgi) in venv and everything is ok.
Q1. When using outside of venv I first installed uWSGI:
pip install uwsgi -> 2.0.18 version
Installed plugins: sudo apt-get install python3-setuptools
Trying to run with python36:
uwsgi --plugin-dir=/usr/lib/uwsgi/plugins --plugin python3 --master --http :5000 --home ~/my_playground/webapps/ --chdir ~/my_playground/webapps/p_tscze/ --module p_tscze.wsgi:application
Plugins in /usr/lib/uwsgi/plugins:
/usr/lib/uwsgi/plugins$ ls | grep python3
asyncio_python36_plugin.so
asyncio_python3_plugin.so
python36_plugin.so
python3_plugin.so
Result obtained -> note python 2.7:
!!! UNABLE to load uWSGI plugin: ./python36_plugin.so: undefined symbol: uwsgi_legion_scrolls !!!
*** Starting uWSGI 2.0.18 (64bit) on [Wed May 29 18:04:24 2019] ***
compiled with version: 7.4.0 on 29 May 2019 15:30:14
os: Linux-4.15.0-45-generic #48-Ubuntu SMP Tue Jan 29 16:28:13 UTC 2019
nodename: start-tehnicka
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /usr/lib/uwsgi/plugins
detected binary path: /usr/local/bin/uwsgi
chdir() to /home/anel/my_playground/webapps/p_tscze/
your processes number limit is 3618
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 :5000 fd 4
uwsgi socket 0 bound to TCP address 127.0.0.1:34279 (port auto-assigned) fd 3
Python version: 2.7.15rc1 (default, Nov 12 2018, 14:31:15) [GCC 7.3.0]
Set PythonHome to /home/anel/my_playground/webapps/
ImportError: No module named site
So please can you let me know what I'm doing wrong?
Q2. When comparing the result of the admin page (./manage.py runserver) and result obtained using uwsgi frontend is not the same, why?
Thanks in advance
For future references:
wget https://projects.unbit.it/downloads/uwsgi-2.0.18.tar.gz
tar -xzvf uwsgi-2.0.18.tar.gz
cd uwsgi-2.0.18
python3 uwsgiconfig.py --build nolang #build binary with python3
python3 uwsgiconfig.py --plugin plugins/python nolang python36 # create a new binary
Run newly created binary uwsgi (we can link to this binary):
./uwsgi --plugin-dir=/home/anel/uwsgi-2.0.18 --plugin python36 --master --http :5000 --home ~/my_playground/webapps/ --chdir ~/my_playground/webapps/p_tscze/ --module p_tscze.wsgi:application

'No module named 'encodings'' error when deploying django app on apache with mod_wsgi

Versions list:
Centos 7---django 2.1.7---Apache 2.4.6---Python 3.6
Check EDIT 5 for most recent error log
followed this tutorial here: https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-centos-7
I am able to run sudo systemctl start httpd and when i go to the url i get a Internal sever error (logs at the bottom)
My file structure
home
└── user
└── projects
└── myapp
├── app
│ ├── <All Code for Webapp including static dir> 
├── env (virtualenv)
├── manage.py
├── new
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── requirements.txt
And i hoping someone can see a mistake in my django.conf located in my further down in my httpd folder. And hoping these are the endpoints i'm looking for
EDIT: This file is located here: /etc/httpd/conf.d/django.conf
django.conf
Alias /static /home/user/projects/myapp/app/static
<Directory /home/user/projects/myapp/app/static>
Require all granted
</Directory>
<Directory /home/user/projects/myapp/new>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIApplicationGroup %{GLOBAL}
WSGIDaemonProcess myapp python-path=/home/user/projects/myapp python-home=/home/user/projects/myapp/env
WSGIProcessGroup myapp
WSGIScriptAlias / /home/user/projects/myapp/new/wsgi.py
I'm not sure if these are pointing to all the right places and was hoping someone could give me a second look.
And i havent touched wsgi.py and was wondering if i am missing any logic there.
my wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'new.settings')
application = get_wsgi_application()
EDIT2: i am able to run the server but get a 500 internal server error and when i check the error_logs i get this
Updated Logs
[Mon Mar 11 10:40:39.865611 2019] [core:notice] [pid 9371] SELinux policy enabled; httpd running as context system_u:system_r:httpd_t:s0
[Mon Mar 11 10:40:39.868149 2019] [suexec:notice] [pid 9371] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Mon Mar 11 10:40:39.918015 2019] [auth_digest:notice] [pid 9371] AH01757: generating secret for digest authentication ...
[Mon Mar 11 10:40:39.919681 2019] [lbmethod_heartbeat:notice] [pid 9371] AH02282: No slotmem from mod_heartmonitor
[Mon Mar 11 10:40:39.948303 2019] [mpm_prefork:notice] [pid 9371] AH00163: Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5 configured -- resuming normal operations
[Mon Mar 11 10:40:39.948370 2019] [core:notice] [pid 9371] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Mon Mar 11 10:40:42.878806 2019] [mime_magic:error] [pid 9376] [client ip] AH01512: mod_mime_magic: can't read `/home/user/projects/myapp/new/wsgi.py'
[Mon Mar 11 10:40:42.879459 2019] [mime_magic:error] [pid 9376] [client ip] AH01512: mod_mime_magic: can't read `/home/user/projects/myapp/new/wsgi.py'
[Mon Mar 11 10:40:42.905048 2019] [:error] [pid 9372] (13)Permission denied: [remote ip] mod_wsgi (pid=9372, process='myapp', application='<url>|'): Call to fopen() failed for '/home/user/projects/myapp/new/wsgi.py'
EDIT3
In this log file it says this Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5 configured when i am running python3.6 not 2.7.5, Maybe changing this would help?
EDIT 4
So i changed my WSGIDaemonProcess to
WSGIDaemonProcess myapp python-path=/home/user/projects/myapp python-home=/home/user/projects/app/env/lib/python3.6/site-packages user=<user>
Now im getting these errors
[Tue Mar 12 10:38:09.111397 2019] [mime_magic:error] [pid 18804] [client ip] AH01512: mod_mime_magic: can't read `/home/user/project/myapp/new/wsgi.py'
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site
so it looks like i need to uninstall mod_wsgi and reinstall to compile with python3.6 and not 2.7
How can i succesfully do this? i uninstall mod_wsgi and try mreinstall using sudo pip3.6 install mod_wsgi Now it doesnt recognize the new install of mod_wsgi and wont start the server because of an invalid command 'WSGIDaemonProcess' this is bc the new download isnt configured with httpd? how would i do this?
EDIT 5
Successfully installed mod_wsgi compiled with correct python version and able to run server, set SELinux to permissive to get rid of cant find '/route/to/wsgi/py. and now I am now getting no module name 'encoding' errors that repeats.
[Wed Mar 13 15:20:46.133597 2019] [core:notice] [pid 4403] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
[Tue Mar 12 14:11:57.520271 2019] [core:notice] [pid 866] AH00052: child pid 891 exit signal Aborted (6)
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
currently my permissions are all under user apache and group apache, and seems they are all under the right permissions, and i tried changing the root user/group to apache as well, and ran chmod -R 777 at the Myapp directory to make sure it had proper permissions.
Permissions under projects:
home/user/projects# ls -l
total 0
drwxrw-r-x. 7 apache apache 109 Mar 12 12:48 myapp
home/user/projects/myapp# ls -l
Permissions under myapp:
total 12
drwxrwxr-x. 7 apache apache 4096 Mar 7 13:18 app
drwxr-xr-x. 5 apache apache 56 Mar 12 12:48 env
-rwxrwxr-x. 1 apache apache 535 Mar 5 13:33 manage.py
drwxrwxr-x. 3 apache apache 110 Mar 7 14:27 new
-rw-rw-r--. 1 apache apache 869 Mar 7 14:42 requirements.txt
According to mod_wsgi's documentation:
user=name | user=#uid
Defines the UNIX user name or numeric user uid of the user that the daemon processes should be run as. If this option is not supplied the daemon processes will be run as the same user that Apache would run child processes, as defined by the User directive, and it is not necessary to set this to the Apache user yourself.
I suppose that the apache user cannot access your home directory. Either try to add the user=myUserdirective to your WSGIDaemonProcess or chown your directory to the apache user. Another possible way would be to put your application inside a directory owned by apache user.
EDIT
I really should learn to read. According to the docs, it looks like you should recompile mod_wsgi using a python3.X interpreter in order to run python3.X applications (use the same versions for both compiling and application's environment)
If you want to use a different version of Python, you will need to reinstall mod_wsgi, compiling it for the version you want. It is not possible for the one mod_wsgi instance to run applications for both Python 2 and 3 at the same time.
EDIT 2
To answer one of your comments: It's normal that yum won't remove the version you installed through pip. It has no way to guess that you installed packages through other package managers AFAIK. I think that you should copy some the .so (I think it's mod_wsgi[version].so) files that lies in the directory where pip installed mod_wsgi (inside your venv/global site_packages I think) into the folder where Apache/Httpd loads its modules files (it's something like /usr/lib/apache2/modules on alpine/ubuntu so I guess it is /usr/lib/httpd/modules for CentOS, but can be different, sorry I can't give you more specific help) after moving out of this folder the old mod_wsgi.so. Don't forget to shutdown httpd before doing this, and restart it after.
Source: https://modwsgi.readthedocs.io/en/develop/configuration-directives/WSGIDaemonProcess.html (section python-home)
Try permission on dir level rather on file.
<Directory /home/user/projects/myapp/new>
Require all granted
</Directory>
You can check user and group for apache by
egrep -i '^user|^group' /etc/httpd/conf/httpd.conf
So make sure you have permissions on /home/user/projects/myapp/new/wsgi.py
recheck by ls -l
Try after for example if User is apache and group is apache
sudo chown apache:apache /home/user/projects/myapp/new/wsgi.py
You have to pass library location for env variable.
WSGIDaemonProcess myapp python-path=/home/user/projects/myapp python-home=/home/user/projects/myapp/env/lib/<PYTHON VERSION>/site-packages/

Python3 uWSGI stops instantly

I am having a an issue where I can't get uwsgi to run on my machine. I keep getting this:
/usr/bin$ sudo service ppuwsgi start
>>ppuwsgi start/running, process 17452
/usr/bin$ sudo service ppuwsgi status
>>ppuwsgi stop/waiting
I think I might be having a similar issue as this guy here. However, I didn't install from source. I saw that the machine came with python3.4. But I did do
sudo apt-get install libpq-dev python3-dev
and maybe even
sudo apt-get install python-dev
I want to think I don't have python installed twice. And I have no idea how to check and see. I opened up my usr/local/bin and saw uwsgi and some pip stuff but no python.
Lastly, here is my /etc/init/ppuwsgi.conf
# file: /etc/init/ppuwsgi.conf
description "uWSGI server for PingPlot"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
exec /usr/local/bin/uwsgi --home /home/ubuntu/web/app/ --socket /home/ubuntu/web/ppuwsgi.sock --chmod-socket --module wsgi --pythonpath /home/ubuntu/web/ -H /home/ubuntu/.virtualenvs/pingplot --logto /home/ubuntu/web/logs/uwsgi.log
The sock file and log file seem to get created as well (the log file is empty). I don't know what the deal is.
Edit:
So I opened up user/bin/ and here are all the python things I have.
EDIT: I LIED
I am getting stuff in the error log. I just couldn't see it because I opened vim without sudo. Here is the error log:
*** Starting uWSGI 2.0.8 (64bit) on [Thu Dec 11 05:36:59 2014] ***
compiled with version: 4.8.2 on 09 December 2014 02:42:22
os: Linux-3.13.0-36-generic #63-Ubuntu SMP Wed Sep 3 21:30:07 UTC 2014
nodename: ip-172-31-9-208
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 15925
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)
chmod() socket to 666 for lazy and brave users
uwsgi socket 0 bound to UNIX address /home/ubuntu/web/ppuwsgi.sock fd 3
Python version: 2.7.6 (default, Mar 22 2014, 23:03:41) [GCC 4.8.2]
Set PythonHome to /home/ubuntu/.virtualenvs/pennypledge
ImportError: No module named site
Based on the error log. I changed my version of Python. I uninstalled uwsgi and pip and then reinstalled it using python3 pip.
sudo apt-get install python3-pip
sudo pip install uwsgi
Then I was able to get uwsgi running.

Run django project on cherokee + uwsgi

That have:
1) ubuntu 11.10
2) cherokee (https://launchpad.net/~cherokee-webserver/+archive/ppa) (version 1.2.101)
installed like this:
sudo apt-get install cherokee
sudo apt-get install libcherokee-mod-rrd
sudo apt-get install cherokee-doc
3) uwsgi (version 0.9.8.1-1)
installed like this:
sudo apt-get install uwsgi uwsgi-plugin-python
4) Django 1.3.1 (sudo pip install django)
Run:
1) add example.com to hosts file (example.com 127.0.0.1)
2) create uwsgi.xml
<uwsgi>
<pythonpath>/var/www/vtest/</pythonpath>
<pythonpath>/var/www/</pythonpath>
<app mountpoint="/">
<script>django_wsgi</script>
</app>
</uwsgi>
and django_wsgi.py
import os
import django.core.handlers.wsgi
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
application = django.core.handlers.wsgi.WSGIHandler()
3)
/var/www$ sudo django-admin.py startproject vtest
/var/www$ sudo chown -R www-data:www-data vtest/
Move file uwsgi.xml, django_wsgi.py to /var/www/vtest and chmod +x django_wsgi.py
4) add virtual server in cherokee-admin vservers -> add -> platforms -> uwsgi choise uwsgi.xml document root /var/www/vtest
in console:
ps -A | grep uwsgi
6352 ? 00:00:00 uwsgi
if run uwsgi command in console:
/var/www/vtest$ /usr/bin/uwsgi -s 127.0.0.1:59238 -x /var/www/vtest/uwsgi.xml
tmp = /
[uWSGI] parsing config file /var/www/vtest/uwsgi.xml
*** Starting uWSGI 0.9.8.1-debian (32bit) on [Sun Jan 15 13:28:42 2012] ***
compiled with version: 4.6.1 on 28 June 2011 10:38:32
*** WARNING: you are running uWSGI without its master process manager ***
your memory page size is 4096 bytes
uwsgi socket 0 bound to TCP address 127.0.0.1:59238 fd 3
your server socket listen backlog is limited to 100 connections
*** Operational MODE: single process ***
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 6515, cores: 1)
in browser example.com:
500 Internal Server Error
what could be the cause of the error? And this line: "no app loaded. going in full dynamic mode" in the console.
you have not loaded the python plugin with
<plugins>python</plugins>