【发布时间】:2013-12-02 01:05:26
【问题描述】:
我有一组用 Python (Django) 编写的网站。一个站点的流量是其他站点的 10 倍。所有这些都在一台专用服务器上工作。为什么最大的网站运行速度比其他网站慢?
配置:Ubuntu 12.04、Apache2、Django 1.3.1(通过 mod_wsgi 运行)、PostgreSQL 9.1.3
【问题讨论】:
我有一组用 Python (Django) 编写的网站。一个站点的流量是其他站点的 10 倍。所有这些都在一台专用服务器上工作。为什么最大的网站运行速度比其他网站慢?
配置:Ubuntu 12.04、Apache2、Django 1.3.1(通过 mod_wsgi 运行)、PostgreSQL 9.1.3
【问题讨论】:
dpkg --get-selections | grep mpm 是否表明您正在使用apache2-mpm-worker 或apache2-mpm-prefork?我使用 apache2-mpm-worker 并且还调整了 apache2.conf 以更好地分配进程/线程。 prefork worker 不适合 Python/WSGI(尽管这只是我的看法)。
patrick@romulus:~$ sudo apt-get install apache2-mpm-worker
patrick@romulus:~$ sudo vim /etc/apache2/apache2.conf
# worker MPM
# StartServers: initial number of server processes to start
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadLimit: ThreadsPerChild can be changed to this maximum value during a
# graceful restart. ThreadLimit can only be changed by stopping
# and starting Apache.
# ThreadsPerChild: constant number of worker threads in each server process
# MaxClients: maximum number of simultaneous client connections
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule mpm_worker_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxClients 150
MaxRequestsPerChild 0
</IfModule>
然后,在您的 /etc/apache2/sites-available/site.conf 配置中:
<VirtualHost *:80>
# ...
# The following installs the Django WSGI app
WSGIDaemonProcess www.site.io threads=20 display-name=%{GROUP}
WSGIProcessGroup www.site.io
WSGIScriptAlias / /var/www/site.io/public/wsgi.py
</VirtualHost>
我相信这种调整可能会改善您的网站;但是,请务必在进行任何更改之前复制您的 apache2.conf 和 site.conf,以便在网站性能变差时恢复。
patrick@romulus:~$ sudo cp /etc/apache2/apache2.conf ~/apache2.conf.bak
patrick@romulus:~$ sudo cp /etc/apache2/sites-available/site.conf ~/site.conf.bak
【讨论】:
没有足够的信息,我们需要一个非常好的水晶球才能猜测。去看看:
这至少可以让您了解如何解决问题。
【讨论】: