nginx 3.nginx+fastcgi

死磕nginx 3.nginx+fastcgi

互联网服务器有个非常典型的架构lamp(linux+apache+mysql+php),由于其开源和强大的兼容性而风靡一时,不过随着nginx的横空出世,lamp逐渐被lnmp替代,即Linux+nginx+mysql+php

fastcgi:fastcgi是语言无关的,可伸缩架构的CGI开放扩展,其主要的行为将CGI解释器进程保持在内存中并因此获得较高的性能,众所周知,CGI的反复加载时CGI性能低下的主要原因,如果CGI解释器保持在内存中并接受FastCGI进程金鼎管理调度,则可提供良好的性能、伸缩性、Fail-Over特性等。


1、FastCGI的工作原理

  1. FastCGI进程管理器自身初始化,启动多个CGI解释器进程(多个php-cgi进程)并等待来自webserver的请求,在本文中,采用PHP-FPM进程管理器启动多个php-cgi FastCGI进程,启动php-cgi FastCGI进程时,可以配置TCP和UNIX套接字两种方式启动
  2. 当客户端请求到达web服务器(nginx)时,web服务器将采用TCP协议或者UNIX套接字方式转到FastCGI主进程,FastCGI主进程选择并连接到一个CGI解释器(子进程),web服务器将CGI环境变量和标准输入发送到FastCGI子进程php-cgi
  3. FastCGI子进程完成处理后将标准输出和错误信息从同一连接返回到web服务器(nginx),当FastCGI子进程关闭连接时,请求便告知处理完成,。FastCGI子进程接着等待来自FastCGI管理器的下一个连接,而在一般普通的CGI模式中,php-cgi在此时便退出了。

所以你可以想象普通的CGI模式有多慢,每一个web请求PHP都必须重新解析php.ini、重新载入全部扩展并重新初始化全部数据结构。使用FastCGI,所有的这些都只在进程启动时发生一次,一个额外的好处是,持续数据库连接可以工作。

php FastCGI的优点

  • PHP脚本运行速度更快,PHP解释程序被载入内存而不用每次都需要从存储器中读取,此举极大提升了依靠脚本运行站点的性能。
  • 需要使用的系统资源更少,由于服务器不用每次都需要载入PHP解释程序,你可以将站点传输速度提升而不必增加CPU负担。
  • 不需要对现有代码进行任何改变

部署lnmp环境示例

组件版本

linux:centos6.5

php:5.5

nginx:1.8

mysql:5.5.18

nginx的安装

可以参考nginx的第一篇文章

mysql的安装

1、依赖
yum -y install cmake gcc-c++ make bison-devel ncurses-devel
增加用户
useradd mysql
2、解压安装 安装包自行下载
tar -zxf mysql-5.5.18.tar.gz
cd mysql-5.5.18
cmake \
-DCMAKE_INSTALL_PREFIX=/home/nick/mysql \
-DMYSQL_UNIX_ADDR=/home/nick/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS:STRING=utf8,gbk \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_DATADIR=/home/nick/mysql/data
make && make install cd /home/nick/mysql && mkdir etc tmp var log 3、重命名系统自带的my.cnf
mv /etc/my.cnf /etc/my.cnf.bak 4、配置文件 复制到/home/nick/mysql/etc/my.cnf里面 [client]
port = 3306
socket = /home/nick/mysql/mysql.sock
default-character-set = utf8mb4 [mysqld]
port = 3306
socket = /home/nick/mysql/mysql.sock basedir = /home/nick/mysql
datadir = /home/nick/mysql/data
pid-file = /home/nick/mysql/log/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1 init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4 #skip-name-resolve
#skip-networking
back_log = 300 max_connections = 100
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 1M
binlog_cache_size = 1M
max_heap_table_size = 2M
tmp_table_size = 4M read_buffer_size = 1M
read_rnd_buffer_size = 2M
sort_buffer_size = 2M
join_buffer_size = 2M
key_buffer_size = 1M thread_cache_size = 8 query_cache_type = 1
query_cache_size = 2M
query_cache_limit = 1M ft_min_word_len = 4 log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 30 log_error = /home/nick/mysql/log/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /home/nick/mysql/log/mysql-slow.log performance_schema = 0
explicit_defaults_for_timestamp #lower_case_table_names = 1 skip-external-locking default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 16M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 1M
innodb_log_file_size = 8M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120 bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 1G
myisam_repair_threads = 1 interactive_timeout = 28800
wait_timeout = 28800 [mysqldump]
quick
max_allowed_packet = 8M 5、初始化数据库
/home/nick/mysql/scripts/mysql_install_db 6、启动数据库
home/qfpay/mysql/bin/mysqld_safe & 7、supervisr的启动方式
[program:mysql]
command= /home/nick/mysql/bin/mysqld --defaults-file=/home/nick/mysql/etc/my.cnf
directory=/home/nick/mysql
user=nick

PHP的安装

 下载程序
wget wget http://cn2.php.net/get/php-5.5.33.tar.gz
先装第三方源
yum install epel-release
安装依赖
yum install -y gd gd-devel libmcrypt libmcrypt-devel libtool-ltdl libtool-ltdl-devel libxml2 libxml2-devel curl curl-devel t1lib t1lib-devel openssl openssl-devel
解压安装
/configure \
--disable-debug \
--prefix=/usr/local/php5.5 \
--enable-shmop \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-libxml-dir \
--with-t1lib \
--with-zlib \
--with-mcrypt \
--with-mysqli=mysqlnd \
--with-mysql=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-sockets \
--with-iconv \
--enable-mbstring \
--enable-mbregex \
--enable-ftp \
--enable-gd-native-ttf \
--with-curl \
--enable-fpm \
--enable-pcntl \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-zip \
--with-freetype-dir \
--enable-exif \
--with-openssl \
--with-pear \ make && make install 拷贝php.ini:
cp php.ini-development /usr/local/php/lib/php.ini #php-fpm 的简单配置 ;this configuration is for PHP 5.5 for heavy loaded servers [global]
error_log = syslog
log_level = error
rlimit_files = 655360
rlimit_core = 0 [php-nick219]
user = nobody
group = nobody
#使用tcp
listen = 127.0.0.1:9000
#使用sock
#listen = fpm.sock
#listen.allowed_clients = 127.0.0.1
#listen.mode = 666 pm = dynamic
pm.max_children = 1000
pm.start_servers = 20 ;20
pm.min_spare_servers = 10 ;10
pm.max_spare_servers = 30 ;30
pm.max_requests = 500
; system max load (request number) = max_children * max_requests = 500,000 pm.status_path = /fpm_status
ping.path = /status
ping.response = ok slowlog = /data/phplogs/phpfpm_slow.log
request_slowlog_timeout = 3s
listen.backlog = 4096 request_terminate_timeout = 5m rlimit_files = 655360
rlimit_core = 0 security.limit_extensions = .php .html php_admin_value['date.timezone'] = 'Asia/Shanghai' ;include rigger ENV files
include=env.conf ;add conf overrides from yaml

nginx配置

一个简单的配置,直接从server[...]开始

server
{
listen 80;
server_name nick219.com;
index index.html index.htm index.php;
root /home/nick/www/nick219; location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf
}
#下面可以配置日志,缓存或者其它位置
}

nginx+fastcgi的配置结束

上一篇:解决Nginx: [error] open() "/usr/local/Nginx/logs/Nginx.pid


下一篇:C#闪屏