文章内内容均参考网络上各类文章后自行整理,感谢原文作者的分享.
OS环境CentOS release 6.6
一,安装配置nginx
下载目前nginx最新的稳定版本nginx-1.8.0
# wget http://nginx.org/download/nginx-1.8.0.tar.gz
解压编译安装
# tar -xvf nginx-1.8.0.tar.gz# cd nginx-1.8.0
# ./configure --prefix=/usr/local/nginx --with-http_realip_module --with-http_sub_module \
--with-http_gzip_static_module --with-http_stub_status_module --with-pcre
# make && make install
如果提示错误找不到pcre就用yum装一下
# yum install pcre-devel -y
建立nginx用户
# useradd -M -s /sbin/nologin nginx
修改nginx默认配置文件,有一些为新添
# vim /usr/local/nginx/conf/nginx.conf
#运行用户 user nginx; #运行进程数,一般设置成和cpu的数量相等 worker_processes 1; #开启全局错误日志及存放路径 error_log logs/error.log; #pid文件路径 pid logs/nginx.pid; #设定工作模式 events { #epoll是多路复用IO(I/O Multiplexing)中的一种方式,仅用于linux2.6以上内核,可大大提高nginx的性能 use epoll; #最大连接数 worker_connections 1024; } #设定http服务器 http { #设定mime类型,类型由mime.type文件定义 include 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 logs/access.log; #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用, #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off #以平衡磁盘与网络I/O处理速度,降低系统的uptime. sendfile on; #设定连接超时时间,单位秒 keepalive_timeout 65; #gzip压缩开关 #gzip on; #设定负载均衡的服务器列表,weight为权重,数字越大,被分配的几率越大,此处仅设为本地php服务 upstream php_processes { server 127.0.0.1:9000 weight=1; server 127.0.0.1:9001 weight=1; } #虚拟主机设定,一个server代表一个主机 server { #http服务的端口,自行设定 listen 80; #设定访问域名 server_name localhost; #本虚拟主机内的访问日志记录 access_log logs/host.access.log; #默认主目录和主索引文件 root /var/www/html; index index.php index.html index.htm; #默认http请求根目录,location类似虚拟目录的概念 location / { #设定PHP文件的处理对象 location ~ \.php$ { root /var/www/html; try_files $uri =404; #fastcgi_pass可写单个IP,亦可用upstream名定义一个集群池 fastcgi_pass php_processes; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } #设定静态文件处理的配置 location ~* \.(?:css|gif|htc|ico|js|jpe?g|png|swf)$ { expires 30d; log_not_found off; tcp_nodelay off; open_file_cache max=1000 inactive=120s; open_file_cache_valid 45s; open_file_cache_min_uses 2; open_file_cache_errors off; } #设定一些比较大文件类型的缓存时间 location ~* ^.+\.(?:ogg|pdf|pptx?)$ { expires 30d; tcp_nodelay off; } } #最外层location结束 #定义错误提示页面 error_page 404 500 502 503 504 /50x.html; location = /50x.html { root /root; } }#一个server配置结束 }#http结束
配置文件修改后,运行nginx命令查看是否有错误
# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
输出正确信息后启动服务
# /usr/local/nginx/sbin/nginx
关闭服务命令,-s 后可加stop quit reopen reload
# /usr/local/nginx/sbin/nginx -s stop
添加开机启动
# echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local
随后在/var/www/html下新建个index.html,随意写点东西,然后打开http://localhost查看
二,编译安装php-fpm
编译过程中缺少的包用yum provides搜索后安装,不一一记录
# wget http://cn2.php.net/get/php-5.6.15.tar.bz2/from/this/mirror -O phpphp-5.6.15.tar.bz2
# tar -xvf phpphp-5.6.15.tar.bz2
# cd phpphp-5.6.15.tar.bz2
# ./configure \
--prefix=/usr/local/php5.6 \
--with-config-file-path=/usr/local/php5.6/etc \
--enable-fpm \
--with-mysql=shared,mysqlnd \
--with-fpm-user=php-fpm \
--with-fpm-group=php-fpm \
--with-libxml-dir \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-zlib-dir \
--with-mysql=shared,mysqlnd \
--enable-sockets=shared \
--enable-soap \
--enable-gd-native-ttf \
--enable-ftp \
--enable-mbstring \
--enable-exif \
--with-pear \
--with-curl \
--with-openssl
# make && make install
建立用户,拷贝两份配置文件,因为将准备启动两个不同端口的php-fpm进程
# useradd -M -s /sbin/nologin php-fpm
# cd /usr/local/php5.6
# cp etc/php-fpm.conf.default etc/php-fpm-9000.conf
# cp etc/php-fpm.conf.default etc/php-fpm-9001.conf
其中一份配置文件内容如下,两份配置文件端口号需配置不同,日志可以写同一个
# cat php-fpm-9000.conf |grep "^[^;]" -n
21:[global] ;指定该配置文件启动的进程的pid文件位置 25:pid = run/php-fpm-9000.pid ;指定错误日志位置 32:error_log = log/php-fpm-9000.log ;错误级别. 可用级别为: alert(必须立即处理), error(错误情况), warning(警告情况) ;notice(一般重要信息), debug(调试信息). 默认: notice. 50:log_level = notice ;表示在emergency_restart_interval所设值内出现SIGSEGV或者SIGBUS错误的php-cgi进程数如果超过 ;emergency_restart_threshold个,php-fpm就会优雅重启。这两个选项一般保持默认值。 56:emergency_restart_threshold = 60 64:emergency_restart_interval = 60 ;设置子进程接受主进程复用信号的超时时间. 可用单位: s(秒), m(分), h(小时), 或者 d(天) 默认;单位: s(秒). 默认值: 0. 70:process_control_timeout = 0 ;后台执行fpm,默认值为yes,如果为了调试可以改为no。在FPM中,可以使用不同的设置来运行多个进程池。 ;这些设置可以针对每个进程池单独设置。 89:daemonize = yes 130:[www] ;指定运行用户和组 149:user = php-fpm 150:group = php-fpm ;指定监听地址和端口 164:listen = 127.0.0.1:9000 189:listen.allowed_clients = 127.0.0.1 ;如何控制子进程,选项有static和dynamic。如果选择static,则由pm.max_children指定固定的子进程数。 ;如果选择dynamic,则由下面参数决定 223:pm = dynamic ;子进程最大数 234:pm.max_children = 5 ;启动时的进程数 239:pm.start_servers = 2 ;保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程 244:pm.min_spare_servers = 1 ;保证空闲进程数最大值,如果空闲进程大于此值,此进行清理 249:pm.max_spare_servers = 3 ;设置每个子进程重生之前服务的请求数. 对于可能存在内存泄漏的第三方模块来说是非常有用的. ;如果设置为 '0' 则一直接受请求. 等同于 PHP_FCGI_MAX_REQUESTS 环境变量. 默认值: 0. 260:pm.max_requests = 500 ;FPM监控页面的ping网址. 如果没有设置, 则无法访问ping页面. 该页面用于外部检测FPM是否存活并且可以响应请求. ;请注意必须以斜线开头 (/)。 371:ping.path = /ping ;用于定义ping请求的返回相应. 返回为 HTTP 200 的 text/plain 格式文本. 默认值: pong. 376:ping.response = pong ;慢请求的记录日志,配合request_slowlog_timeout使用 440:slowlog = var/log/$pool.log.slow ;当一个请求该设置的超时时间后,就会将对应的PHP调用堆栈信息完整写入到慢日志中 ;设置为 '0' 表示 'Off' 446:request_slowlog_timeout = 10 ;设置单个请求的超时中止时间.该选项可能会对php.ini设置中的'max_execution_time'因为某些特殊 ;原因没有中止运行的脚本有用.设置为 '0' 表示 'Off'.当经常出现502错误时可以尝试更改此选项 453:request_terminate_timeout = 0 ;设置文件打开描述符的rlimit限制. 默认值: 系统定义值默认可打开句柄是1024 ;可使用 ulimit -n查看,ulimit -n 2048修改。 457:rlimit_files = 1024 ;设置核心rlimit最大限制值. 可用值: ‘unlimited' 、0或者正整数. 默认值: 系统定义值 462:rlimit_core = 0 ;重定向运行过程中的stdout和stderr到主要的错误日志文件中. 如果没有设置 ;stdout 和 stderr 将会根据FastCGI的规则被重定向到 /dev/null . 默认值: 空. 485:catch_workers_output = yes
测试配置文件是否正确
# /usr/local/php5.6/sbin/php-fpm -t -y etc/php-fpm-9000.conf
NOTICE: configuration file etc/php-fpm-9000.conf test is successful
分别启动php-fpm两个服务进程
# /usr/local/php5.6/sbin/php-fpm -y etc/php-fpm-9000.conf
# /usr/local/php5.6/sbin/php-fpm -y etc/php-fpm-9001.conf
# netstat -nlp |grep fpm
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 29925/php-fpm
tcp 0 0 127.0.0.1:9001 0.0.0.0:* LISTEN 29938/php-fpm
关闭服务的方法
# killall php-fpm
或单个获取pid关闭
# cat var/run/php-fpm-9000.pid | xargs kill
测试php
在/var/www/html下新建个phpinfo.php,写入
<?php
phpinfo();
?>
修改页面文件权限
# chmod 777 /var/www/html -R
# 然后打开http://localhost查看,显示php配置信息,配置完成
转载于:https://blog.51cto.com/nextsky/1714338