I recently moved Pegasus News from Perlbal, Lighttpd, and Apache to Nginx and uWSGI. We balance the traffic between 3 physical servers, and the systems were struggling under the load even after weeks of Apache conf tweaking. We began having issues with excessively slow page loads, request timeouts, and intermittent errors with OGR transformations. I decided to move us to a lighter application server so that we could get the most out of our system resources, and after a lot of research and testing I chose uWSGI. While I was at it, I decided to replace Perlbal and Lighttpd with Nginx because of its great configuration syntax and excellent performance. I also upgraded us to Ubuntu 10.04 and Postgres 8.4. The result was a resounding success! Memory usage and CPU load on each of the web nodes dropped dramatically. Swap usage dropped to almost nothing. The overall responsiveness of the site improved noticeably, and the timeout errors and OGR failures disappeared entirely. If you'd like to give this stack a try, read on for an overview of the setup on Ubuntu 10.04. I'm using the standard Ubuntu source package for Nginx, but modifying it slightly and them installing it with This post is just about the setup relevant to Nginx and uWSGI. If you need a more complete server setup guide, try my Provisioning a new server post. uWSGIBefore we build Nginx, uWSGI needs to be compiled so that its module can be included in the Nginx build. $ sudo apt-get install build-essential python-dev libxml2-dev $ wget http://projects./downloads/uwsgi-0.9.5.4.tar.gz $ tar -xzf uwsgi-0.9.5.4.tar.gz $ cd uwsgi-0.9.5.4 $ make -f Makefile.Py26 Copy the executable to the local sbin directory. $ sudo cp uwsgi /usr/local/sbin
Also, copy the default uwsgi settings to the /etc/nginx directory. $ sudo mkdir /etc/nginx $ sudo cp nginx/uwsgi_params /etc/nginx $ cd .. NginxWe need to slightly modify the nginx package from Ubuntu to add uWSGI (and, optionally, SSL). $ sudo apt-get install libssl-dev $ sudo apt-get build-dep nginx $ apt-get source nginx $ cd nginx-0.7* $ emacs debian/rules Add the following lines to the end of the configure options. Make sure to include backslashes so that all the options are interpreted as being on one line. If you don't need SSL, ignore that line. --with-http_ssl_module --add-module=$(CURDIR)/../uwsgi-0.9.5.4/nginx Build, install, and hold the packages. $ dpkg-buildpackage $ cd .. $ sudo dpkg -i nginx*.deb $ echo "nginx hold" | sudo dpkg --set-selections $ echo "nginx-dbg hold" | sudo dpkg --set-selections Starting with nginx-0.8.41, you can add something like $ sudo mkdir -p /usr/local/nginx/uwsgi_temp
SupervisorTo manage the uWSGI processes, I use Supervisor. In Ubuntu 10.04, you can simply install it with apt-get. $ sudo apt-get install supervisor
ConfigurationSupervisorTo configure uWSGI, I use command-line options in my Supervisor config. The example below is similar to what I use in production for Pegasus, but you'll want to take a look at the uWSGI docs and tweak for your situation. [program:myapp] command=/usr/local/sbin/uwsgi --home /home/myuser/.virtualenvs/myapp/ --module myapp.deploy.wsgi --socket 10.1.2.3:10000 --pythonpath /sites/myapp.com/code/myapp --processes 5 --master --harakiri 120 --max-requests 5000 directory=/sites/myapp.com/code/myapp environment=DJANGO_SETTINGS_MODULE='myapp.settings' user=www-data autostart=true autorestart=true stdout_logfile=/sites/myapp.com/logs/uwsgi.log redirect_stderr=true stopsignal=QUIT The module I specify in the import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler() The IP address in the My value for NginxFor the site-specific Nginx config, I'm using something like this:
If you're just using uWSGI on localhost, then skip the |
|
来自: python_diango > 《Server》