apache官方宣称apache 2.4在性能上提升了很多,所以就把第一版的LNAMP进行了升级,nginx、apache、mysql、php全部采用最新版本,php 5.4有很多扩展没有跟进,这个是比较大的遗憾,比如:Zend Guard Loade、accelerator、Suhosin
一、系统初始化
- chmod +x /opt/init_system.sh
- /opt/init_system.sh
二、更新组件
#yum源修改
- cd /etc/yum.repos.d/
- mv CentOS-Base.repo CentOS-Base.repo.backup
- wget http://mirrors.163.com/.help/CentOS5-Base-163.repo
- yum makecache
- yum -y install gcc gcc-c++ bison patch unzip mlocate flex wget automake autoconf gd cpp gettext readline-devel libjpeg \
- libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 \
- glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel libidn libidn-devel openldap \
- openldap-devel openldap-clients openldap-servers nss_ldap expat-devel libtool libtool-ltdl-devel bison
三、LANMP安装配置(nginx、apache整合)
#=============== download software =============#
cd /opt
wget
#2.4.2分成两个包,把apr单独做成一个dep包
wget
wget
wget
wget
http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.23.tar.gz/from/http://mysql.he.net/#rpaf模块,该模块用于apache做后端时获取访客真实的IP
wget
#字符转换库libiconv
wget
wget
wget
wget
#php5-mail-header.patch为php被丁,有助于防止邮件发送被滥用
wget
#PHP的memcache扩展
wget
wget
wget
#将eaccelerator换成xcache(xcache 2.0支持php 5.4)
wget
wget
#ImageMagick完美代替GB类库处理图像
wget
ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick-6.7.6-7.tar.bz2wget
wget
http://downloads2.ioncube.com/loader_downloads/ioncube_loaders_lin_x86-64.tar.bz2wget
#memcached缓存
wget
wget
#Tcmalloc 优化nginx、mysql
#64位操作系统请先安装 libunwind库,32位操作系统不要安装。libunwind库为基于64位CPU和操作系统的程序提供了基本的堆栈辗转开解功能,其中包括用于输出堆栈跟踪的API、用于以编程方式辗转开解堆栈的API以及支持C++异常处理机制的API
wget
wget
#初始化mysql
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql/data --user=mysql
#利用TCMalloc提高mysql在高并发下的性能
vi /usr/local/mysql/bin/mysqld_safe
#在# executing mysqld_safe的下一行,加上:
export LD_PRELOAD=/usr/local/lib/libtcmalloc.so
#或者通sed添加
sed -i '/# executing mysqld_safe/a\export LD_PRELOAD=/usr/local/lib/libtcmalloc.so' /usr/local/mysql/bin/mysqld_safe
#设置mysql启动文件
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
vi /etc/rc.d/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql/data
#或者通sed修改
- sed -i '46 s#basedir=#basedir=/usr/local/mysql#' /etc/rc.d/init.d/mysqld
- sed -i '47 s#datadir=#datadir=/data/mysql/data#' /etc/rc.d/init.d/mysqld
chmod 700 /etc/rc.d/init.d/mysqld
/etc/rc.d/init.d/mysqld start
#使用lsof命令查看tcmalloc是否起效(如下图)
/usr/sbin/lsof -n | grep tcmalloc
#设置mysql开机启动
- /sbin/chkconfig --add mysqld
- /sbin/chkconfig --level 2345 mysqld on
- ln -s /usr/local/mysql/bin/mysql /sbin/mysql
- ln -s /usr/local/mysql/bin/mysqladmin /sbin/mysqladmin
#设置root密码(753951)
/sbin/mysqladmin -u root password 753951
#配置库文件搜索路径
echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf
/sbin/ldconfig
#添加/usr/local/mysql/bin到环境变量PATH中
echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile
#添加mysql管理帐户
#mysql -h localhost -u root -p753951
#msqyl> use mysql;
#msqyl> grant all on *.* to identified by '852741';
#msqyl> flush privileges;
#msqyl> exit;
#================= Install apache 2.4.2 ==============#
/usr/sbin/groupadd www
/usr/sbin/useradd -g www www -s /sbin/nologin
mkdir -p /data/www/{kerry,king}
mkdir -p /data/logs/{kerry,king}
chown -R www:www /data/www
chown -R www:www /data/logs
cd /opt
wget
rpm -e libtool-1.5.22-7.el5_4
tar -zxvf libtool-2.4.2.tar.gz
cd libtool-2.4.2
./configure
make;make install
cd ../
tar -jxvf pcre-8.30.tar.bz2
cd pcre-8.30
./configure --prefix=/usr/local/pcre
make;make install
cd ../
tar -jxvf httpd-2.4.2.tar.bz2
tar -jxvf httpd-2.4.2-deps.tar.bz2
cd httpd-2.4.2
#隐藏apache版本信息
- sed -i 's/#define AP_SERVER_BASEPRODUCT "Apache"/#define AP_SERVER_BASEPRODUCT "Microsoft-IIS 5.0"/' include/ap_release.h
- sed -i 's/#define PLATFORM "Unix"/#define PLATFORM "win32"/' os/unix/os.h
./configure --prefix=/usr/local/apache \
--enable-deflate \
--enable-headers \
--enable-mime-magic \
--enable-proxy \
--enable-ssl \
--enable-so \
--enable-rewrite \
--enable-suexec \
--with-suexec-bin=/usr/sbin/suexec \
--with-suexec-caller=www \
--with-pcre=/usr/local/pcre/bin/pcre-config \
--with-mpm=prefork \
--with-ssl=/usr
make;make install
#make的时候报错,“/usr/lib/libexpat.so: could not read symbols: File in wrong format”
#解决方法:
\cp /usr/lib64/libexpat.* /usr/lib/
#配置自启动文件
cp /usr/local/apache/bin/apachectl /etc/init.d/httpd
vi /etc/init.d/httpd
#在首行#!/bin/sh下添加
# Startup script for the Apache Web Server
#
# chkconfig: - 85 15
# description: web server. It is used to serve \
# HTML files and CGI.
# processname: httpd
# pidfile: /usr/local/apache/logs/httpd.pid
# config: /usr/local/apache/conf/httpd.conf
#或者直接使用sed添加
- sed -i '/#!\/bin\/sh/a\# chkconfig: - 85 15\n# description: web server\n# processname: httpd\n# pidfile: /usr/local/apache/logs/httpd.pid\n# config: /usr/local/apache/conf/httpd.conf' /etc/init.d/httpd
#修改apache配置文件
cd /usr/local/apache/conf/
mv httpd.conf httpd.conf.bak
#配置mpm_prefork_module
mv extra/httpd-mpm.conf extra/httpd-mpm.conf.bak
- cat >> extra/httpd-mpm.conf <
-
- ServerLimit 10000
- StartServers 5
- MinSpareServers 5
- MaxSpareServers 10
- MaxRequestWorkers 10000
- MaxConnectionsPerChild 10000
-
- EOF
#配置虚拟主机(apache虚拟主机,通过IP访问默认会访问到第一个虚拟主机上,那么就第一个虚拟主机定义为127.0.0.1,目录指到/usr/local/apache/htdocs)
mv extra/httpd-vhosts.conf extra/httpd-vhosts.conf.bak
- cat >> extra/httpd-vhosts.conf <
- #Vhosts
-
- ServerAdmin king_819@163.com
- DocumentRoot "/usr/local/apache/htdocs"
-
- Options Indexes FollowSymLinks
- AllowOverride None
- Require all granted
-
- ServerName 127.0.0.1
-
-
- ServerAdmin king_819@163.com
- DocumentRoot "/data/www/kerry"
-
- Options Indexes FollowSymLinks
- AllowOverride None
- Require all granted
-
- ServerName
- ErrorLog "logs/kerry-error_log"
- CustomLog "|/usr/local/apache/bin/rotatelogs /data/logs/kerry/%y_%m_%d.access_log 86400" common
-
-
- ServerAdmin king_819@163.com
- DocumentRoot "/data/www/king"
-
- Options Indexes FollowSymLinks
- AllowOverride None
- Require all granted
-
- ServerName
- ErrorLog "logs/king-error_log"
- CustomLog "|/usr/local/apache/bin/rotatelogs /data/logs/king/%y_%m_%d.access_log 86400" common
-
- EOF
#设置apache自启动
- chmod 700 /etc/init.d/httpd
- /etc/init.d/httpd start
- /sbin/chkconfig --add httpd
- /sbin/chkconfig --level 2345 httpd on
- cd ../
#=================== Install PHP 5.4.0 ===============#
#编译安装相关支持库
cd /opt
tar -zxvf libiconv-1.14.tar.gz
cd libiconv-1.14/
./configure
make;make install
cd ../
tar -jxvf libmcrypt-2.5.8.tar.bz2
cd libmcrypt-2.5.8/
./configure
make;make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make;make install
cd /opt
tar -jxvf mhash-0.9.9.9.tar.bz2
cd mhash-0.9.9.9/
./configure
make;make install
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
cd /opt
tar -zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure
make;make install
#编译php,这里我们为php打入补丁.有助于防止邮件发送被滥用(多用户)以及在邮件中提供有价值的信息.补丁介绍信息请点击:~steveb/patches/php-mail-header-patch/
cd /opt
tar -jxvf php-5.4.0.tar.gz
patch -d php-5.4.0 -p1 < php5-mail-header.patch
cd php-5.4.0
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-apxs2=/usr/local/apache/bin/apxs \
--with-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-iconv-dir=/usr/local \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--with-curlwrappers \
--enable-mbregex \
--enable-mbstring \
--with-mcrypt \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-ldap \
--with-ldap-sasl \
--with-xmlrpc \
--enable-zip \
--enable-soap
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-production /usr/local/php/etc/php.ini
cd ../
#安装php扩展模块
cd /opt
tar -zxvf memcache-3.0.6.tgz
cd memcache-3.0.6/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir --enable-memcache
make;make install
cd ../
#make时出错(如下图)
#解决办法:修改memcache.c
将721行:
zend_list_insert(mmc, le_memcache_server);
改为:
zend_list_insert(mmc TSRMLS_CC, le_memcache_server);
将738行:
zend_list_insert(mmc, le_memcache_server);
改为:
zend_list_insert(mmc TSRMLS_CC, le_memcache_server);
将778行:
list_id = zend_list_insert(pool, le_memcache_pool);
改为:
list_id = zend_list_insert(pool TSRMLS_CC, le_memcache_pool);
将839行:
list_id = zend_list_insert(pool TSRMLS_CC, le_memcache_pool);
改为:
list_id = zend_list_insert(pool, le_memcache_pool);
#用sed修改
- sed -i 's#zend_list_insert(mmc, le_memcache_server);#zend_list_insert(mmc TSRMLS_CC, le_memcache_server);#' memcache.c
- sed -i 's#list_id = zend_list_insert(pool, le_memcache_pool);#list_id = zend_list_insert(pool TSRMLS_CC, le_memcache_pool);#' memcache.c
#php的扩展memcache,不支持cas,所以我们要装memcached扩展,memcached扩展是基于libmemcached,所以要先安装libmemcached
#安装memcached的服务端支持库
cd /opt
tar -xzf libevent-2.0.18-stable.tar.gz
cd libevent-2.0.18-stable
./configure
make;make install
ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib
#安装Memcached服务端
cd /opt
tar -xzf memcached-1.4.13.tar.gz
cd memcached-1.4.13
./configure --prefix=/usr/local/memcached --with-libevent=/usr
make;make install
cd /opt
tar -zxvf libmemcached-1.0.2.tar.tar
cd libmemcached-1.0.2
./configure --prefix=/usr/local/libmemcached --with-memcached
make;make install
#安装php的memcached扩展库
cd /opt
tar -zxvf memcached-2.0.1.tgz
cd memcached-2.0.1
/usr/local/php/bin/phpize
./configure --enable-memcached --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --with-memcached --with-zlib
make;make install
#因eaccelerator-0.9.6.1不支持php 5.4.0,所以就改用XCache 2.0.0
cd /opt
tar -zxvf xcache-2.0.0.tar.gz
cd xcache-2.0.0
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make;make install
#安装pdo扩展
cd /opt
php-5.4.0/ext/pdo_mysql
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql
make;make install
cd /opt
tar -jxvf ImageMagick-6.7.6-7.tar.bz2
cd ImageMagick-6.7.6-7
./configure --prefix=/usr/local/imagemagick
make;make install
#imagick最新正式版为imagick-3.0.1.tgz,但imagick-3.0.1.tgz在make的时候会出现大量的报错信息,所以就改用imagick-3.1.0RC1.tgz
cd /opt
tar -zxvf imagick-3.1.0RC1.tgz
cd imagick-3.1.0RC1/
export PKG_CONFIG_PATH=/usr/local/imagemagick/lib/pkgconfig
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-imagick=/usr/local/imagemagick
make;make install
cd /opt
tar -jxf ioncube_loaders_lin_x86.tar.bz2
cd ioncube
mkdir /usr/local/ioncube
mv ioncube_loader_lin_5.2.so /usr/local/ioncube/
#修改php.ini添加php扩展
- sed -i 's#; extension_dir = "./"#extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20100525/"\nextension = "memcache.so"\nextension = "pdo_mysql.so"\nextension = "memcached.so"\nextension = "imagick.so"\n#' /usr/local/php/etc/php.ini
- cat >> /usr/local/php/etc/php.ini <
- [xcache-common]
- extension = xcache.so
- [xcache]
- xcache.shm_scheme = "mmap"
- xcache.size = 256M
- xcache.count = 8
- xcache.slots = 8K
- xcache.ttl = 0
- xcache.gc_interval = 0
- xcache.var_size = 8M
- xcache.var_count = 8
- xcache.var_slots = 8K
- xcache.var_ttl = 0
- xcache.var_maxttl = 0
- xcache.var_gc_interval = 300
- xcache.test = Off
- xcache.readonly_protection = Off
- xcache.mmap_path = "/dev/zero"
- xcache.coredump_directory = ""
- xcache.cacher = On
- xcache.stat = On
- xcache.optimizer = Off
- [xcache.coverager]
- xcache.coverager = Off
- xcache.coveragedump_directory = ""
- EOF
#隐藏php版本
- sed -i 's#expose_php = On#expose_php = Off#' /usr/local/php/etc/php.ini
#php安全设置,禁用函数
- sed -i 's#disable_functions =#disable_functions =phpinfo,exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source#' /usr/local/php/etc/php.ini
#重启apache,查看php扩展加载的情况
/etc/init.d/httpd restart
#============= Install Nginx ===================#
cd /opt
tar -zxvf nginx-1.2.0.tar.gz
cd nginx-1.2.0
./configure --user=www --group=www --prefix=/usr/local/nginx --with-pcre=/opt/pcre-8.30 --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-google_perftools_module
make;make install
cd ../
#添加nginx启动脚本
- cat >> /etc/init.d/nginx <
- #! /bin/sh
- ulimit -n 65535
- # Description: Startup script for nginx
- # chkconfig: 2345 55 25
- PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
- DESC="nginx daemon"
- NAME=nginx
- DAEMON=/usr/local/nginx/sbin/$NAME
- CONFIGFILE=/usr/local/nginx/conf/nginx.conf
- PIDFILE=/usr/local/nginx/logs/$NAME.pid
- SCRIPTNAME=/etc/init.d/$NAME
- set -e
- [ -x "$DAEMON" ] || exit 0
- do_start() {
- $DAEMON -c $CONFIGFILE || echo -n "nginx already running"
- }
- do_stop() {
- kill -QUIT `cat $PIDFILE` || echo -n "nginx not running"
- }
- do_reload() {
- kill -HUP `cat $PIDFILE` || echo -n "nginx can't reload"
- }
- case "$1" in
- start)
- echo -n "Starting $DESC: $NAME"
- do_start
- echo "."
- /etc/init.d/httpd start
- ;;
- stop)
- echo -n "Stopping $DESC: $NAME"
- do_stop
- echo "."
- /etc/init.d/httpd stop
- ;;
- reload)
- echo -n "Reloading $DESC configuration..."
- do_reload
- echo "."
- /etc/init.d/httpd restart
- ;;
- restart)
- echo -n "Restarting $DESC: $NAME"
- do_stop
- sleep 1
- do_start
- echo "."
- /etc/init.d/httpd restart
- ;;
- *)
- echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
- exit 3
- ;;
- esac
- exit 0
- EOF
#添加nginx配置文件
mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
#将nginx添加到启动服务中
chmod 700 /etc/init.d/nginx
/etc/init.d/nginx start
/sbin/chkconfig --add nginx
/sbin/chkconfig --level 2345 nginx on
#每天定时切割Nginx日志
- cat >> /usr/local/nginx/sbin/cut_nginx_log.sh <
- #!/bin/bash
- # This script run at 00:00
- # The Nginx logs path
- logs_path_kerry="/data/logs/kerry/"
- logs_path_kerry="/data/logs/king/"
- mv ${logs_path_kerry}kerry_nginx.log ${logs_path_kerry}$kerry_nginx_$(date -d "yesterday" +"%Y%m%d").log
- mv ${logs_path_king}king_nginx.log ${logs_path_king}king_nginx_$(date -d "yesterday" +"%Y%m%d").log
- kill -USR1 `cat /usr/local/nginx/nginx.pid`
- EOF
chmod +x /usr/local/nginx/sbin/cut_nginx_log.sh
#添加计划任务,每天凌晨00:00切割nginx访问日志
crontab -e
00 00 * * * /bin/bash /usr/local/nginx/sbin/cut_nginx_log.sh
#为apache安装rpaf模块,该模块用于apache做后端时获取访客真实的IP
#使用apxs安装模块.这里要使用此前apache编译安装后的apxs
cd /opt
tar -zxf mod_rpaf-0.6.tar.gz
cd mod_rpaf-0.6
/usr/local/apache/bin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c
#错误提示:
mod_rpaf-2.0.c: In function 'rpaf_cleanup':
mod_rpaf-2.0.c:150: error: 'conn_rec' has no member named 'remote_ip'
mod_rpaf-2.0.c:151: error: 'conn_rec' has no member named 'remote_addr'
mod_rpaf-2.0.c:151: warning: implicit declaration of function 'inet_addr'
mod_rpaf-2.0.c:151: error: 'conn_rec' has no member named 'remote_ip'
mod_rpaf-2.0.c: In function 'change_remote_ip':
mod_rpaf-2.0.c:164: error: 'conn_rec' has no member named 'remote_ip'
mod_rpaf-2.0.c:183: error: 'conn_rec' has no member named 'remote_ip'
mod_rpaf-2.0.c:186: error: 'conn_rec' has no member named 'remote_ip'
mod_rpaf-2.0.c:187: error: 'conn_rec' has no member named 'remote_addr'
mod_rpaf-2.0.c:187: error: 'conn_rec' has no member named 'remote_ip'
apxs:Error: Command failed with rc=65536
#解决办法:
#http://httpd.apache.org/docs/trunk/developer/new_api_2_4.html
#将150、151、164、183、186、187这几行的remote_ip修改成client_ip,remote_addr修改成client_addr
#编辑/usr/local/apache/conf/httpd.conf,添加模块参数,查找LoadModule php5_module modules/libphp5.so,在下方添加:
- LoadModule rpaf_module modules/mod_rpaf-2.0.so
- #Mod_rpaf settings
- RPAFenable On
- #上面出现的192.168.9.9请修改为你本机所监听web服务的ip.多个IP用空格空开
- RPAFproxy_ips 127.0.0.1 192.168.9.9
- RPAFsethostname On
- RPAFheader X-Forwarded-For