针对VPS的LNMP生产环境:CentOS+Nginx+MySQL+PHP架构配置

文章目录
[隐藏]

我们为什么采用LNMP这种架构?

采用Linux、PHP、MySQL的优点我们不必多说。 nginx是一个小巧而高效的Linux下的Web服务器软件,是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru 站点开发的,已经在一些俄罗斯的大型网站上运行多年,相当的稳定。 Nginx相当的稳定、功能丰富、安装配置简单、低系统资源……

LNMP这种架构有什么优势?

  • 作为Web服务器:相比Apache,Nginx使用更少的资源,支持更多的并发连接,体现更高的效率。
  • 作为负载均衡服务器:Nginx既可以在内部直接支持Rails和PHP,也可以支持作为HTTP代理服务器 对外进行服务。Nginx用C编写, 不论是系统资源开销还是CPU使用效率都比Perlbal要好的多。
  • 作为邮件代理服务器:Nginx同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last.fm描述了成功并且美妙的使用经验。

Nginx安装非常的简单,配置文件 非常简洁(还能够支持perl语法),Bugs非常少的服务器。 Nginx启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够在不间断服务的情况下进行软件版本的升级,升级方法可以参考《Nginx平滑升级脚本,实现不间断服务》。

软件下载和系统优化

这里我把需要用到的软件全部放在了百度网盘,下载地址:http://pan.baidu.com/share/link?shareid=155996&uk=587980571,百度网盘的注册地址

系统在安装前进行必要的优化是很有必要的,具体方法参考:《CentOS安装LNMP之前的系统优化和更新事项》。(必须)

LNMP安装过程

本文介绍的CentOS+Nginx+MySQL+PHP架构配置是按照我自己的风格安装,软件安装路径和配置文件可以根据自己需要修改。需要注意的是所有配置文件参数都是基于内存小于1G的机器所设置,低于1G内存的VPS使用最为合适。大内存可自己进行参数调优。

Nginx安装与配置

创建nginx相关目录和用户组并赋予权限。

groupadd www
useradd -g www -s /sbin/nologin www
mkdir -p /data/{/wsdata/wwwroot,wsdata/tools,wslogs}
chmod +w /data/wsdata/wwwroot
chmod 777 /data/wslogs
chown -R www:www /data/wsdata/wwwroot

安装Pcre,为了支持正则。

tar zxvf pcre-8.12.tar.gz
cd pcre-8.12/
./configure
make && make install && cd ..

安装Nginx:

tar zxvf nginx-1.2.6.tar.gz
cd nginx-1.2.6/
./configure \
--user=www \
--group=www \
--prefix=/usr/local/webserver/nginx \
--with-http_stub_status_module \
--with-cc-opt='-O3' \
--with-cpu-opt=opteron \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_gzip_static_module
make && make install && cd ..

Nginx的主配文件

nginx.conf配置文件位于nginx安装目录下conf/nginx.conf,配置参数:

