Apache2 not serving django content - python

I am taking over a django project which another developer maintained. The service is run on an Ubuntu machine. ZEO is used for content caching. Ajax/Dajax is used for asynchronious content. Celery is used for task management and Django is used for the project itself.
The service is usually reached via a specific IP address which limits access to specific URLs. http://my_server_ip. Without knowingly changing anything, this started to not work. Instead of taking me to the splash page, entering the IP would hang, unsuccesfully connecting. I don't get a 404, 500 or some other error, it just sits and continually tries to load as if waiting to connect or to receive content.
I attempted to restart the service in the hopes that this would solve the problem, it did not. I performed a system reboot and followed the following commands, as per the prior developer's documentation, to reboot the server.
From within the django project:
runzeo -a localhost:8090 -f /path/to/operations_cache.fs
su project_owner
python manage.py celery worker -n multiprocessing_worker --loglevel=debug -Q multiprocessing_queue
python manage.py celery worker --concurrency=500 --pool=eventlet --loglevel=debug -Q celery -n eventlet_worker
The two celery commands had to be run as the owner of the project directory.
Finally I ran sudo service apache2 restart. Upon completion, I tried to navigate to the webpage but received the same response: hung on connecting. The trac pages do work at http://my_server_ip/trac.
The following is all I have found in the apache log files.
error.log
[Fri Feb 06 16:01:11 2015] [error] /usr/lib/python2.7/dist-packages/configobj.py:145: DeprecationWarning: The compiler package is deprecated and removed in Python 3.x.
[Fri Feb 06 16:01:11 2015] [error] import compiler
[Fri Feb 06 16:01:11 2015] [error]
access.log
my.ip - user [06/Feb/2015:15:55:40 -0500] "GET / HTTP/1.1" 500 632 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:35.0 Gecko/20100101 Firefox/35.0"
I have tried looking into the django logs, but nothing appears there. Perhaps I am not finding the correct logs.
As a good start, where can I find out what the system is hanging on. How do I determine if it is an issue with django, apache, the interaction between the two, etc. This would help me zero in on specifically what is happening.
Edit
I was able to resolve my problem, though I cannot say for sure what resolved the issue. I suspect that the issue had to do with permissions on the static files folder. I serve my content through the www-user. After going through the steps described above I then ran yes yes | python manage.py collectstatic as the user www-data. The server was able restart and I was able to access the trac content as well as the django content.
I am hesitant to post this as an answer because I do not know with certainty whether my solution described here is the step which solved the problem.

Related

uWSGI will not run in mixed Python environment in order to operate correctly with nginx and run Django app

