Debian, nginx + AMP. Set up in under-resourced settings.

Today we’ll talk about configuring nginx in conjunction with apache + mysql + php in under-resourced settings. I was able to run this bunch for 128 megabytes of RAM, although in such circumstances, it worked erratically. The 192 already was all right
So you install apache, mysql, php. More precisely, you need to run everything in the first article. The rest is just available so far.
Now we need to edit configure apache so that it did not hold 80th port on which we need to hang the nginx.
Let’s agree on that article/49 you all did.
In/etc/apache2/ports.conf change:

NameVirtualHost *:80
Listen 80

NameVirtualHost *:8181
Listen 8181

Now actually put nginx:

debian-nginx:~# aptitude install nginx

Next, open and give it to/etc/nginx/nginx.conf roughly the following:

user www-data;
worker_processes 1;
error_log /var/log/nginx-error.log;
pid /var/run/;
events {
worker_connections 1024;
use epoll;
http {
include /etc/nginx/mime.types;
access_log /var/log/nginx-access.log;
sendfile on;
tcp_nodelay on;
keepalive_timeout 4;
client_max_body_size 100m;
gzip on;
gzip_disable «MSIE [1-6]\.(?!.*SV1)»;
gzip_min_length 1024;
gzip_comp_level 4;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

Now we need to create an entry for VirtualHost in nginx.
For example, create a file/etc/nginx/sites-enabled/ (do not use these files in the title character) and write the following:

server {
listen 80;
root /var/www/sites/;
server_name_in_redirect off;
index index.php index.html index.htm;
access_log /var/www/logs/;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/nginx-default;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location ~*
root /var/www/sites/;
expires 30d;
access_log off;
location ~ /\.ht {
deny all;

Go throw on important moments in config:
server_name; list all domainaliases for the us site.
root/var/www/sites/; is the root of the site
proxy_pass; IP and port of Apache. In this case, the link between apache and nginx will be through the local loop.
In the future you will come across a problem that nginx will not transmit the IP address Apaču. Actually, take better account of this now so that your scripts on sites whose work is based on the IP of the visitor to work correctly. Here we help mod_rpaf. Will install it:
Debian, nginx: ~ # aptitude install libapache2-modrpaf
In a line should appear:/etc/apache2/apache2.conf
LoadModule rpaf_module libexec/apache22/
If it is not visible, add it if zakomentirovana is uncomment.
At the end of the config (apache2.conf), but before we describe virtual hosts (and hence to include sitesenabled/*) vpisyvam the following:

RPAFenable On
RPAFsethostname On
RPAFheader X-Forwarded-For #

may not work if that is the comment this line
Now actually the reason we have nginx. He is the reversnym proxy, so the need to keep many processes are running apache is not needed.
Find the line:

StartServers 1
MinSpareServers 1
MaxSpareServers 5
MaxClients 10
MaxRequestsPerChild 0

They are in the section IfModule mpm_prefork_module. Meaning you can have different, including the three-digit. We don’t have to, so here is the verse to mean:

StartServers 1
MinSpareServers 1
MaxSpareServers 4
MaxClients 4
MaxRequestsPerChild 2000

And change the KeepAlive On KeepAlive Off required! Otherwise apache will not eat put it in our environment resources.
Now let’s create a VirtualHost in Apache. For example, I create a file/etc/apache2/sites-enabled/example (here’s the linksample file)
Actually everything. If you read the manual carefully, then everything should work. Reload both Web servers:
Debian, nginx: ~ #/etc/init.d/nginx restart & &/etc/init.d/apache2 restart
You can come across such a problem that the php files are downloaded instead of displayed pages. In this case, you need to add the line:/etc/apache2/apache2.conf
AddType application/xhttpd-php .php .PHP4 .php3 .phtml php5.
Add, again, you need to Include the string “/etc/apache2/sites-enabled/“. Do not forget to restart Apache. It happens rarely, but it happens, especially in Ubunte.
Now figures in order not to be golosovnym. For the test I put Drupal and dërgal it through the siege. Don’t forget to create the necessary directories for nginx logs, otherwise it will not start.
With 700 MHz, 192 MB of RAM this configuration vydeživaet 3.20 pages per second.
After 218 MB of memory and a 1500 MHz it has issued already 78 pages per second.
When 218 MB of memory and 700 MHz are 3 pages per second.
I have deliberately not raped the weak VDSki. During the tests, I quietly fixed the site itself, has used drupal admin area etc. That is, these figures is not the limit.
But Atom 270 and 1 GB of RAM (~ 500 MB worth) have given me far beyond 50 requests per second on a network.
My favorite VDSki (KVM, 2660 MHz, 1 GB RAM) was about 30. The main thing-the right to choose MaxSpareServers and MaxClients.
So you need a good CPU. If you have free memory increase value of MaxSpareServers and MaxClients. If there is an error of Bad Gateways is also increase these values. Also it is preferable to use a KVM or a dedicated processor, as some strange ideas about OpenVZ working with processes, because of this lost quite a lot of ligaments nginx + AMP.
What are cons. The unscrupulous hosterah on VDSkah is VIRT 192mb have such memory. won’t be enough even to run mysql. Some have incorrectly configured OpenVZ is the mysql at zero free RAM drops. The KVM or dedicated servers, I have not encountered such problems.