#user options
user www www;
#CPU Core options
#Nginx每个进程耗费10M~12M内存,这里只开启一个Nginx进程,节省内存。
worker_processes 1;
#nginx Process options
pid /usr/local/webserver/nginx/nginx.pid;
#[ debug | info | notice | warn | error | crit ]
error_log /data/wslogs/nginx_error.log crit;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events {
    use epoll;
    #maxclient = worker_processes * worker_connections / cpu_number
    worker_connections 51200;
}
http {
    include    mime.types;
    default_type application/octet-stream;
    #charset gb2312;

    #General options
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;

    #ignore_invalid_headers on;
    #recursive_error_pages on;
    server_name_in_redirect off;
    sendfile on;
    #timeouts
    keepalive_timeout 60;
    #TCP options
    tcp_nopush on;
    tcp_nodelay on;
    #fastcgi options
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    #fastcgi_intercept_errors on;
    #size limits
    client_max_body_size 100m;
    #client_body_buffer_size 256k;
    #gzip  compression
    #对网页文件、CSS、JS、XML等启动gzip压缩,减少数据传输量,提高访问速度。
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/plain text/css application/x-javascript application/xml;
    gzip_vary on;
    #limit_zone  crawler  $binary_remote_addr  10m;
    #virtual hosts options
    include vhosts/*.conf;
}

FastCGI的配置文件enable_fcgi.conf

#PHP PATH_INFO bugs
if ($request_filename ~* (.*)\.php) {
    set $php_url $1;
}
if (!-e $php_url.php) {
    return 403;
}
fastcgi_param        GATEWAY_INTERFACE        CGI/1.1;
fastcgi_param        SERVER_SOFTWARE            nginx;
fastcgi_param        QUERY_STRING            $query_string;
fastcgi_param        REQUEST_METHOD            $request_method;
fastcgi_param        CONTENT_TYPE            $content_type;
fastcgi_param        CONTENT_LENGTH            $content_length;
fastcgi_param        SCRIPT_FILENAME            $document_root$fastcgi_script_name;
fastcgi_param        SCRIPT_NAME            $fastcgi_script_name;
fastcgi_param        REQUEST_URI            $request_uri;
fastcgi_param        DOCUMENT_URI            $document_uri;
fastcgi_param        DOCUMENT_ROOT            $document_root;
fastcgi_param        SERVER_PROTOCOL            $server_protocol;
fastcgi_param        REMOTE_ADDR            $remote_addr;
fastcgi_param        REMOTE_PORT            $remote_port;
fastcgi_param        SERVER_ADDR            $server_addr;
fastcgi_param        SERVER_PORT            $server_port;
fastcgi_param        SERVER_NAME            $server_name;
#for test
fastcgi_param HTTP_ACCEPT_ENCODING gzip,deflate;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

虚拟主机配置文件:
因为我把虚拟主机的配置文件放在了vhosts目录下,所以需要在vhosts下建立相关网站的配置文件,我习惯的方法是每个网站建立一个配置文件,例如1987.name,就建立一个1987.name.conf的配置文件,这样便于管理。这里我是使用我自己博客1987.name的配置文件做为演示。

server {
        listen 80;
        server_name www.1987.name 1987.name;
        access_log off;
        index index.html index.htm index.php;
        root /data/wsdata/wwwroot/1987/www;
        if ($host !~ "^www\.1987\.name$") {
                rewrite ^(.*) http://www.1987.name$1 permanent;  #域名重定向,访问1987.name会跳转到www.1987.name。
        }
        if (-d $request_filename) {
                rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;   #在域名结尾自动添加斜线/。
        }
        location ~ .*\.(php|php5)?$ {
                fastcgi_pass  unix:/tmp/php-cgi.sock;   #将Nginx与FastCGI的通信方式由TCP改为Unix Socket。TCP在高并发访问下比Unix Socket稳定,但Unix Socket速度要比TCP快。
                #fastcgi_pass   127.0.0.1:9000;
                fastcgi_index   index.php;
                include enable_fcgi.conf;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {    #设置这些文件在浏览器本地缓存30天,可以提高下次打开我博客的页面加载速度。
                access_log      off;
                expires 30d;
        }
        location ~ .*\.(js|css)$ {    #设置样式文件在本地浏览器缓存时间为1点,也是为了提高下一次的访问速度。
                access_log      off;
                expires 1d;
        }
        location / {      #这里是wordpress的伪静态规则。
                if (-f $request_filename/index.html){
                        rewrite (.*) $1/index.html break;
                }
                if (-f $request_filename/index.php){
                        rewrite (.*) $1/index.php;
                }
                if (!-f $request_filename){
                        rewrite (.*) /index.php;
                }
        }   #将网站根目录下docs目录设置为目录样式。
        location /docs {
                autoindex on;
        }
}

MySQL安装与配置

创建目录和用户:

groupadd mysql
useradd -g mysql -s /sbin/nologin mysql
mkdir -p /data/mysqldata/{database,log,pid,binlog}
mkdir -p /usr/local/webserver/mysql/
chmod +w /usr/local/webserver/mysql/
chown -R mysql:mysql /data/mysqldata/*

安装MySQL:

是使用的是mysql 5.1.X,如果使用mysql 5.5.X编译参数会大有不同,这里需要注意。

tar zxvf mysql-5.1.66.tar.gz && cd mysql-5.1.66
./configure \
--prefix=/usr/local/webserver/mysql/ \
--without-debug \
--enable-assembler \
--enable-thread-safe-client \
--enable-local-infile \
--with-client-ldflags=-all-static \
--with-mysqld-ldflags=-all-static \
--with-extra-charsets=complex \
--with-big-tables \
--with-readline \
--with-ssl \
--with-embedded-server \
--with-plugins=partition,innobase,myisammrg
make && make install

mysql编译参数解释:

--prefix=/usr/local/webserver/mysql/         指定安装路径
--without-debug                              去除debug信息
--enable-assembler                           允许使用汇编模式(优化性能)
--enable-thread-safe-client                  指定线程安全模式
--enable-local-infile                        指定允许本地导入文件
--with-client-ldflags=-all-static            静态编译
--with-mysqld-ldflags=-all-static            静态编译,提升性能13%
--with-extra-charsets=complex                安装所有字符
--with-big-tables                            指定支持超大表
--with-readline                              通过readline,可以方便的在命令行上面移动,增删,复制,粘贴,搜索。
--with-ssl                                   支持https安全连接
--with-embedded-server                       编译成embedded MySQL library
--with-plugins=partition,innobase,myisammrg  指定安装的存储引擎

mysql主配文件my.cnf
参数太多我也不一一解释,可以去网上搜索了解。这里的参数配置只适用于内存在512~1G的VPS服务器。

# This is for a large system with memory of 256M where the system runs mainly
[client]
port = 3306
socket = /tmp/mysql.sock

[mysqld]
user = mysql
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/webserver/mysql
datadir = /data/mysqldata/database
pid-file = /data/mysqldata/pid/mysql.pid
log-error = /data/mysqldata/log/mysql_error.log

#character-set-server = utf8
skip-locking

#skip-name-resolve
#skip-networking
#log-bin = /data/mysqldata/binlog/mysql-bin
#binlog_format = mixed
#binlog_cache_size = 4M
#max_binlog_cache_size = 8M
#max_binlog_size = 1G
#expire_logs_days = 90
#binlog-ignore-db = mysql
#binlog-ignore-db = test
#binlog-ignore-db = information_schema

#4G memory=256~384M
key_buffer_size = 8M
sort_buffer_size = 512K
read_buffer_size = 2M

#16G memory=16M
read_rnd_buffer_size = 4M

#16G memory=2M
join_buffer_size = 256K
thread_cache_size = 10
query_cache_size = 1M
query_cache_limit = 2M
query_cache_min_res_unit = 4k

#CPU's*2
thread_concurrency = 4

table_cache = 720
table_open_cache = 720

#max_connections*5 / max_connections + table_cache*2
open_files_limit = 1000
back_log = 80
max_connections = 100
max_connect_errors = 200
external-locking = FALSE

max_allowed_packet = 16M
default-storage-engine = MyISAM
thread_stack = 192K
transaction_isolation = READ-COMMITTED

#16G memory=256M
tmp_table_size = 4M

#16G memory=512M
max_heap_table_size = 32M

#16G memory=64M
bulk_insert_buffer_size = 4M

#16G memory=64M
myisam_sort_buffer_size = 8M

myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover

#interactive_timeout = 120
#wait_timeout = 120

#long_query_time = 2
#log-slow-queries = /data/mysqldata/log/slow.log

server-id = 1

#innodb_additional_mem_pool_size = 16M
#innodb_buffer_pool_size = 512M
#innodb_data_file_path = ibdata1:256M:autoextend
#innodb_file_io_threads = 4
#innodb_thread_concurrency = 8
#innodb_flush_log_at_trx_commit = 2
#innodb_log_buffer_size = 16M
#innodb_log_file_size = 128M
#innodb_log_files_in_group = 3
#innodb_max_dirty_pages_pct = 90
#innodb_lock_wait_timeout = 120
#innodb_file_per_table = 0

[mysqldump]
quick
max_allowed_packet = 32M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

#[myisamchk]
#key_buffer_size = 256M
#sort_buffer_size = 256M
#read_buffer = 2M
#write_buffer = 2M

#[mysqlhotcopy]
#interactive-timeout

my.cnf文件放在/etc/目录下。

初始化mysql数据库:

/usr/local/webserver/mysql/bin/mysql_install_db \
--basedir=/usr/local/webserver/mysql \
--datadir=/data/mysqldata/database \
--user=mysql
cd ..

启动mysql并设置root的密码:

/usr/local/webserver/mysql/bin/mysqld_safe --user=mysql &
/usr/local/webserver/mysql/bin/mysqladmin -u root password '123456'

PHP的安装和配置

首先需要安装PHP相关的库文件。

#libiconv
tar zxvf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1/
./configure --prefix=/usr/local
make && make install && cd ..

#libmcrypt
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure && make && make install
cd libltdl/
./configure --enable-ltdl-install
make && make install && cd ../..

#mhash
tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make && make install && cd ..

ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1

#mcrypt
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure --prefix=/usr
make && make install && cd ..

这里有一个脚本需要链接PHP相关库文件,有32位和64位区分。可以使用脚本执行,也可以手动修改。

if [ `getconf WORD_BIT` = '32' ] && [ `getconf LONG_BIT` = '64' ] ; then
    ln -s /usr/lib64/libpng.* /usr/lib/
    ln -s /usr/lib64/libjpeg.* /usr/lib/
fi

if [ ! `grep -l "/lib" '/etc/ld.so.conf'` ]; then
    echo "/lib" >> /etc/ld.so.conf
fi

if [ ! `grep -l '/usr/lib' '/etc/ld.so.conf'` ]; then
    echo "/usr/lib" >> /etc/ld.so.conf
fi

if [ -d "/usr/lib64" ] && [ ! `grep -l '/usr/lib64' '/etc/ld.so.conf'` ]; then
    echo "/usr/lib64" >> /etc/ld.so.conf
fi

if [ ! `grep -l '/usr/local/lib' '/etc/ld.so.conf'` ]; then
    echo "/usr/local/lib" >> /etc/ld.so.conf
fi

ldconfig

安装Autoconf

tar zxvf autoconf-2.13.tar.gz
cd autoconf-2.13/
./configure --prefix=/usr/local/autoconf-2.13
make && make install && cd ..

安装PHP

以下的安装中我给php5.2.17打了补丁。

export PHP_AUTOCONF=/usr/local/autoconf-2.13/bin/autoconf
export PHP_AUTOHEADER=/usr/local/autoconf-2.13/bin/autoheader
tar zxvf php-5.2.17.tar.gz
gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1
cd php-5.2.17/
mv ../php-5.2.17-max-input-vars.patch ./php-5.2.17-max-input-vars.patch
patch -p1 < php-5.2.17-max-input-vars.patch
./buildconf --force
./configure \
--prefix=/usr/local/webserver/php \
--with-config-file-path=/usr/local/webserver/php/etc \
--with-mysql=/usr/local/webserver/mysql \
--with-mysqli=/usr/local/webserver/mysql/bin/mysql_config \
--with-gd \
--with-jpeg-dir \
--with-iconv-dir=/usr/local \
--with-libxml-dir=/usr \
--with-zlib \
--with-png-dir \
--with-curl \
--with-ldap \
--with-xmlrpc \
--with-openssl \
--with-mhash \
--with-mcrypt \
--with-freetype-dir \
--with-ldap-sasl \
--with-curlwrappers \
--enable-xml \
--enable-shared \
--enable-discard-path \
--enable-safe-mode \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-fastcgi \
--enable-fpm \
--enable-force-cgi-redirect \
--enable-mbstring=all  \
--enable-gd-native-ttf \
--enable-pcntl \
--enable-sockets \
--enable-zip \
--enable-soap
make ZEND_EXTRA_LIBS='-liconv' && make install && cd ..

PHP编译安装参数解释:

--prefix=/usr/local/webserver/php                            安装路径
--with-config-file-path=/usr/local/webserver/php/etc         指定php.ini位置
--with-mysql=/usr/local/webserver/mysql                      mysql安装目录,对mysql的支持
--with-mysqli=/usr/local/webserver/mysql/bin/mysql_config    mysqli文件目录,优化支持
--with-gd                                                    打开gd库的支持
--with-jpeg-dir                                              打开对jpeg图片的支持
--with-iconv-dir=/usr/local                                  iconv函数,对字符集间的转换
--with-libxml-dir=/usr                                       打开libxml2库的支持
--with-zlib                                                  打开zlib库的支持
--with-png-dir                                               打开对png图片的支持
--with-curl                                                  打开curl浏览工具的支持
--with-ldap                                                  打开对LDAP的支持
--with-xmlrpc                                                打开xml-rpc的C语言
--with-openssl                                               openssl的支持,加密传输时用到的
--with-mhash                                                 算法
--with-mcrypt                                                算法
--with-freetype-dir                                          打开对freetype字体库的支持
--with-ldap-sasl                                             对Cyrus SASL的支持
--with-curlwrappers                                          运用curl工具打开url流
--enable-xml                                                 打开XML的支持
--enable-shared                                              编译后会链接成共享对象
--enable-discard-path                                        打开这个选项,使用者就不能透过浏览器读取.htaccess等和系统安全相关的档案
--enable-safe-mode                                           打开安全模式
--enable-bcmath                                              打开图片大小调整,用到zabbix监控的时候用到了这个模块
--enable-shmop                                               共享内存
--enable-sysvsem                                             启用System V semaphore支持
--enable-inline-optimization                                 优化线程
--enable-mbregex                                             打开对mbregex的支持
--enable-fastcgi                                             fastcgi支持
--enable-fpm
--enable-force-cgi-redirect                                  此编译选项可以防止任何人通过如 http://my.host/cgi-bin/php/secretdir/script.php这样的URL直接调用PHP
--enable-mbstring=all
--enable-gd-native-ttf                                       支持TrueType字符串函数库
--enable-pcntl                                               freeTDS需要用到的,可能是链接mssql才用到
--enable-sockets                                             打开sockets支持
--enable-zip                                                 打开对zip的支持
--enable-soap                                                打开SOAP

安装PHP扩展程序

#memcache
tar zxvf memcache-2.2.6.tgz
cd memcache-2.2.6/
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
make && make install && cd ..

#PDO_MYSQL
tar zxvf PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2/
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config --with-pdo-mysql=/usr/local/webserver/mysql
make && make install && cd ..

#创建eAccelerator缓存目录
mkdir -p /usr/local/webserver/eaccelerator_cache

#eaccelerator
tar jxvf eaccelerator-0.9.6.1.tar.bz2
cd eaccelerator-0.9.6.1/
/usr/local/webserver/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/webserver/php/bin/php-config
make && make install && cd ..

#ImageMagick
tar zxvf ImageMagick-6.7.1-6.tar.gz
cd ImageMagick-6.7.1-6/
./configure --prefix=/usr/local/imagemagick
make && make install && cd ..

#imagick
tar zxvf imagick-3.0.1.tgz
cd imagick-3.0.1
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config --with-imagick=/usr/local/imagemagick
make && make install && cd ..

PHP的配置文件:

php.ini文件目录在PHP安装目录下etc/php.ini,配置方法如下。

php.ini中查找 extension_dir = "./",修改为:extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"

并在这一行的下面添加:

extension = "memcache.so"
extension = "pdo_mysql.so"
extension = "imagick.so"

再查找output_buffering = Off,修改为output_buffering = On

再查找; cgi.fix_pathinfo=0,修改为cgi.fix_pathinfo=0,防止Nginx文件类型错误解析漏洞。

在文件结尾处添加下面内容,红色区域是eAcelerator的优化。只使用16M共享内存,删除所有在最后3600秒内无法存取的脚本缓存,用磁盘辅助进行缓存。内存低于1G的VPS可以添加或修改这些地方:

[eaccelerator]
eaccelerator.allowed_admin_path="/data/wsdata/wwwroot"
zend_extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/usr/local/webserver/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
eaccelerator.keys = "disk_only"
eaccelerator.sessions = "disk_only"
eaccelerator.content = "disk_only"

php-fpm.conf文件配置
位于PHP安装目录下etc/php-fpm.conf,如果安装Nginx+PHP用于程序调试,<value name="display_errors">0</value>改为<value name="display_errors">1</value>,可以显示PHP的错误信息,否则Nginx 会报状态为500的空白错误页。

<?xml version="1.0" ?>
<configuration>
    All relative paths in this config are relative to php's install prefix
    <section name="global_options">
        Pid file
        <value name="pid_file">/usr/local/webserver/php/logs/php-fpm.pid</value>
        Error log file
        <value name="error_log">/usr/local/webserver/php/logs/php-fpm.log</value>
        Log level
        <value name="log_level">notice</value>
        When this amount of php processes exited with SIGSEGV or SIGBUS ...
        <value name="emergency_restart_threshold">10</value>
        ... in a less than this interval of time, a graceful restart will be initiated.
        Useful to work around accidental curruptions in accelerator's shared memory.
        <value name="emergency_restart_interval">1m</value>
        Time limit on waiting child's reaction on signals from master
        <value name="process_control_timeout">5s</value>
        Set to 'no' to debug fpm
        <value name="daemonize">yes</value>
    </section>
    <workers>
        <section name="pool">
            Name of pool. Used in logs and stats.
            <value name="name">default</value>
            Address to accept fastcgi requests on.
            Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
            <value name="listen_address">/tmp/php-cgi.sock</value>
            <value name="listen_options">
                Set listen(2) backlog
                <value name="backlog">-1</value>
                Set permissions for unix socket, if one used.
                In Linux read/write permissions must be set in order to allow connections from web server.
                Many BSD-derrived systems allow connections regardless of permissions.
                <value name="owner"></value>
                <value name="group"></value>
                <value name="mode">0666</value>
            </value>
            Additional php.ini defines, specific to this pool of workers.
            <value name="php_defines">
            <value name="sendmail_path">/usr/sbin/sendmail.postfix -t</value>
            <value name="display_errors">1</value>
            </value>
            Unix user of processes
            <value name="user">www</value>
            Unix group of processes
            <value name="group">www</value>
            Process manager settings
            <value name="pm">
                Sets style of controling worker process count.
                Valid values are 'static' and 'apache-like'
                <value name="style">static</value>
                Sets the limit on the number of simultaneous requests that will be served.
                Equivalent to Apache MaxClients directive.
                Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi
                Used with any pm_style.
                <value name="max_children">8</value>
                Settings group for 'apache-like' pm style
                <value name="apache_like">
                    Sets the number of server processes created on startup.
                    Used only when 'apache-like' pm_style is selected
                    <value name="StartServers">20</value>
                    Sets the desired minimum number of idle server processes.
                    Used only when 'apache-like' pm_style is selected
                    <value name="MinSpareServers">5</value>
                    Sets the desired maximum number of idle server processes.
                    Used only when 'apache-like' pm_style is selected
                    <value name="MaxSpareServers">35</value>
                </value>
            </value>
            The timeout (in seconds) for serving a single request after which the worker process will be terminated
            Should be used when 'max_execution_time' ini option does not stop script execution for some reason
            '0s' means 'off'
            <value name="request_terminate_timeout">0s</value>
            The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file
            '0s' means 'off'
            <value name="request_slowlog_timeout">0s</value>
            The log file for slow requests
            <value name="slowlog">logs/slow.log</value>
            Set open file desc rlimit
            <value name="rlimit_files">51200</value>
            Set max core size rlimit
            <value name="rlimit_core">0</value>
            Chroot to this directory at the start, absolute path
            <value name="chroot"></value>
            Chdir to this directory at the start, absolute path
            <value name="chdir"></value>
            Redirect workers' stdout and stderr into main error log.
            If not set, they will be redirected to /dev/null, according to FastCGI specs
            <value name="catch_workers_output">yes</value>
            How much requests each process should execute before respawn.
            Useful to work around memory leaks in 3rd party libraries.
            For endless request processing please specify 0
            Equivalent to PHP_FCGI_MAX_REQUESTS
            <value name="max_requests">1024</value>
            Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.
            Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)
            Makes sense only with AF_INET listening socket.
            <value name="allowed_clients">127.0.0.1</value>
            Pass environment variables like LD_LIBRARY_PATH
            All $VARIABLEs are taken from current environment
            <value name="environment">
                <value name="HOSTNAME">$HOSTNAME</value>
                <value name="PATH">/usr/local/bin:/usr/bin:/bin</value>
                <value name="TMP">/tmp</value>
                <value name="TMPDIR">/tmp</value>
                <value name="TEMP">/tmp</value>
                <value name="OSTYPE">$OSTYPE</value>
                <value name="MACHTYPE">$MACHTYPE</value>
                <value name="MALLOC_CHECK_">2</value>
            </value>
        </section>
    </workers>
</configuration>

需要注意的地方:

  • 针对小内存:<value name="max_children">8</value>,将启动的php-cgi进程数改为8个。
  • 替换sendmail为postfix:<value name="sendmail_path">/usr/sbin/sendmail.postfix -t</value>
  • Nginx中配置了使用Unix Socket模式,这里需要对应修改:<value name="listen_address">/tmp/php-cgi.sock</value>

启动Nginx和PHP

/usr/local/webserver/php/sbin/php-fpm start
/usr/local/webserver/nginx/sbin/nginx

暂无评论

写评论

您必须登录后才可以发表评论。

本人从事电脑网络维护工作多年,对电脑配置较为熟悉,闲暇之余精心搜罗了目前主流电脑配置清单推荐给大家。紧跟市场动态,定期更新电脑硬件配置方案,扫除一切垃圾坑爹配置,提供最具性价比的电脑配置清单!本站涵盖内容包括:电脑组装机配置单、电脑组装机报价、电脑组装机教程等,另外还提供很多技术文章供您学习!