首先声明该文章有部分片段是从先辈blog中摘录的,也有自己的小小修正~
环境:mysql-5.1.51.tar.gz httpd-2.2.19.tar.bz2 php-5.3.6.tar.bz2
源码包都下载放在/usr/local目录下
数据库数据目录:/data  (当然这些目录需要自己提前创建)
Web目录:/html

1 安装必备的开发包
   #yum -y install ntp vim-enhanced gcc gcc-c++ flex bison autoconf automake bzip2-devel \
   ncurses-devel zlib-devel libjpeg-devel libpng-devel libtiff-devel freetype-devel libXpm-devel \
   gettext-devel  pam-devel libtool libtool-ltdl openssl openssl-devel fontconfig-devel \
   libxml2-devel curl-devel  libicu libicu-devel libmcrypt libmcrypt-devel libmhash libmhash-devel 
2 移除之前安装的http、mysql、php及其相关组件
  #yum remove httpd mysql mysql-server php php-cli php-common php-devel php-gd  -y
3.设置同步时间系统
新装的CentOS系统服务器可能设置了错误的,需要重新调整时区和时间步骤如下
#cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime    [ 该语句是把当前时区调整为上海也就是+8     区,详情参看/usr/share/zoneinfo目录 ]
#ntdate us.pool.ntp.org [ 用ntdate同步标准时间,yum install ntpdate -y 可以安装ntpdate ]
#crontab -e
0-59/10 * * * * /usr/sbin/ntpdate us.pool.ntp.org | logger -t NTP [ 加入计划任务每隔10分钟同步一   下时间 ]
4编译安装mysql
cd mysql-5.1.51
./configure \
--prefix=/usr/local/mysql \
--exec-prefix=/usr/local/mysql \
--with-mysqld-user=mysql \
--with-charset=utf8 \
--with-extra-charsets=all \
--with-innodb \
--with-pthread \
--enable-assembler \
--enable-thread-safe-client \
--with-big-tables \
--with-readline \
--with-ssl \
--with-embedded-server \
--enable-local-infile \
make && make install
添加用户
groupadd –g 306 mysql
useradd mysql–g mysql –u 306 –M –s /sbin/nologin
chown –R mysql:mysql /data

/usr/local/mysql/bin/mysql_install_db --user=mysql --datadir=/data
chown -R root:mysql /usr/local/mysql/
chown -R mysql /usr/local/mysql
chgrp -R mysql /usr/local/mysql/
cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
编辑/etc/my.cnf,在[mysqld]加入 datadir = /data
cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
chkconfig --add mysqld  
chkconfig --level 3 mysqld on 
service mysqld start
测试数据库服务器
/usr/local/mysql/bin/mysql -uroot
root用户的默认密码是空
如出现标识符
mysql>
说明MySQL数据库安装成功,并且已经启动了!
为root用户设置密码
mysql>use mysql;
mysql>set password for root@localhost=password("centosmysqla#"); 
mysql>set password for root@127.0.0.1=password("centosmysqla#"); 
删除空帐户,查看帐户信息
mysql>delete from mysql.user where user='';
mysql>select user,host,password from mysql.user; 
允许root用户远程登录
mysql>grant all privileges on *.* to root@'%' identified by 'centosmysqla#';
mysql>flush privileges;
mysql>quit
 
参考选项:Mysql 优化,在[mysqld]配置参数下面添加下面几行(大概37行下面),根据服务器不同配置进行不同的设置
max_allowed_packet = 500M  //先找到这行内容,修改成500
innodb_file_per_table
log-bin-trust-function-creators=1
skip-name-resolv//禁用DNS解析
sync-binlog=1
lower_case_table_names=1
max_connections = 1500 (默认100)
log-error=/data/mysql/log/mysql.err.log  //指定错误日志位置
max_heap_table_size = 256M
join_buffer_size = 128M
thread_cache_size = 1200   //线程缓存
thread_concurrency = 4  //设置成cpu数x2,只有一个设置2
thread_stack = 256K  
query_cache_type = 1   //指定是否使用查询缓冲,可以设置为0、1、2,该变量是SESSION级的变量
query_cache_size = 512M  //查询缓冲大小
query_cache_limit = 4M   //单个查询缓冲大小。默认1M
query_cache_min_res_unit = 4k    //指定分配缓冲区空间的最小单位,缺省为4K   tmp_table_size = 256M       
myisam_sort_buffer_size = 64M
back_log = 1024//设定缓存队列数,节省连接开销
long_query_time = 3  
open_files_limit    = 10240
interactive_timeout = 120
wait_timeout = 120
external-locking = FALSE//禁用文件系统外部锁
table_cache = 1024//高速缓存大小,4G内存设置为2048
修改配置文件里下面的参数,去掉前面的#
innodb_buffer_pool_size = 2048M (默认16M,可以为系统内存50%~70%)
innodb_additional_mem_pool_size = 256M (默认2M)
innodb_log_file_size = 512M    (默认5M,innodb_buffer_pool_size的四分之一)
innodb_log_buffer_size = 16M  //设置位每秒的数据量
innodb_max_dirty_pages_pct = 90
innodb_file_io_threads = 4
innodb_thread_concurrency = 16
innodb_flush_log_at_trx_commit = 0
innodb_flush_method=O_DIRECT
innodb_open_files=4096
innodb_lock_wait_timeout = 120
innodb_file_per_table=1
 
:wq //保存退出
service mysqld restart
 
 
5 apache安装
cd ../httpd-2.2.19
cd srclib/apr
./configure --prefix=/usr/local/apr;make;make install

