so today I tried to host a website using django on a Ubuntu vServer using Apache and mod_wsgi.
First I tested it with a blank django project, and everything worked perfectly, but as soon as I put my django project on the server and set Apache up to run with that, the performance of the server dropped a lot. When accessing the website using apache, the response time is about 10 seconds per page, in contrast do about 100ms when using the django dev server.
I should mentiona that the server has 8GB of ram and a quad-code cpu. When running htop not even 1% CPU usage and about 50MB of ram are used. When im accessing the website, the cpu usage gets really high for a second, just before the website is loaded and then goes back to <1%
Does any body have any tips on what could be the cause of that? I've read that there may be problems with the some mpm workers, but i didnt really find the files i would need to adjust on my server.
The apache server config looks like this(paths changed):
<VirtualHost *:80>
. . .
Alias /static /home/sammy/myproject/static
<Directory /home/sammy/myproject/static>
Require all granted
</Directory>
<Directory /home/sammy/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-home=/home/sammy/myproject/myprojectenv python-path=/home/sammy/myproject
WSGIProcessGroup myproject
WSGIScriptAlias / /home/sammy/myproject/myproject/wsgi.py
In my error.log file, i also get a lot of these errors:
Mon Nov 23 11:20:12.551962 2020] [wsgi:alert] [pid 26252:tid 140403038272448] (11)Resource temporarily unavailable: mod_wsgi (pid=26252): Couldn't create worker thread 2 in daemon process 'myproject'.
and
Exception ignored in: <bound method BaseEventLoop.del of <_UnixSelectorEventLoop running=False closed=False debug=False>>
Traceback (most recent call last):
File "/usr/lib/python3.6/asyncio/base_events.py", line 526, in del
NameError: name 'ResourceWarning' is not defined
Also, here is the "apachectl status" when idling: https://pastebin.com/D40pRNSY
Related
I have installed django website in a new Ubuntu 17.10 server. It is based on python3. It works fine when i run the website in development mode using
python manage.py runserver
But when i tried to configure it via apache2 webserver in port 80 by adding wsgi in apache by editing default config file as shown..
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /home/usr/mygpsproject/gps_restapi/gps_restapi>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess gps_restapi python-path=/home/usr/mygpsproject/gps_restapi/ python-home=/home/usr/mygpsproject/venv/bin/
WSGIProcessGroup gps_restapi
WSGIScriptAlias / /home/usr/mygpsproject/gps_restapi/gps_restapi/wsgi.py
</VirtualHost>
but Site is not working. When I checked the apache error log to find this error...
[Sat Apr 14 16:16:09.201739 2018] [core:notice] [pid 5089:tid 140258267696064] AH00051: child pid 7500 exit signal Aborted (6), possible coredump in /etc/apache2
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00007f906c390bc0 (most recent call first):
I am not able to find what is this encodings module. it worked fine in development mode. so python is not missing any libraries for sure.
What could be the problem with my configuration.
Instead of:
python-home=/home/usr/mygpsproject/venv/bin/
you should be using:
python-home=/home/usr/mygpsproject/venv
Review the documentation on using virtual environments. You are supposed to point to the root of the virtual environment as given by sys.prefix, not the bin directory.
http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html
Also ensure that the virtual environment is created from the same Python version that mod_wsgi was compiled for.
Finally, depending on the Linux distribution used, you also shouldn't place your code under your personal home directory as the permissions on the home directory can be such that the user that runs your code as under Apache can't access the home directory.
I've installed Apache and mod_wsgi on windows xp service pack 3 and added these line to my httpd.conf :
WSGIScriptAlias / "C:/Documents and Settings/X/My Documents/Downloads/Foo/Foo/wsgi.py"
WSGIPythonPath "C:/Documents and Settings/X/My Documents/Downloads/Foo"
<Directory "C:/Documents and Settings/X/My Documents/Downloads/Foo/Foo">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
but when I open localhost on my firefox, it shows Apache's It Works! message, what should I do to run my project on localhost ?
EDIT :
I checked and recognized that my project's path is not included in PYTHONPATH. Isn't the line WSGIPythonPath ... expected to add the address to PYTHONPATH ?
Alright, so my setup is in linux so this is not tested on windows, but:
I did not see your LoadModule statement
File: httpd.conf
LoadModule wsgi_module modules/mod_wsgi.so
modwsgi wont work without that.
Also: the your grant statement seems a bit suspicious.
In the wsgi configuration guide suggests using a Directory directive for allowing this access to your mod_wsgi application.
<Directory "C:/Documents and Settings/X/My Documents/Downloads/Foo/Foo/">
Order allow,deny
Allow from all
</Directory>
Finally:
Make your life easy down the road.
configure apache in worker mode
configure mod_wsgi in daemon mode.
profit
Might I suggest watching this PyCon talk Making Apache suck less for hosting Python web applications from 'the-man' Graham. I wish I knew all of that stuff years ago.
Note: To figure out if you have apache in mpm worker mode.
httpd.exe -V
look for the "Server MPM" value of worker.
Django runs on port 8000 so you'll want to do two things. First, you need to run the server by entering into your console python manage.py runserver. Second, you need to direct your browser to localhost:8000.
As an aside, you don't need Apache to run a simple, local development environment. Django has its own server built in that you can leverage.
The setup is:
Windows XP VM (Stuck with this for the time being - we're on an Intranet)
Apache 2,
mod_wsgi
django 1.4
virtualenv
We only have two users at most using this application simultaneously
Everything works but there is significant delay (10-20 seconds) between the browser's request and the response sent back by the server.
If I replace the Apache2 web server with the Django development server (which I do not want to do in production) the app is very responsive. So my assumption is that the problem is with Apache2 configuration or mod_wsgi configuration.
I am not an Apache expert and have spent hours looking for the right settings to configure the Apache2 web server but have failed to find anything that will improve the response.
Any assistance would be greatly appreciated.
Here are the settings that I have either changed or added to my httpd.conf:
# ThreadsPerChild: constant number of worker threads in the server process
ThreadsPerChild 10
# Changed MaxRequestsPerChild 0 to 1 for Django
MaxRequestsPerChild 1
# For Django KeepAlive should be OFF
KeepAlive Off
WSGIApplicationGroup %{GLOBAL}
#######################################
WSGIScriptAlias / "C:/virtual_env/sitar_env2/cissimp/cissimp/wsgi.py"
WSGIPythonPath C:/virtual_env/sitar_env2/Lib/site-packages;C:/virtual_env/sitar_env2/cissimp
Alias /static "C:/virtual_env/sitar_env2/cissimp/cissimp/static"
<Directory "C:/virtual_env/sitar_env2/cissimp/cissimp">
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>
##########################################
Dont set:
MaxRequestsPerChild 1
You are effectively restarting Apache on every request which means having to load the whole Django application on every request. You should not do that.
I'm deploying my first ever Django project and I get the feeling I'm very close, but just need some help getting over the line. Here's the problem:
My httpd.conf changes look like this:
WSGIScriptAlias / /home/miketnc/frontend/tncsite/wsgi.py
WSGIPythonPath /home/miketnc/frontend/tncsite
<Directory /home/miketnc/frontend/tncsite>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
So far so good, the "hello world" script in wsgi.py runs just fine. The problem comes when I use the Django doc-recommended wsgi script:
import os, sys
sys.path.append('/home/miketnc/frontend/tncsite')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tncsite.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
This causes a 500 error:
File "/home/miketnc/frontend/tncsite/wsgi.py", line 10, in ?
mod_wsgi (pid=15494): Exception occurred processing WSGI script '/home/miketnc/frontend/tncsite/wsgi.py'.
mod_wsgi (pid=15494): Target WSGI script '/home/miketnc/frontend/tncsite/wsgi.py' cannot be loaded as Python module.
All of the support I've seen on the error relates back to bad installations in apache, not finding modules etc, which can't be the case if "hello world" is running.
Any ideas?
UPDATE
After restarting on a fresh server, I've managed to move things forward slightly. The good news is that python and WSGI seem to be playing nice, the bad is that I'm now getting a different kind of 500 error.
The only error I'm getting back in the log is:
"[Mon Dec 05 18:22:45 2011] [error] [client ip] mod_wsgi (pid=19804): Exception occurred processing WSGI script '/home/miketnc/frontend/tncsite/apache/wsgi.py'."
The Hello World script still runs fine, the trigger for the error is the final line:
application = django.core.handlers.wsgi.WSGIHandler()
I've set all directories containing the project from frontend/ down to 777 and I've added a daemon process as myself:
LoadModule wsgi_module /usr/local/apache/extramodules/mod_wsgi.so
AddHandler wsgi-script .wsgi
WSGIDaemonProcess miketnc processes=2 maximum-requests=500 threads=1
WSGIProcessGroup miketnc
WSGIScriptAlias / /home/miketnc/frontend/tncsite/apache/wsgi.py
<Directory /home/miketnc/frontend/tncsite>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
Anyone please able to advise further on how to get this working?
In your case:
WSGIPythonPath /home/miketnc/frontend/tncsite
is redundant, as your are setting sys.path in the WSGI script file.
What you are missing though is adding the parent directory of the site:
sys.path.append('/home/miketnc/frontend')
This is in addition to the existing line adding '/home/miketnc/frontend/tncsite'.
Read:
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
and watch:
http://code.google.com/p/modwsgi/wiki/WhereToGetHelp?tm=6#Conference_Presentations
which talk about paths and permissions.
Solved.
I didn't realise that the error log in Cpanel is a tiny fraction of the error log Apache outputs. Once I viewed the Apache logs, the problem was obvious. In this case it was MySQLdb not being set up properly.
I worked through it, making some adjustments around .python-eggs, and all is now well.
Thanks for the responses and in particular thank you Graham for the work you've put into WSGI.
Apache & mod_wsgi are configured correctly (I've created a hello
world .html apache file and a hello world mod_wsgi application with
no problems). I now need my Django app to work with my django.wsgi
file. What makes me think that it's not recognizing my wsgi file is that I
went into my django.wsgi file I created and completely deleted all of
the code in the file and restarted Apache and it still gives me the
same page (a listing of the files from Django app, not my actual
Django application. Configuring Apache and mod_wsgi went really well
but I'm at a loss of how to fix this. Here are some details:
Here is my current django.wsgi file:
import os
import sys
sys.path.append('/srv/www/duckling.org/store/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'store.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I've tried a few different versions of the django.wsgi file
(including a version like the one over at http://www.djangoproject.com/).
This version of my wsgi is from here:
http://library.linode.com/frameworks/django-apache-mod-wsgi/ubuntu-10...
Also, here is my vhost apache configuration file below. I think these
are the main files that are suppose to do the job for me. Let me know if
you see any errors in what I'm doing and what else I might do to fix
this. The django app runs fine on the django's built-in development
server so I'm thinking it might have something with my paths.
No errors in my apache error.log file as well. It's acting as there's
no problem at all, which is not the case...the project isn't loading,
like I said just a listing of my files and directories of my Django
project. Here is my apache config file:
<VirtualHost 1.2.3.4:80>
ServerAdmin hi#duckling.org
ServerName duckling.org
ServerAlias www.duckling.org
DocumentRoot /srv/www/duckling.org/store/
<Directory /srv/www/duckling.org/store/>
Order Allow,Deny
Allow from all
</Directory>
Alias /static/ /srv/www/duckling.org/store/static/
<Directory /srv/www/duckling.org/store/static>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias store/ /srv/www/duckling.org/store/wsgi-scripts/django.wsgi
<Directory /srv/www/wsgi-scripts>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
And here are versions of the stack that I'm using, I saw over at the
mod_wsgi site that you all would like the versions of what I'm using
on the server:
Apache/2.2.14 (Ubuntu) PHP/5.3.2-1ubuntu4.5 with Suhosin-Patch
mod_python/3.3.1 Python/2.6.5 mod_wsgi/2.8
thanks,
j.
For a start, you should definitely not keep your Django files under your DocumentRoot. There's no need for them to be there, and it's a potential security risk - as you've seen, your current misconfiguration allows Apache to serve up your files directly: an attacker could guess that and download your settings.py, complete with your database password.
So, get rid of that DocumentRoot directive completely, as well as the first Directory section which allows direct access to /srv/www/duckling.org/store/. (You probably don't need the one serving up /srv/www/wsgi-scripts either.) That should make things a bit better.
By the way, this configuration will serve your website under duckling.org/store - is that what you want? If you want it under the root, you should just use:
WSGIScriptAlias / /srv/www/duckling.org/store/wsgi-scripts/django.wsgi