百度空间 | 百度首页 
 
查看文章
 
构建高性能的LAMP平台标准流程
2009-10-04 13:17

                 构建高性能的LAMP平台标准流程

LAMP(Linux-Apache-MySQL-PHP)网站架构是目前国际流行的Web框架,该框架包括:Linux操作系统,Apache网络服务器,MySQL数据库,Perl、PHP或者Python编程语言,所有组成产品均是开源软件,是国际上成熟的架构框架,很多流行的商业应用都是采取这个架构,和Java/J2EE架构相比,LAMP具有Web资源丰富、轻量、快速开发等特点,微软的.NET架构相比,LAMP具有通用、跨平台、高性能、低价格的优势,因此LAMP无论是性能、质量还是价格都是企业搭建网站的首选平台。笔者为几家企业-杭州网跃、东北大学、北京龙多等搭建的Disczu论坛及iredmail邮件系统,无一不用到了LAMP架构,有鉴于如此,所以笔者将整个配置过程流程化及脚本自动化,也方便以后在其它企业布署,整个过程如下:


一、系统安装
二、编译安装基本环境
三、配置虚拟主机及基本性能调优
四、基本安全设置
五、后述


一、系统安装
1. 分区
   /boot 100M左右
   SWAP 物理内存的2倍(如果你的物理内存大于4G以上,分配4G即可)
   /            15G
   /usr/local 20G (用于安装软件)
   /data 剩余所有空间

2. 系统初始化脚本(根据具体需求关闭不需要的服务) #vim init.sh

-------------------cut begin-------------------------------------------

#!/bin/bash
#disable ipv6
cat << EOF
+--------------------------------------------------------------+
|         === Welcome to Disable IPV6 ===                      |
+--------------------------------------------------------------+
EOF
echo "alias net-pf-10 off" >> /etc/modprobe.conf
echo "alias ipv6 off" >> /etc/modprobe.conf
/sbin/chkconfig --level 35 ip6tables off
echo "ipv6 is disabled!"
#disable selinux
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
echo "selinux is disabled,you must reboot!"
#vim
sed -i "8 s/^/alias vi='vim'/" /root/.bashrc
echo 'syntax on' > /root/.vimrc
#tunoff services
#--------------------------------------------------------------------------------
cat << EOF
+--------------------------------------------------------------+
|         === Welcome to Tunoff services ===                   |
+--------------------------------------------------------------+
EOF
#---------------------------------------------------------------------------------
for i in `ls /etc/rc3.d/S*`
do
             CURSRV=`echo $i|cut -c 15-`
echo $CURSRV
case $CURSRV in
         crond | irqbalance | microcode_ctl | network | random | sshd | syslog | local | mysqld )
     echo "Base services, Skip!"
     ;;
     *)
         echo "change $CURSRV to off"
         chkconfig --level 235 $CURSRV off
         service $CURSRV stop
     ;;
esac
done
-------------------cut end-------------------------------------------

#sh init.sh (执行上面保存的脚本,仍后重启)
复制代码二、编译安装基本环境

1. 安装准备
   1) 系统约定
    软件源代码包存放位置        /usr/local/src
    源码包编译安装位置(prefix)        /usr/local/software_name
    脚本以及维护程序存放位置        /usr/local/sbin
    MySQL 数据库位置        /data/mysql/data(可按情况设置)
    Apache 网站根目录        /data/www/wwwroot(可按情况设置)
    Apache 虚拟主机日志根目录        /data/www/logs(可按情况设置)
    Apache 运行账户        www:www (useradd -d /data/www/;chown
www.www /data/www/wwwroot)
   目录及www用户可用脚本建立 add_user.sh
   #!/bin/bash
   /usr/sbin/groupadd www
   /usr/sbin/useradd -g www www
   mkdir -p /data/www/wwwroot
   chmod +w /data/www/wwwroot
   chown www:www /data/www/wwwroot -R


2) 系统环境部署及调整
   检查系统是否正常
   # tail -n100 /var/log/messages        (检查有无系统级错误信息)
   # dmesg (检查硬件设备是否有错误信息)
   # ifconfig(检查网卡设置是否正确)
   # ping
www.linuxtone.org        (检查网络是否正常)

3) 使用 yum 程序安装所需开发包(以下为标准的 RPM 包名称)
   #rpm --import
http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
   #yum install ntp vim-enhanced gcc gcc-c++ gcc-g77 flex bison autoconf automake bzip2-devel
    ncurses-devel zlib-devel libjpeg-devel libpng-devel libtiff-devel freetype-devel libXpm-devel
    gettext-devel pam-devel libstdc
  

