Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2295812
  • 博文数量: 168
  • 博客积分: 6641
  • 博客等级: 准将
  • 技术积分: 1996
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-02 11:49
文章存档

2020年(4)

2019年(6)

2017年(1)

2016年(3)

2015年(3)

2014年(8)

2013年(2)

2012年(12)

2011年(19)

2010年(10)

2009年(3)

2008年(17)

2007年(80)

分类: LINUX

2011-12-28 09:15:08

 
搭建Nginx+MySQL+PHP网站平台
 
2011-12-27  TsengYia#126.com http://tsengyia.blog.chinaunix.net/  
 
附注:
    新版的php已经自带FPM(FastCGI Process Manager)模块,用来管理php进程、优化解析效率。只要在编译时添加 --enable-fpm 即可提供FPM支持。
 
##############################################################################
 
系统环境:
    RHEL 6.1 [2.6.32-131.0.15.el6.i686]
 
软件环境:
    pcre-7.8.3.1.el6.i686
    pcre-devel-7.8.3.1.el6.i686
    zlib-1.2.3-25.el6.i686
    zlib-devel-1.2.3-25.el6.i686
 
    nginx-1.0.8.tar.gz
    mysql-5.1.55.tar.gz
    php-5.3.6.tar.gz
    ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz
 
##############################################################################
 
一、Nginx安装与基本配置
 
1. 编译安装

[root@localhost ~]# useradd -M -s /sbin/nologin nginx
 
[root@localhost ~]# tar zxf nginx-1.0.8.tar.gz
[root@localhost ~]# cd nginx-1.0.8/
[root@localhost nginx-1.0.8]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost nginx-1.0.8]# make
[root@localhost nginx-1.0.8]# make install
 
[root@localhost nginx-1.0.8]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
 
2. 添加为nginx服务

[root@localhost ~]# vi /etc/init.d/nginx
#!/bin/bash
# by TsengYia#126.com 2011.11.27
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"                     //主程序位置
PIDF="/usr/local/nginx/logs/nginx.pid"                 //PID文件位置
case "$1" in
  start)
    $PROG
    ;;
  stop)
    kill -s QUIT $(cat $PIDF)
    ;;
  restart)
    $0 stop
    $0 start
    ;;
  reload)
    kill -s HUP $(cat $PIDF)
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|reload}"
    exit 1
esac
exit 0

[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# chkconfig nginx on

3. 典型的静态Web站点配置

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
user    nginx;
worker_process     1;                             //工作进程数,建议参考CPU个数
error_log    logs/error.log;
pid    logs/nginx.pid;
events {
    use epoll;                                    //2.6以上内核采用epoll模型以提高性能
    worker_connections 4096;                      //每进程处理的连接数
}
http {
    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    main;
    sendfile    on;                               //支持文件发送(下载)
    keepalive_timeout    65;                      //连接保持超时(秒)
    server {                                      //Web服务的监听设置
        listen    80;
        server_name   
        charset    utf-8;
        location / {                              //网站根目录设置
            root    html;                         //根目录的实际位置
            index    index.php index.html;        //默认索引页(首页)文件
        }
        error_page    500 502 503 504;            //错误代码响应
        location = /50x.html {
            root    html;
        }
    }
}

[root@localhost ~]# 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

[root@localhost ~]# service nginx start
[root@localhost ~]# netstat -anpt | grep nginx
tcp    0    0 0.0.0.0:80    0.0.0.0:*    LISTEN    12079/nginx:master



二、安装MySQL、PHP

1. 编译安装MySQL

[root@localhost ~]# tar zxf mysql-5.1.55.tar.gz
[root@localhost ~]# cd mysql-5.1.55/
[root@localhost mysql-5.1.55]# ./configure --prefix=/usr/local/mysql --with-charset=utf8 --with-collation=utf8_general_ci --with-extra-charsets=gbk,gb2312
[root@localhost mysql-5.1.55]# make
[root@localhost mysql-5.1.55]# make install

[root@localhost mysql-5.1.55]# cp support-files/my-medium.cnf /etc/my.cnf
[root@localhost mysql-5.1.55]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql-5.1.55]# chmod +x /etc/init.d/mysqld
[root@localhost mysql-5.1.55]# chkconfig --add mysqld

[root@localhost mysql-5.1.55]# ln -s /usr/local/mysql/bin/* /usr/local/bin/
[root@localhost mysql-5.1.55]# ln -s /usr/local/mysql/lib/mysql/* /usr/lib/
[root@localhost mysql-5.1.55]# ln -s /usr/local/mysql/include/mysql/* /usr/include/

[root@localhost mysql-5.1.55]# useradd -M -u 27 -s /sbin/nologin mysql
[root@localhost mysql-5.1.55]# cd /usr/local/mysql/bin/
[root@localhost bin]# ./mysql_install_db --user=mysql
[root@localhost bin]# chown -R root:mysql /usr/local/mysql/
[root@localhost bin]# chown -R mysql /usr/local/mysql/var/