Environment: Ubuntu 16.04 (with system Python at 2.7.12) running in Vagrant/Virtualbox on Windows 10 host
Python Setup: System python verified by doing python -V with no virtualenv's activated. Python 3.5 is also installed, and I've done pipenv --three to create the virtualenv for this project. Doing python -V within the activated virtualenv (pipenv shell to activate) shows Python 3.5.2.
Additional Background: I'm developing a Wagtail 2 app. Wagtail 2 requires Django 2 which, of course, requires Python 3. I have other Django apps on this machine that were developed in Django 1.11/Python 2.7 and are served by Apache. We are moving to Django 2/Python 3 for development going forward and are moving to nginx/uWSGI for serving the apps.
I have gone through many tutorials/many iterations. All Vagrant port mapping is set up fine with nginx serving media/static files and passing requests upstream to the Django app on a unix socket, but this is giving a 502 Gateway not found error because uWSGI will not run correctly. Therefore, right now I'm simply running the following from the command line to try to get uWSGI to run: uwsgi --ini /etc/uwsgi/sites/my_site.com.ini. This file contains:
[uwsgi]
uid = www-data
gid = www-data
plugin = python35
# Django-related settings
# the base directory (full path)
chdir=/var/sites/my_site
# Django's wsgi file
wsgi-file = my_site.wsgi
# the virtualenv (full path)
virtualenv=/root/.local/share/virtualenvs/my_site-gmmiTMID
# process-related settings
# master
master = True
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe)
socket = /tmp/my_site.sock
# clear environment on exit
vacuum = True
I've tried installing uWSGI in the following ways:
system-wide with pip install uwsgi as well as pip3 install uwsgi
using apt-get install uwsgi uwsgi-plugin-python3
I've ensured that only one install is in place at a time by uninstalling any previous uwsgi installs. The latter install method places uwsgi-core in usr/bin and also places in usr/bin shortcuts to uwsgi, uwsgi_python3, and uwsgi_python35.
In the .ini file I've also tried plugin = python3. I've also tried from the command line:
uwsgi_python3 --ini /etc/uwsgi/sites/my_site.com.ini
uwsgi_python35 --ini /etc/uwsgi/sites/my_site.com.ini
I've tried executing the uwsgi ... .ini commands from both within the activated virtual environment and with the virtualenv deactivated. Each of the three command line uwsgi ... .ini executions (uwsgi ..., uwsgi_python3 ... and uwsgi_python35 ...) DO cause the .ini file to be executed, but each time I'm getting the following error (the last two lines of the following statements):
[uwsgi] implicit plugin requested python35
[uWSGI] getting INI configuration from /etc/uwsgi/sites/my_site.com.ini
*** Starting uWSGI 2.0.12-debian (64bit) on [Wed Mar 7 03:54:44 2018] ***
compiled with version: 5.4.0 20160609 on 31 August 2017 21:02:04
os: Linux-4.4.0-116-generic #140-Ubuntu SMP Mon Feb 12 21:23:04 UTC 2018
nodename: vagrant-ubuntu-trusty-64
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 2
current working directory: /vagrant/my_site
detected binary path: /usr/bin/uwsgi-core
setgid() to 33
setuid() to 33
chdir() to /var/sites/my_site
your processes number limit is 7743
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 socket 0 bound to UNIX address /tmp/my_site.sock fd 3
Python version: 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609]
Set PythonHome to /root/.local/share/virtualenvs/my_site-gmmiTMID
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
If I go into the Python command line within the activated virtualenv and do import encodings, it imports fine (no message - just comes back to command line). A search for this particular error has turned up nothing of use. Any idea why the ImportError: No module named 'encodings' is coming up?
UPDATE - PROBLEM STILL OCCURRING
I'm using pipenv, and it stores individual virtualenvs in the /home/username/.local/share/virtualenvs folder. Though I was able to start uWSGI from the command line by executing the uWSGI config file as the vagrant user (see comment below), I have still not been able start the service with /home/vagrant/.local/share/virtualenvs/my_venv in the uWSGI config file. I tried adding the vagrant user to the www-data group and adding the www-data user to the vagrant group. I also put both read and execute permission for the world on the whole path (including the individual venv), but the uWSGI service will still not start.
A HACKISH WORKAROUND
I did finally get the uWSGI service to start by copying the venv to /opt/virtualenvs/my_venv. I was then able to start the service with sudo service uwsgi start. Ownership of that whole path is root:root.
STILL LOOKING FOR A SOLUTION...
This solution is not optimal since I am now executing from a virtualenv that will have to be updated when the default virtualenv is updated since this location is not the default for pipenv, so I'm still looking for answers. Perhaps it is a Ubuntu permissions error, but I just can't find the problem.
It might be the problem with your virtual environment. Try the following
rm -rf /root/.local/share/virtualenvs/my_site-gmmiTMID
virtualenv -p python3 /root/.local/share/virtualenvs/my_site-gmmiTMID
source /root/.local/share/virtualenvs/my_site-gmmiTMID/bin/activate
pip install -r requirements.txt
and in uwsgi conf try to change
virtualenv=/root/.local/share/virtualenvs/my_site-gmmiTMID
to
home=/root/.local/share/virtualenvs/my_site-gmmiTMID
Reference:
http://uwsgi-docs.readthedocs.io/en/latest/Options.html#virtualenv

mod_wsgi-express service not stating

I'm trying to use mod_wsgi-express for a django project. This project will be served through the main instance of apache, with a proxy. My purpose is to serve a python 3 application with a mod_wsgi for python 2 installed on the "main" apache server. (I've many apps on the same server)
I've created a systemd service which should launch the mod_wsgi-express instance, using my own service file :
https://gist.github.com/frague59/87529fc28b098dd116f09be92cf66af0
and
https://gist.github.com/frague59/8de1d03800042db95c82452af280dffe
I've to mention that those scripts works on another server, same distro, same version (debian oldstable)
... but the mod_wsgi-express does not start : no error message, no log...
I've noticed that a dir is created in tmp:
/tmp/mod_wsgi-127.0.0.1:8081:993
I've tried to start the apachectl from here:
# apachectl configtest
And I have a weird message:
AH00526: Syntax error on line 241 of /tmp/mod_wsgi-127.0.0.1:8081:993/httpd.conf:
Invalid option to WSGI daemon process definition.
I posts the generated httpd.conf file for example:
https://gist.github.com/frague59/a6d8d26b704565b39f7352a7c16e07d3
Error seems to be around:
send-buffer-size=0 \

"Python-Eggs is writable by group/others" Error in Windows 7

