本文涉及的服务器构架主要针对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.系统环境
CentOS5.x(内核2.6+)
简单的说下前期工作:安装系统尽可能的精简,检查系统时间,dmesg看看有无错误,关闭selinux,ipv6,升级系统到最新yum-yupdate
安装如下web环境必要的软件包:
#yuminstall-yntpvim-enhancedgccgcc-c++gcc-g77flexbisonautoconfautomakebzip2-develncurses-devellibjpeg-devellibpng-devellibtiff-develfreetype-develpam-develkernel
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库
#tarzxvfgd-2.0.34.tar.gz
#cdgd-2.0.34
#./configure--prefix=/usr/local/gd2
#make
#makeinstall
(2)安装LibXML2
#tarzxvflibxml2-2.6.30.tar.gz
#cdlibxml2-2.6.30
#./configure--prefix=/usr/local/libxml2
#make
#makeinstall
(3)安装LibMcrypt
#cd/usr/local/src
#tarjxvflibmcrypt-2.5.8.tar.bz2
#cdlibmcrypt-2.5.8
#./configure--prefix=/usr/local/libmcrypt
#make
#makeinstall
(4)解压mysql-5.0.51a-linux-i686-glibc23.tar.gz,然后把解开的包里的lib和include目录转移到任意目录,例如/usr/local/mysql
因为编译php5需要用到mysql的支持(--with-mysql)
#tarzxvfmysql-5.0.51a-linux-i686-glibc23.tar.gz
#cdmysql-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
#makeinstall
#cpphp.ini-dist/usr/local/php-fcgi/etc/php.ini
(6)安装ZendOptimizer
#tarzxvfZendOptimizer-3.3.0-linux-glibc21-i386.tar.gz
#cdZendOptimizer-3.3.0-linux-glibc21-i386
#./install
基本上是一直回车,值得注意一点的是:
当您看到要选择php的路径时就写:
/usr/local/php-fcgi/etc
看到AreyouusingApacheWebserver?
这里要选NO
(7)安装eaccelerator
#tarjxvfeaccelerator-0.9.5.3.tar.bz2
#cdeaccelerator-0.9.5.3
#./configure--enable-eaccelerator=shared--with-php-config=/usr/local/php-fcgi/bin/php-config
#make
#makeinstall
#mkdir/tmp/eaccelerator&&chmod777/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库
#tarzxvfpcre-7.3.tar.gz
#cdpcre-7.3
#./configure
#make&&makeinstall
(9)安装nginx
#./configure--prefix=/usr/local/nginx/--with-http_stub_status_module
#make&&makeinstall
(10)编译出lighttpd中附带的spawn-fcgi,它的作用是来启动php-cgi
#tarjxvflighttpd-1.4.19.tar.bz2
#cdlighttpd-1.4.19
#./configure
#make
#cpsrc/spawn-fcgi/usr/local/php-fcgi/bin/
到现在为之,安装过程已经结束
5.开始配置环境:
#cd/usr/local/nginx/conf
#mkdirvhosts
先备份即将要修改的文件
#cpnginx.confnginx.conf.bak
#cpfastcgi_paramsfastcgi_params.bak
#mkdir-p/usr/local/nginx/temp/client_body
#mkdir/usr/local/nginx/var
(1)编辑nginx主配置文件nginx.conf
编辑nginx.conf,并且把其内容修改为,最后几行注意自定义一下
daemonon;
worker_processes4;
worker_cpu_affinity0001001001001000;
error_log/dev/null;
pidvar/nginx.pid;
lock_filevar/nginx.lock;
events{
worker_connections2048;
useepoll;
multi_accepton;
}
http{
includemime.types;
default_typeapplication/octet-stream;
server_names_hash_max_size512;
server_names_hash_bucket_size128;
sendfileon;
tcp_nopushon;
tcp_nodelayon;
keepalive_timeout1;
client_header_timeout10;
client_body_timeout10;
client_max_body_size10m;
client_body_buffer_size256k;
send_timeout30;
client_header_buffer_size1k;
large_client_header_buffers44k;
client_body_temp_path/usr/local/nginx/temp/client_body12;
gzipon;
gzip_min_length1000;
gzip_buffers48k;
gzip_http_version1.1;
gzip_comp_level1;
gzip_typestext/plaintext/cssapplication/x-javascripttext/xmlapplication/xmlapplication/xml+rsstext/javascript;
log_formatmain‘$remote_addr-$remote_user[$time_local]$request‘
‘"$status"$body_bytes_sent"$http_referer"‘
‘"$http_user_agent""$http_x_forwarded_for"‘;
access_log/dev/null;
#引用你即将要放置的虚拟主机配置文件
includevhosts/hao32.com;
includevhosts/linuxsense.org;
}
(2)编辑php在fastcgi模式下的配置文件:
#vifastcgi_params
内容修改为:
fastcgi_paramQUERY_STRING$query_string;
fastcgi_paramREQUEST_METHOD$request_method;
fastcgi_paramCONTENT_TYPE$content_type;
fastcgi_paramCONTENT_LENGTH$content_length;
fastcgi_paramSCRIPT_NAME$fastcgi_script_name;
fastcgi_paramREQUEST_URI$request_uri;
fastcgi_paramDOCUMENT_URI$document_uri;
fastcgi_paramDOCUMENT_ROOT$document_root;
fastcgi_paramSERVER_PROTOCOL$server_protocol;
fastcgi_paramGATEWAY_INTERFACECGI/1.1;
fastcgi_paramSERVER_SOFTWAREnginx/$nginx_version;
fastcgi_paramREMOTE_ADDR$remote_addr;
fastcgi_paramREMOTE_PORT$remote_port;
fastcgi_paramSERVER_ADDR$server_addr;
fastcgi_paramSERVER_PORT$server_port;
fastcgi_paramSERVER_NAME$server_name;
#PHPonly,requiredifPHPwasbuiltwith--enable-force-cgi-redirect
fastcgi_paramREDIRECT_STATUS200;
(2)编辑虚拟主机的配置文件
#cdvhosts/
#vihao32.com
添加内容如下
server{
listen80;
server_namehao32.comwww.hao32.com;
#可以选择打开日志
#access_log/var/log/hao32.com/access.logcombinedbuffer=32k;
#error_log/var/log/hao32.com/error.logwarn;
location/{
indexindex.htmlindex.htmindex.php;
root/www/wwwroot/hao32.com;
location~/.php${
includefastcgi_params;
fastcgi_indexindex.php;
fastcgi_pass127.0.0.1:8407;
fastcgi_paramSCRIPT_FILENAME/www/wwwroot/hao32.com$fastcgi_script_name;
}
location/hao32-status{
stub_statuson;
access_logoff;
}
}
#vilinuxsense.org
添加内容如下,这是一个带有discuz!默认rewrite规则的主机事例
server{
listen80;
server_namelinuxsense.orgwww.linuxsense.org;
#可以选择打开日志
#access_log/var/log/linuxsense.org/access.logcombinedbuffer=32k;
#error_log/var/log/linuxsense.org/error.logwarn;
location/{
indexindex.htmlindex.htmindex.php;
root/www/wwwroot/linuxsense.org;
rewrite^(.*)/archiver/((fid|tid)-[/w/-]+/.html)$$1/archiver/index.php?$2last;
rewrite^(.*)/forum-([0-9]+)-([0-9]+)/.html$$1/forumdisplay.php?fid=$2&page=$3last;
rewrite^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)/.html$$1/viewthread.php?tid=$2&extra=page/%3D$4&page=$3last;
rewrite^(.*)/profile-(username|uid)-(.+)/.html$$1/viewpro.php?$2=$3last;
rewrite^(.*)/space-(username|uid)-(.+)/.html$$1/space.php?$2=$3last;
rewrite^(.*)/tag-(.+)/.html$$1/tag.php?name=$2last;
location~/.php${
includefastcgi_params;
fastcgi_indexindex.php;
fastcgi_pass127.0.0.1:8408;
fastcgi_paramSCRIPT_FILENAME/www/wwwroot/linuxsense.org$fastcgi_script_name;
}
location/linuxsense-status{
stub_statuson;
access_logoff;
}
}
(3)增加网站用户
useraddhao32-d/www/wwwroot/hao32.com-s/sbin/nologin
useraddlinuxsense-d/www/wwwroot/linuxsense.org-s/sbin/nologin
如果参数-d后的路径本身就存在,会提示
useradd:warning:thehomedirectoryalreadyexists.
Notcopyinganyfilefromskeldirectoryintoit.
这并不影响,
chown-Rhao32:hao32/www/wwwroot/hao32.com
chown-Rlinuxsense:linuxsense/www/wwwroot/linuxsense
(5)创建一个要等会要使用的用户列表USER_LIST
#vi/usr/local/nginx/conf/vhosts/USER_LIST
格式如下
去掉-a-p-C-u-f等参数,分表是本机php要使用的ip地址,端口,用户
注意,如果有两个用户就要两行,主要不要有空白行
-a127.0.0.1-p8407-C20-uhao32-f
-a127.0.0.1-p8408-C200-ulinuxsense-f
(6)制作php-cgi,nginx等启动脚本,并加入系统服务
#vi/etc/init.d/php-cgi
#chmod755/etc/init.d/php-cgi
编辑php-cgi启动脚本,加入以下代码:
#!/bin/bash
#
#PHP-FastCGIControlScriptforRedHatbasedLinux.
#Writtenbyhao32
#chkconfig:38989
#description:PHPFast-CGI
#PHPUsersSettings
USER_INFO="/usr/local/nginx/conf/vhosts/USER_LIST"
#SourceFunctionLibrary
./etc/init.d/functions
PHP_SPAWN="/usr/local/php-fcgi/bin/spawn-fcgi"
PHP_SBIN="/usr/local/php-fcgi/bin/php-cgi"
PHP_P
RETVAL=0
prog="PHPFastCGI"
mkdir-p/var/run/php-fcgi2>/dev/null
start(){
echo-n$"Starting$prog:"
cat$USER_INFO|whilereadlist
do
daemon$PHP_SPAWN$list$PHP_SBIN
done
RETVAL=$?
if[$RETVAL-eq0]
then
touch/var/lock/subsys/php-fcgi
fi
echo
return$RETVAL
}
stop(){
echo-n$"Stopping$prog:"
killprocphp-cgi
RETVAL=$?
if[$RETVAL-eq0]
then
rm-f/var/lock/subsys/php-fcgi
fi
echo
return$RETVAL
}
restart(){
stop
echo-ne"Restart.../n"
sleep3
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
#chmod755/etc/init.d/nginx
编辑nginx启动脚本,加入以下代码
#!/bin/bash
#
#NginxControlScriptforRedHatbasedLinux.
#chkconfig:39090
#description:NginxWebServerControl
#SourceFunctionLibrary
./etc/init.d/functions
#NginxSettings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_P
RETVAL=0
prog="nginx"
mkdir-p/var/run/nginx2>/dev/null
start(){
echo-n$"Starting$prog:"
daemon$NGINX_SBIN-c$NGINX_CONF
RETVAL=$?
if[$RETVAL-eq0]
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-eq0]
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--addnginx
chkconfig--addphp-cgi
现在可以启动php-cgi和nginx了
servicenginxstart
servicephp-cgistart
重启动使用restart
开始安装配置mysql
#tarzxvfmysql-5.0.51a-linux-i686-glibc23.tar.gz
#mvmysql-5.0.51a-linux-i686-glibc23/usr/local/
#ln-s/usr/local/mysql-5.0.51a-linux-i686-glibc23/usr/local/mysql
#useraddmysql
#chown-Rmysql: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
#chmod755/etc/rc.d/init.d/mysqld
#chkconfig--addmysqld
#chkconfig--level3mysqldon
#cp./support-files/my-huge.cnf/etc/my.cnf
#mv/usr/local/mysql/data/var/lib/mysql
#chown-Rmysql:mysql/var/lib/mysql
#cd/usr/local/mysql/bin
#foriin*;doln-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
#TrynumberofCPU‘s*2forthread_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服务器,删增相应的用户
INSERTINTO`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‘
);
完毕!
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
针对中小型网站 3000人左右/15分钟 的服务器架构
原文地址:https://www.cnblogs.com/skiwnxhw/p/10326265.html