歡迎您光臨本站 註冊首頁

針對中小型網站(3000人左右/15分鐘)的伺服器架構

←手機掃碼閱讀     火星人 @ 2014-03-04 , reply:0

針對中小型網站(3000人左右/15分鐘)的伺服器架構

針對中小型網站(3000人左右/15分鐘)的伺服器架構

本文系原創, 轉載註明來自: www.hao32.com

本文涉及的伺服器構架主要針對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
一般頭幾個都是可以下載的, 找個最快的就可以了

<!---------------以下都是web伺服器上的配置--------------->

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-(+)-(+)\.html$ $1/forumdisplay.php?fid=$2&page=$3 last;
         rewrite ^(.*)/thread-(+)-(+)-(+)\.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
<!---------------以上都是web伺服器上的配置--------------->

<!---------------以下都是db伺服器上的配置--------------->
開始安裝配置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
內容為:

port            = 3306
socket          = /tmp/mysql.sock

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

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'
);
<!---------------以上都是db伺服器上的配置--------------->

完畢!

[ 本帖最後由 hao32 於 2008-6-12 09:13 編輯 ]
《解決方案》

不錯 頂一下
《解決方案》

貌似這機器配置 優化再優化 15分鐘 30000人也是木有問題的:wink:  :wink:
《解決方案》

配置很好哦。建議貼子名字可以改下。nginx 的文章還是挺少的。

對於php-cgi 樓主可以打上php-fpm 補丁來管理,這樣的話可以平滑啟動,(在更改配置文件的前提下)  
與apache 的-k graceful 類似
《解決方案》

3000人/15分鐘 2台伺服器

太奢侈了
《解決方案》

回復 #5 real_lufeng 的帖子

應該是為以後做準備吧。呵。訪問量肯定是不斷增加的。
《解決方案》

單台可以解決了吧。
《解決方案》

算是3000人在線吧。  

這樣的配置, 根本是不可能抗住的。
《解決方案》

事實上8000人也沒問題, 偶就保守點, 為的是不出現任何問題
《解決方案》

回復 #9 hao32 的帖子

實驗室出來的數據和真正生產環境差別是巨大的。

cu也不過最多3000人在線, 你知道cu用了多少伺服器?

[火星人 ] 針對中小型網站(3000人左右/15分鐘)的伺服器架構已經有1151次圍觀

http://coctec.com/docs/service/show-post-22170.html