立即注册 登录
社友网(新版) 返回首页

admin的个人空间 http://su.sseuu.com/?1 [收藏] [复制] [分享] [RSS]

日志

CentOS 5 全功能WWW服务器搭建全教程

已有 454 次阅读2010-11-9 07:01 |个人分类:CentOS|

CentOS 5 全功能WWW服务器搭建全教程
转:主要做为历史记录,以后用。另外很少见这么好的编译的文章,其实我不推荐用编译安装。但这个文章不错.
V3.0 2007年11月11日 将搭建教程划分阶段以适应不同需要。加入程序优化,程序安全,加入memcache,squid, mod_security等高级应用, 安装vBulletin+vBseo, 服务器安全设置,以及日常管理功能。
V2.0 2007年10月18日 操作系统更新为centos 5.0 apache 更新为2.2.6版本,php更新为5.2.5, mysql 更新为5.0.46, Zend Optimizer 更新为3.3.0。解决新平台下的一些问题。修正文字错误。
v1.2 2007年2月12日 apache 更新为2.2.4版本,增加mod_ssl和openssl支持,增加apr和apr-util的编译, php更新为5.2.1,增加集成Suhosin补丁,mysql 更新为5.0.33, Zend Optimizer 更新为3.2.2,增加phpmyadmin。修改一些文字错误。
v1.0 2007年1月11日
V3.0版本文字文明在
http://www.cnprint.org/bbs/blogs/1/blog66.html
长期在线维护。欢迎转帖,转帖时请勿删除本说明。
By 二戒
先声明,以下的这些文字个人的安装笔记,是参考了网上许多高手的成功经验组合而成的,说实话我自己的东西不多 :)我想把这些经验写下来,既可以对其他跟我一样摸索的兄弟一个经验,也可以防止以后自己都忘记了,嘿嘿。
所以如果某个大虾看到我借用了你的资源,请不要生气。同时欢迎大家指出错误。
一、基本系统安装
1、下载CentOS 5
我是下载的DVD版本,大家也可以下载服务器CD安装版本,其实都差不多。大家可以到这儿下载,速度很快的。
http://ftp.iasi.roedu.net/mirrors/centos.org/5/isos/i386/CentOS-5.0-i386-bin-DVD.iso
建议在windows下用BT或迅雷下载,速度会快很多。
下载后刻录成光盘。我建议你刻录DVD啦,如果是菜鸟,把图形界面也装上,可以在图形界面进行学习,当然强烈不建议在服务器上装桌面。
Centos 5系列任何一个版本就行了,安装后可以通过yum upgrade直接更新为最新版本。
2、安装CentOS 5
作为服务器,不安装不需要的组件,所以在选择组件的时候,除了选择FTP SERVER外取消所有组件的选择。也不要选web服务器。因为我们后面要手动编译安装。
系统约定RPM包和源码包存放位置
RPM包和源码包存放位置 /usr/local/src
源码包编译安装位置(prefix) /usr/local/xxx
脚本以及维护程序存放位置 /usr/local/sbin
MySQL 数据库位置 /var/lib/mysql
Apache 网站根目录 /usr/local/apache2/htdocs
Apache 虚拟主机日志根目录 /data/logs/www
yum RPM包信息文件 /etc/yum.list
3、系统环境部署及调整
(1)检查系统是否正常
# more /var/log/messages //检查有无系统内核级错误信息
# demesg //检查硬件设备是否有错误信息
# ifconfig //检查网卡设置是否正确
# ping www.163.com // 检查网络是否正常
(2)关闭不需要的服务
# export LANG='en_US' //设置语言
# setup //选择启动的服务
进入system service 选项。
以space 键选定所需服务。
以下仅列出需要启动的服务,未列出的服务一律关闭:
crond
irqbalance 仅当服务器CPU为S.M.P架构或支持双核心、HT技术时,才需开启,否则关闭。
microcode_ctl
network
iptables
vsftpd
sshd
syslog
yum-updatesd
(3)修改/etc/yum.repos.d/CentOS-Base.repo,将镜象站点地址改为在中国的镜象站点地址。不然我们通过yum安装软件速度会极慢。修改如下:
# CentOS-Base.repo
#
# This file uses a new mirrorlist system developed by Lance Davis for CentOS.
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#
[base]
name=CentOS-$releasever - Base
baseurl=http://mirrors.shlug.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
protect=1
#released updates
[updates]
name=CentOS-$releasever - Updates
baseurl=http://mirrors.shlug.org/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
protect=1
#packages used/produced in the build but not released
[addons]
name=CentOS-$releasever - Addons
baseurl=http://mirrors.shlug.org/centos/$releasever/addons/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
protect=0
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
baseurl=http://mirrors.shlug.org/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
protect=0
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
baseurl=http://mirrors.shlug.org/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
protect=1
#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib
baseurl=http://mirrors.shlug.org/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
protect=0
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
保存文件。
(4)更新系统,我们使用yum,
运行:
# yum upgrade
建议更新所有列出的程序,包括内核,rhel 5.X的稳定性还要继续努力呢。
(5)定时校正服务器时间
# yum install –y ntp
# crontab -e
0 23 * * * /usr/sbin/ntpdate 210.72.145.44
以上命令设置好后存盘。
# /sbin/service crond reload
重载计划任务配置
您的机器将在每天的23:00根据中国国家授时中心的NTP服务器时间自动校准时间。
(6)FTP服务器的配置
vi /etc/vsftpd/vsftpd.conf
把anonymous_enable=YES
修改为anonymous_enable=No不允许匿名登录。
添加两句
chroot_local_user=yes
listen_port=2121
将用户锁定在主目录,并将FTP监听端口修改为2121
把ftpd_banner=*前的注释去掉。后面改成你的欢迎信息(这样设置可以避免显示ftp服务器的版本信息)
然后保存,service vsftpd start就可以了。
这时应当添加用户,因为root默认不能通过FTP方式登录,也不安全。
groupadd upload
useradd upload -g upload -d /usr/local/apache2/htdocs/ -M
如果FTP登录时出现
ftp服务器连接失败,错误提示:
500 OOPS: cannot change directory:/home/*******
500 OOPS: child died
解决方法:
# setsebool ftpd_disable_trans 1
# service vsftpd restart
这样对于我们上传一些文件到系统中很方便。
大家有兴趣的可以看下这篇文章对vsftpd进行一个比较详细的了解
http://blog.chinaunix.net/u/10047/showart_198837.html
4、重新启动系统
# init 6
此时系统启动成功,可以删除老的内核
二、安装mysql, apache, php, Zend Optimizer等基本环境
5. 使用 yum 程序安装所需开发包(以下为标准的rpm包名称)
# yum install gcc gcc-c++ gcc-g77 flex bison autoconf automake bzip2-devel zlib-devel ncurses-devel libjpeg-devel libpng-devel libtiff-devel freetype-devel pam-devel openssl-devel libxml2-devel gettext-devel pcre-devel
#这里我们将编译GD所必须的一些小软件比如libpng,libtiff,freetype,libjpeg,gettext-devel等先用RPM 的方式一并安装好,避免手动编译浪费时间,同时也能避免很多错误,这几个小软件的编译很麻烦。这几个小软件编译错误了,GD当然安装不了,php5的编译 当然也没戏了。所以我们抓大放小,对这些小牛鬼蛇神采取快速简洁的方式进行安装。并且对服务器的性能也不能产生什么影响。
另外libxml2系统已经默认安装了,所以我们不需要手工编译了,直接安装它的开发包就行了。
6. 源码编译安装所需包 (Source)
(1) GD2
# cd /usr/local/src
# wget http://www.libgd.org/releases/gd-2.0.35.tar.gz
# tar xzvf gd-2.0.35.tar.gz
# cd gd-2.0.35
# yum install libtool libtool-ltdl
# aclocal
# CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -felide-constructors -fno-exceptions -fno-rtti -fomit-frame-pointer" ./configure --prefix=/usr/local/gd2 --mandir=/usr/share/man
// 注意,CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -felide-constructors -fno-exceptions -fno-rtti -fomit-frame-pointer" 这个环境参数只针对intel P4 芯片,如果你的CPU是AMD的,注意不能使用。请查看相应的编译优化参数。否则程序会无法编译,即使编译成功也无法运行,嘿嘿。
关于其他CPU的优化参见我的BLOG的一篇转贴:
http://www.cnprint.org/bbs/blogs/1/blog43.html
//./configure 配置。
# make //make 是用来编译的,它从 Makefile 中读取指令,然后编译。
# make install //make install 是用来安装的,它也从 Makefile 中读取指令,安装到指定的位置。
(2) Apache 日志截断程序
# cd /usr/local/src
# wget http://cronolog.org/patches/cronolog-1.7.0-beta.tar.gz
# tar cronolog-1.7.0-beta.tar.gz
# cd cronolog-1.7.0-beta
#CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -felide-constructors -fno-exceptions -fno-rtti -fomit-frame-pointer" ./configure --prefix=/usr/local/cronolog && make && make install
7、编译mysql 5.0.50
mysql 5.0.50是企业版本,貌似双数版本都是企业版本了。个人觉得代码质量要比社区版本要好一些。大家可以下载,免费使用。并不需要向mysql公司交钱。
#cd /usr/local/src
# wget http://mirror.provenscaling.com/mysql/enterprise/source/5.0/mysql-5.0.50.tar.gz
# tar xzvf mysql-5.0.50.tar.gz
# cd mysql-5.0.50
修改mysql 客户端最大连接数, 默认的只有100,远远达不到我们的要求。
# vi sql/mysqld.cc
搜索找到下面一行:
{"max_connections", OPT_MAX_CONNECTIONS,
"The number of simultaneous clients allowed.", (gptr*) &max_connections,
(gptr*) &max_connections, 0, GET_ULONG, REQUIRED_ARG, 100, 1, 16384, 0, 1,
0},
将其中的100改为1500, 当然小点也可以,根据你的需要来,不建议改的太大。
{"max_connections", OPT_MAX_CONNECTIONS,
"The number of simultaneous clients allowed.", (gptr*) &max_connections,
(gptr*) &max_connections, 0, GET_ULONG, REQUIRED_ARG, 1500, 1, 16384, 0, 1,
0},
保存。
# CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -felide-constructors -fno-exceptions -fno-rtti -fomit-frame-pointer" ./configure --prefix=/usr/local/mysql --localstatedir=/var/lib/mysql --with-comment=Source --with-server-suffix=-enterprise-gpl --with-mysqld-user=mysql --without-debug --with-big-tables --with-charset=utf8 --with-collation=utf8_general_ci --with-extra-charsets=gbk,latin1 --with-pthread --enable-static --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --enable-assembler --without-innodb --without-ndb-debug --without-isam --enable-local-infile --with-readline --with-raid
配置成功会提示:
MySQL has a Web site at http://www.mysql.com/ which carries details on the
latest release, upcoming features, and other information to make your
work or play with MySQL more productive. There you can also find
information about mailing lists for MySQL discussion.
Remember to check the platform specific part of the reference manual for
hints about installing MySQL on your platform. Also have a look at the
files in the Docs directory.
Thank you for choosing MySQL!
# make
编译的时间可能会比较长,毕竟优化的比较厉害。
# make install
编译安装完成后执行后续操作:
# useradd mysql //添加 mysql 用户
# cd /usr/local/mysql
# bin/mysql_install_db --user=mysql
# chown -R root:mysql . //设置权限,注意后面有一个 "."
# chown -R mysql /var/lib/mysql //设置 mysql 目录权限
# chgrp -R mysql . //注意后面有一个 "."
# cp share/mysql/my-medium.cnf /etc/my.cnf
# cp share/mysql/mysql.server /etc/rc.d/init.d/mysqld //开机自动启动 mysql。
# chmod 755 /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
#添加LIB PATH
echo "/usr/local/mysql/lib" >> /etc/ld.so.conf && ldconfig
vi /etc/my.cnf
修改 MySQL 配置,增加部分优化参数,如下:
[mysqld]
ft_min_word_len=2
运行以下命令即可启动 MySQL 服务器:
# /etc/rc.d/init.d/mysqld start //启动 MySQL
# bin/mysqladmin -u root password "password_for_root"
# service mysqld stop //关闭 MySQL
8. 编译安装 Apache
# cd /usr/local/src
# wget http://www.ip97.com/apache.org/httpd/httpd-2.2.6.tar.gz
# tar zxvf httpd-2.2.6.tar.gz
# cd httpd-2.2.6
先依次安装apr和apr-util
# cd srclib/apr
# CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -felide-constructors -fno-exceptions -fno-rtti -fomit-frame-pointer" ./configure --prefix=/usr/local/apr --enable-threads --enable-other-child --enable-static
# make && make install
# cd ../apr-util
# CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -felide-constructors -fno-exceptions -fno-rtti -fomit-frame-pointer" ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/ --with-mysql=/usr/local/mysql
# make && make install
cd /usr/local/src/httpd-2.2.6
# CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -felide-constructors -fno-exceptions -fno-rtti -fomit-frame-pointer" ./configure --prefix=/usr/local/apache2 --enable-mods-shared=all --with-mysql=/usr/local/mysql --enable-cache --enable-file-cache --enable-mem-cache --enable-disk-cache --enable-static-support --enable-static-htpasswd --enable-static-htdigest --enable-static-rotatelogs --enable-static-logresolve --enable-static-htdbm --enable-static-ab --enable-static-checkgid --disable-cgid --disable-cgi --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr-util/ --enable-ssl --with-ssl=/usr/include/openssl --with-pcre
# make
# make install
注解:
./configure //配置源代码树
--prefix=/usr/local/apache2 //体系无关文件的顶级安装目录PREFIX ,也就Apache的安装目录。
--enable-module=so //打开 so 模块,so 模块是用来提 DSO 支持的 apache 核心模块
--enable-mods-shared=all //编译全部的模板,对于不需要我们可以在httpd.conf去掉。
--enable-cache //支持缓存
--enable-file-cache //支持文件缓存
--enable-mem-cache //支持记忆缓存
--enable-disk-cache //支持磁盘缓存
--enable-static-support //支持静态连接(默认为动态连接)
--enable-static-htpasswd //使用静态连接编译 htpasswd - 管理用于基本认证的用户文件
--enable-static-htdigest //使用静态连接编译 htdigest - 管理用于摘要认证的用户文件
--enable-static-rotatelogs //使用静态连接编译 rotatelogs - 滚动 Apache 日志的管道日志程序
--enable-static-logresolve //使用静态连接编译 logresolve - 解析 Apache 日志中的IP地址为主机名
--enable-static-htdbm //使用静态连接编译 htdbm - 操作 DBM 密码数据库
--enable-static-ab //使用静态连接编译 ab - Apache HTTP 服务器性能测试工具
--enable-static-checkgid //使用静态连接编译 checkgid
--disable-cgid //禁止用一个外部 CGI 守护进程执行CGI脚本
--disable-cgi //禁止编译 CGI 版本的 PHP
--enable-ssl // 编译 ssl模块。
我们不再使用worker模式编译apache,worker模式和php貌似有一些不协调不稳定之处。所以使用了默认的perfork模式。
将apache设置成开机自启动:
在/etc/rc.d/rc.local文件中加入一行
/usr/local/apache2/bin/apachectl start
这样每次重新启动系统以后,apache也会随系统一起启动.
或者将apache安装为系统服务
# cp /usr/local/apache2/bin/apachectl /etc/rc.d/init.d/httpd
然后 vi /etc/rc.d/init.d/httpd 添加(#!/bin/sh下面)
# chkconfig: 2345 50 90
# description: Activates/Deactivates Apache Web Server
最后,运行chkconfig把Apache添加到系统的启动服务组里面:
# chkconfig --add httpd
# chkconfig httpd on
9、编译php 5.2.5
Suhosin是php增强型安全补丁,可以编译到静态内核中,也可以编译成php动态扩展。我个人强烈你建议安装成静态内核。Suhosin已经进入 Gentoo Linux、FreeBSD、OpenSuSE Linux、Mandriva Linux、Debian Linux官方包。下面的以下先说静态安装步骤。当然你也可以在安装php后将它编译成php的动态扩展。
# cd /usr/local/src
# wget http://cn.php.net/get/php-5.2.5.tar.gz/from/this/mirror
wget http://www.hardened-php.net/suhosin/_media/suhosin-patch-5.2.5-0.9.6.2.patch.gz
# tar zxvf php-5.2.5.tar.gz
# gunzip suhosin-patch-5.2.5-0.9.6.2.patch.gz
# cd php-5.2.5
# patch -p 1 -i ../suhosin-patch-5.2.5-0.9.6.2.patch
# ./buildconf --force
# CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -felide-constructors -fno-exceptions -fno-rtti -fomit-frame-pointer" ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-pear=/usr/share/php --with-zlib-dir --with-bz2 --with-libxml-dir=/usr --with-gd=/usr/local/gd2 --enable-gd-native-ttf --enable-gd-jis-conv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-ttf=shared,/usr --enable-mbstring --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-config-file-path=/etc --with-iconv --disable-ipv6 --enable-static --enable-zend-multibyte --enable-inline-optimization --enable-zend-multibyte --enable-sockets --enable-soap --with-openssl --with-gettext --enable-suhosin
配置成功会提示:
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+
Thank you for using PHP.
# make
# make test
# make install
# cp php.ini-recommended /etc/php.ini
# echo "/usr/local/php/lib" >> /etc/ld.so.conf && ldconfig
在这里也顺便说一下将suhosin安装成为php的动态扩展的方法。毕竟网上根本不见它的中文安装教程。
虽然我个人不推荐这种方式。
wget http://www.hardened-php.net/suhosin/_media/suhosin-0.9.20.tgz
tar zxvf suhosin-0.9.20.tgz
cd suhosin-0.9.20
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
会提示编译的模块存在的目录,记住它。
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20060613/
然后在php.ini中增加一行下列语句。
extension="/usr/local/php/lib/php/extensions/no-debug-zts-20060613/suhosin.so"
10、整合apache 与php
# vi /usr/local/apache2/conf/httpd.conf
在最后一行加上:
AddType application/x-httpd-php .php
查找:(设置 WEB 默认文件)
DirectoryIndex index.html
修改为:
DirectoryIndex index.php index.html index.htm
找到这一段:
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride none
更改为AllowOverride all
允许apache rewrite
保存httpd.conf,退出。
# /usr/local/apache2/bin/apachectl restart //重启 Apache
这时会出现错误:
/usr/local/apache2/bin/apachectl start
httpd: Syntax error on line 107 of /usr/local/apache2/conf/httpd.conf: Cannot load /usr/local/apache2/modules/libphp5.so into server: /usr/local/apache2/modules/libphp5.so: cannot restore segment prot after reloc: Permission denied
不急,我们慢慢解决。
这个Permission denied问题,在centos 5下面一般是Selinux引起的,作为生产用服务器,我建议你千万别草率地关掉Selinux一了百了。就像家里的防盗网,阻碍了你的猫自由进出窗户, 你不能为了猫方便,就把防盗网简单拆除是同样的道理。我看见网上许多人建议把Selinux简单关闭来解决这个问题,这是削足适履的做法,不值得提倡。
我们可以这样操作:
# audit2allow -a //查看究竟问题出在什么地方
allow unconfined_t usr_t:file execmod;
allow useradd_t var_log_t:file { read write };
然后
# cd /etc/selinux/targeted/modules/
# audit2allow -M local -d
屏幕产生如下提示:
Generating type enforcment file: local.te
Compiling policy
checkmodule -M -m -o local.mod local.te
semodule_package -o local.pp -m local.mod
******************** IMPORTANT ***********************
In order to load this newly created policy package into the kernel,
you are required to execute
semodule -i local.pp
我们运行
# semodule -i local.pp
这样就让Selinux加载了新的规则。
更详细的内容请看我在BLOG上的转贴:
http://www.cnprint.org/bbs/blogs/1/blog48.html
重启apache
哈哈,apache不会再报错了吧?
这样我保留了selinux的功能,同时apache也能正常运行。
PHP5.1.x开始需要设置时区,默认时区与中国时区差8个小时,这种情况需要在php.ini中这么设置,找到date.timezone,去掉前面 的分号,修改为以下值,大陆地区可用的值是:Asia/Chongqing ,Asia/Shanghai ,Asia/Urumqi (依次为重庆,上海,乌鲁木齐)
不然一些php程序的时间老是和中国标准时间相差8个小时。我的我的VBB论坛在windows上就是这样。这儿有亚洲地区的对应时区。
http://www.php.net/manual/en/timezones.asia.php
11、安装 Zend Optimizer
# cd /usr/local/src
# wget http://downloads.zend.com/optimizer/3.3.0/ZendOptimizer-3.3.0-linux-glibc21-i386.tar.gz
# tar xzvf ZendOptimizer-3.3.0-linux-glibc21-i386.tar.gz
# ./ZendOptimizer-3.3.0-linux-glibc21-i386/install.sh
按照它的提示一步步进行就行了。
总之一句话。如果你的服务器环境不需要ZendOptimizer,那么能不安就不安装这个。避免和eaccelerator冲突。
12. 查看确认 L.A.M.P 环境信息
vi /usr/local/apache2/htdocs/phpinfo.php
新增加下面一行,并保存。
# chmod 755 /usr/local/apache2/htdocs/phpinfo.php
用浏览器打开 http://192.168.9.150/phpinfo.php
检查 phpinfo中的各项信息是否正确。
测试php与mysql的连接
# vi /usr/local/apache2/htdocs/testdb.php
增加下面几行,并保存。
# chmod 755 /usr/local/apache2/htdocs/testdb.php
# service mysqld start
用浏览器打开 http://192.168.9.150/testdb.php
如果输出success就OK了
到这一步,一个基本的lamp就建立完成了。如果你是初学者,下面的步骤根据需要参考,不必全部照做,记住一句话,功能越多,越容易出错,在任何领域都是这样。
三、LAMP环境加速,包括squid, memcache, eaccelerator
13、安装eaccelerator
eaccelerator是php的加速软件,使用后php的执行效率会有较大幅度的提升。目前eaccelerator 0.9.5.2已经和ZendOptimizer-3.3.0能够基本上兼容啦。不过我个人觉得,ZendOptimizer-3.3.0没有加速的功 能,反而使php运行变慢,只是起到了运行zend加密文件的作用而已。闲话不多说了,大家有兴趣的,可以去google下。
# cd /usr/local/src
# wget http://bart.eaccelerator.net/source/0.9.5.2/eaccelerator-0.9.5.2.tar.bz2
# tar -jxvf eaccelerator-0.9.5.2.tar.bz2
# cd eaccelerator-0.9.5.2
export PHP_PREFIX="/usr/local/php"
$PHP_PREFIX/bin/phpize //指定一下php的目录
# CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -felide-constructors -fno-exceptions -fno-rtti -fomit-frame-pointer" ./configure --enable-eaccelerator=shared --with-php-config=$PHP_PREFIX/bin/php-config --with-eaccelerator-shared-memory // 设置
# make & make install
编译安装后我们会看到屏幕提示的eaccelerator.so所在的目录,php5.2.X系列是在 /usr/local/php/lib/php/extensions/no-debug-zts-20060613/eaccelerator.so, 记住这个路径,待会要用到。
修改php.ini(安装完zend之后,php.ini存放于/usr/local/Zend/etc)
在文件最后,zend之前,注意,这部分内容务必放在zend之前,不然可能会出现不可预期的服务器问题。添加下列信息:
[eaccelerator]
extension="/usr/local/php/lib/php/extensions/no-debug-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="32"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
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"
然后
# mkdir /tmp/eaccelerator // 建立目录
# chmod 0777 /tmp/eaccelerator // 修改目录属性
解释:
zend_extension 是安装完程序自动指示给我们的文件路径和文件名
如果您使用 thread safe 编译安装 PHP,则您必须使用 “zend_extension_ts” 代替 默认添加的“zend_extension”。我就必须修改这里,否则不起作用。
zend_extension_ts="/home/php/lib/php/extensions/no-debug-zts-20060613/eaccelerator.so"
实际上,安装好eAccelerator以后, /home/php/lib/php/extensions/下会生成2个目录,一个是no-debug-zts-xxxxxxxx,一个是no- debug-non-zts-xxxxxxxx,2个目录下都有eaccelerator.so文件。
eaccelerator.shm_size="32"
eAccelerator 可以使用的共享内存的数量 (以兆为单位) . "0" 是指操作系统的默认值. 默认值是 "0".可根据服务器的实际情况来调整,16,32,64,128都是可以的。
eaccelerator.cache_dir="/home/php/tmp"
这个目录是给磁盘缓存使用. eAccelerator 在这里储存预先编译好的代码, 进程数据, 内容以及用户的自定义内容. 同样的数据也能被储存在共享内存中 (这样可以提高访问速度). 默认的设置是 "/tmp/eaccelerator".
eaccelerator.enable="1"
开启或关闭 eAccelerator。”1″ 为开启,”0″ 为关闭。默认值为 “1″。
eaccelerator.optimizer="1"
启或关闭内部优化器,可以提升代码执行速度。”1″ 为开启,”0″ 为关闭。默认值为 “1″。
eaccelerator.check_mtime="1"
打开或者关闭 PHP 的文件修改检查. "1" 是指打开, "0" 是指关闭. 如果您在修改以后重新编译 PHP 的文件,那么您应当设置为 "1". 默认值是 "1".
eaccelerator.debug="0"
开启或关闭调试日志记录。”1″ 为开启,”0″ 为关闭。默认值为 “0″。
eaccelerator.filter=""
判断哪些 PHP 文件必须缓存。您可以指定缓存和不缓存的文件类型(如 “*.php *.phtml”等)
如果参数以 “!” 开头,则匹配这些参数的文件被忽略缓存。默认值为 “”,即,所有 PHP 文件
都将被缓存。
eaccelerator.shm_max="0"
当使用 ” eaccelerator_put() ” 函数时禁止其向共享内存中存储过大的文件。该参数指定允许
存储的最大值,单位:字节 (10240, 10K, 1M)。”0″ 为不限制。默认值为 “0″。
eaccelerator.shm_ttl="0"
当 eAccelerator 获取新脚本的共享内存大小失败时,它将从共享内存中删除所有在
最后 “shm_ttl” 秒内无法存取的脚本缓存。默认值为 “0″,即:不从共享内春中删除
任何缓存文件。
eaccelerator.shm_prune_period="0"
当 eAccelerator 获取新脚本的共享内存大小失败时,他将试图从共享内存中删除早于
“shm_prune_period” 秒的缓存脚本。默认值为 “0″,即:不从共享内春中删除
任何缓存文件。
eaccelerator.shm_only="0"
允许或禁止将已编译脚本缓存在磁盘上。该选项对 session 数据和内容缓存无效。默认
值为 “0″,即:使用磁盘和共享内存进行缓存。
eaccelerator.compress="1"
允许或禁止压缩内容缓存。默认值为 “1″,即:允许压缩。
eaccelerator.compress_level="9"
指定内容缓存的压缩等级。默认值为 “9″,为最高等级。
最后重新启动apachectl
重启apache,phpinfo显示:
This program makes use of the Zend Scripting Language Engine:
Zend Engine v2.2.0, Copyright (c) 1998-2006 Zend Technologies
with eAccelerator v0.9.5.2, Copyright (c) 2004-2006 eAccelerator, by eAccelerator
with Zend Extension Manager v1.0.11, Copyright (c) 2003-2006, by Zend Technologies
with Zend Optimizer v3.3.0, Copyright (c) 1998-2006, by Zend Technologies
也会有eAccelerator的具体信息。
14、安装Squid,提供web反向代理缓存
Squid是一个更专业的代理服务器,性能和效率会比Apache的mod_proxy高很多。
Squid Internet Object Cache (Harvest Project的后续版本) 是美国政府大力助的一项研究计划,其目的为解决网络带宽不足的问题,是现在Unix系统上使用者最多功能也最完整的一套软体。Apache和 Netscape虽附有相关的Proxy模块,但因其功能简单而不够普及。有关squid的详细说明可到squid网站(http: //www.squid-cache.org)查询。
反向代理是和前两种代理完全不同的一种代理服务。使用它可以降低原始WEB服务器的负载。反向代理服务器承担了对原始WEB服务器的静态页面的请求,防止 原始服务器过载。它位于本地WEB服务器和Internet之间,处理所有对WEB服务器的请求,组织了WEB服务器和Internet的直接通信。如果 互联网用户请求的页面在代理服务器上有缓冲的话,代理服务器直接将缓冲内容发送给用户。如果没有缓冲则先向WEB服务器发出请求,取回数据,本地缓存后再 发送给用户。这种方式通过降低了向WEB服务器的请求数从而降低了WEB服务器的负载。
下载squid
# wget http://www.squid-cache.org/Versions/v2/2.6/squid-2.6.STABLE16.tar.bz2
# tar jxvf squid-2.6.STABLE16.tar.bz2
# cd squid-2.6.STABLE16
# CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -felide-constructors -fno-exceptions -fno-rtti -fomit-frame-pointer" ./configure --prefix=/usr/local/squid --enable-async-io=500 --with-maxfd=65536 --disable-delay-pools --disable-mem-gen-trace --disable-useragent-log --enable-kill-parent-hack --disable-arp-acl --enable-epoll --disable-ident-lookups --enable-snmp --enable-large-cache-files --with-large-files --with-pthreads --enable-underscore --enable-storeio="aufs,coss,diskd,ufs" --enable-err-language="Simplify_Chinese" --enable-default-err-languages="Simplify_Chinese"
# make && make install
如果是2.6的内核,才能支持epoll的IO模式,旧版本的内核则只能选择poll或其他模式了;另外,记得带上支持大文件的选项,否则在access log等文件达到2G的时候就会报错。
设定 squid 的配置大概如下内容:
# mv /usr/local/squid/etc/squid.conf /usr/local/squid/etc/squid.conf.bak
# vi /usr/local/squid/etc/squid.conf
#服务器IP 192.168.9.150
#监听服务器的80端口,反向代理,支持域名和IP的虚拟主机
http_port 192.168.9.150:80 vhost vport
#防止天涯盗链,转嫁给百度
acl tianya referer_regex -i tianya
http_access deny tianya
deny_info http://www.baidu.com/logs.gif tianya
#防止百度机器人爬死服务器,这将导致百度不能收录你的网站内容,请注意
acl AntiBaidu req_header User-Agent Baiduspider
http_access deny AntiBaidu
#Squid信息设置
visible_hostname www.cnprint.org
cache_mgr admin@cnprint.org
#squid使用的用户组和用户名
cache_effective_user nobody
cache_effective_group nobody
tcp_recv_bufsize 65535 bytes
client_persistent_connections off
server_persistent_connections on
half_closed_clients off
#单台使用,不使用该功能
icp_port 0
#设置Squid所能使用的内存共40MB,这个值因人而异,每次处理缓存大小为40MB,当缓存空间使用达到95%时新的内容将取代旧的而不直接添加到目录中,直到空间又下降到90%才停止这一活动
cache_mem 40 MB
cache_swap_low 90
cache_swap_high 95
#最大缓存文件大小,超过这个值则不缓存,这个值因人而异,maximum_object_size_in_memory #装入内存缓存的文件大小,这个值对Squid的性能影响比较大,因为默认值是8K,超过8K的文件都不装入内存,而实际应用中很多网页和图片等都超过 8KB, 个人认为如果缓存不装入内存而存在磁盘上,性能和apache直接读取磁盘文件没什么区别,甚至不如直接访问apache,现在设置成小于4兆的文件通通 装入内存缓存.
maximum_object_size 4096 KB
minimum_object_size 0 KB
maximum_object_size_in_memory 4096 KB
ipcache_size 1024
ipcache_low 90
ipcache_high 95
cache_replacement_policy lru
memory_replacement_policy lru
#磁盘缓存的类型和目录,大小,一二级目录的设置,这里磁盘缓存大小是100MB,都是 16*256 级子目录
cache_dir ufs /usr/local/squid/var/cache 100 16 256
#这个设置是不记录store.log
cache_store_log none
#设置默认刷新规则
refresh_pattern -i ^ftp: 1440 20% 10080
refresh_pattern -i ^gopher: 1440 0% 1440
refresh_pattern . 0 20% 4320
#不要相信ETag 因为有gzip
acl apache rep_header Server ^Apache
broken_vary_encoding allow apache
#设置超时策略
forward_timeout 20 seconds
connect_timeout 15 seconds
read_timeout 3 minutes
request_timeout 1 minutes
persistent_request_timeout 15 seconds
client_lifetime 15 minutes
shutdown_lifetime 5 seconds
negative_ttl 10 seconds
#打开emulate_httpd_log选项,将使Squid仿照Aapche的日志格式
emulate_httpd_log on
#日志格式combined的设置
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %h" "%{User-Agent}>h" %Ss:%Sh
#这里是设置pid和日志文件的位置,因人而异,同时日志格式是combined,awstats可以直接调用分析了
pid_filename /usr/local/squid/var/squid.pid
cache_log /usr/local/squid/var/logs/cache.log
access_log /usr/local/squid/var/logs/access.log combined
#设置不想缓存的目录或者文件类型
acl all src 0.0.0.0/0.0.0.0
acl QUERY urlpath_regex cgi-bin .php .cgi .avi .wmv .rm .ram .mpg .mpeg .zip .exe
cache deny QUERY
#2.6的反向代理加速配置
#代理到本机的80端口的服务,仅仅做为原始内容服务器
cache_peer 127.0.0.1 parent 80 0 no-query originserver login=PASS
#错误文档
error_directory /usr/local/squid/share/errors/Simplify_Chinese
保存。
mkdir /usr/local/squid/var/cache
chown -R nobody:nobody /usr/local/squid/var/cache
chown -R nobody:nobody /usr/local/squid/var/logs/
chmod 777 /usr/local/squid/var/cache
修改httpd.conf
初始化和启动squid
# /usr/local/squid/sbin/squid -z
# /usr/local/squid/sbin/squid -NCd1
第一条命令是先初始化squid缓存哈希子目录,只需执行一次即可。
最好还编辑/etc/hosts 文件
添加以下内容
192.168.9.150 cnprint.org www cnprint.org
这样免去查询DNS,速度也快一些。
现在大家肯定急着要打开浏览器访问你的网站看看效果吧,其实没啥变化,要等到有流量访问,squid把文件都装到内存后,效果才明显。可以用top命令观察squid的内存使用情况或者用
cat /usr/local/squid/var/logs/access.log |grep TCP_MEM_HIT
如果看到很多的TCP_MEM_HIT ,这表明该文件是从内存缓存读取的,squid已经起作用了!你再用浏览器打开该文件,应该是快如闪电了。。呵呵,大功告成了!还有其他类型的HIT,如 TCP_HIT等等,这些是从磁盘读取的,我觉得加速的意义不大,只不过缓解了apache的压力而已。
3. 启动squid:./bin/RunCache &
这边我使用squid附带的启动脚本来启动squid,有个好处就是,如果squid的进程死了的话,这个脚本会自动启动squid,对于运行在线上的服务器来说,这点太重要了。
开机自动运行squid
修改开机自动运行文件,将 /usr/local/squid/bin/RunCache & 加进开机自动执行的程序中。
因为第一次是以 root 身份启动,所以,/usr/local/squid/var/squid.out 及 /usr/local/squid/var/squid.pid 的拥有者是 root ,这会造成下列以 nobody 身份启动 squid 时,会有错误出现,所以,必须在第二次由 rc.local 启动前,先行将 squid.out 及 squid.pid 两个文件的所有者(owner)改为 nobody,即是
chown nobody:nobody /usr/local/squid/var/squid.out
chown nobody:nobody /usr/local/squid/var/logs/squid.pid
在 /etc/rc.d/rc.local 中加入
/usr/local/squid/bin/RunCache &

来自: http://hi.baidu.com/ganxuequan/blog/item/6995b813abbe19d5f6039e7d.html

全部作者的其他最新日志

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

小黑屋|Archiver|手机版|su.sseuu.com ( 粤ICP备12005776号

GMT+8, 2024-5-10 21:39 , Processed in 0.051656 second(s), 17 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2013 Comsenz Inc.

返回顶部