linux

Конфигурирование nginx+fpm

Nginx and PHP-FPM Configuration and Optimizing Tips and Tricks

I wrote before a guide Howto install Nginx/PHP-FPM on Fedora 20/19, CentOS/RHEL 6.5/5.10, but this guide is just installation guide and many cases Nginx and PHP-FPM basic configuration is good enough, but if you want to squeeze all the juice out of your VPS or web server / servers and do your maintenance work little bit easier, then this guide might be useful. These tips are based entirely on my own experience, so they may not be an absolute truth, and in some situations, a completely different configuration may work better. It’s also good to remember leave resources for another services also if you run example, MySQL, PostgreSQL, MongoDB, Mail server, Name server and/or SSH server on same machine.

And yes here we go…

Nginx Configuration and Optimizing Tips and Tricks

Nginx Tip 1. – Organize Nginx Configuration Files

Normally Nginx configuration files are located under /etc/nginx path.
One good way to organize configuration files is use Debian/Ubuntu Apache style setup:

Virtualhost files have 2 paths, because sites-available directory can contain any stuff, like test configs, just copied/created configs, old configs and so on. And sites-enabled contains only really enabled configurations, actually just only symbolic links to sites-available directory.

Remember add following includes at the end of your nginx.conf file:

Nginx Tip 2. – Determine Nginx worker_processes and worker_connections

Default setup is okay for worker_processes and worker_connections, but these values could be little bit optimized:
max_clients = worker_processes * worker_connections

Just Nginx basic setup can handle hundreds of concurrent connection:

Normally 1000 concurrent connection / per one server is good, but sometimes other parts like disks on server might be slow, and it causes that the Nginx is locked on I/O operations. To avoid locking use example following setup: one worker_precess / per processor core, like:
Worker Processes

To check how many processor cores do you have, run following command:

So here is 4 cores and worker_processes final setup could be following:

Worker Connections
Personally I stick with 1024 worker connections, because I don’t have any reason to raise this value. But if example 4096 connections per second is not enough then it’s possible to try to double this and set 2048 connections per process.

worker_processes final setup could be following:

I have seen some configurations where server admins are used too much Apache and think if I set Nginx worker_processes to 50 and worker_connections to 20000 then my server could handle all traffic once what we get monthly…but yes it’s not true. It’s just wasting of resources and might cause some serious problems…

Nginx Tip 3. – Hide Nginx Server Tokens / Hide Nginx version number

This is good for security reasons hide server tokens / hide Nginx version number, especially, if run some outdated version of Nginx. This is very easy to do just set server_tokens off under http/server/location section, like:

Nginx Tip 4. – Nginx Request / Upload Max Body Size (client_max_body_size)

If you want to allow users upload something or upload personally something over the HTTP then you should maybe increase post size. It can be done with client_max_body_size value which goes underhttp/server/location section. On default it’s 1 Mb, but it can be set example to 20 Mb and also increase buffer size with following configuration:

If you get following error, then you know that client_max_body_size is too low:
“Request Entity Too Large” (413)

Nginx Tip 5. – Nginx Cache Control for Static Files (Browser Cache Control Directives)

Browser caching is import if you want save resources and bandwith. It’s easy setup with Nginx, following is very basic setup where logging (access log and not found log) is turned off and expires headers are set to 360 days.

If you want more complicated headers or some other expiration by filetypes then you could configure those separately.

Nginx Tip 6. – Nginx Pass PHP requests to PHP-FPM

Here you could use default tpc/ip stack or use directly Unix socket connection. You have to also setup PHP-FPM listen exactly same ip:port or unix socket (with Unix socket also socket permission have to be right). Default setup is use ip:port (127.0.0.1:9000) you could of course change ips and ports what PHP-FPM listens. Here is very basic configuration with Unix socket example commented out:

It’s also possible to run PHP-FPM another server and Nginx another.

Nginx Tip 7. – Prevent (deny) Access to Hidden Files with Nginx

