Debian系统下如何为PHP程序配置Nginx服务器

48次阅读
没有评论

共计 8594 个字符,预计需要花费 22 分钟才能阅读完成。

这篇文章主要介绍了 Debian 系统下如何为 PHP 程序配置 Nginx 服务器的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇 Debian 系统下如何为 PHP 程序配置 Nginx 服务器文章都会有所收获,下面我们一起来看看吧。

nginx 安装方法:

1 apt 网络安装

修改源列表

vi /etc/apt/sources.list
#加入以下源
deb http://nginx.org/packages/debian/ squeeze nginx
deb-src http://nginx.org/packages/debian/ squeeze nginx

更新源列表并通过 apt 安装 nginx

apt-key add nginx_signing.key
apt-get update
apt-get install nginx

2 源码编译安装
先安装编译环境,由于 nginx 在以后的使用中会需要用到 perl 正则、压缩算法、ssl 等特性,所以我们需要提前安装相关库文件。

apt-get install build-essential 
apt-get install libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl libssl-dev libssl0.9.8

下载最新的稳定版 nginx

解压查看编译选项

tar zxvf nginx-1.2.3.tar.gz
cd nginx-1.2.3
#--help 可以看到可以配置的参数
./configure --help

查看编译可选的配置参数(以下只是一些常用的配置项):

–prefix=path  nginx 的默认安装路径,没有指定的话。默认为 /usr/local/nginx

–sbin-path=path  nginx 可执行命令文件的路径,没有指定的话,默认 prefix /sbin/nginx

–conf-path=path  nginx 配置文件路径,没有指定的话,默认为 prefix /conf/nginx.conf

–error-log-path=path  在 nginx.conf 中没有使用 error_log 指定错误日志路径时,默认 prefix /logs/error.log

–http-log-path=path  定义被访问文件的日志存放路径,如果在 nginx.conf 中没有使用 access_log 指定,默认为 prefix /logs/access.log

–pid-path=path  当在 nginx.conf 中没有使用 pid 指定 pid 文件路径时,默认为 prefix /logs/nginx.pid

–lock-path=path  锁文件存放路由,如果在 nginx.conf 中没有指定,则默认为 prefix /logs/nginx.lock

–user=user  如果在 nginx.conf 中没有使用 user 定义进程运行的属主,则默认为 nobody

–group=group  如果在 nginx.conf 中没有使用 user 定义进程运行的属组,则默认为 nobody

–builddir=dir  set build directory

–with-rtsig_module  使用 rtsig 模式

–with-select_module  使用 select 模式,如果当前平台没有其他有效模式,则默认编译

–without-select_module  禁止 select 模式

–with-poll_module  使用 poll 模式,如果当前平台没有其他有效模式,则默认编译

–without-poll_module  禁止 poll 模式

–with-file-aio  enable file aio support

–with-ipv6  开启 ipv6 支持

–with-debug  开启 debug

以下这些模块默认没有开启,可以使用 –with 命令开启以下模块

  –with-http_ssl_module  开启 ssl 模块

  –with-http_realip_module  可以在后端记录客户端 ip

  –with-http_addition_module  enable ngx_http_addition_module

  –with-http_flv_module  开启 flv 模块

  –with-http_mp4_module  开启 mp4 模块

  –with-http_gzip_static_module  开启 gzip 模块

  –with-http_secure_link_module  enable ngx_http_secure_link_module

  –with-http_stub_status_module  开启状态查看模块

  以下模块默认是开启的,可以使用 –without 选项关闭相关模块

  –without-http_charset_module  disable ngx_http_charset_module

  –without-http_gzip_module  禁用 gzip 压缩模块

  –without-http_ssi_module  禁用 ssl 模块

  –without-http_userid_module  disable ngx_http_userid_module

  –without-http_access_module  禁用 access 模块

  –without-http_auth_basic_module  禁用 auth_basic 认证模块

  –without-http_autoindex_module  禁用列目录模块

  –without-http_geo_module  禁用 geo 模块

  –without-http_map_module  禁用 map 模块

  –without-http_referer_module  disable ngx_http_referer_module

  –without-http_rewrite_module  禁用重定向模块

  –without-http_proxy_module  禁用代理模块

  –without-http_fastcgi_module  禁用 fastcgi 模块

  –without-http_uwsgi_module  disable ngx_http_uwsgi_module

  –without-http_scgi_module  disable ngx_http_scgi_module

  –without-http_memcached_module  disable ngx_http_memcached_module

  –without-http_limit_conn_module  disable ngx_http_limit_conn_module

  –without-http_limit_req_module  disable ngx_http_limit_req_module

  –without-http_empty_gif_module  disable ngx_http_empty_gif_module

  –without-http_browser_module  disable ngx_http_browser_module

  –without-http_upstream_ip_hash_module  禁用 upstream 模块

 –with-http_perl_module  开启 perl 模块
  –with-perl_modules_path=path  设置 perl 模块路径
  –with-perl=path  为 perl 库设置路径
  –http-client-body-temp-path=path  set path to store http client request body temporary files
  –http-proxy-temp-path=path  set path to store http proxy temporary files
  –http-fastcgi-temp-path=path  set path to store http fastcgi temporary files
  –http-uwsgi-temp-path=path  set path to store http uwsgi temporary files
  –http-scgi-temp-path=path  set path to store http scgi temporary files
  –without-http  禁用 http 服务
  –without-http-cache  禁用 http cache
  –with-mail  开启 mail 服务
  –with-mail_ssl_module  在 mail 服务中开启 ssl
  –without-mail_pop3_module  disable ngx_mail_pop3_module
  –without-mail_imap_module  disable ngx_mail_imap_module
  –without-mail_smtp_module  disable ngx_mail_smtp_module
  –with-google_perftools_module  开启 google_perftools 模块
  –with-cpp_test_module  开启 cpp_test 模块
  –add-module=path  enable an external module
  –without-pcre  禁止使用 perl 正则库
  –with-pcre  强制使用 perl 正则库

  编译安装

