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

一、基本系统安装
1、下载CentOS 5
我是下载的DVD版本,大家也可以下载服务器CD安装版本,其实都差不多。大家可以到这儿下载,速度很快的。
http://ftp.iasi.roedu.net/mirrors/ce...86-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 //检查有无系统内核级错误信息
# dmesg //检查硬件设备是否有错误信息
# 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/mysq...-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/...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/...sin-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 //查看究竟问题出在什么地方
<no matches>
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/...21-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
新增加下面一行,并保存。
<?php phpinfo(); ?>
# 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
增加下面几行,并保存。

<?php
$link=mysql_connect('localhost','root','yourpassword');
if(!$link) echo "fail";
else echo "success";
mysql_close();
?>

# 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/....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/...ABLE16.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 %<st "%{Referer}>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 &

15、memcache + libevent 安装
memcached是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。最初为了加速 LiveJournal 访问速度而开发的,后来被很多大型的网站采用。起初作者编写它可能是为了提高动态网页应用,为了减轻数据库检索的压力,来做的这个缓存系统。它的缓存是一种分布式的,也就是可以允许不同主机上的多个用户同时访问这个缓存系统,这种方法不仅解决了共享内存只能是单机的弊端,同时也解决了数据库检索的压力,最大的优点是提高了访问获取数据的速度!基于memcached作者对分布式cache的理解和解决方案。 memcached完全可以用到其他地方比如分布式数据库,分布式计算等领域。
Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力。(关于Memcache的更多信息请Google)
Memcache官方网站:
http://www.danga.com/memcached
服务器端主要是安装memcache服务器端,目前的最新版本是 memcached-1.2.3。
另外,Memcache用到了libevent这个库用于Socket的处理,所以还需要安装libevent,libevent的最新版本是libevent-1.3e。(如果你的系统已经安装了libevent,可以不用安装)
官网:
http://www.monkey.org/~provos/libevent/
(1)、安装memcached服务器端
编译安装:
# cd /usr/local/src
# wget 
http://monkey.org/~provos/libevent-1.3e.tar.gz
# tar zxvf libevent-1.3e.tar.gz
# cd libevent-1.3e
#
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 && make && make install

# echo "/usr/local/lib" >> /etc/ld.so.conf && ldconfig
# cd ../
# wget  http://www.danga.com/memcached/dist/...d-1.2.3.tar.gz
# tar zxvf memcached-1.2.3.tar.gz
# cd memcached-1.2.3
#
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/memcached --with-libevent=/usr/local
# make && make install
备注:如果 libevent 不是安装在 /usr 目录下,那么需要把 libevent-1.3e.so.1 拷贝/链接到 /usr/lib 中,否则 memcached 有可能无法正常加载。
(2)、安装php对memcache支持模块
安装php-memcache模块,这是memcache的php客户端,php-memcache需要pecl库的支持。
# cd /usr/local/src
# wget 
http://pecl.php.net/get/memcache-2.2.0.tgz
# tar zxvf memcache-2.2.0.tgz
#cd memcache-2.2.0
#export PHP_PREFIX=/usr/local/php
#$PHP_PREFIX/bin/phpize
#
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-memcache --with-zlib-dir --with-php-config=$PHP_PREFIX/bin/php-config
#make && make install
修改php.ini
在最末尾增加一行:
extension="/usr/local/php/lib/php/extensions/no-debug-zts-20060613/memcache.so"
运行以下命令来启动 memcached:
#/usr/local/memcached/bin/memcached \
-l 192.168.9.139 -d -p 11211 -u nobody -m 128
表示用 daemon 的方式启动 memcached,监听在 192.168.9.19 的 11211 端口上,运行用户为 nobody,为其分配 128MB 的内存。
这时查看一下进程,看memcached启动成功没有。
top -U nobody
可以看到
5867 nobody 15 0 2352 684 276 S 0.0 0.1 0:00.00 memcached
说明 memcached安装成功,并正在运行。
设置成开机自动运行
在 /etc/rc.d/rc.local 中加入
/usr/local/memcached/bin/memcached -l 192.168.9.150 -d -p 11211 -u nobody -m 128

四、附加功能配置, 包括ssl, mysqlhotcopy, phpmyadmin

15、安装DBI和DBD for mysql

//用于提供perl访问mysql数据库的接口规范,请确认你已经安装了perl,一般默认系统都装上了。
因为我们要用到mysqlhotcopy功能,需要这两个小程序的支持。
# wget 
http://mirrors.xueron.com/CPAN/autho...I-1.601.tar.gz
首先,安装DBI包:
# tar zxvf DBI-1.061.tar.gz
# cd DBI-1.061
# perl Makefile.PL
# make
# make test
# make install

wget 
http://search.cpan.org/CPAN/authors/...l-4.005.tar.gz
# tar zxvf DBD-mysql-4.005.tar.gz
# cp /usr/local/mysql/lib/mysql/libmysqlclient.so.15 /usr/lib/
#
perl Makefile.PL --libs="-L/usr/local/mysql/lib/mysql -lmysqlclient -L/usr/lib -lz " --cflags=-I/usr/local/mysql/include/mysql --mysql_config=/usr/local/mysql/bin/mysql_config --testhost=127.0.0.1--testsocket=/tmp/mysql.sock --testdb=test --testuser=root --testpassword="youpassword"