I installed trac using BitNami the other day and after restarting my computer I'm not able to get it running as a service today. I see in the error log this error
[Fri Dec 02 08:52:40.565865 2016] [:error] [pid 4052:tid 968] C:\Bitnami\trac-1.0.13-0\python\lib\site-packages\setuptools-7.0-py2.7.egg\pkg_resources.py:1045: UserWarning: C:\WINDOWS\system32\config\systemprofile\AppData\Roaming\Python-Eggs is writable by group/others and vulnerable to attack when used with get_resource_filename. Consider a more secure location (set with .set_extraction_path or the PYTHON_EGG_CACHE environment variable).
Everyone's suggestion is to move the folder path PYTHON_EGG_CACHE to the C:\egg folder or to suppress the warning at the command line.
I've already set the PYTHON_EGG_CACHE for the system, I set it in trac's setenv.bat file, and in the trac.wsgi file but it's not picking up on the changes when I try to start the service.
Alternately I can't change the permissions on the folder in Roaming using chmod like in Linux, and I can't remove any more permissions on the folder in Roaming (myself, Administrators, System) as Corporate IT doesn't allow for Administrators to be removed and this isn't an unreasonable policy.
I found out that there was another service running on the 8080 port that I had setup trac on and that was causing the trouble. The error in the logs was not pointing to that as being the issue however.

Why would mod_wsgi be serving sporadic 500 errors?

I was having issues with my Django app (running on Debian with Apache 2.2.22) serving occasional bursts of 500 errors with the mod_wsgi "Premature Script exit" errors in the logs. I dug around and it looked like the crashes might be a result of running out of memory, as a result I found a bad memory hog bug and fixed it. However at the same time I was reading about mod_wsgi configs and decided to make a couple other changes to my setup. I added...
stack-size=524288 maximum-requests=500 inactivity-timeout=5
to my Apache config, making it look like this...
WSGIScriptAlias / /home/myapp/src/myapp-api/myapp/wsgi.py
WSGIDaemonProcess myapp user=me group=team processes=4 threads=16 stack-size=524288 maximum-requests=500 inactivity-timeout=5 python-path=/home/myapp/src/myapp-api:/home/myapp/.virtualenvs/myapp/lib/python2.7/site-packages
WSGIProcessGroup myapp
WSGIPassAuthorization On
I tested it out on my dev servers and all seemed fine, but once I deployed this to servers under more load I saw a large number of sporadic 500 errors, again logging "Premature Script exit" but this time there was no memory issues and the system had plenty of free RAM.
I'm at a loss as to what caused this. The only other error that was new in the logs was
[Wed Jan 14 20:17:16 2015] [error] Exception AttributeError: "'NoneType' object has no attribute 'error'" in <bound method TCPTransport.__del__ of <amqp.transport.TCPTransport object at 0x7ff4b0e69d10>> ignored
which seems to relate to my usage of Celery and AMQP, so perhaps this is a problem with C module interop?
If the stack size issue was a problem I would expect to see errors in my django code, not in mod_wsgi, so what else could cause this?

Python web script doesn't run on apache

I am trying getting starting with python web programming but I have a problem. I want to work with CGI
I installed Ampps web-server on pc (Linux). According to this instruction Apache Tutorial: Dynamic Content with CGI. I put my file index.pl in the folder '/usr/local/ampps/www/cgi-bin' and tried to accesses it
http://localhost/cgi-bin/index.py
Here is code of my file:
#!/usr/local/ampps/python/bin/python
print "Content-type: text/html\n\n"
print "Hello World from PythonStandard Hello World from a Python"
But it gives me mistake Sorry, the page you are looking for is currently unavailable.
Here is error log of Apache:
[Tue Jul 22 20:05:45.019799 2014] [cgi:error] [pid 11952] [client 127.0.0.1:38359] AH01215: (13)Permission denied: exec of '/usr/local/ampps/www/cgi-bin/index.py' failed
[Tue Jul 22 20:05:45.020262 2014] [cgi:error] [pid 11952] [client 127.0.0.1:38359] End of script output before headers: index.py
I corrected conf of Apache
AddHandler cgi-script .cgi .pl .py
it looked ok too
LoadModule cgi_module modules/mod_cgi.so
ScriptAlias /cgi-bin/ "{$path}/www/cgi-bin/"
What can be wrong else?
Your script needs to have the executable bit set (using chmod) and the Apache user or group (usually named 'Apache' or 'www-data' depending on your distro - you can check /etc/passwd to find out) must have the 'execute' permission on it.
I was getting similar errors. It seemed that the AMPPS installation of Python was missing one of the DLLs. I downloaded the 2.7 .msi package from ActiveState and installed it.
A couple of things before you start: the location, and the permissions.
I may be wrong on this, but Apache will be looking for the Python executable in the directory that was created for it in the AMPPS installation -- c:\whatever\whatever\ampps\python. I tried installing it in c:\whatever\whatever\ampps\python2.7, but it didn't work. I suppose I could have edited the httpd.conf file, but since the original installation wasn't working anyway, I overwrote the c:\w\w\ampps\python files.
I also ran the .msi as an administrator. This was tricky, because my company-issued machine doesn't let me simply right-click on an .msi and select "run as administrator." I finally went into the task manager (option 8 here: http://www.eightforums.com/tutorials/9564-run-administrator-windows-8-a.html), and put the full path to the downloaded file in the entry. And voila, "hello world."

Categories