4) 定时校正服务器时钟,定时与中国国家授时中心授时服务器同步
   # crontab -e
   加入一行:
   15 3 * * * /usr/sbin/ntpdate 210.72.145.44 > /dev/null 2>&1

5)下载源码包
cd /usr/local/src
vim list
http://www.libgd.org/releases/gd-2.0.35.tar.bz2
http://xmlsoft.org/sources/libxml2-2.6.30.tar.gz
http://jaist.dl.sourceforge.net/sourceforge/mcrypt/libmcrypt-2.5.8.tar.bz2
http://down1.chinaunix.net/distfiles/cronolog-1.6.2.tar.gz
http://jaist.dl.sourceforge.net/sourceforge/mcrypt/mcrypt-2.6.7.tar.gz
http://cn.php.net/get/php-5.2.8.tar.bz2/from/cn2.php.net/mirror
http://www.phpchina.com/download/soft/zend/optimizer/3.3.3/ZendOptimizer-3.3.3-linux-glibc23-i386.tar.gz
http://apache.etoak.com/httpd/httpd-2.2.14.tar.gz
http://www.phpchina.com/download/soft/mysql/mysql-5.1.30.tar.gz
http://xcache.lighttpd.net/pub/Releases/1.2.2/xcache-1.2.2.tar.gz

2. 编译安装软件包
   源码编译安装所需包(Source)
   1) GD2
       # cd /usr/local/src
       # tar xvf gd-2.0.35.tar.gz
       # cd gd-2.0.35
       # ./configure --prefix=/usr/local/gd2
       # make
       # make install

   2) LibXML2
       # cd /usr/local/src
                # tar xvf libxml2-2.6.29.tar.bz2
                # cd libxml2-2.6.29
                # ./configure --prefix=/usr/local/libxml2
                # make
                # make install

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

   4) Apache日志截断程序
                # cd /usr/local/src
                # tar xvf cronolog-1.6.2.tar.gz
                # cd cronolog-1.6.2
                # ./configure --prefix=/usr/local/cronolog
                # make
                # make install
   5)编译安装mysql环境

1.下载软件编译安装
1)下载软件
    # cd /usr/local/src
    httpd-2.2.8.tar.gz
    mysql-5.0.51b.tar.gz  
    php-5.2.6.tar.bz2
    ZendOptimizer-3.3.3-linux-glibc23-i386.tar.gz

2)安装MySQL,安装及配置过程比较繁琐,用脚本install_mysql.sh简化其过程
    # tar xvf mysql-5.0.51b.tar.gz  
    # cd mysql-5.0.51b

   # vim install_mysql.sh,内容如下:

CHOST="i686-pc-linux-gnu"
CFLAGS="-march=prescott -O2 -pipe -fomit-frame-pointer"
CXXFLAGS="${CFLAGS}"
./configure \
        "--prefix=/usr/local/mysql" \
        "--localstatedir=/data/mysql/data" \
        "--with-comment=Source" \
        "--with-server-suffix=-LinuxTone" \
        "--with-mysqld-user=mysql" \
        "--without-debug" \
        "--with-big-tables" \
        "--with-charset=gbk" \
        "--with-collation=gbk_chinese_ci" \
        "--with-extra-charsets=all" \
        "--with-pthread" \
        "--enable-static" \
        "--enable-thread-safe-client" \
        "--with-client-ldflags=-all-static" \
        "--with-mysqld-ldflags=-all-static" \
        "--enable-assembler" \
        "--without-isam" \
        "--without-innodb" \
        "--without-ndb-debug"
make && make install
mkdir -p /data/mysql/data
useradd mysql -d /data/mysql -s /sbin/nologin
/usr/local/mysql/bin/mysql_install_db --user=mysql
cd /usr/local/mysql
chown -R root:mysql .
chown -R mysql /data/mysql/data
cp share/mysql/my-huge.cnf /etc/my.cnf
cp share/mysql/mysql.server /etc/rc.d/init.d/mysqld
chmod 755 /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
/etc/rc.d/init.d/mysqld start
cd /usr/local/mysql/bin
for i in *; do ln -s /usr/local/mysql/bin/$i /usr/bin/$i; done

PS:mysql5.1.30 初次启动会出现Starting MySQL.Manager of pid-file quit without updating fi[FAILED]的报错,需要注释/etc/my.cnf

里的skip-federated注释掉即#skip-federated就OK了!
  
   6)编译安装apache