# make
# make test
# make instll
测试执行 mysqlhotcopy 並出現如下类似錯誤訊息
#/usr/local/mysql/bin/mysqlhotcopy mysql /tmp/test -u root -p 'password'
Invalid db.table name 'mysql.mysql`.`activity' at /usr/local/bin/mysqlhotcopy line 855.
Ans:
找到了 [MySQL Bugs: #27303: mysqlhotcopy dies with error Invalid db.table name 'foo.bar`.`baz'] 说明将 mysqlhotcopy 文件修改一下后即可顺利执行 mysqlhotcopy 了
#vi /usr/local/mysql/bin/mysqlhotcopy //在第 835 下新增一行
835 my @dbh_tables = eval { $dbh->tables() };
836 map { s/^.*?\.//o } @dbh_tables; //加入此行
mysql 5.0.50后已经修正这个错误。

16、配置https
vi /usr/local/apache2/conf/httpd.conf
# 监听443端口,支持https连接
取消注释 httpd.conf 中的 Include conf/extra/httpd-ssl.conf
设置SSL并创建自己的CA
# cd /etc/pki/tls/misc
# ./CA -newca
屏幕上出现如下的提示:CA certificate filename (or enter to create)
这是要求输入要创建的CA的证书文件名, 可以直接回车或输入证书文件名。
Making CA certificate ...
Generating a 1024 bit RSA private key
.........++++++
................................++++++
writing new private key to './demoCA/private/./cakey.pem'
Enter PEM pass phrase:
Verifying password - Enter PEM pass phrase:-
此时要求输入和验证CA的私钥口令、国家代码(中国是CN)、省份、城市或地区、组织或企业名称、部门名称、CA的名称或服务器的主机名称、管理员电子邮件地址。
至此,在当前目录下生成了demoCA的目录,CA的证书就在该目录下,文件名为cacert.pem
生成服务器的证书请求
# ./CA -newreq
屏幕上出现如下的提示:
Generating a 1024 bit RSA private key
.....................................................++++++
.....++++++
writing new private key to 'newreq.pem'
Enter PEM pass phrase:
Verifying password - Enter PEM pass phrase:
此时要求输入和验证服务器的私钥口令、国家代码(中国是CN)、省份、城市或地区、组织或企业名称、部门名称、CA的名称或服务器的主机名称、管理员电子邮件地址。
Please enter the following 'extra' attributes
to be sent with your certificaterequest
A challenge password []:
An optional company name []:
.Request (and private key) is in newreq.pem
这是要求输入服务器的相关信息。
此时,在当前目录下生成了一个名为newreq.pem的文件,包含了要生成服务器数字证书的请求。
签署证书
# ./CA -sign
屏幕上出现如下的提示:
Using configuration from /usr/share/ssl/openssl.cnf
Enter PEM pass phrase:
此时一样需要输入CA的私钥口令、国家代码(中国是CN)、省份、城市或地区、组织或企业名称、部门名称、CA的名称或服务器的主机名称、管理员电子邮件地址。
Certificate is to be certified until Nov 19 13:46:19 2002 GMT (365 days)
Sign the certificate? [y/n]:y
这时显示证书请求文件中的各项信息,并询问是否要签署证书,回答y,进行签署。
1 out of 1 certificate requests certified, commit? [y/n]y
回答y,会显示已经签署的证书的信息,并在当前目录下生成服务器的证书文件newcert.pem。
# mkdir /usr/local/apache2/conf/ssl.crt/
# mkdir /usr/local/apache2/conf/ssl.key/
# cp newcert.pem /usr/local/apache2/conf/ssl.crt/server.pem
# cp newreq.pem /usr/local/apache2/conf/ssl.key/server.pem
更改服务器的证书文件的相关配置
# vi /usr/local/apache2/conf/extra/httpd-ssl.conf
查找并修改
# Server Certificate:
# Point SSLCertificateFile at a PEM encoded certificate. If
# the certificate is encrypted, then you will be prompted for a
# pass phrase. Note that a kill -HUP will prompt again. Keep
# in mind that if you have both an RSA and a DSA certificate you
# can configure both in parallel (to also allow the use of DSA
# ciphers, etc.)
SSLCertificateFile /usr/local/apache2/conf/ssl.crt/server.pem
#SSLCertificateFile /usr/local/apache2/conf/server-dsa.crt
# Server Private Key:
# If the key is not combined with the certificate, use this
# directive to point at the key file. Keep in mind that if
# you've both a RSA and a DSA private key you can configure
# both in parallel (to also allow the use of DSA ciphers, etc.)
SSLCertificateKeyFile /usr/local/apache2/conf/ssl.key/server.pem
#SSLCertificateKeyFile /usr/local/apache2/conf/server-dsa.key
示例文件
在SSL的根目录中生成一个index.html,它是如下所示:
<html>
这是SSL示例!
</html>
测试
假如Web服务器的DNS名称是
www.cnprint.org .
在浏览器的URL地址栏里输入 
http://www.cnprint.org/,浏览器便会显示APACHE安装时确省的Test  Page.
在浏览器的URL地址栏里输入 
https://www.cnprint.org/,注意:是  https 而不是http !
浏览器会提示站点已经采用了SSL进行数据的加密传输.由于我们的CA证书不是浏览器缺省的信任的根证书,所以,浏览器会说无法确认服务器的证书可信。暂时不管,一直NEXT,最后,浏览器会显示:这是SSL示例!
可以把CA的证书放在非SSL的站点上,让浏览器下载并安装CA证书,并将其设置成可信任的根证书,便可解决上面的问题.8 解除HTTPD起动时的口令输入。
由于安全的原因,Web服务器的私钥是口令加密了的,每次重新起动HTTPD或Linux时,都会要求输入Web服务器的私钥的口令。
如果要解除HTTPD起动时的口令输入,可以这样:
# cd /usr/local/apache2/conf/ssl.key/
# cp server.pem server.pem.org
# openssl rsa -in server.pem.org -out server.pem
# chmod 400 server.pem
另外在网上看到一个方法,我没有试。有兴趣的可以试下。
创建SSL密码自动应答文件,否则每次Apache启动的时候,都会要求你输入SSL的密码.
创建 /usr/local/apache2/conf/ssl.key/sendsslpwd ,内容如下.
#!/bin/bash
SSLpasswd="YOUR PASSPHRASE"
echo $SSLpasswd
chmod 755 /usr/local/apache2/conf/ssl.key/sendsslpwd
此时,Web服务器的私钥已经没有口令加密,一定要确保server.pem文件除root外,任何用户均无权读取它。

17、安装phpmyadmin,管理mysql数据库
# cd /usr/local/apache2/htdocs/
# wget 
http://nchc.dl.sourceforge.net/sourc...-8-only.tar.gz
# tar zxvf phpMyAdmin-2.11.1-all-languages-utf-8-only.tar.gz
# mv phpMyAdmin-2.11.1-all-languages-utf-8-only phpmyadmin
# cd phpmyadmin/libraries
修改配置文件
# vi config.default.php
找到这几行进行修改:
$cfg['Servers'][$i]['auth_type'] = 'http'; // Authentication method (valid choices: config, http, HTTP, signon or cookie)
$cfg['Servers'][$i]['user'] = 'root'; // MySQL user
$cfg['Servers'][$i]['password'] = 'PASSWORD'; // MySQL password (only needed

五、服务器安全配置

18、编译安装mod_security
mod_security是一个集入侵检测和防御引擎功能的开源web应用安全程序(或web应用程序防火墙)。
它以Apache Web服务器的模块方式运行, 目标是增强web应用程序的安全性, 防止web应用程序受到已知或未知的攻击。
# cd /usr/local/src
# wget 
http://www.modsecurity.org/download/...e_2.1.3.tar.gz
# tar -zxvf modsecurity-apache_2.1.3.tar.gz
# cd modsecurity-apache_2.1.3/apache2
# cat /usr/local/apache2/conf/httpd.conf | grep "ServerRoot" | grep -v "#"
ServerRoot "/usr/local/apache2"
#
#vi Makefile
top_dir = /usr/local/apache2
#
# make
# make install
vi /usr/local/apache2/conf/httpd.conf
加载下列模块
LoadModule unique_id_module modules/mod_unique_id.so
LoadModule security2_module modules/mod_security2.so
添加一行:
Include conf/modsecurity/*.conf
保存。
cd /usr/local/src/modsecurity-apache_2.1.3/rules
# mkdir /usr/local/apache2/conf/modsecurity
# cp *.conf /usr/local/apache2/conf/modsecurity/
根据你的需要,You may want to edit and customize modsecurity_crs_10_config.conf.
Additionally you may want to edit modsecurity_crs_30_http_policy.conf which enforces an application specific HTTP protocol usage.
重启apache

19、Iptables规则
vi /usr/local/sbin/fw.sh
将以下脚本命令粘贴到 fw.sh 文件中。


#!/bin/bash
# Stop iptables service first
service iptables stop
# Load FTP Kernel modules
/sbin/modprobe ip_conntrack_ftp
/sbin/modprobe ip_nat_ftp
# Inital chains default policy
/sbin/iptables -F -t filter
/sbin/iptables -P INPUT DROP
/sbin/iptables -P OUTPUT ACCEPT
# Enable Native Network Transfer
/sbin/iptables -A INPUT -i lo -j ACCEPT
# Accept Established Connections
/sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# ICMP Control
/sbin/iptables -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
# WWW Service
/sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# FTP Service
/sbin/iptables -A INPUT -p tcp --dport 2121 -j ACCEPT
# SSH Service
/sbin/iptables -A INPUT -p tcp --dport 59825 -j ACCEPT
# Anti DDOS
/sbin/iptables -I INPUT -p tcp --syn -m ttl --ttl-eq 117 -j DROP
/sbin/iptables -I INPUT -p tcp --syn -m length --length :40 -j DROP

# chmod 755 /usr/local/sbin/fw.sh
# echo '/usr/local/sbin/fw.sh' >> /etc/rc.d/rc.local
# /usr/local/sbin/fw.sh

20、apachet和 php 优化设置
对于访问量稍大的站点,Apache的这些默认配置是无法满足需求的,我们仍需调整Apache的一些参数,使Apache能够在大访问量环境下发挥出更好的性能。以下我们对Apache配置文件httpd.conf中对性能影响较大的参数进行一些说明。
(1) Timeout 该参数指定Apache在接收请求或发送所请求内容之前的最长等待时间(秒),若超过该时间Apache则放弃处理该请求,并释放连接。该参数默认值为120,推荐设置为60,对于访问量较大的网站可以设置为30。
(2) KeepAlive 该参数控制Apache是否允许在一个连接中有多个请求,默认打开。但对于大多数论坛类型站点来说,通常设置为off以关闭该支持。
(3) MPM - prefork.c 在默认情况下Apache使用Prefork(进程)工作模式,可以说这部分的参数设置是对Apache性能影响的核心和关键。
查看你的apache运行的模式:
# /usr/local/apache2/bin/httpd -l
Compiled in modules:
core.c
prefork.c
http_core.c
mod_so.c
然后用户可以在配置文档中找到以下配置段:

<IfModule prefork.c>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 15
MaxRequestsPerChild 0
</IfModule>
这就是控制Apache进程工作的配置段,为了更好的理解上述配置中的各项参数,下面让我们先了解一下Apache是如何控制进程工作的。我们知道,在 Unix系统中,很多服务(Service)的守护进程(Daemon)在启动时会创建一个进程以准备应答可能的连接请求,服务即进入了端口监听状态,当一个来自客户端(Client)的请求被发送至服务所监听的端口时,该服务进程即会处理该请求,在处理过程中,该进程处于独占状态,也就是说如果此时有其他请求到达,这些请求只能“排队”等待当前请求处理完成且服务进程释放。这样就会导致越来越多的请求处于队列等待状态,实际表现就是该服务处理能力非常低下。Apache使用Prefork模式很好的解决了这一问题。下面我们来看看Apache实际上是如何高效率工作的。
当Apache启动时,Apache会启动StartSpareServers个空闲进程同时准备接收处理请求,当多个请求到来时, StarSpareServers进行会越来越少,当空闲进程减少到MinSpareServers个时,Apache为了能够继续有充裕的进程处理请求,它会再启动StartsServers个进程备用,这样就大大减少了请求队列等待的可能,使得服务效率提高,这也是为什么叫做Pre-fork的原因;让我们继续跟踪Apache的工作,我们假设Apache已经启动了200个进程来处理请求,理论上来说,此时Apache一共有205个进程,而过了一段时间,假设有100个请求都得到了Apache的响应和处理,那么此时这100个进程就被释放成为空闲进程,那么此时Apache有105个空闲进程。而对于服务而言,启动太多的空闲进程时没有任何意义的,反而会降低服务器的整体性能,那么Apache真的会有105个空闲进程么?当然不会!实际上 Apache随时在检查自己,当发现有超过MaxSpareServers个空闲进程时,则会自动停止关闭一些进程,以保证空闲进程不过过多。说到这里,用户应该对Apache的工作方式有了一定的了解,如果想获得更多更详细的说明请参阅Apache手册文档。
我们还有两个参数没有介绍:MaxClients和MaxRequestPerchild;MaxClients指定Apache在同一时间内最多允许有多少客户端能够与其连接,如果超过MaxClients个连接,客户端将会得到一个“服务器繁忙”的错误页面。我们看到默认情况下MaxClients设置为15,这对一些中型站点和大型站点显然是远远不够的!也许您需要同时允许512个客户端连接才能满足应用需求,好吧,那么就让我们把 MaxClients修改为512,保存httpd.conf并退出,重启Apache,很遗憾,在重启过程当中您看到了一些错误提示,Apache重启失败。错误提示中告诉您MaxClients最大只能设定为256,相信您一定很失望。不过不要沮丧,Apache作为世界一流的Web Server一定不会如此单薄的!在默认情况下,MaxClients的确只能设定为不超过256的整数,但是,如果您有需要完全可以随意定制,此时就需要使用ServerLimit参数来配合使用,简单的说ServerLimit就像是水桶,而MaxClients就像是水,您可以通过更换更大的水桶(将ServerLimit设定为一个较大值)来容纳更多的水(MaxClients),但要注意,MaxClients的设定数值是不能大于 ServerLimit的设定数值的!
下面让我们了解一下MaxRequestPerChild参数,该参数指定一个连接进程中可以有多少个线程同时工作。也许这样解释过于专业,那么您只要想想“网络蚂蚁”、“网际快车FlashGet”中的“多点同时下载”即可,该参数实际上就是限制最多可以用几个“点”。默认设置为0,即为:不限制。但需要注意,如果将该值设置的过小会引起访问问题,如果没有特殊需要或者访问量压力并非很大可以保持默认值,如果访问量很大则推荐设置为2048。
好了,解释了这么多,让我们看看经过修改后Perfork.c配置段的推荐配置:

<IfModule prefork.c>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
ServerLimit 1024
MaxClients 768
MaxRequestsPerChild 0
</IfModule>

完成了上述对Apache的调整,Apache已经获得了较大的性能改善。记住,在修改任何参数后都需要重启Apache才能生效的。有关Apache的优化远远不止这些,有兴趣的用户可以阅读Apache手册文档或者寻找一些文献资料学习。
2. PHP优化对于PHP的优化主要是对php.ini中的相关主要参数进行合理调整和设置,以下我们就来看看php.ini中的一些对性能影响较大的参数应该如何设置。
# vi /etc/php.ini
(1) PHP函数禁用找到:
disable_functions =
该选项可以设置哪些PHP函数是禁止使用的,PHP中有一些函数的风险性还是相当大的,可以直接执行一些系统级脚本命令,如果允许这些函数执行,当PHP程序出现漏洞时,损失是非常严重的!以下我们给出推荐的禁用函数设置:
disable_functions = phpinfo,passthru,exec,system,popen,chroot,escapeshellcmd,escapeshellarg,shell_exec,proc_open,proc_get_status
需注意:如果您的服务器中含有一些系统状态检测的PHP程序,则不要禁用shell_exec,proc_open,proc_get_status等函数。
(2) PHP脚本执行时间找到:
max_execution_time = 30
该选项设定PHP程序的最大执行时间,如果一个PHP脚本被请求,且该PHP脚本在max_execution_time时间内没能执行完毕,则PHP不再继续执行,直接给客户端返回超时错误。没有特殊需要该选项可保持默认设置30秒,如果您的PHP脚本确实需要长执行时间则可以适当增大该时间设置。
(3) PHP脚本处理内存占用找到:
memory_limit = 8M
该选项指定PHP脚本处理所能占用的最大内存,默认为8MB,如果您的服务器内存为1GB以上,则该选项可以设置为12MB以获得更快的PHP脚本处理效率。
(4) PHP全局函数声明找到:
register_globals = Off
网络上很多关于PHP设置的文章都推荐将该选项设置为On,其实这是一种及其危险的设置方法,很可能引起严重的安全性问题。如果没有特殊的需要,强烈推荐保留默认设置!
(5) PHP上传文件大小限制找到:
upload_max_filesize = 2M
该选项设定PHP所能允许最大上传文件大小,默认为2MB。根据实际应用需求,可以适当增大该设置。
(6) Session存储介质找到:
session.save_path
如果你的PHP程序使用Session对话,则可以将Session存储位置设置为/dev/shm,/dev/shm是Linux系统独有的TMPFS 文件系统,是以内存为主要存储方式的文件系统,比RAMDISK更优秀,因为可以使用DISKSWAP作为补充,而且是系统自带的功能模块,不需要另行配置。想想看,从磁盘IO操作到内存操作,速度会快多少?只是需要注意,存储在/dev/shm的数据,在服务器重启后会全部丢失。不过这对于 Session来说是无足轻重的
(7)找到short_open_tag = Off
修改成short_open_tag = On,一些国内的php程序不标准,关闭可能会出错。

21、mysql优化及安全设置
Mysql的优化设置
打开/etc/my.cnf文件,修改以下设置,如果没有,可手动添加。调整设置时,请量力而行,这与你的服务器的配置有关,特别是内存大小。以下设置比较适合于1G内存的服务器,但并不绝对。
#指定索引缓冲区的大小,它决定索引处理的速度,尤其是索引读的速度。通过检查状态值Key_read_requests和Key_reads,可以知道 key_buffer_size设置是否合理。比例key_reads / key_read_requests应该尽可能的低,至少是1:100,1:1000更好(上述状态值可以使用show status like 'key_reads'获得)。key_buffer_size只对MyISAM表起作用。即使你不使用MyISAM表,但是内部的临时磁盘表是 MyISAM表,也要使用该值。可以使用检查状态值created_tmp_disk_tables得知详情。
key_buffer = 384M
#要求MySQL能有的连接数量。当主要MySQL线程在一个很短时间内得到非常多的连接请求,这就起作用,然后主线程花些时间(尽管很短)检查连接并且启动一个新线程。back_log值指出在MySQL暂时停止回答新请求之前的短时间内多少个请求可以被存在堆栈中。只有如果期望在一个短时间内有很多连接,你需要增加它,换句话说,这值对到来的TCP/IP连接的侦听队列的大小。你的操作系统在这个队列大小上有它自己的限制。试图设定back_log高于你的操作系统的限制将是无效的。默认数值是50
back_log = 200
#一个包的最大尺寸。消息缓冲区被初始化为net_buffer_length字节,但是可在需要时增加到max_allowed_packet个字节。缺省地,该值太小必能捕捉大的(可能错误)包。如果你正在使用大的BLOB列,你必须增加该值。它应该象你想要使用的最大BLOB的那么大。
max_allowed_packet = 4M
#允许的同时客户的数量。增加该值增加 mysqld要求的文件描述符的数量。这个数字应该增加,否则,你将经常看到 Too many connections 错误。 默认数值是100
max_connections = 1024
#指定表高速缓存的大小。每当MySQL访问一个表时,如果在表缓冲区中还有空间,该表就被打开并放入其中,这样可以更快地访问表内容。通过检查峰值时间的状态值Open_tables和Opened_tables,可以决定是否需要增加table_cache的值。如果你发现open_tables等于 table_cache,并且opened_tables在不断增长,那么你就需要增加table_cache的值了(上述状态值可以使用show status like 'Open_tables'获得)。注意,不能盲目地把table_cache设置成很大的值。如果设置得太高,可能会造成文件描述符不足,从而造成性能不稳定或者连接失败。
table_cache = 512
#每个线程排序所需的缓冲
sort_buffer_size = 4M
#当一个查询不断地扫描某一个表,MySQL会为它分配一段内存缓冲区。read_buffer_size变量控制这一缓冲区的大小。如果你认为连续扫描进行得太慢,可以通过增加该变量值以及内存缓冲区大小提高其性能。
read_buffer_size = 4M
#加速排序操作后的读数据,提高读分类行的速度。如果正对远远大于可用内存的表执行GROUP BY或ORDER BY操作,应增加read_rnd_buffer_size的值以加速排序操作后面的行读取。仍然不明白这个选项的用处……
read_rnd_buffer_size = 8M
#用于REPAIR TABLE。不明白这个选项的用处,百度上找到的设置方向也是五花八门,有128M、64M、32M等,折中选一个。
myisam_sort_buffer_size = 64M
#可以复用的保存在中的线程的数量。如果有,新的线程从缓存中取得,当断开连接的时候如果有空间,客户的线置在缓存中。如果有很多新的线程,为了提高性能可以这个变量值。通过比较 Connections 和 Threads_created 状态的变量,可以看到这个变量的作用。
thread_cache_size = 128
#查询结果缓存。第一次执行某条SELECT语句的时候,服务器记住该查询的文本内容和它返回的结果。服务器下一次碰到这个语句的时候,它不会再次执行该语句。作为代替,它直接从查询缓存中的得到结果并把结果返回给客户端。
query_cache_size = 32M
#最大并发线程数,cpu数量*2
thread_concurrency = 2
#设置超时时间,能避免长连接
wait_timeout = 120
#关闭不需要的表类型,如果你需要,就不要加上这个
skip-innodb
skip-bdb
关于mysql的优化设置及检查,这篇文章很值得一看 
http://tech.itdb.cn/n/200607/27/n20060727_30398.shtml
Mysql的安全设置
打开/etc/my.cnf文件,修改以下设置,如果没有,可手动添加。
#取消文件系统的外部锁
skip-locking
#不进行域名反解析,注意由此带来的权限/授权问题
skip-name-resolve
#禁止MySQL中用“LOAD DATA LOCAL INFILE”命令。这个命令会利用MySQL把本地文件读到数据库中,然后用户就可以非法获取敏感信息了。网络上流传的一些攻击方法中就有用它的,它也是很多新发现的SQL Injection攻击利用的手段!
local-infile = 0
#关闭远程连接,即3306端口。这是MySQL的默认监听端口。由于此处MySQL只服务于本地脚本,所以不需要远程连接。尽管MySQL内建的安全机制很严格,但监听一个TCP端口仍然是危险的行为,因为如果MySQL程序本身有问题,那么未授权的访问完全可以绕过MySQL的内建安全机制。(你必须确定,你是否真的不需要远程连接mysql)
skip-networking
修改完my.cnf后,还需要对mysql的用户名、帐号、及默认数据库进行调整
首先先登录mysql,在终端窗口输入 /usr/local/mysql/bin/mysql -u root -p
然后会提示输入密码,输入正确密码后,会出现mysql>提示符。
输入以下命令:
mysql>use mysql;
mysql>update user set user="centos" where user="root"; (将mysql的root用户名修改成centos,防止root的密码被暴力破解)
mysql>select Host,User,Password,Select_priv,Grant_priv from user;
mysql>delete from user where user=''; (删除user用户)
mysql>delete from user where password=''; (删除user用户)
mysql>delete from user where host=''; (删除user用户)
mysql>drop database test; (删除默认的test数据库)
mysql>flush privileges; (刷新mysql的缓存,让以上设置立即生效)
mysql>quit;
为了使以上优化和安全设置生效,请重启Mysql服务或Linux。
关于Mysql的安全设置,这篇文章很值得一看
http://www.unixren.com/linux/bencandy.php?fid=21&id=459

22、操作系统安全调整
1、 CentOS或Red Had Enterprise Linux 4 的用户要首先要打开SElinux,方法是修改/etc/selinux/config文件中的SELINUX="" 为enforcing 。它可以保证你的系统不会非正常的崩溃。有些人认为应该关闭,我强烈不推荐,当然只是将centos用来玩玩,不是用于实际服务器则无所谓了。
2、启用iptables 防火墙,对增加系统安全有许多好处。设置好防火墙的规则。
3、执行setup 关闭那些不需要的服务 ,记住少开一个服务,就少一个危险。
4、禁止Control-Alt-Delete 键盘关闭命令
在"/etc/inittab" 文件中注释掉下面这行(使用#):
ca::ctrlaltdel:/sbin/shutdown -t3 -r now 
改为:
#ca::ctrlaltdel:/sbin/shutdown -t3 -r now 
为了使这项改动起作用,输入下面这个命令:
# /sbin/init q
5、给"/etc/rc.d/init.d" 下script文件设置权限
给执行或关闭启动时执行的程序的script文件设置权限。
# chmod -R 700 /etc/rc.d/init.d/* 
这表示只有root才允许读、写、执行该目录下的script文件。
6、修改"/etc/host.conf"文件
"/etc/host.conf"说明了如何解析地址。编辑"/etc/host.conf"文件(vi /etc/host.conf),加入下面这行:
# Lookup names via DNS first then fall back to /etc/hosts. 
order bind,hosts 
# We have machines with multiple IP addresses. 
multi on 
# Check for IP address spoofing. 
nospoof on 
第一项设置首先通过DNS解析IP地址,然后通过hosts文件解析。第二项设置检测是否"/etc/hosts"文件中的主机是否拥有多个IP地址(比如有多个以太口网卡)。第三项设置说明要注意对本机未经许可的电子欺骗。
7、使"/etc/services"文件免疫
使"/etc/services"文件免疫,防止未经许可的删除或添加服务:
# chattr +i /etc/services
8.阻止你的系统响应任何从外部/内部来的ping请求。
既然没有人能ping通你的机器并收到响应,你可以大大增强你的站点的安全性。你可以加下面的一行命令到/etc/rc.d/rc.local,以使每次启动后自动运行。
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
9、对你的系统上所有的用户设置资源限制可以防止DoS类型攻击(denial of service attacks)
如最大进程数,内存数量等。例如,对所有用户的限制象下面这样:
vi /etc/security/limits.conf
下面的代码示例中,所有用户每个会话都限制在 10 MB,并允许同时有四个登录。第三行禁用了每个人的内核转储。第四行除去了用户 bin 的所有限制。ftp 允许有 10 个并发会话(对匿名 ftp 帐号尤其实用);managers 组的成员的进程数目限制为 40 个。developers 有 64 MB 的 memlock 限制,wwwusers 的成员不能创建大于 50 MB 的文件。
清单 3. 设置配额和限制
* hard rss 10000
* hard maxlogins 4
* hard core 0
bin -
ftp hard maxlogins 10
@managers hard nproc 40
@developers hard memlock 64000
@wwwusers hard fsize 50000
要激活这些限制,您需要在 /etc/pam.d/login 底部添加下面一行: session required /lib/security/pam_limits.so。
10、注释掉不需要的用户和用户组。
vipw
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
news:x:9:13:news:/etc/news:
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
rpm:x:37:37::/var/lib/rpm:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
netdump:x:34:34:Network Crash Dump user:/var/crash:/bin/bash
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
sshd:x:74:74:Privilerpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin
pcap:x:77:77::/var/arpwatch:/sbin/nologin
xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
gdm:x:42:42::/var/gdm:/sbin/nologin
pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/sbin/nologin
htt:x:100:101:IIIMF Htt:/usr/lib/im:/sbin/nologin
wangjing:x:500:500::/home/wangjing:/bin/bash
mysql:x:101:102:MySQL server:/var/lib/mysql:/bin/bash
apache:x:48:48:Apache:/var/www:/sbin/nologin
ge-separated SSH:/var/empty/sshd:/sbin/nologin
对于不需要的用户全部加 # 注释掉。注意,我不建议直接删除,当你某种原因需要某个用户时,自己重新会很麻烦。
vi /etc/group
root:x:0:root
bin:x:1:root,bin,daemon
daemon:x:2:root,bin,daemon
sys:x:3:root,bin,adm
adm:x:4:root,adm,daemon
tty:x:5:
disk:x:6:root
lp:x:7:daemon,lp
mem:x:8:
kmem:x:9:
wheel:x:10:root
mail:x:12:mail
news:x:13:news
uucp:x:14:uucp
man:x:15:
games:x:20:
gopher:x:30:
dip:x:40:
ftp:x:50:
lock:x:54:
nobody:x:99:
users:x:100:
dbus:x:81:
floppy:x:19:
vcsa:x:69:
rpm:x:37:
haldaemon:x:68:
utmp:x:22:
netdump:x:34:
nscd:x:28:
slocate:x:21:
sshd:x:74:
rpc:x:32:
rpcuser:x:29:
nfsnobody:x:65534:
mailnull:x:47:
smmsp:x:51:
pcap:x:77:
xfs:x:43:
ntp:x:38:
gdm:x:42:
pegasus:x:65:
htt:x:101:
wangjing:x:500:
mysql:x:102:
apache:x:48:
对于不需要的用户组全部加 # 注释掉。注意,我不建议直接删除,当你某种原因需要某个用户组时,自己重新会很麻烦。
11、用chattr命令给下面的文件加上不可更改属性。
# chattr +i /etc/passwd
# chattr +i /etc/shadow
# chattr +i /etc/group
# chattr +i /etc/gshadow
注意执行这个操作后,以root身份都不能向系统增加用户或者修改密码了。如果我们要增加用户或者修改密码的。应该先用chattr -i /etc/passwd等命令解除不可写设置,再进行操作。
12、修改sshd的端口。
修改/etc/ssh/sshd_config,将里面的 Port 改为 59825,(具体的端口你随意。当然不能和其他程序的端口冲突了)。并注释掉前面的#号,然后
pkill sshd
service sshd start
就行了
注意最好在本地修改这个端口,否则容易出现把自己锁在外面的情况。修改了本处端口后,还要注意修改防火墙的ssh端口。
13、 内核参数调整
vi /etc/sysctl.conf

net.ipv4.conf.default.accept_source_route=0
net.ipv4.icmp_echo_ignore_broadcasts=1
#net.ipv4.icmp_echo_ignore_all=1
net.ipv4.icmp_ignore_bogus_error_responses=1
net.ipv4.ip_conntrack_max=65535
net.ipv4.tcp_syn_retries=1
net.ipv4.tcp_fin_timeout=5
net.ipv4.tcp_synack_retries=1
net.ipv4.tcp_syncookies=1
net.ipv4.route.gc_timeout=100
net.ipv4.tcp_keepalive_time=500
net.ipv4.tcp_max_syn_backlog=10000

# sysctl -p //查看
14、经常检查系统日志。系统日志主要位于/var/log/目录下。防患于未然。
通过以上设置你的系统一般来说就比较安全了。当然安全与不安全是道与魔的斗争。
经过这几个步骤,我们一个比较安全的LAMP服务器就环境基本建立成功啦。感觉上也不是很难,是吧?

六、日常常用的管理功能
# cd /usr/local/src
# wget 
ftp://ftp.ncftp.com/ncftp/ncftp-3.2.1-src.tar.gz
# tar zxvf ncftp-3.2.1-src.tar.gz
# cd ncftp-3.2.1-src
# ./configure --prefix=/usr/local/ncftp
# make && make install

23、mysql数据库自动备份并上传到服务器
服务器数据的备份
1、数据备份
为了防止意外情况的发生造成数据的丢失,关键数据或整个系统或对有选择的系统部分,在本地和异地进行定时备份,以保证系统全部或部分在灾害出现时也能持续运行。
2、具体方案
1)、数据库每日备份
每天4:00,将Cnprintbbs数据库拷贝至/root/back后进行压缩,然后上传至192.168.1.9服务器上,/root/back留有压缩版本。
运行脚本/root/scripts/back.sh
例子:

#!/bin/bash
rm /root/back/Cnprintbbs/* -rf
/usr/local/mysql/bin/mysqlhotcopy Cnprintbbs /root/back/Cnprintbbs -u root -p uefer77693
sleep 5
cd /root/back
tar zcf `hostname`-Cnprintbbs`date +%Y%m%d`.tar.gz Cnprintbbs
sleep 5
echo "tar ok!"
/usr/local/ncftp/bin/ncftpput -u gamebak -p gamebak@root 192.168.1.9 / /root/back/`hostname`-Cnprintbbs`date +%Y%m%d`.tar.gz
sleep 10
echo "upload Cnprintbbs ok!"

保存,并设置计划任务。
# crontab -e
增加一行:
00 4 * * * /root/scripts/back.sh
每天凌晨4点将自动备份数据库并上传。
2)、数据库即时备份
每隔1小时,将Cnprintbbs数据库拷贝至/root/back/hour目录进行备份,然后将压缩文件传给特定服务器。
例子:

#!/bin/bash
hottime=`date +%Y%m%d%H%M`
mkdir /root/back/hour/Cnprintbbs"$hottime"
/usr/local/mysql/bin/mysqlhotcopy Cnprintbbs /root/back/hour/Cnprintbbs"$hottime" -u root -p uefer77693
sleep 5
cd /root/back/hour
tar zcf `hostname`-Cnprintbbs"$hottime".tar.gz Cnprintbbs"$hottime"
sleep 5
echo "tar ok!"
/usr/local/ncftp/bin/ncftpput -u backupdb -p backupdb 192.168.102.119 / /root/back/hour/`hostname`-Cnprintbbs"$hottime".tar.gz
sleep 20
echo "upload Cnprintbbs ok!"
rm `hostname`-Cnprintbbs"$hottime".tar.gz -f

3)、日志备份
每天02:00,将/log/下前一天的日志,进行压缩,然后上传至192.168.9.1服务器。
运行脚本/root/scripts/upload_daily
例子:

#!/bin/bash
cd /log/
mkdir log`date --date '1 days ago' +%Y%m%d`
mv *.log.`date --date '1 days ago' +%y%m%d`-* log`date --date '1 days ago' +%Y%m%d`
sleep 10
tar zcvf `hostname`-log`date --date '1 days ago' +%Y%m%d`.tar.gz log`date --date '1 days ago' +%Y%m%d`
/usr/local/ncftp/bin/ncftpput -u log -p log@root 218.80.198.234 / /log/`hostname`-log`date --date '1 days ago' +%Y%m%d`.tar.gz
rm `hostname`-log`date --date '1 days ago' +%Y%m%d`.tar.gz

注:计划任务通过/etc/crontab –e来进行设置
4)、即时日志备份
直接运行/home/root/tools/upload 将最新日志上传至192.168.1.9服务器,方便研发部门查看。
运行脚本/home/root/tools/upload,(如果所有服务器的日志都需要上传,可以运行网关服务器上的/home/root/tools/allupload)
24、Squid缓存删除及重启
1. squid使用时间长了,速度会变慢,我的建议是每2小时kill掉squid 进程,RunAccel脚本会自动再启动的它。
2. 写一脚本,放进crontab中,每天凌晨4点左右把cache目录清空。

#!/bin/sh
# squid clean swap and restart scrīpt by marco lu
SQUID_DIR=/usr/local/squid/
PID_FILE=${SQUID_DIR}var/logs/squid.pid
CACHE_DIR=${SQUID_DIR}var/cache
PPID=`ps aux | grep -i squid | grep -v grep|awk '{print $2}'`
kill -9 ${PPID} > /dev/null
kill -9 `cat ${PID_FILE}` > /dev/null
rm -rf $CACHE_DIR/*
${SQUID_DIR}sbin/squid -z > /dev/null
if [ $? -eq 0 ]
then
${SQUID_DIR}bin/RunAccel & > /dev/null
fi

七、安装vBulletin 3.6.8和vBseo 3.1.0
vBulletin和vbseo 3.0.1都是商业软件,本地址的下载链接仅供试用,请于下载后24小时内删除,购买正版请联系相应官方。
25、安装vBulletin 3.6.8
vBulletin 是一个强大的论坛社区解决方案,使用它您可以轻易为您的网站创建论坛系统。vBulletin 基于 PHP 和 MySQL (一个高效开源的数据库引擎)。这些坚固后台技术使我们开发的产品有着不同凡响的速度和可靠的稳定性。
wget 
http://www.cnprint.org/bbs/blogs/1/a...1234567890.zip
安装教程请见:
http://www.vbulletin-china.cn/docs/h...rsion=30608602
特别提醒:请打开config.php有关memcache设置。
26、安装vBseo 3.1.0
vBSEO为vBulletin(最流行的网站论坛)搜索优化程序, 用它可很容易地为你的vBulletin网站提供强大的搜索功能。
下载vBseo安装程序:
wget 
http://www.cnprint.org/bbs/blogs/1/a...eygen-gysn.zip
1、打开vBseo压缩包,解压缩,FTP以二进制方式上传upload文件夹下所有文件及目录至vbb对应目录。
2、Linux系统下需首先修改"vB-root/includes/config_vbseo.php" 文件属性为可写(CHMOD 666)
3、确认vbb控制台启动插件功能, 在插件与产品栏目--产品管理--添加/管理产品,import导入'Product'目录中的crawlability_vbseo.xml (如果中文UTF-8版个别情况下导入错误则可以把此文件用编辑软件另存为utf-8编码),产品添加完毕。
4、将'htaccess'目录中.htaccess 文件上传至论坛根目录,有些操作系统下.htaccess 不可见,这时可以只将htaccess.txt文件上传到vbb根目录,删除刚才上传的.htaccess文件,将刚才上传的txt文件更名为. htaccess。
5、在浏览器中输入http: //你的网址/你的VBB目录/vbseocp.php配置你的vbseo,输入两次你的Vbseo管理面板密码,也可以事先编辑upload\ includes\config_vbseo.php文件,在define('VBSEO_ADMIN_PASSWORD', 'ABC')中加入你想要的管理密码(就在后面的引号中间加入,比如ABC)。
6、vbseo管理界面下如果需要输入授权码请用附带的keygen为你的Domain算号并拷贝32位授权码即可,配置完毕后将第二步中'config_vbseo.php' 文件属性改回只读(CHMOD 644)
7、开始使用你的VBSEO,第一次安装以后可以直接通过VBB后台进入Vbseo管理界面。
8、如果有必要,将htacess规则移到httpd.conf中。可以大大降低apache的负载。

NameVirtualHost *:80
<VirtualHost *:80>
ServerName  www.cnprint.org
DocumentRoot "/usr/local/apache2/htdocs"
#ErrorLog logs/error_log
# CustomLog logs/access_log combined
</VirtualHost>
<Directory "/usr/local/apache2/htdocs">
Options Indexes FollowSymLinks
AllowOverride none
Order allow,deny
Allow from all
</Directory>
<Directory "/usr/local/apache2/htdocs/bbs">
Options Indexes FollowSymLinks
AllowOverride all
RewriteEngine On
#RewriteBase /bbs
RewriteCond %{HTTP_HOST} !^www\.cnprint\.org
RewriteRule (.*) http://www.cnprint.org/bbs/$1 [L,R=301]
#RewriteRule ^((urllist|sitemap_).*\.(xml|txt)(\.gz)?)$ vbseo_sitemap/vbseo_getsitemap.php?sitemap=$1 [L]
RewriteCond %{QUERY_STRING} !vbseourl=
RewriteCond %{REQUEST_URI} !(admincp/|modcp/|chat|cron)
RewriteRule ^(.*\.php)$ vbseo.php?vbseourl=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !\.(jpg|gif)$
RewriteRule ^(archive/.*)$ vbseo.php?vbseourl=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^(admincp|modcp|clientscript|cpstyles|images)/
RewriteRule ^(.+)$ vbseo.php?vbseourl=$1 [L,QSA]
Order allow,deny
Allow from all
</Directory>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值