[root@localhost bin]# service mysqld start
[root@localhost bin]# chkconfig mysqld on
[root@localhost bin]# mysqladmin -u root password 'pwd123'

2. 编译安装PHP,加入FPM、Zend支持

[root@localhost ~]# tar zxf php-5.3.6.tar.gz
[root@localhost ~]# cd php-5.3.6/
[root@localhost php-5.3.6]# ./configure --prefix=/usr/local/php5 --with-gd --with-zlib --with-mysql=/usr/local/mysql --with-config-file-path=/usr/local/php5 --enable-mbstring --enable-fpm
[root@localhost php-5.3.6]# make
[root@localhost php-5.3.6]# make install

[root@localhost php-5.3.6]# cp php.ini-development /usr/local/php5/php.ini
[root@localhost php-5.3.6]# ln -s /usr/local/php5/bin/* /usr/local/bin/
[root@localhost php-5.3.6]# ln -s /usr/local/php5/sbin/* /usr/local/sbin/

[root@localhost ~]# tar zxf ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz
[root@localhost ~]# cd ZendGuardLoader-php-5.3-linux-glibc23-i386/php-5.3.x/
[root@localhost php-5.3.x]# cp ZendGuardLoader.so /usr/local/php5/lib/php/

[root@localhost php-5.3.x]# vi /usr/local/php5/php.ini
......
zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so
zend_loader.enable=1



三、配置Nginx支持PHP

1. 配置并启用php-fpm进程

[root@localhost ~]# cd /usr/local/php5/etc/
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# vi php-fpm.conf
......
pid = run/php-fpm.pid
user = nginx
group = nginx
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35

[root@localhost etc]# /usr/local/php5/sbin/php-fpm
[root@localhost etc]# netstat -anpt | grep php-fpm
tcp     0    0 127.0.0.1:9000    0.0.0.0:*    LISTEN    4448/php-fpm.conf)

2. 修改nginx服务脚本,加入对php-fpm的控制

[root@localhost ~]# vi /etc/init.d/nginx
......
PROG_FPM="/usr/loca l /php5/sbin/php-fpm"
PIDF_FPM="/usr/local/php5/var/run/php-fpm.pid"
case "$1" in
  start)
    $PROG
    $PROG_FPM
    ;;
  stop)
    kill -s QUIT $(cat $PIDF)
    kill -s QUIT $(cat $PIDF_FPM)
    ;;
......

3. 修改Nginx的配置,自动调用php-fpm进程

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
......
http {
    ......
    server {
        ......
        location ~\.php$ {
            root    /usr/local/nginx/html;            //PHP网页文档根目录
            fastcgi_pass    127.0.0.1:9000;           //告知php-fpm的监听地址
            fastcgi_index    index.php;
            include    fastcgi.conf;                  //包含FastCGI样例配置
        }
    }
}

[root@localhost ~]# service nginx restart

—— 若要转由其他的Web服务器来解析PHP页面(例如Nginx-->LAMP),则只需要简单的采用proxy_pass配置指明目标Web服务器的地址即可,可以参考以下location配置段。
        location ~\.php$ {
            proxy_pass   
        }

4. 测试PHP页面解析、数据库连接

[root@localhost ~]# vi /usr/local/nginx/html/test.php
$link=mysql_connect('localhost','root','pwd123');
if($link) echo "Success!!";
mysql_close();
?>
 
[root@localhost ~]# elinks //看到Success!!则表示成功



四、其他常见配置

1. 访问状态统计

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
......
http {
    ......
    server {
        listen    80;
        server_name   
        charset    utf-8;
        location / {
            root    html;
            index    index.php index.html;
        }
        location ~ /status {                    //访问路径为
            stub_status    on;                  //打开状态统计功能
            access_log    off;                  //关闭日志记录
        }
    }
}

[root@localhost ~]# service nginx restart

2. 虚拟Web主机

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
......
http {
    ......
    server {                                               //第1个虚拟主机
        listen    80;
        server_name   
        charset    utf-8;
        access_log    logs/vhost1.access.log  main;
        location / {
            root    /var/www/vhost1;
            index    index.php index.html;
        }
    }
    server {                                               //第2个虚拟主机
        listen    80;
        server_name   
        charset    utf-8;
        access_log    logs/vhost2.access.log  main;
        location / {
            root    /var/www/vhost2;
            index    index.php index.html;
        }
    }
    server {                                               //第n个虚拟主机
        ......
    }
}

[root@localhost ~]# service nginx restart

3. Web反向代理

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
......
http {
    ......
    upstream wwwcluster {                                   //定义上游配置
        ip_hash;                                            //启用会话保持功能
        server 192.168.7.21:80;                             //后端的真实Web服务器
        server 192.168.7.22:80;
        server 192.168.7.23:80;
    }
    server {
        ......
        listen    80;
        server_name   
        charset    utf-8;
        location / {
            proxy_pass    //转交给反向代理处理
            proxy_set_header x-real-IP $remote_addr;        //调整HTTP响应头
        }
    }
}

[root@localhost ~]# service nginx restart
 
##############################################################################

阅读(1360) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~