./configure --prefix=/etc/nginx/ --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module 
--with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module 
--with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-file-aio --with-ipv6

  结果如下图:

  编译并安装

make   make install

  ps:fastcgi 回顾
  fastcgi 是一个可伸缩地、高速地在 http server 和动态脚本语言间通信的接口。多数流行的 http server 都支持 fastcgi,包括 apache、nginx 和 lighttpd 等,同时,fastcgi 也被许多脚本语言所支持,其中就有 php。fastcgi 是从 cgi 发展改进而来的。传统 cgi 接口方式的主要缺点是性能很差,因为每次 http 服务器遇到动态程序时都需要重新启动脚本解析器来执行解析,然后结果被返回给 http 服务器。这在处理高并发访问时,几乎是不可用的。另外传统的 cgi 接口方式安全性也很差,现在已经很少被使用了。fastcgi 接口方式采用 c / s 结构,可以将 http 服务器和脚本解析服务器分开,同时在脚本解析服务器上启动一个或者多个脚本解析守护进程。当 http 服务器每次遇到动态程序时,可以将其直接交付给 fastcgi 进程来执行,然后将得到的结果返回给浏览器。这种方式可以让 http 服务器专一地处理静态请求或者将动态脚本服务器的结果返回给客户端,这在很大程度上提高了整个应用系统的性能。
  nginx 不支持对外部程序的直接解析,所有的外部程序(包括 php)必须通过 fastcgi 接口来调用。fastcgi 接口在 linux 下是 socket,(这个 socket 可以是文件 socket,也可以是 ip socket)。为了调用 cgi 程序,还需要一个 fastcgi 的 wrapper(wrapper 可以理解为用于启动另一个程序的程序),这个 wrapper 绑定在某个固定 socket 上,如端口或者文件 socket。当 nginx 将 cgi 请求发送给这个 socket 的时候,通过 fastcgi 接口,wrapper 接纳到请求,然后派生出一个新的线程,这个线程调用解释器或者外部程序处理脚本并读取返回数据;接着,wrapper 再将返回的数据通过 fastcgi 接口,沿着固定的 socket 传递给 nginx;最后,nginx 将返回的数据发送给客户端,这就是 nginx+fastcgi 的整个运作过程。

php-fpm

php5.3 版本源码已经默认支持 php-fpm 了,但是 debian6 认为它还没经过广泛的测试,所以在 debian6 的软件仓库中,虽然 php 版本为 5.3.3,但是却没包含 php-fpm,如果不想手工编译安装 php 的话可以换一个源。
 
  修改源列表

vi /etc/apt/sources.list
deb http://packages.dotdeb.org stable all
deb-src http://packages.dotdeb.org stable all

  更新源列表,安装 php5-fpm

apt-get update wget http://www.dotdeb.org/dotdeb.gpg
cat dotdeb.gpg | apt-key add -
apt-get install php5-fpm

  安装其他常用 php5 组件

apt-get install php5 php5-cgi php5-cli php5-mysql php5-memcache

  启动 php-fpm

/etc/init.d/php5-fpm start

  这样的话最基本的 nginx+php 环境就搭建完毕了。 

nginx 配置文件粗解
nginx 的配置文件结构类似下图这样的结构:

配置文件主要参照编译完成后生成的默认配置文件。

  主模块的配置选项

  user 指令设置进程以什么用户运行,在源码编译安装时指定的 nginx 用户,如果在编译时没有指定,默认是 nobody 账户,在配置文件中此行处于注释状态,  user 指令可以设置两个参数,第一个指定进程所属用户,第二个是可选,指定进程所属组

 user nginx nobody;

  设置工作进程数,一个工作进程为一个单线程,在 cpu 密集型环境中,可以设置 worker_processes 数目为 cpu 核数

 worker_processes 4;

  指定 nginx 错误日志文件的位置,如果要禁止错误日志使用 error_log /dev/null,error_log 可以存在于不同的字段 main、http、server 等,文件后面可以指定记录的日志的默认等级。

 error_log logs/error.log;

  设置 pid 文件路径,可以使用 kill 命令发送相关信号

 pid logs/nginx.pid;

  event 模块配置选项,event 模块主要控制 nginx 处理连接的方式

 events {
 # 如果在 configure 时指定的不止一个事件模型,可以通过 use 告诉 nginx 要使用哪一个模型:seletc、poll、kqueue、epoll、rtsig、/dev/poll、eventport 等
 use epoll;
 #worker_connections 和 worker_processes 可以计算你的理论最大链接数, worker_connections*worker_processes
 worker_connections 1024;
}

  http 模块里面主要是对 http 服务器相关属性进行设置

 http {
 # 可以用 include 指令包含一些其他文件,支持通配符,可以使用绝对路径,也可以使用相对路径,相对路径以 nginx.conf 为根据
 include mime.types;
 # 设置默认的 mime 类型
 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 main;
 #sendfile 拷贝文件在内核态完成,更加高效
 sendfile on;
 #tcp_nopush on;
 # 可以设置两个值,第一个表示客户端与服务器长连接的超时时间,超过这个时间,服务器将关闭连接。第二个值指定的应答头中 keep-alive 中 timeout 的值,让浏览器知道什么时候关闭连接。 keepalive_timeout 65;
 # 开启 gzip 压缩
 gzip on;
 # 在三次握手时,发送给客户端应答后的超时时间,目前还没进入连接状态,只完成了两次握手,如果在规定时间没收到应答包,nginx 将关闭链接
 send_timeout 30
 server {
 xxx
 }
}

  server 模块嵌在 http 模块中,主要用来配置虚拟主机

 server {
 # 指定 server 字段中可以被访问到的 ip 地址及端口
 listen 80;
 # 将 http 请求的主机头与 server 中的 server_name 参数进行匹配,并找出第一个结果,如果没有 server_name 参数匹配上,则第一个出现 listen 的 server 将被匹配,多域名用空格分割
 server_name www.nginx.com;
 # 设个指令是应答头重的 content-type 字段使用指定的编码集,off 表示不在应答头重添加 content-type 信息
 charset off;
 # 指定 www.nginx.com 域名的访问日志路径及格式
 access_log logs/host.access.log main;
 # 如果在 url 中没有指定文件,则设置一个默认主页,可以设置多个文件,空格分开,可以在 http、server、location 中设置
 index index.php index.htm;
 # 根据 url 的不同需求进行配置,可以使用字符串和正则匹配,最确切的匹配被使用,搜索到第一个后会停止
 # ~*  不区分大小写;~  区分大小写;^~  禁止在字符串匹配后检查正则;=  在 url 和 location 之间精确匹配,匹配完成后不做额外搜索。 location /i/ {
 # 请求到达后的文件根目录,在请求中 root 会把 location 匹配的值加到 root 指定的值后面,请求 /i/a.php,则会是 /html/i/a.php 响应
 root html;
 # 在 location 中设置 index
 index index.html index.htm;
 }
 # 为错误代码指定相应的错误界面,可以用在 http、server、location 字段中。 error_page 404 /404.html;
 # redirect server error pages to the static page /50x.html
 error_page 500 502 503 504 /50x.html;
 # 精确匹配 50x.html,真实响应是 /html/50x.html
 location = /50x.html {
 root html;
 }
 # proxy the php scripts to apache listening on 127.0.0.1:80
 location ~ \.php$ {
 proxy_pass http://127.0.0.1;
 }
 # 配置 php 脚本传至 fastcgi
 location ~ \.php$ {
 root html;
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index index.php;
 #/scripts 是 php 脚本所在的目录
 fastcgi_param script_filename /scripts$fastcgi_script_name;
 include fastcgi_params;
 }
 # 拒绝访问.htaccess 文件
 location ~ /\.ht {
 deny all;
 }
 }

关于“Debian 系统下如何为 PHP 程序配置 Nginx 服务器”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Debian 系统下如何为 PHP 程序配置 Nginx 服务器”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注丸趣 TV 行业资讯频道。

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-17发表,共计8594字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)