Загружаем и устанавливаем ключ, которым подписаны пакеты и репозитарий nginx
1 |
# wget http://nginx.org/keys/nginx_signing.key |
1 |
# apt-key add nginx_signing.key |
Добавляем в
1 |
/etc/apt/sources.list |
Для Debian-Wheezy
1 2 |
deb http://nginx.org/packages/debian/ wheezy nginx deb-src http://nginx.org/packages/debian/ wheezy nginx |
Для Debian-Squeeze
1 2 |
deb http://nginx.org/packages/debian/ squeeze nginx deb-src http://nginx.org/packages/debian/ sqeeze nginx |
1 |
# apt-get update |
1.Установка и настройка Nginx
1 |
# apt-get install nginx |
1 |
# sysv-rc-conf --level 2345 nginx on |
1 |
# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf~ |
1 |
# nano /etc/nginx/nginx.conf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
user nginx; worker_processes 1; #число равно количеству процессов на сервере #cat /proc/cpuinfo | grep processor | wc -l worker_priority -5; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; use epoll; multi_accept on; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #access_log /var/log/nginx/access.log main; access_log off; #restricting on numbers of connection from one ip-adress limit_conn_zone $binary_remote_addr zone=connections:10m; limit_conn_log_level notice; #limit_conn connections 15; #restricting numbers of connection per second from one ip-adress limit_req_zone $binary_remote_addr zone=requests:10m rate=5r/s; limit_req_log_level warn; #limit_req zone=requests burst=10; sendfile on; tcp_nopush on; tcp_nodelay on; server_tokens off; keepalive_timeout 65; types_hash_max_size 2048; gzip on; gzip_static on; gzip_comp_level 5; gzip_min_length 1024; gzip_proxied any; gzip_vary on; gzip_types text/plain text/xml application/xml application/x-javascript text/javascript text/css text/json; gzip_disable "msie6"; open_file_cache max=5000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; fastcgi_buffer_size 128k; fastcgi_buffers 256 16k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; fastcgi_connect_timeout 90; fastcgi_send_timeout 90; fastcgi_read_timeout 90; client_max_body_size 128m; client_body_buffer_size 16K; index index.php index.html; include /etc/nginx/conf.d/*.conf; } |
1 |
# nano /etc/nginx/conf.d/default.conf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
server { listen 192.168.1.72:80 default_server; server_name localhost; root /usr/share/nginx/html; location / { #root /usr/share/nginx/html; try_files $uri $uri/ /index.php?$args; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { #root /usr/share/nginx/html; try_files $uri =404; fastcgi_split_path_info ^(.+?\.php)(/.*)?$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } location /nginx_status { stub_status on; access_log off; allow 127.0.0.1; allow 192.168.1.0/24; deny all; } location = /favicon.ico { log_not_found off; access_log off; allow all; } location = /robots.txt { access_log off; log_not_found off; } } |
Настройка конфигурационных файлов виртуальных хостов Nginx(содержание в конце статьи)
1 |
# nano /etc/nginx/conf.d/joomla.uk.conf |
1 |
# nano /etc/nginx/conf.d/wordpress.uk.conf |
1 |
# nano /etc/nginx/conf.d/kamaok.uk.conf |
1 |
# nginx –t |
1 |
# /etc/init.d/nginx restart |
1 |
# tail -f /var/log/nginx/error.log |
Установка и настройка PHP-FPM
1 |
# apt-get install php5-mysql php5-fpm |
1 |
# sysv-rc-conf --level 2345 php-fpm on |
1 |
# cp /etc/php5/fpm/php.ini /etc/php5/fpm/php.ini~ |
1 |
# nano /etc/php5/fpm/php.ini |
1 2 3 4 5 6 7 8 |
expose_php = Off max_execution_time = 300 max_input_time = 600 memory_limit = 256M post_max_size = 128M cgi.fix_pathinfo=0 upload_max_filesize = 128M date.timezone = Europe/Kiev |
1 |
# cp /etc/php5/fpm/php-fpm.conf /etc/php5/fpm/php-fpm.conf~ |
1 |
# nano /etc/php5/fpm/php-fpm.conf |
1 2 3 |
emergency_restart_threshold = 10 emergency_restart_interval = 1m process_control_timeout = 10s |
1 |
# cp /etc/php5/fpm/pool.d/www.conf /etc/php5/fpm/pool.d/www.conf~ |
1 |
# nano /etc/php5/fpm/pool.d/www.conf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
[www] user = nginx group = nginx listen = /var/run/php5-fpm.sock listen.owner = nginx listen.group = nginx listen.mode = 0660 pm = dynamic pm.max_children = 50 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3 pm.status_path = /status ping.path = /ping ping.response = pong access.log = /var/log/$pool.access.log access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%" slowlog = /var/log/$pool.log.slow request_slowlog_timeout = 3s request_terminate_timeout = 300 catch_workers_output = yes chdir = / php_flag[display_errors] = on php_admin_value[error_log] = /var/log/fpm-php.www.log php_admin_flag[log_errors] = on php_admin_value[memory_limit] = 32M |
Если сайты всех пользователей будут использовать только один пул(например,www),тогда значение user,group,listen.owner,listen.group выставляем имя пользователя, от которого запускается наш Web-сервер Nginx (у меня это пользователь nginx), при этом listen.mode оставляем 0660
1 |
# touch /var/log/www.access.slow |
1 |
# touch /var/log/www.log.slow |
1 |
# service php5-fpm start |
1 |
# service php5-fpm status |
1 |
# tail -f /var/log/php5-fpm.log |
1 |
# netstat -na | grep php5 |
1 |
unix 2 [ ACC ] STREAM LISTENING 16023 /var/run/php5-fpm.sock |
1 |
# ls -al /var/run/ | grep php5 |
1 2 |
-rw-r--r-- 1 root root 4 Июл 8 23:41 php5-fpm.pid srw-rw---- 1 www-data www-data 0 Июл 8 23:41 php5-fpm.sock |
1 |
# nano /usr/share/nginx/html/index.php |
1 |
<?php phpinfo(); ?> |
Если использовать множество пулов, запуская каждый пул под отдельным пользователем, тогда значение user,group,listen.owner,listen.group выставляем имя пользователя, который является владельцем папки с сайтом(joomla,wordpress,kamaok)
при этом listen.modeвыставляем в 0666(чтобы дать доступ чтения/записи для Web-сервера)
1 |
# nano /etc/php5/fpm/pool.d/joomla.conf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
[joomla] user = joomla group = joomla listen = /var/run/php5-fpm-joomla.sock listen.owner = joomla listen.group = joomla listen.mode = 0666 pm = dynamic pm.max_children = 50 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3 pm.status_path = /status-joomla ping.path = /ping-joomla ping.response = pong access.log = /var/log/$pool.access.log access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%" slowlog = /var/log/$pool.log.slow request_slowlog_timeout = 3s request_terminate_timeout = 300 catch_workers_output = yes php_flag[display_errors] = on php_admin_value[error_log] = /var/log/fpm-php.joomla.log php_admin_flag[log_errors] = on php_admin_value[memory_limit] = 32M |
1 |
# nano /etc/php5/fpm/pool.d/wordpress.conf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
[wordpress] user = wordpress group = wordpress listen = /var/run/php5-fpm-wordpress.sock listen.owner = wordpress listen.group = wordpress listen.mode = 0666 pm = dynamic pm.max_children = 50 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3 pm.status_path = /status-wordpress ping.path = /ping-wordpress ping.response = pong access.log = /var/log/$pool.access.log access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%" slowlog = /var/log/$pool.log.slow request_slowlog_timeout = 3s request_terminate_timeout = 300 catch_workers_output = yes php_flag[display_errors] = on php_admin_value[error_log] = /var/log/fpm-php.wordpress.log php_admin_flag[log_errors] = on php_admin_value[memory_limit] = 32M |
1 |
# nano /etc/php5/fpm/pool.d/kamaok.conf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
[kamaok] user = kamaok group = kamaok listen = /var/run/php5-fpm-kamaok.sock listen.owner = kamaok listen.group = kamaok listen.mode = 0666 pm = dynamic pm.max_children = 50 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3 pm.status_path = /status-kamaok ping.path = /ping-kamaok ping.response = pong access.log = /var/log/$pool.access.log access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%" slowlog = /var/log/$pool.log.slow request_slowlog_timeout = 3s request_terminate_timeout = 300 catch_workers_output = yes php_flag[display_errors] = on php_admin_value[error_log] = /var/log/fpm-php.kamaok.log php_admin_flag[log_errors] = on php_admin_value[memory_limit] = 32M |
Делаем соответствующие изменения в виртуальных хостах Nginx
1 |
# nano /etc/nginx/conf.d/joomla.uk.conf |
1 2 3 4 5 |
location ~ \.php$ { …………… fastcgi_pass unix:/var/run/php5-fpm-joomla.sock; …………… } |
1 |
# nano /etc/nginx/conf.d/wordpress.uk.conf |
1 2 3 4 5 |
location ~ \.php$ { …………… fastcgi_pass unix:/var/run/php5-fpm-wordpress.sock; …………… } |
1 |
# nano /etc/nginx/conf.d/kamaok.uk.conf |
1 2 3 4 5 |
location ~ \.php$ { …………… fastcgi_pass unix:/var/run/php5-fpm-kamaok.sock; …………… } |
Для снятия PHP-FPMстатистики по нескольким пулам, добавляем в
1 |
/etc/nginx/conf.d/default.conf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
location ~ ^/(status-joomla|ping-joomla)$ { fastcgi_pass unix:/var/run/php5-fpm-joomla.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; access_log off; allow 127.0.0.1; allow 192.168.1.0/24; deny all; } location ~ ^/(status-wordpress|ping-wordpress)$ { fastcgi_pass unix:/var/run/php5-fpm-wordpress.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; access_log off; allow 127.0.0.1; allow 192.168.1.0/24; deny all; } location ~ ^/(status-kamaok|ping-kamaok)$ { fastcgi_pass unix:/var/run/php5-fpm-kamaok.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; access_log off; allow 127.0.0.1; allow 192.168.1.0/24; deny all; } |
Примеры виртуальных хостов Nginx при использовании нескольких PHP-пулов или одного общего для всех пользователей пула
# nano /etc/nginx/conf.d/joomla.uk.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
server { listen 192.168.1.72:80; server_name joomla.uk *.joomla.uk; root /home/users/joomla/joomla.uk; access_log /var/log/nginx/joomla.uk.access.log main; location / { #root /home/users/joomla/joomla.uk; try_files $uri $uri/ /index.php?$args; location ~* ^.+\.(jpg|jpeg|gif|png|css|tgz|rar|doc|xls|exe|pdf|ppt|txt|tar|wav|bmp|rtf|js|svg|js|mp3|ogg|mpe?g|avi|zip|gz|bz2?|swf|ico|woff|ttf|eot|otf)$ { expires +30d; add_header Cache-Control "public"; } } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { #root /home/users/joomla/joomla.uk; try_files $uri =404; fastcgi_split_path_info ^(.+?\.php)(/.*)?$; # Если используется один общий пул fastcgi_pass unix:/var/run/php5-fpm.sock; # Если используется отдельный пул для каждого пользователя fastcgi_pass unix:/var/run/php5-fpm-joomla.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location = /favicon.ico { log_not_found off; access_log off; allow all; } location = /robots.txt { access_log off; log_not_found off; } location ~ /\.ht { deny all; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# nano /etc/nginx/conf.d/wordpress.uk.conf server { listen 192.168.1.72:80; server_name wordpress.uk *.wordpress.uk; root /home/users/wordpress/wordpress.uk; access_log /var/log/nginx/wordpress.uk.access.log main; location / { #root /home/users/wordpress/wordpress.uk; try_files $uri $uri/ /index.php?$args; location ~* ^.+\.(jpg|jpeg|gif|png|css|tgz|rar|doc|xls|exe|pdf|ppt|txt|tar|wav|bmp|rtf|js|svg|js|mp3|ogg|mpe?g|avi|zip|gz|bz2?|swf|ico|woff|ttf|eot|otf)$ { expires +30d; add_header Cache-Control "public"; } } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { #root /home/users/wordpress/wordpress.uk; try_files $uri =404; fastcgi_split_path_info ^(.+?\.php)(/.*)?$; # Если используется один общий пул fastcgi_pass unix:/var/run/php5-fpm.sock; # Если используется отдельный пул для каждого пользователя fastcgi_pass unix:/var/run/php5-fpm-wordpress.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location = /favicon.ico { log_not_found off; access_log off; allow all; } location = /robots.txt { access_log off; log_not_found off; } location ~ /\.ht { deny all; } } |
1 |
# nano /etc/nginx/conf.d/kamaok.uk.conf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
server { listen 192.168.1.72:80; server_name kamaok.uk *.kamaok.uk; root /home/users/kamaok/kamaok.uk; access_log /var/log/nginx/kamaok.uk.access.log main; location / { #root /home/users/kamaok/kamaok.uk; try_files $uri $uri/ /index.php?$args; location ~* ^.+\.(jpg|jpeg|gif|png|css|tgz|rar|doc|xls|exe|pdf|ppt|txt|tar|wav|bmp|rtf|js|svg|js|mp3|ogg|mpe?g|avi|zip|gz|bz2?|swf|ico|woff|ttf|eot|otf)$ { expires +30d; add_header Cache-Control "public"; } } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { #root /home/users/kamaok/kamaok.uk; try_files $uri =404; fastcgi_split_path_info ^(.+?\.php)(/.*)?$; # Если используется один общий пул fastcgi_pass unix:/var/run/php5-fpm.sock; # Если используется отдельный пул для каждого пользователя fastcgi_pass unix:/var/run/php5-fpm-kamaok.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location = /favicon.ico { log_not_found off; access_log off; allow all; } location = /robots.txt { access_log off; log_not_found off; } location ~ /\.ht { deny all; } } |
Источник:
1. https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-debian-7
2. http://www.howtoforge.com/installing-nginx-with-php5-and-php-fpm-and-mysql-support-lemp-on-debian- wheezy
3.http://www.howtoforge.com/configuring-your-lemp-system-linux-nginx-mysql-php-fpm-for-maximum-performance