
I have a server (yes, yes, it is a server, it's hard to call it a server). Old iron (2 gig of RAM, AMD Athlon (tm) 64 Processor 3500+, software RAID). Adminin him myself, without any special skills and knowledge. Once upon a time (more than a year ago) I put Debian 5.0 Lenny on it (it was the second linux system installation in my life, before that I installed only Ubuntu on a working laptop) and the ISPConfig3 control panel by manual. I keep on it several (pieces 40) sites of friends and clients, Redmine, SVN and some more trifles.
Periodically, all this disgrace falls (load average> 20), and you have to reload apache every couple of hours on the server or suck the next optimization attempt out of your finger. In general, complete strife and devastation. And so on one beautiful Saturday, I thought - why not solve the issue once and ... And in general.
Under the cut - the story of the dead weekend + prehistory. First of all, it's interesting to me, so that later it is easy to remember what it was and why I put it. It may be interesting to newbies in an interesting and difficult (oh, ...) server optimization process by gradual (!) Translation of sites from under Apache from its ModRewrite under Nginx (by the
way, this word is correctly read "Enginx" I was corrected, Sysoev didnβt once said that the server name is worth reading, as βengineer-Xβ, thanks to
bayandin and
DorBer ). It may be of interest to more or less experienced comrades who find themselves in the same conditions (Debian Lenny, ISPConfig3, weak iron, some good, not very good, and different sites). And it might be interesting to go more experienced, leave a couple of comments.
Summary of this series:
Instead of the preface - Novice and its server
1. Listen to someone else's advice and waste time on nonsense.
2. Reverse proxy and another admin (set and clumsily configure nginx)
3. Finally, they took up the mind (nginx + php-fpm + eAccelerator)
Let's start with the story
The server was
installed according
to this manual - classic LAMP + Hosting Panel + phpMyAdmin.
Later
Redmine was installed on it, which was used for its intended purpose and SVN. Both are using Apache as the web server. One through
Passenger , the second through mod_dav_svn. This is important because Apache has become heavier after these installations.
')
Optimization first - mpm_worker (extra step)
After a while, the first wave of suspensions began. Either there are more sites, or their attendance has increased so much, or the active development (and consequently the active use of Redmine + SVN) has become to blame. But the server went to bed constantly and seriously. I found it among my acquaintances like a more or less adequate linuxoid, but I didnβt get a sensible answer from him. (Well, do not take the advice to change the network card as such!)
Another admin spent a 3-day inspection, advised to transfer Apache to worker mode. Ok, they sat down, sleeves rolled up, websites in ISPConfig on FastCGI were translated (there is a select type field in the admin area :)), they danced around phpMyAdmin with a tambourine, all .htaccess was checked for php_ * directives. Admin lost. Well, ok, we rearranged Apache into the worker on our own.
The server is more or less normal (hanging no more than once a week) worked for a couple of months.
I do not give references to manuly, because the step is really superfluous.
Optimization second - welcome to nginx (step in the right direction)
They found some admin again, but this time they cheated - first they talked, he didnβt hear about MPM and didnβt hear anything, the Apache didnβt read configs, the level of qualification was considered unsatisfactory.
True, I mentioned a couple of times about nginx. I googled, read Habr and decided to put nginx as a reverse proxy in front of Apache. Moving completely to nginx was scary, for some reason I was sure that PHP scripts would feel uncomfortable (some old case was probably remembered when someone else's code on a foreign server without Apache was there, and there were no extensions there, or php itself was old). This is how children's injuries affect the further fate of servers.
So, we put nginx on Debian (do not forget that we have ISPConfig) for this
tutorial . In principle, everything is standard. First we translate Apache to port: 82 (port: 8080 is busy - ISPConfig is hanging on it):
vi /etc/apache2/ports.conf
sed -ie 's/YOUR-IP:80/YOUR-IP:82/g' /etc/apache2/sites-available/*.vhost
mkdir /root/apache2_vhost_backup/
mv /etc/apache2/sites-available/*.vhoste /root/apache2_vhost_backup/
( phpMyAdmin, SVN Redmine), /etc/apache2/sites-available/ ( ).
β , ISPConfig 82- ( , β . (ISPConfig 3.0.2) 80-. - ):
cd /usr/local/ispconfig/server/
cp conf/apache_ispconfig.conf.master conf-custom/
cp conf/vhost.conf.master conf-custom/
# :82
vi conf/apache_ispconfig.conf.master conf/vhost.conf.master
β / libapache2-mod-rpaf ( IP ) nginx
apt-get install libapache2-mod-rpaf nginx
Apache /etc/apache2/apache2.conf 2
RPAFsethostname On
RPAFproxy_ips 127.0.0.1 YOU_IP_ADDRESS
default nginx /etc/nginx/sites-available/default ( , . . ,
www.site.com site.com, β / . β /var/www/site.com/web/, ):
server {
listen 80 default;
server_name _;
server_name_in_redirect off;
resolver 127.0.0.1;
#ACHTUNG! . UPD
if ($host ~* ^(www\.)(.+)) {
set $host2 $2;
}
if ($host !~* ^(www\.)(.+)) {
set $host2 $host;
}
access_log /var/log/ispconfig/httpd/$host/access.log;
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|swf|flv|mp3)$ {
root /var/www/$host2/web;
access_log off;
expires 30d;
}
location / {
root /var/www/$host2/web;
index index.html index.htm index.php;
access_log off;
proxy_pass http://127.0.0.1:82/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
apache nginx ( )
/etc/init.d/apache2 restart
/etc/init.d/nginx restart
! . ( ). , nginx. ? , . β 2-3 load average > 20, β .
β Apache?
. ,
β . ( ! ! ...), β (Debian Squeeze), .
β . , nginx, , /var/www/ ( ISPConfig ), , β , . , wordpress .
3.1 () β fastcgi-wrapper
, Apache β ? -, β¦
! FastCGI wrapper. ! ! ! ! , . , lighttpd. 1 ? β¦ , , . .
$ sudo aptitude install lighttpd
# , (, , Squeeze spawn-fcgi . . , , , lighttpd)
$ sudo /etc/init.d/lighttpd stop
#,
$ sudo update-rc.d -f lighttpd remove
#,
$ sudo /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 \
-u www-data -g www-data \
-f /usr/bin/php5-cgi \
-P /var/run/fastcgi-php.pid
# - - "child process return 2" - , :9000 , . ? "/usr/bin/php5-cgi"!
$ sudo aptitude install php5-cgi
, php5-cgi β spawn-fcgi , /etc/rc.local β . ! ? ! β , β . , nginx 127.0.0.1:9000 ! , .
3.2 β nginx
. . β
β nginx β
forum.woweb.ru/topic47631.htmlβ
habrahabr.ru/blogs/nginx/74135Β« Β» β
vkurseweba.ru/files/nginx-doc.pdf β
, β
wiki.nginx.org/NginxHttpCoreModule, 2 , - ( proxy):
server {
# 80- , ( nginx) -
listen 80 default;
# ,
server_name mysite.com www.mysite.com;
# http://nginx.org/ru/docs/http/ngx_http_core_module.html#server_name_in_redirect
server_name_in_redirect off;
http://nginx.org/ru/docs/http/ngx_http_core_module.html#resolver
resolver 127.0.0.1;
# if,
#ACHTUNG! . UPD
if ($host ~* ^(www\.)(.+)) {
set $host2 $2;
}
if ($host !~* ^(www\.)(.+)) {
set $host2 $host;
}
#
access_log /var/log/ispconfig/httpd/$host/access.log;
access_log off;
#DOCUMENT_ROOT
root /var/www/$host2/web;
#,
index index.html index.htm index.php;
# . . - . - index.php . ( , /some-file.php , location)
location / {
try_files $uri /index.php;
}
# location /some-file.php, - location
location ~* \.php$ {
proxy_pass http://127.0.0.1:82;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
( nginx), php ( default):
server {
listen 80;
server_name _;
server_name_in_redirect off;
resolver 127.0.0.1;
#ACHTUNG! . UPD
if ($host ~* ^(www\.)(.+)) {
set $host2 $2;
}
if ($host !~* ^(www\.)(.+)) {
set $host2 $host;
}
access_log /var/log/ispconfig/httpd/$host/access.log;
root /var/www/$host2/web;
access_log off;
index index.html index.htm index.php;
# location
location / {
try_files $uri $uri/ @fastcgi;
}
# location, , . proxy , named-location proxy_pass http://127.0.0.1:82 - nginx , upstream ( ). . @fastcgi, /index.php, - , .
location @fastcgi{
#
fastcgi_pass 127.0.0.1:9000;
# /etc/nginx/fastcgi_params
include fastcgi_params;
# -
fastcgi_param SCRIPT_FILENAME /var/www/$host2/web/index.php;
}
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/$host2/web/$fastcgi_script_name;
include fastcgi_params;
}
}
. ( sites-enabled) nginx . - , , nginx, , 1 . β . default ModRewrite Wordpress 100%. β
,
. ~80% . , ModRewrite CodeIgniter . . nginx 2 β Β« Β» + Β« phpΒ», . 48 .htaccess apache , .
, (proxy) listen:80 default; ? , , Apache. - . , , default (- ). proxy «».
- . , , nginx, default Joomla, β !
( , .htaccess, .htpasswd, application.ini -php nginx
), production . .
- nginx .htaccess.
, β
http://www.anilcetin.com/convert-apache-htaccess-to-nginx/. - ? .htaccess. - nginx, 100% , .
UPD: vbart www.
/etc/nginx/nginx.conf http map
map $host $host_wo_www {
default $host;
~^www\.(?P<wo_www>.+)$ $wo_www;
}
if, $host2 $host_wo_www.
3.3 β fastcgi-wrapper
. 5 . , , . , . ?
- php-fpm .
. β php 5.3. 5.2. . .
/etc/apt/sources.list
deb http://packages.dotdeb.org lenny all
deb-src http://packages.dotdeb.org lenny all
# stable ( ) lenny - stable apt-get php php-fpm
deb http://php53.dotdeb.org lenny all
deb-src http://php53.dotdeb.org lenny all
apt-get update
apt-get install php php5-fpm
php5 lighttpd, β 80- nginx. ( , lighttpg 81-, )
β (short_open_tag On default_timezone php.ini ). , , Joomla - . .
update β , - , 2 .
3.1 β fastcgi-wrapper , ( , id /var/run/fastcgi-php.pid kill), lighttpd.
/etc/init.d/php-fpm start
. , . , . .
UPD: Nc_Soft (!)
.
/etc/php5/fpm/php.ini, / cgi.fix_pathinfo=0
3.4 β :9000?
β ? , ? ! . .
. php-fpm β /etc/php5/fpm/pool.d/www.conf:
;listen = 127.0.0.1:9000
listen = /var/run/php5-fpm.sock
nginx β /etc/nginx/nginx.conf, upstream β
...
http{
...
# http - nginx unknown directive
upstream php5-fpm-sock {
server unix:/var/run/php5-fpm.sock;
}
...
}
...
127.0.0.1:9000 php5-fpm-sock. proxy, , .
nginx php-fpm
/etc/init.d/nginx restart
/etc/init.d/php-fpm restart
,
, , ,
, , back-end' nginx . β , UNIX- (POSIX ).
3.5 β
. β . php-apc apt-get downgrade php remove php-fpm. checkinstall
eAccelerator . β eAccelerator , Apache ( cgi, ). , - warning β ( include ), (1 !).
, eaccelerator.ini /etc/php5/conf.d /etc/php5/fpm/php.ini
, php-fpm eAccelerator .
β php -v , php apc.so. apc.ini /etc/php5/conf.d/. . Joomla. , , php-apc, Joomla apc_fetch. , false, . apc, php apc (Debian Lenny). - β .
, load average 2 ( 5 , β , - ). , Zend Framework , Application β base_path.
PS: Redmine SVN nginx, Node.js, Nginx git. β . .
UPD:
VBart, .
UPD: php-fpm
Nc_Soft.