# cd /usr/local/src
# tar xvf httpd-2.2.14.tar.gz
# cd httpd-2.2.14
./configure \
        "--prefix=/usr/local/apache2" \
        "--with-included-apr" \
        "--enable-so" \
        "--enable-deflate=shared" \
        "--enable-expires=shared" \
        "--enable-rewrite=shared" \
        "--enable-static-support" \
        "--disable-userdir"
make
make install
echo '/usr/local/apache2/bin/apachectl start ' >> /etc/rc.local

   7)编译安装PHP
    # cd /usr/local/src
    # tar xjvf php-5.2.6.tar.bz2
    # cd php-5.2.6./configure \
         "--prefix=/usr/local/php" \
         "--with-apxs2=/usr/local/apache2/bin/apxs" \
         "--with-config-file-path=/usr/local/php/etc" \
         "--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-openssl=/usr/local/openssl" \
"--with-mcrypt=/usr/local/libmcrypt" \
"--enable-soap" \
         "--enable-gd-native-ttf" \
         "--enable-ftp" \
         "--enable-mbstring" \
         "--enable-exif" \
         "--disable-ipv6" \
         "--disable-cgi" \
         "--disable-cli"           #禁掉ipv6,禁掉cli模式,提升速度和安全性.请根据具体需求定制相关的编译数.
make
make install
mkdir /usr/local/php/etc
cp php.ini-dist /usr/local/php/etc/php.ini
    8)Xcache的安装.
#tar xvf xcache-1.2.2.tar.gz
#/usr/local/php/bin/phpize
./configure --enable-xcache --enable-xcache-coverager --with-php-config=/usr/local/php/bin/php-config \
--enable-inline-optimization --disable-debug

复制代码#vim /usr/local/php/etc/php.ini
(将以下内容加入php.ini最后面)-------------------cut begin-------------------------------------------
[xcache-common]
zend_extension      = /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/xcache.so
[xcache.admin]
xcache.admin.user   = "admin"
;如何生成md5密码: echo -n "password"| md5sum
xcache.admin.pass   = "035d849226a8a10be1a5e0fec1f0f3ce" #密码为52netseek
[xcache]
; Change xcache.size to tune the size of the opcode cache
xcache.size         = 24M
xcache.shm_scheme   = "mmap"
xcache.count        = 4
xcache.slots        = 8K
xcache.ttl          = 0
xcache.gc_interval = 0
; Change xcache.var_size to adjust the size of variable cache
xcache.var_size     = 8M
xcache.var_count    = 1
xcache.var_slots    = 8K
xcache.var_ttl      = 0
xcache.var_maxttl   = 0
xcache.var_gc_interval =     300
xcache.test         = Off
xcache.readonly_protection = On
xcache.mmap_path    = "/tmp/xcache"
xcache.coredump_directory =   ""
xcache.cacher       = On
xcache.stat         = On
xcache.optimizer    = Off
[xcache.coverager]
xcache.coverager    = On
xcache.coveragedump_directory = ""

-------------------cut end---------------------------------------------

9) 安装Zend Optimizer
    # cd /usr/local/src
    # tar xzvf ZendOptimizer-3.3.3-linux-glibc23-i386.tar.gz
    # ./ZendOptimizer-3.3.3-linux-glibc23-i386/install.sh
    安装Zend Optimizer过程的最后不要选择重启Apache。


10) 整合Apache与PHP及系统初化配置.
   1)整合Apache与PHP
    # vi /usr/local/apache2/conf/httpd.conf
    找到:
    AddType application/x-gzip .gz .tgz
    在该行下面添加
    AddType application/x-httpd-php .php

   找到:
   <IfModule dir_module>
       DirectoryIndex index.html
   </IfModule>
   将该行改为
   <IfModule dir_module>
        DirectoryIndex index.html index.htm index.php
   </IfModule>

找到:
#Include conf/extra/httpd-mpm.conf
#Include conf/extra/httpd-info.conf
#Include conf/extra/httpd-vhosts.conf (虚拟主机配置文件存放目录.)
#Include conf/extra/httpd-default.conf
去掉前面的“#”号,取消注释。
注意:以上 4 个扩展配置文件中的设置请按照相关原则进行合理配置!

修改完成后保存退出。
# /usr/local/apache2/bin/apachectl restart

11)查看确认L.A.M.P环境信息、提升 PHP 安全性
   在网站根目录放置 info.php 脚本,检查phpinfo中的各项信息是否正确。
   <?php
   phpinfo();
   ?>
  
   确认 PHP 能够正常工作后,在 php.ini 中进行设置提升 PHP 安全性,禁掉危险的函数.
   # vi /etc/php.ini找到:disable_functions =设置为:

