Nginx安装笔记(含PHP支持、虚拟主机、反向代理负载均衡)
系统环境:RHEL5[2.6.18-8.el5xen]
软件环境:
nginx-0.7.17
lighttpd-1.4.20.tar.gz
pcre-6.6-1.1
pcre-devel-6.6-1.1
php-5.1.6-5.el5
参考下载地址:
http://sysoev.ru/nginx/nginx-0.7.17.tar.gz(最新稳定版为0.6.32)
http://www.lighttpd.net/download/lighttpd-1.4.20.tar.gz
##########################################################################
一、安装支持软件
1、安装lighttpd以提取spawn-fcgi(如果站点不包含php页面,可以不安装spaw-fcgi、PHP)
shell>tarzxvflighttpd-1.4.20.tar.gz
shell>cdlighttpd-1.4.20/
shell>./configure&&make
shell>cp-psrc/spawn-fcgi/usr/sbin/spawn-fcgi
2、安装pcre和php(以下软件)
可使用RHEL5自带的rpm包安装,过程略。
二、安装nginx
shell>tarzxvfnginx-0.7.17.tar.gz
shell>cdnginx-0.7.17/
shell>./configure--prefix=/opt/nginx--with-http_stub_status_module--with-http_ssl_module
shell>make&&makeinstall
shell>ln-sf/opt/nginx/sbin/nginx/usr/sbin/
三、nginx运行控制
1、检查配置文件有无语法错误
shell>nginx-t
2、启动(不带任何参数直接运行即可)
shell>nginx
3、重新加载nginx配置
shell>killall-sHUPnginx#//或者killall-1nginx
4、处理完当前请求后退出nginx
shell>killall-sQUITnginx#//或者killall-3nginx
四、nginx配置用例
1、常规配置
shell>vi/opt/nginx/conf/nginx.conf
worker_processes1;#//工作进程数
events{
useepoll;#//增加该事件提高I/O性能
work_connections4096;
}
http{
includemime.types;
default_typesapplication/octet-stream;
sendfileon;
tcp_nodelayon
keepalive_timeout60;
server{
listen80;#//设置监听端口,注意不要和Apache等其他Web程序冲突
server_namewww.linux.org;#//指定使用的主机名
charsetutf-8;#//指定站点文件的默认编码
location/{
roothtml;#//设置网站根目录
indexindex.htmlindex.html;
}
error_page500502503504/50x.html
location=/50x.html{
roothtml;
}
}
}
2、添加状态监控
shell>vi/opt/nginx/conf/nginx.conf#//增加以下内容
location~^/NginxStatus/{
stub_statuson;
access_logoff;
}
shell>killall-1nginx
#//使用浏览器访问http://nginx_server_ip/NginxStatus/即可看到状态统计页面。(三个数字分别表示:总共处理连接数、成功创建的握手次数、总共处理的请求数)
3、通过FastCGI方式支持PHP语言
1)启动FastCGI服务(用php-cgi做实际处理php页面的程序,用spawn-fcgi是便于同时开启多个php-cgi进程——“-C”选项控制子进程数)
shell>/usr/sbin/spawn-fcgi-a127.0.0.1-p9000-f/usr/bin/php-cgi-C10
2)修改/opt/nginx/conf/nginx.conf配置文件,添加以下内容:
location~\.php${
roothtml;
fastcgi_pass127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
includefastcgi_params;
}
3)重新加载配置
shell>killall-1nginx
4、虚拟主机设置
修改nginx.conf文件,增加一个server{……}配置即可,每个虚拟主机的参数可以独立配置。
http{
server{
listen80;
server_namewww.vhost1.com;
access_loglogs/vhost1.access.logmain;
location/{
indexindex.html;
root/var/www/vhost1;#//第1个虚拟主机的网页根目录
}
}
server{
listen80;
server_namewww.vhost2.com;
access_loglogs/vhost2.access.logmain;
location/{
indexindex.html;
root/var/www/vhost2;#//第2个虚拟主机的网页根目录
}
}
}
5、基于反向代理的负载均衡
修改nginx.conf文件,增加upstream配置,指定对应服务器群的IP和权重,并调整server段中的网页根目录配置。使访问nginx服务器的HTTP请求分散到Web群集中的服务器来处理。
http{
upstreammy_web_cluster{
server192.168.2.11:8000weight=3;
server192.168.2.12:8000weight=3;
server192.168.2.13:8000weight=3;
server192.168.2.14:8000weight=3;
server192.168.2.15:8000weight=3;
}
server{
listen80;
server_namewww.domain.com;
location/{
proxy_passhttp://my_web_cluster;
proxy_set_headerx-real-IP$remote_addr;
}
#//注:其他的location配置段(如关于.php文件的)需注释掉,否则可能影响该类文件的重定向。
}
}
相关文章
- 笛卡尔积 php,PHP笛卡尔积实现算法示例
- window 安装多个 php
- 编译安装Nginx + PHP + MySQL
- PHP安装配置Opcache加速你的网站
- PHP安装MySQL模块:简单易行(php添加mysql模块)
- PHP设计模式依我之见详解编程语言
- 程序的安装及测试在Linux环境下安装PHP程序及其测试(Linux下php)
- Linux下的PHP编程之旅(linux的php)
- 初探Linux中的PHP文件之旅(linux创建php文件)
- PHP使用Redis扩展连接Redis服务(php开启redis)
- Linux下如何调试PHP程序(linux调试php)
- PHP在Linux下安装配置指南(php搭建环境linux)
- 安装Linux下的PHP:从源码进行编译(linux源码安装php)
- Linux下简易安装 PHP(linux下php安装)
- 、mysqlLinux安装PHP和MySQL的指南(linux 安装php)
- 解决办法:使用PHP支持MSSQL扩展操作(php支持mssql扩展)
- 如何使用Linux 查找安装PHP的位置?(linux查找php)
- PHP脚本管理MSSQL数据库:优化时间精度(php mssql 时间)
- 安装MongoDB,接入PHP开发新篇章(php安装mongodb)
- 在PHP中安装MySQL扩展:一步一步操作指引(php安装mysql扩展)
- 检查PHP Redis环境实现快速可靠性服务(检查php redis)
- 基于Redis和PHP技术的投票系统优化(投票 redis php)
- 使用PHP实现Redis自旋锁(redis自旋锁php)
- linux下安装apache与php;Apache+PHP+MySQL配置攻略
- PHP得到某段时间区间的时间戳php定时任务
- Windows下搭建PHP开发环境(Apache+PHP+MySQL)
- PHP管理依赖(dependency)关系工具Composer安装与使用