cd ../apr-util
./configure \
--prefix=/usr/local/apr-util \
--with-apr=/usr/local/apr/ \
--with-mysql=/usr/local/mysql;
make;make install

cd ../.. 
mkdir -p /usr/local/apache
./configure \
--prefix=/usr/local/apache \
--enable-mods-shared=all \
--with-mysql=/usr/local/mysql \
--enable-deflate \
--enable-cache \
--enable-file-cache \
--enable-mem-cache \
--enable-disk-cache \
--with-apr=/usr/local/apr/ \
--with-apr-util=/usr/local/apr-util/ \
--enable-rewrite \
--enable-expires \
--enable-authn-dbm \
--enable-vhost-alias \
--with-mpm=worker \
--with-ssl \
--enable-so \
--enable-ssl \
--enable-track-vars \
--with-z \
--disable-ipv6 \
--enable-dav \
make && make install
 
 
启动apahce
/usr/local/apache/bin/apachectl -k start
用浏览器查看http://localhost,得到it works,说明apache已经配置成功了.
停止apache
/usr/local/apache/bin/apachectl -k stop
设为开机自动启动
cp /usr/local/apache/bin/apachectl /etc/rc.d/init.d/httpd
vi /etc/rc.d/init.d/httpd
找到:#!/bin/sh
另起一行,增加下面两行:
#chkconfig:35 85 15
#description: Apache HTTP Server.
chkconfig --add httpd
启动httpd服务
service httpd start

更改网站根目录:
vi /usr/local/apache/conf/httpd.conf
查找:
DocumentRoot "/usr/local/apache/htdocs"
改为:
DocumentRoot "/html"

设置管理员邮箱和ServerName:
ServerAdmin zalifei@126.com
ServerName IP:80
建立虚拟主机

在httpd.conf末尾加入以下内容:
<VirtualHost *:80>
    DocumentRoot /html
    ServerName www.example.com
    ErrorLog /var/log/httpd/error.log
    CustomLog /var/log/httpd/access.log combined
    <Directory “/html”>
        Options FollowSymLinks
        AllowOverride None/all
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

设置语言
查找下面关键词并注释掉
AddDefaultCharset

找到以下内容并去掉注释:
Include conf/extra/httpd-mpm.conf
Include conf/extra/httpd-info.conf
Include conf/extra/httpd-default.conf

service httpd restart 测试
 
6 安装PHP[yum install php-snmp net-snmp net-snmp-libs net-snmp-utils net-snmp-devel libmcrypt mhash libevent]

./configure --prefix=/usr/local/php \
--with-apxs2=/usr/local/apache/bin/apxs \
--with-mysql=/usr/local/mysql/ \
--with-pdo-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--enable-zip --enable-sqlite-utf8 --enable-sockets \
--enable-soap --enable-pcntl --enable-mbstring \
--enable-calendar --enable-bcmath \
--enable-exif --with-mhash --with-gd \
--with-png-dir --with-jpeg-dir --with-freetype-dir \
--with-libxml-dir --with-curl --with-curlwrappers \
--with-zlib \
--with-gettext=shared --with-xmlrpc=shared \
--with-libxslt \
--with-libxml-dir \
--with-iconv \
--with-snmp
--enable-gd-native-ttf  \
--disable-debug \
--with-mcrypt \
make && make install

# cp php.ini-development /usr/local/php/lib/php.ini   //拷贝配置文件
编辑/usr/local/php/lib/php.ini文件,date.timezone = 'Asia/Shanghai'
将short_open_tag = Off更改为short_open_tag = On

整合apache和php
vi /usr/local/apache/conf/httpd.conf
找到
LoadModule php5_module modules/libphp5.so
去掉注释符
找到
AddType application/x-gzip .gz .tgz
在下面添加2行:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
找到
DirectoryIndex index.html
改成 
DirectoryIndex index.html index.htm index.php

让Apache 支持rewrite
找到下面的字段:
AllowOverride None
修改为:
AllowOverride All

让页面支持gzip
LoadModule php5_module        modules/libphp5.so
之后添加:
<IfModule mod_deflate.c>
DeflateCompressionLevel 6
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php
AddOutputFilter DEFLATE css js
</IfModule>

重新启动Apache
service httpd restart

测试
/usr/local/apache/bin/apachectl configtest
显示出
Syntax OK
表示正常
默认网站根目录下建立phpinfo.php文件,输入以下内容
<?php 
  phpinfo(); 
?>' 
用浏览器打开
http://127.0.0.1/phpinfo.php
如果页面上出现了PHP的版权信息及系统配置情况,说明你的PHP编译装配置确。

注意:1.如果访问出现错误,由于Linux自带的安全策略导致的方法如下:
chcon –R –references=/var/www/html /html
setenforce 0
2. httpd restart出現錯誤 Could not reliably determine the server’s fully qualified domain name, using 127.0.0.1 for ServerName 
這個問題應該是沒有在 /etc/httpd/conf/httpd.conf 中設定 ServerName 
vi /etc/httpd/conf/httpd.conf
找到 ServerName 可能已經被註解了,這時只要將註解符號拿掉,再取個適當的名稱即可解決!

在默认网站根目录下建立dbtest.php,输入以下内容
<?php 
$link=mysql_connect ('localhost','root','centosmysqla#');
if(!$link) echo "fail"; 
else echo "success"; 
mysql_close(); 
?>
chmod 755 /www/default/dbtest.php
http://127.0.0.1/dbtest.php
如页面显示success说明mysql+php配置正确
 
该博文参看

 
http://blog.chinaunix.net/uid-23914782-id-2609862.html