phpinfo,passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,

dl,pfsockopen,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server

※脚本自动完成初始化配置(以上配置可以用脚本自动化完成)
#cat init_apache_php.sh
#!/bin/bash
#init_httpd.conf
http_cf="/usr/local/apache2/conf/httpd.conf"
sed -i -e "s/User daemon/User www/" -i -e "s/Group daemon/Group www/" $http_cf
sed -i -e '121 s/^/#/' -i -e '122 s/^/#/' $http_cf
sed -i 's#DirectoryIndex index.html# DirectoryIndex index.php index.html index.htm#/g' $http_cf
sed -i -e '374 s/^#//g' -i -e '389 s/^#//g' -i -e '392 s/^#//g' -i -e '401 s/^#//g' $http_cf
#init_php(PHP安全设置及隐藏PHP版本)
php_cf="/usr/local/php/etc/php.ini"
sed -i '205 s#;open_basedir =#open_basedir = /data/www/wwwroot:/tmp#g' $php_cf
sed -i '210 s#disable_functions =#disable_functions =

phpinfo,passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,

dl,pfsockopen,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server#g' $php_cf
sed -i '/expose_php/s/On/Off/' $php_cf
sed -i '/display_errors/s/On/Off/' $php_cf


三、配置虚拟主机及基本性能调优
1) 配置虚拟主机:
#vi /usr/local/apache2/conf/extra/httpd-vhosts.confNameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin
cnseek@gmail.com
DocumentRoot "/data/www/wwwroot/linuxtone.org"
ServerName
www.linuxtone.org
ServerAlias bbs.linxutone.org
ErrorLog "logs/dummy-host.example.com-error_log"
CustomLog "|/usr/sbin/cronolog /data/logs/access_www.linuxtone.org.%Y%m%d" combined </VirtualHost>

2)Apache基本性能调优:
#vim /usr/local/apache2/conf/extra/httpd-default.conf
Timeout 15
KeepAlive Off
MaxKeepAliveRequests 50
KeepAliveTimeout 5
UseCanonicalName Off
AccessFileName .htaccess
ServerTokens Prod
ServerSignature Off
HostnameLookups Off

#vim /usr/local/apache2/conf/extra/httpd-mpm.conf
<IfModule mpm_prefork_module>
ServerLimit         2000
StartServers          10
MinSpareServers       10
MaxSpareServers      15
MaxClients          2000
MaxRequestsPerChild   10000
</IfModule>


四、基本安全设置
1)iptables最好写成脚本形式,想开哪个端口开哪个,想关哪个关哪个,iptables服务都可关闭;写iptables注意配合下crontab ,即让crontab每5分钟关闭一次,如果自己的
SSH被iptables停掉了话很麻烦,毕竟公司离机房还是很远的。
2)不定期扫描,发现可疑端口就关闭,实在不太懂就cat /etc/services或googel
3)多注意连接数和系统性能,有时能从上面发现问题,多看看vmstate。
4)建议多掌握netcat、hping2、nmap等安全工具,配合监测iptables的安全策略。
5)多注意自己服务器的内核漏洞,毕竟现在的linux攻击都是内核级的;请至少保证内核为2.6.9以上(不含2.6.9)
6)多关注Michael Rash大师的书,在安全方面他是权威;这世上,没上攻不破的服务器,但这并不意味着我们啥也不做。
7)密切关注防火墙日志/var/log/messages,防火墙应配置一个默认的日志记录和丢弃规则,以使任何离群的数据包、端口扫描或其他没有明确
允许的连接企图被记录并丢弃。

到这里,构建整个LAMP的流程基本完程,以上过程在32/64位机子上均能通过。

五、后述
结合我上面提到的一些基于LAMP的项目经验,我现在越来越喜欢做的件事是直接将LAMP部署在64位服务器(centos5.3最新内核)上,基本软件采用yum安装,而更注意
细节方面,比如:php性能及安全、网络安全、系统安全、密码安全、mysql的备份及调优等,也欢迎从事运维工作的同志们一起交流心得:yuhongchun027@163.com(抚琴煮酒)


类别:apache+nginx篇 | 添加到搜藏 | 浏览() | 评论 (2)
 
最近读者:
 
网友评论:
1
2009-10-04 15:51 | 回复
推荐一款比较好的,基于PHP或Python的,中文的ERP软件吧。
 
2
2009-10-04 18:17 | 回复
这方面我了解不多,不发表观点,不好意思。
 
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu