zoukankan      html  css  js  c++  java
  • 针对中小型网站(3000人左右/15分钟)的服务器架构

    本文涉及的服务器构架主要针对3000人左右/15分钟的中小型网站, 多见于discuz!论坛构建的社区等等
    环境为linux(内核2.6+), nginx, php(fastcgi), mysql 等高效开源软件构成, 成本低廉, 是普通个人站长朋友的首选, 
    但是针对部分企业, 这个方案也是一个非常不错的选择! 

    至于nginx和apache等web服务器的性能比较这里就不多说了

    本文是以实例的方式来描述安装以及配置等情况, 供各位参考
    1. 硬件环境
    一台web服务器, 一台db服务器, 没有raid
    web服务器 至强双核1.60GHz, 2G, 73GSCSI*2
    db服务器  至强双核1.60GHz, 2G, 73GSCSI*2

    2. 系统环境
    CentOS 5.x (内核2.6+)
    简单的说下前期工作:安装系统尽可能的精简, 检查系统时间, dmesg看看有无错误, 关闭selinux,ipv6, 升级系统到最新yum -y update
    安装如下web环境必要的软件包:
    # yum install -y ntp vim-enhanced gcc gcc-c++ gcc-g77 flex bison autoconf automake bzip2-devel ncurses-devel libjpeg-devel libpng-devel libtiff-devel freetype-devel pam-devel kernel

    3. 准备软件
    nginx-0.6.31.tar.gz
    pcre-7.3.tar.gz
    php-5.2.6.tar.bz2
    eaccelerator-0.9.5.3.tar.bz2
    ZendOptimizer-3.3.0-linux-glibc21-i386.tar.gz
    lighttpd-1.4.19.tar.bz2
    libxml2-2.6.30.tar.gz
    libmcrypt-2.5.8.tar.bz2 
    gd-2.0.34.tar.gz
    mysql-5.0.51a-linux-i686-glibc23.tar.gz

    以上软件都是目前最新最稳定的版本, 查找和下载以上软件或者更新版本
    请在www.google.com输入: 软件全名 intitle:index
    一般头几个都是可以下载的, 找个最快的就可以了



    4.开始安装环境:
    (1)安装gd库
    # tar zxvf gd-2.0.34.tar.gz
    # cd gd-2.0.34
    # ./configure --prefix=/usr/local/gd2
    # make
    # make install
    (2)安装LibXML2
    # tar zxvf libxml2-2.6.30.tar.gz
    # cd libxml2-2.6.30
    # ./configure --prefix=/usr/local/libxml2
    # make
    # make install

    (3)安装LibMcrypt
    # cd /usr/local/src
    # tar jxvf libmcrypt-2.5.8.tar.bz2
    # cd libmcrypt-2.5.8
    # ./configure --prefix=/usr/local/libmcrypt
    # make
    # make install


    (4)解压mysql-5.0.51a-linux-i686-glibc23.tar.gz, 然后把解开的包里的lib和include目录转移到任意目录, 例如/usr/local/mysql
    因为编译php5需要用到mysql的支持(--with-mysql)
    # tar zxvf mysql-5.0.51a-linux-i686-glibc23.tar.gz
    # cd mysql-5.0.51a-linux-i686-glibc23
    # mkdir -p /user/local/mysql
    # mv ./lib ./include /user/local/mysql/

    (5)编译php(fastcfi模式)
    # ./configure --prefix=/usr/local/php-fcgi --enable-fastcgi --enable-discard-path --enable-force-cgi-redirect --with-config-file-path=/usr/local/php-fcgi/etc --enable-zend-multibyte --with-mysql=/usr/local/mysql --with-libxml-dir=/usr/local/libxml2 --with-gd=/usr/local/gd2 --with-jpeg-dir --with-png-dir --with-bz2 --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt=/usr/local/libmcrypt --enable-sysvsem --enable-inline-optimization --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-debug --disable-ipv6
    # make
    # make install
    # cp php.ini-dist /usr/local/php-fcgi/etc/php.ini

    (6)安装ZendOptimizer
    # tar zxvf ZendOptimizer-3.3.0-linux-glibc21-i386.tar.gz
    # cd ZendOptimizer-3.3.0-linux-glibc21-i386
    # ./install
    基本上是一直回车, 值得注意一点的是:
    当您看到要选择php的路径时就写:
    /usr/local/php-fcgi/etc
    看到Are you using Apache Web server? 
    这里要选NO

    (7)安装eaccelerator
    # tar jxvf eaccelerator-0.9.5.3.tar.bz2
    # cd eaccelerator-0.9.5.3
    # ./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php-fcgi/bin/php-config
    # make 
    # make install
    # mkdir /tmp/eaccelerator && chmod 777 /tmp/eaccelerator && touch /var/log/eaccelerator_log
    编辑php.ini
    # vi /usr/local/php-fcgi/etc/php.ini
    在最底部加上:
    extension=/usr/local/php-fcgi/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so
    eaccelerator.shm_size="16"
    eaccelerator.cache_dir="/tmp/eaccelerator"
    eaccelerator.enable="1"
    eaccelerator.optimizer="1"
    eaccelerator.check_mtime="1"
    eaccelerator.debug="0"
    eaccelerator.log_file = "/var/log/eaccelerator_log"
    eaccelerator.filter=""
    eaccelerator.shm_max="0"
    eaccelerator.shm_ttl="0"
    eaccelerator.shm_prune_period="0"
    eaccelerator.shm_only="0"
    eaccelerator.compress="1"
    eaccelerator.compress_level="9"

    (8)安装nginx的rewrite模块需要pcre库
    # tar zxvf pcre-7.3.tar.gz
    # cd pcre-7.3
    # ./configure
    # make && make install

    (9)安装nginx
    # ./configure --prefix=/usr/local/nginx/ --with-http_stub_status_module
    # make && make install

    (10)编译出lighttpd中附带的spawn-fcgi,它的作用是来启动php-cgi
    # tar jxvf lighttpd-1.4.19.tar.bz2
    # cd lighttpd-1.4.19
    # ./configure
    # make
    # cp src/spawn-fcgi /usr/local/php-fcgi/bin/

    到现在为之, 安装过程已经结束

    5.开始配置环境:
    # cd /usr/local/nginx/conf
    # mkdir vhosts
    先备份即将要修改的文件
    # cp nginx.conf nginx.conf.bak
    # cp fastcgi_params fastcgi_params.bak
    # mkdir -p /usr/local/nginx/temp/client_body
    # mkdir /usr/local/nginx/var


    (1)编辑nginx主配置文件nginx.conf
    编辑nginx.conf, 并且把其内容修改为, 最后几行注意自定义一下
    daemon on;
    worker_processes 4;
    worker_cpu_affinity 0001 0010 0100 1000;
    error_log /dev/null;
    pid var/nginx.pid;
    lock_file var/nginx.lock;
    events {
        worker_connections  2048;
        use epoll;
        multi_accept on;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        server_names_hash_max_size 512;
        server_names_hash_bucket_size 128;
        sendfile       on;
        tcp_nopush     on;
        tcp_nodelay    on;
        keepalive_timeout  1;
        client_header_timeout 10;
        client_body_timeout   10;
        client_max_body_size       10m;
        client_body_buffer_size    256k;
        send_timeout          30;
        client_header_buffer_size    1k;
        large_client_header_buffers  4 4k;
        client_body_temp_path  /usr/local/nginx/temp/client_body 1 2;
        gzip on;
        gzip_min_length  1000;
        gzip_buffers     4 8k;
        gzip_http_version 1.1;
        gzip_comp_level 1;
        gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
        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 /dev/null; 
    # 引用你即将要放置的虚拟主机配置文件
        include vhosts/hao32.com;
        include vhosts/linuxsense.org;
    }

    (2)编辑php在fastcgi模式下的配置文件:
    # vi fastcgi_params
    内容修改为:
    fastcgi_param  QUERY_STRING       $query_string;
    fastcgi_param  REQUEST_METHOD     $request_method;
    fastcgi_param  CONTENT_TYPE       $content_type;
    fastcgi_param  CONTENT_LENGTH     $content_length;

    fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
    fastcgi_param  REQUEST_URI        $request_uri;
    fastcgi_param  DOCUMENT_URI       $document_uri;
    fastcgi_param  DOCUMENT_ROOT      $document_root;
    fastcgi_param  SERVER_PROTOCOL    $server_protocol;

    fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
    fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

    fastcgi_param  REMOTE_ADDR        $remote_addr;
    fastcgi_param  REMOTE_PORT        $remote_port;
    fastcgi_param  SERVER_ADDR        $server_addr;
    fastcgi_param  SERVER_PORT        $server_port;
    fastcgi_param  SERVER_NAME        $server_name;

    # PHP only, required if PHP was built with --enable-force-cgi-redirect
    fastcgi_param  REDIRECT_STATUS    200;

    (2)编辑虚拟主机的配置文件
    # cd vhosts/
    # vi hao32.com
    添加内容如下
    server {
        listen       80;
        server_name  hao32.com www.hao32.com;
        # 可以选择打开日志
        #access_log  /var/log/hao32.com/access.log combined buffer=32k;
        #error_log   /var/log/hao32.com/error.log warn;

        location / {
             index           index.html index.htm index.php;
             root            /www/wwwroot/hao32.com;

            location ~ /.php$ {
                    include fastcgi_params;
                    fastcgi_index index.php;
                    fastcgi_pass  127.0.0.1:8407;
                    fastcgi_param SCRIPT_FILENAME /www/wwwroot/hao32.com$fastcgi_script_name;
            }

        location /hao32-status {
             stub_status on;
             access_log  off;
        }
    }

    # vi linuxsense.org
    添加内容如下, 这是一个带有discuz!默认rewrite规则的主机事例
    server {
        listen       80;
        server_name  linuxsense.org www.linuxsense.org;
        # 可以选择打开日志
        #access_log  /var/log/linuxsense.org/access.log combined buffer=32k;
        #error_log   /var/log/linuxsense.org/error.log warn;
        location / {
             index           index.html index.htm index.php;
             root            /www/wwwroot/linuxsense.org;
             rewrite ^(.*)/archiver/((fid|tid)-[/w/-]+/.html)$ $1/archiver/index.php?$2 last;
             rewrite ^(.*)/forum-([0-9]+)-([0-9]+)/.html$ $1/forumdisplay.php?fid=$2&page=$3 last;
             rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)/.html$ $1/viewthread.php?tid=$2&extra=page/%3D$4&page=$3 last;
             rewrite ^(.*)/profile-(username|uid)-(.+)/.html$ $1/viewpro.php?$2=$3 last;
             rewrite ^(.*)/space-(username|uid)-(.+)/.html$ $1/space.php?$2=$3 last;
             rewrite ^(.*)/tag-(.+)/.html$ $1/tag.php?name=$2 last;
             location ~ /.php$ {
                    include fastcgi_params;
                    fastcgi_index index.php;
                    fastcgi_pass  127.0.0.1:8408;
                    fastcgi_param SCRIPT_FILENAME /www/wwwroot/linuxsense.org$fastcgi_script_name;
             }

        location /linuxsense-status {
             stub_status on;
             access_log  off;
        }
    }

    (3)增加网站用户
    useradd hao32 -d /www/wwwroot/hao32.com -s /sbin/nologin
    useradd linuxsense -d /www/wwwroot/linuxsense.org -s /sbin/nologin
    如果参数-d后的路径本身就存在, 会提示
    useradd: warning: the home directory already exists.
    Not copying any file from skel directory into it.
    这并不影响,
    chown -R hao32:hao32 /www/wwwroot/hao32.com
    chown -R linuxsense:linuxsense /www/wwwroot/linuxsense

    (5)创建一个要等会要使用的用户列表USER_LIST
    # vi /usr/local/nginx/conf/vhosts/USER_LIST
    格式如下
    去掉-a -p -C -u -f等参数, 分表是本机php要使用的ip地址, 端口, 用户
    注意, 如果有两个用户就要两行, 主要不要有空白行

    -a 127.0.0.1 -p 8407 -C 20 -u hao32 -f
    -a 127.0.0.1 -p 8408 -C 200 -u linuxsense -f

    (6)制作php-cgi,nginx等启动脚本, 并加入系统服务

    # vi /etc/init.d/php-cgi
    # chmod 755 /etc/init.d/php-cgi
    编辑php-cgi启动脚本, 加入以下代码:
    #!/bin/bash
    #
    # PHP-FastCGI Control Script for Red Hat based Linux.
    # Written by hao32
    # chkconfig: 3 89 89
    # description: PHP Fast-CGI

    # PHP Users Settings
    USER_INFO="/usr/local/nginx/conf/vhosts/USER_LIST"

    # Source Function Library
    . /etc/init.d/functions


    PHP_SPAWN="/usr/local/php-fcgi/bin/spawn-fcgi"
    PHP_SBIN="/usr/local/php-fcgi/bin/php-cgi"
    PHP_PID="/usr/local/nginx/var/php-fcgi.pid"


    RETVAL=0
    prog="PHP FastCGI"

    mkdir -p /var/run/php-fcgi 2> /dev/null

    start() {
            echo -n $"Starting $prog: "
            cat $USER_INFO | while read list
            do
                    daemon $PHP_SPAWN $list $PHP_SBIN
            done
            RETVAL=$?
            if [ $RETVAL -eq 0 ]
            then
                    touch /var/lock/subsys/php-fcgi
            fi
            echo
            return $RETVAL
    }

    stop() {
            echo -n $"Stopping $prog: "
            killproc php-cgi 
            RETVAL=$?
            if [ $RETVAL -eq 0 ]
            then
                    rm -f /var/lock/subsys/php-fcgi
            fi
            echo
            return $RETVAL
    }

    restart() {
            stop
            echo -ne "Restart.../n"
            sleep 3 
            start
    }

    case "$1" in
            start)
                    start
                    ;;
            stop)
                    stop
                    ;;
            restart)
                    restart
                    ;;
            *)
                    echo $"Usage: $0 {start|stop|restart}"
                    RETVAL=1
    esac

    exit $RETVAL


    # vi /etc/init.d/nginx
    # chmod 755 /etc/init.d/nginx
    编辑nginx启动脚本, 加入以下代码
    #!/bin/bash
    #
    # Nginx Control Script for Red Hat based Linux.
    # chkconfig: 3 90 90
    # description: Nginx Web Server Control

    # Source Function Library
    . /etc/init.d/functions
            
    # Nginx Settings
    NGINX_SBIN="/usr/local/nginx/sbin/nginx"
    NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
    NGINX_PID="/usr/local/nginx/var/nginx.pid"
                    
    RETVAL=0
    prog="nginx"

    mkdir -p /var/run/nginx 2> /dev/null
            
    start() {
            echo -n $"Starting $prog: "
            daemon $NGINX_SBIN -c $NGINX_CONF
            RETVAL=$?
            if [ $RETVAL -eq 0 ]
            then
                    touch /var/lock/subsys/nginx
            fi
            echo    
            return $RETVAL
    }
            
    stop() {
            echo -n $"Stopping $prog: "
            killproc -p $NGINX_PID $NGINX_SBIN -TERM
            RETVAL=$?
            if [ $RETVAL -eq 0 ]
            then
                    rm -f /var/lock/subsys/nginx
            fi
            echo
            return $RETVAL
    }

    reload() {
            echo -n $"Reloading $prog: "
            killproc -p $NGINX_PID $NGINX_SBIN -HUP
            RETVAL=$?
            echo
            return $RETVAL
    }

    restart() {
            stop
            start
    }

    configtest() {
            $NGINX_SBIN -c $NGINX_CONF -t
            exit
    }

    case "$1" in
            start)
                    start
                    ;;
            stop)
                    stop
                    ;;
            reload)
                    reload
                    ;;
            restart)
                    restart
                    ;;
            configtest)
                    configtest
                    ;;
            *)
                    echo $"Usage: $0 {start|stop|reload|restart|configtest}"
                    RETVAL=1
    esac

    exit $RETVAL


    增加到系统服务器
    chkconfig --add nginx
    chkconfig --add php-cgi


    现在可以启动php-cgi和nginx了

    service nginx start
    service php-cgi start

    重启动使用restart



    开始安装配置mysql
    # tar zxvf mysql-5.0.51a-linux-i686-glibc23.tar.gz
    # mv mysql-5.0.51a-linux-i686-glibc23 /usr/local/
    # ln -s /usr/local/mysql-5.0.51a-linux-i686-glibc23 /usr/local/mysql
    # useradd mysql
    # chown -R mysql:root  /usr/local/mysql/
    # cd /usr/local/mysql
    # ./scripts/mysql_install_db  --user=mysql
    # cp ./support-files/mysql.server  /etc/rc.d/init.d/mysqld
    # chmod 755 /etc/rc.d/init.d/mysqld
    # chkconfig --add mysqld
    # chkconfig --level 3 mysqld on
    # cp ./support-files/my-huge.cnf  /etc/my.cnf
    # mv /usr/local/mysql/data /var/lib/mysql
    # chown -R mysql:mysql /var/lib/mysql
    # cd /usr/local/mysql/bin
    # for i in *; do ln -s /usr/local/mysql/bin/$i /usr/bin/$i; done
    编辑/etc/my.cnf
    内容为:
    [client]
    port            = 3306
    socket          = /tmp/mysql.sock
    [mysqld]
    port            = 3306
    socket          = /tmp/mysql.sock
    max_connections = 1024
    # max_user_connections = 40
    skip-innodb
    #skip-network
    #skip-bdb
    datadir = /var/lib/mysql
    default-character-set=utf8
    long_query_time = 5
    log-slow-queries = /var/lib/mysql/slow_queries
    wait_timeout = 5
    thread_stack=128K
    interactive_timeout = 20
    skip-locking
    key_buffer = 256M
    max_allowed_packet = 2M
    table_cache = 512
    sort_buffer_size = 2M
    read_buffer_size = 2M
    read_rnd_buffer_size = 8M
    myisam_sort_buffer_size = 64M
    thread_cache_size = 8
    query_cache_size = 32M
    # Try number of CPU's*2 for thread_concurrency
    thread_concurrency = 8
    log-bin=mysql-bin
    server-id       = 1
    [mysql]
    default-character-set=utf8

    在本机上登陆mysql
    # mysql -uroot
    mysql> 

    输入一下内(可以复制粘贴啊), 增加一个可以远程连接的用户, 本例用户是hao32, 密码是:hao32.com
    然后在web服务器上使用诸如phpmyadmin等软件连接上本台db服务器, 删增相应的用户
    INSERT INTO `mysql`.`user` (
    `Host` ,
    `User` ,
    `Password` ,
    `Select_priv` ,
    `Insert_priv` ,
    `Update_priv` ,
    `Delete_priv` ,
    `Create_priv` ,
    `Drop_priv` ,
    `Reload_priv` ,
    `Shutdown_priv` ,
    `Process_priv` ,
    `File_priv` ,
    `Grant_priv` ,
    `References_priv` ,
    `Index_priv` ,
    `Alter_priv` ,
    `Show_db_priv` ,
    `Super_priv` ,
    `Create_tmp_table_priv` ,
    `Lock_tables_priv` ,
    `Execute_priv` ,
    `Repl_slave_priv` ,
    `Repl_client_priv` ,
    `Create_view_priv` ,
    `Show_view_priv` ,
    `Create_routine_priv` ,
    `Alter_routine_priv` ,
    `Create_user_priv` ,
    `ssl_type` ,
    `max_questions` ,
    `max_updates` ,
    `max_connections` ,
    `max_user_connections` 
    )
    VALUES (
    '%', 'hao32', PASSWORD( 'hao32.com' ) , 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', '', '0', '0', '0', '0'
    );



    完毕!

  • 相关阅读:
    SCAU 9504 面试
    SCAU 9503 懒人选座位
    SCAU 8628 相亲
    SCAU 10691 ACM 光环
    SCAU 8626 原子量计数
    SCAU 10674 等差对
    HDU ACM 1048 The Hardest Problem Ever (水题)
    SCAU 9502 ARDF
    SCAU 10686 DeathGod不知道的事情
    SCAU 8629 热身游戏(高精度)
  • 原文地址:https://www.cnblogs.com/encounter/p/2188948.html
Copyright © 2011-2022 走看看