It’s very common that server root or other public directories have hidden files, which starts with dot (.) and normally those is not intended to site users. Public directories can contain version control files and directories, like .svn, some IDE properties files and .htaccess files. Following deny access and turn off logging for all hidden files.

PHP-FPM Configuration Tips and Tricks

PHP-FPM Tip 1. – PHP-FPM Configuration files

Normally PHP-FPM configuration files are located on /etc/php-fpm.conf file and /etc/php-fpm.d path. This is normally excellent start and all pool configs goes to /etc/php-fpm.d directory. You need to add following include line on your php-fpm.conf file:

PHP-FPM Tip 2. – PHP-FPM Global Configuration Tweaks

Set up emergency_restart_threshold, emergency_restart_interval and process_control_timeout. Default values for these options are totally off, but I think it’s better use these options example like following:

What this mean? So if 10 PHP-FPM child processes exit with SIGSEGV or SIGBUS within 1 minute then PHP-FPM restart automatically. This configuration also sets 10 seconds time limit for child processes to wait for a reaction on signals from master.

PHP-FPM Tip 3. – PHP-FPM Pools Configuration

With PHP-FPM it’s possible to use different pools for different sites and allocate resources very accurately and even use different users and groups for every pool. Following is just example configuration files structure for PHP-FPM pools for three different sites (or actually three different part of same site):

Just example configurations for every pool:
/etc/php-fpm.d/site.conf

/etc/php-fpm.d/blog.conf

/etc/php-fpm.d/forums.conf

So this is just example howto configure multiple different size pools.

PHP-FPM Tip 4. – PHP-FPM Pool Process Manager (pm) Configuration

Best way to use PHP-FPM process manager is use dynamic process management, so PHP-FPM processes are started only when needed. This is almost same style setup than Nginx worker_processes and worker_connections setup. So very high values does not mean necessarily anything good. Every process eat memory and of course if site have very high traffic and server lot’s of memory then higher values are right choise, but servers, like VPS (Virtual Private Servers) memory is normally limited to 256 Mb, 512 Mb, 1024 Mb. This low RAM is enough to handle even very high traffic (even dozens of requests per second), if it’s used wisely.

It’s good to test how many PHP-FPM processes a server could handle easily, first start Nginx and PHP-FPM and load some PHP pages, preferably all of the heaviest pages. Then check memory usage per PHP-FPM process example with Linux top or htop command. Let’s assume that the server has 512 Mb memory and 220 Mb could be used for PHP-FPM, every process use 24 Mb RAM (some huge content management system with plugins can easily use 20-40 Mb / per PHP page request or even more). Then simply calculate the server max_children value:
220 / 24 = 9.17

So good pm.max_children value is 9. This is based just quick average and later this could be something else when you see longer time memory usage / per process. After quick testing it’s much easier to setuppm.start_servers value, pm.min_spare_servers value and pm.max_spare_servers value.

Final example configuration could be following:

Max request per process is unlimited by default, but it’s good to set some low value, like 200 and avoid some memory issues. This style setup could handle large amount of requests, even if the numbers seems to be small.

 

Скомуниздено отсюда: http://www.if-not-true-then-false.com/2011/nginx-and-php-fpm-configuration-and-optimizing-tips-and-tricks/

Далее...

Too many open files

текущее число открытых файлов:
cat /proc/sys/fs/file-nr
показывает:
открытыеоткрытые, но не используемыемаксимально возможное количество открытых

Увеличить максимальное:
vim /etc/sysctl.conf
fs.file-max = 9999999
sysctl -p

Увеличить для mysql соответственно количество файлов и процессов:
vim /etc/security/limits.conf
mysql soft nofile 28192
mysql hard nofile 28192
mysql soft nproc 20240
mysql hard nproc 20240

Далее...

Конвертация файла из cp-1251 в utf-8

iconv -f WINDOWS-1251 -t UTF-8 src_filename > dst_filename

Далее...

Путь до запускаемого скрипта

Для красивого решения проблемы нужно знать всего три вещи:
Путь до выполняющегося скрипта можно узнать с помощью $0, но проблема в том, что он относительный, т.е. если вы запустите скрипт как ./script.sh, то и $0 будет содержать ./script.sh
Команда readlink с параметром -e решит сразу две проблемы: во-первых она вернет полный путь до файла скрипта, если вы воспользовались для запуска символической ссылкой (даже если это была цепочка симлинков), а во-вторых преобразует относительный путь, если такой получен с помощью $0, в абсолютный
Чтобы избавиться от имени файла скрипта в конце абсолютного пути, нужно воспользоваться командой dirname
Пример:

#!/bin/bash

# полный путь до скрипта
ABSOLUTE_FILENAME=`readlink -e "$0"`
# каталог в котором лежит скрипт
DIRECTORY=`dirname "$ABSOLUTE_FILENAME"`

# запуск "соседних" скриптов
$DIRECTORY/script1.sh
$DIRECTORY/script2.sh

в php есть константа __DIR__

Далее...

добавляются лишние символы в ответе сервера nginx

добавляются лишние символы в ответе сервера nginx.
Бэкэндом выступает apache.
Что-то вроде 23af ... 0

Решение:
SetEnv force-response-1.0 1
SetEnv downgrade-1.0 1

BrowserMatch ".*" downgrade-1.0 force-response-1.0

Далее...

Ошибка “child pid XXXXX exit signal Segmentation fault (11)”

Как бороться?
Ставим отладчик:
yum install gdb.x86_64
В добавляем файлик /etc/httpd/conf.d/debug.conf
с таким содержанием:
CoreDumpDirectory /tmp
После чего перечитываем конфиг апача:

/etc/init.d/httpd configtest
/etc/init.d/httpd graceful

Всё. Теперь запускаем чтение файла логов и ждём пока отвалится процесс:
tail -f /var/log/httpd/error_log

Собственно, должно быть что-то вроде такого:
[notice] child pid 15774 exit signal Segmentation fault (11), possible coredump in /tmp

Это значит, что у нас теперь есть "слепок" падения.
Натравим на него наш отладчик и посмотрим, что там есть:
gdb httpd /tmp/core.xxx(PID)

Далее...

VFSTPD и символическая ссылка

Понадобилось через зенд забубенить удаленный проект.

Vsftpd не дает работать с каталогами, подключенными в рабочий с помощью симлинка, поэтому создать проект на удаленном сервере не удавалось (в рабочем каталоге символическая ссылка на myadmin).

Решается так:
Создаем директорию-назначение (DEST)
Монтируем с параметром источник в приемник: mount --bind SOURCE DEST
Если нужно, чтобы "хардлинк" работал после перезагрузки, то добавляем в /etc/fstab строку:
SOURCE DEST none bind 0 0

Далее...

Как вывести звездочку в bash.

Имеем:
* - служебный символ, который используется для вывода всех файлов.

Нужно:
В bash написать скрипт, который делает "SELECT *" из таблицы.
Писать отдельный SQL-файл лень/нельзя.

Проблема:
Работать не будут:

sql="SELECT * FROM table" && echo $sql | mysql
sql="SELECT \* FROM table" && echo $sql | mysql
sql="SELECT [*] FROM table" && echo $sql | mysql
sql="SELECT (*) FROM table" && echo $sql | mysql

Решение - взять в двойные кавычки упоминание переменной $sql:

sql="(SELECT * FROM import.tovars);" && echo "$sql" | mysql

Далее...

Найти и запаковать в архив нужные файлы

Задача: найти файлы, модифицированные после определенной даты и собрать их в одном месте, сохранив структуру каталогов. В папке "./logs" искать не нужно.

Решение:
#find -type f -mtime -22 | grep -v "./logs/" > /tmp/03.01.2013.txt
#tar czf /tmp/update.tar.gz -T /tmp/03.01.2013.txt

Вуа-ля в файлике /tmp/update.tar.gz лежит то, что менялось за последние 22 дня)

Далее...

синхронизация

http://habrahabr.ru/post/132098/

Далее...