最近抽空在虚拟机上测试成功了LAMP各个最新版本的整合编译安装,算是把之前的博文整合精简,以下内容均在CENTOS6.3(安装minimal desktop和默认开发包)下测试安装成功,并做了相应优化配置,如有遗漏,还请留言指教.
LINUX操作系统:centOS6.3 64bit(安装了系统默认开发包)
APACHE:httpd-2.4.4
MYSQL:mysql-5.6.10
PHP:php-5.4.13
注:推荐安装centos6.3系统时,在系统安装向导中将默认开发包勾上,避免后期编译报错。
一.安装开发包(使用默认CENTOS更新源):
# yum -y install wget gcc-c++ ncurses-devel cmake make perl
二.关闭iptables和SELINUX
# service iptables stop
# setenforce 0
# vi /etc/sysconfig/selinux
---------------
SELINUX=disabled
---------------
三.安装mysql数据库
# wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.10.tar.gz/from/http://cdn.mysql.com/
# useradd -d /usr/local/mysql/ mysql 创建一个Mysql用户,指定家目录到/use/local目录下。
# mkdir /usr/local/mysql/data
# mkdir /usr/local/mysql/log 新建一个目录
# chown -R mysql:mysql /usr/local/mysql/data/
# chown -R mysql:mysql /usr/local/mysql/log/
# chmod 750 /usr/local/mysql/data
# chmod 750 /usr/local/mysql/log 修改目录的所属者以及所属组
# tar -zxv -f mysql-5.6.10.tar.gz 解包
# cd mysql-5.6.10
# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
-DDEFAULT_CHARSET=gbk \
-DDEFAULT_COLLATION=gbk_chinese_ci \
-DEXTRA_CHARSETS=all \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306
# make
# make install
# 复制配置文件
cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
# vi /etc/my.cnf
-------------------
[mysql]
# CLIENT #
port = 3306
socket = /var/lib/mysql/mysql.sock
[mysqld]
# GENERAL #
user = mysql
default_storage_engine = InnoDB
socket = /var/lib/mysql/mysql.sock
pid_file = /var/run/mysqld/mysqld.pid
# MyISAM #
key_buffer_size = 32M
myisam_recover = FORCE,BACKUP
# SAFETY #
max_allowed_packet = 16M
max_connect_errors = 1000000
skip_name_resolve
sql_mode = STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ONLY_FULL_GROUP_BY
sysdate_is_now = 1
innodb = FORCE
innodb_strict_mode = 1
# DATA STORAGE #
datadir = /usr/local/mysql/data
# BINARY LOGGING #
log-bin =/usr/local/mysql/log/bin.log
expire_logs_days = 30
sync_binlog = 1
# CACHES AND LIMITS #
key_buffer = 256M
max_allowed_packet = 32M
sort_buffer_size = 16M
read_buffer_size = 4M
read_rnd_buffer_size = 16M
thread_stack = 8M
tmp_table_size = 32M
max_heap_table_size = 32M
query_cache_type = 1
query_cache_size = 128M
query_cache_limit = 2M
max_connections = 2048
thread_cache_size = 512
open_files_limit = 65535
table_definition_cache = 1024
table_open_cache = 2048
# INNODB #
innodb_log_files_in_group = 2
innodb_log_file_size = 64M
innodb_flush_log_at_trx_commit = 1
innodb_file_per_table = 1
innodb_buffer_pool_size = 2G
# LOGGING #
log-error=/usr/local/mysql/log/error.log
general_log=1
general_log_file=/usr/local/mysql/log/mysql.log
slow_query_log=1
slow_query_log_file=/usr/local/mysql/log/slowquery.log
log-output=FILE
# 避免MySQL的外部锁定,减少出错几率增强稳定性 #
skip-external-locking
# 禁止sql读取本地文件 #
local-infile=0
--------------------
# 进入安装路径
cd /usr/local/mysql
# 执行配置脚本
scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data
# 复制服务启动脚本
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# chkconfig --add mysqld #添加开机启动服务
# chkconfig --level 35 mysqld on #设置mysql启动
# service mysqld start #启动数据库
# ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock 创建一个mysql接口的软链接
# /usr/local/mysql/bin/mysqladmin -u root password 123456 #设置密码
# /usr/local/mysql/bin/mysql -u root -p123456 #连接数据库
mysql> create database phpwind; #---创建数据库
mysql> grant all privileges on *.* to root@'%' identified by '123456' with grant option; #给root用户非本地链接所有权限,并改密码和赋予其给其他人下发权限.
mysql> show variables; #查看mysql设置.
# 这里是把mysql文件链接到默认位置,必须做这步,在编译其他软件的时候自定义mysql的库文件路径。
# ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
# ln -s /usr/local/mysql/include/mysql /usr/include/mysql
注:从启MYSQL也可使用以下命令开启此服务
# /usr/local/mysql/bin/safe_mysqld
如果不设置chkconfig启动项,也可在/etc/rc.local下添加如下命令,使mysql服务利用系统启动脚本运行.
# /usr/local/mysql/bin/safe_mysqld --user=mysql &
四.安装apache网站服务
1 pcre安装
# wget http://sourceforge.net/projects/pcre/files/pcre/8.32/pcre-8.32.tar.gz/download
# tar -xzvf pcre-8.32.tar.gz
# cd pcre-8.32
# ./configure --prefix=/usr/local/pcre
# make && make install
2.安装httpd
其实这里在apache官网找不到httpd-2.4.4-deps.tar.bz2的编译包,不知道官方怎么想的,最后研究了下最近的2.43deps版本,解压该编译包,发现里面放的其实就是apr和apr-util两个apache的依赖包,现在的方法是直接把2.43的包解压到apache安装根目录即可连同apache编译安装,最后能正常安装使用,算是一个无奈之举,但安装起来较网上单独编译安装依赖包要简便一些.
# wget http://archive.apache.org/dist/httpd/httpd-2.4.3-deps.tar.bz2
# wget http://archive.apache.org/dist/httpd/httpd-2.4.4.tar.bz2
解压安装
注:httpd-2.4.3-deps.tar.bz2已集成APR,安装apache前检查pcre是否安装成功.
# tar jxvf httpd-2.4.4.tar.bz2
# tar jxvf httpd-2.4.3-deps.tar.bz2
# cp -rf httpd-2.4.3/* httpd-2.4.4
# cd httpd-2.4.4
# 伪装Apache版本信息
---------------
修改 include/ap_release.h文件 "Apache" 参数为 "Microsoft-IIS/5.0"
修改 os/unix/os.h文件 "Unix" 参数为 "Win32"
# cd httpd-2.4.4
# vi include/ap_release.h
#define AP_SERVER_BASEPRODUCT "Apache"
改为
#define AP_SERVER_BASEPRODUCT "Microsoft-IIS/5.0"
# vi os/unix/os.h
#define PLATFORM "Unix"
改成
#define PLATFORM "Win32"
----------------
----------随 笔-----------------
这里走了一些弯路,网上很多资料上写到apache2.4版本打开默认多线程模式时,都是在修改conf/extra/httpd-mpm.conf下prefork MPM处参数,但当我实际操作的时候发现,修改后系统的apache进程并未增加,但在2.2下修改此参数则没有任何问题,最近研究worker模式下才发现到,实际你启用worker模式后,执行此命令
# /usr/local/apache2/bin/apachectl -l
返回的结果如下:
core.c
mod_so.c
http_core.c
worker.c
看到worker.c,这里就想到是不是使用默认的线程模式显示的*.c的这个*就是apache根目录conf/extra/httpd-mpm.conf下对应的参数,想到这个疑问然后就使用默认线程模式从新编译了下apache,执行命令
# /usr/local/apache2/bin/apachectl -l
返回:
core.c
mod_so.c
http_core.c
event.c
OK,到conf/extra/httpd-mpm.conf下,查看有如下几行参数
<IfModule mpm_event_module>
StartServers 3
MinSpareThreads 75
MaxSpareThreads 250
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 0
</IfModule>
将StartServers 参数修改成10
# lsof -i:80
[root@lamp apache2]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 1302 root 4u IPv6 106523 0t0 TCP *:http (LISTEN)
httpd 21852 daemon 4u IPv6 106523 0t0 TCP *:http (LISTEN)
httpd 21854 daemon 4u IPv6 106523 0t0 TCP *:http (LISTEN)
httpd 21855 daemon 4u IPv6 106523 0t0 TCP *:http (LISTEN)
httpd 21856 daemon 4u IPv6 106523 0t0 TCP *:http (LISTEN)
httpd 21857 daemon 4u IPv6 106523 0t0 TCP *:http (LISTEN)
httpd 21858 daemon 4u IPv6 106523 0t0 TCP *:http (LISTEN)
httpd 21859 daemon 4u IPv6 106523 0t0 TCP *:http (LISTEN)
httpd 21860 daemon 4u IPv6 106523 0t0 TCP *:http (LISTEN)
httpd 21936 daemon 4u IPv6 106523 0t0 TCP *:http (LISTEN)
httpd 21937 daemon 4u IPv6 106523 0t0 TCP *:http (LISTEN)
问题解决,看来2.4版本的apache默认线程是需要修改event参数,这里请注意.
--------------------
编译默认event MPM模式(推荐)
# ./configure --prefix=/usr/local/apache2 --enable-so --enable-rewrite --with-pcre=/usr/local/pcre/bin/pcre-config
编译worker MPM模式(可选,能正常开启服务,但本人未做过相关网站测试,不推荐生产环境下使用)
# ./configure --prefix=/usr/local/apache2 --enable-so --enable-rewrite -with-mpm=worker --with-pcre=/usr/local/pcre/bin/pcre-config
# make && make install
# 防止apache启动报错.
vi /usr/local/apache2/conf/httpd.conf
添加:
ServerName localhost:80
# /usr/local/apache2/bin/apachectl start 启动apache
注: vi /etc/rc.local
添加如下命令,使apache服务开机运行
/usr/local/apache2/bin/apachectl start
配置apache:
1.禁止地址目录访问和索引:
#vi /usr/local/apache2/conf/httpd.conf
搜索 Options Indexes FollowSymLinks
将其前面加#注释
# Options Indexes FollowSymLinks
2.隐藏版本号:
#vi /usr/local/apache2/conf/httpd.conf
加入以下两行:
ServerTokens ProductOnly
ServerSignature Off
3.优化线程数:
如果apche访问量过大,将会导致页面打开迟缓,下载速度也降低,如果由于经费和环境问题,可以通过对Apache2增加模块MPM来进行优化, 这里我选择线程型MPM加以优化:
注:此方法仅对编译安装apache有效:
a.开启mpm:
#vi /usr/local/apache2/conf/httpd.conf
找到以下这行内容,去掉注释.
Include conf/extra/httpd-mpm.conf (如果没有此段代码可以新加。没有此文件httpd-mpm.conf可以新建,也可以直接加代码到)
b.优化配置:
#vi /usr/local/apache2/conf/extra/httpd-mpm.conf
找到如下代码,修改成以下参数即可.
# event MPM配置:
<IfModule mpm_event_module>
StartServers 5
ServerLimit 5000
MaxClients 4000
MinSpareThreads 100
MaxSpareThreads 400
ThreadLimit 200
ThreadsPerChild 100
MaxRequestWorkers 400
MaxRequestsPerChild 100
</IfModule>
# worker MPM配置:
<IfModule mpm_event_module>
StartServers 5
ServerLimit 5000
MaxClients 4000
MinSpareThreads 100
MaxSpareThreads 400
ThreadLimit 200
ThreadsPerChild 100
MaxRequestWorkers 400
MaxRequestsPerChild 100
</IfModule>
---------------------------
其中最重要的参数是 ThreadsPerChild和 MaxClients:
#ThreadsPerChild
每个子进程建立的线程数,子进程在启动时建立这些线程后就不再建立新的线程了
#MaxClients
允许同时伺服的最大接入请求数量(在worker下就是最大线程数量)
#ServerLimit:
对最大子进程数的上限,该值必须大于等于MaxClients/ThreadsPerChild
#ThreadLimit:
对ThreadsPerChild的上限,该值必须大于等于 ThreadsPerChild,如果将ThreadLimit设置成一个高出实际需要很多的ThreadsPerChild值,将会有过多的共享内存被 分配,应当和ThreadsPerChild可能达到的最大值保持一致.
#StartServers:
服务器启动时的服务进程数目,该值肯定小于等于ServerLimit
#MinSpareThreads和MaxSpareThreads:
通过新建或结束子进程的方式,将空闲线程的总数维持在这个范围内
#MaxRequestsPerChild:
用于控制服务器建立新进程和结束旧进程的频 率,其实是一个为了防止内存溢出的参数,每个子进程在其生存期内允许伺服的最大请求数量。到达MaxRequestsPerChild的限制后,子进程将 会结束。对于KeepAlive链接,只有第一个请求会被计数。事实上,它改变了每个子进程限制最大链接数量的行为。
可以通过检查HTTPServer/logs/error_log日志,判断MaxClients是否需要增加,如果有下面的报错,就说明apache自上次重启至今,曾经发生过达到MaxClients的情况:
--------------------------
4.关闭TRACE Method.
#vi /usr/local/apache2/conf/httpd.conf
TraceEnable off
5.实现service和chkconfig系统控制httpd开启关闭
# vi /etc/ld.so.conf.d/apache2.4.4.conf
-----------
/usr/local/apache2/lib
------------
# cp /usr/local/apache2/bin/apachectl /etc/init.d/httpd
# vi /etc/init.d/httpd
在首行#!/bin/sh 下面添加:
----------------
#chkconfig:345 61 61
#description:Apache httpd
---------------
# chkconfig --add httpd
# chkconfig httpd on
# service httpd restart
6.修改log日志格式实现每天保存.
#vi /usr/local/apache2/conf/httpd.conf
找到CustomLog和ErrorLog行,注释掉修改为以下参数
CustomLog "|/usr/local/apache2/bin/rotatelogs /usr/local/apache2/logs/%Y_%m_%d.access_log 86400 480" combined
ErrorLog "|/usr/local/apache2/bin/rotatelogs /usr/local/apache2/logs/%Y_%m_%d.error_log 86400 480"
五.安装php模块
1.处理依赖包
暂无..
2.安装PHP
# wget http://cn2.php.net/distributions/php-5.4.13.tar.bz2
# tar -jxvf php-5.4.13.tar.bz2
# cd php-5.4.13
此处编译安装了我们项目经常用到的PHP模块,如有其它需要可以自定义添加.
# ./configure --prefix=/usr/local/php5 --with-apxs2=/usr/local/apache2/bin/apxs --with-libxml-dir=/usr/local/lib --with-zlib-dir=/usr/local/lib --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-gd --enable-soap --enable-sockets --enable-xml --enable-mbstring --with-png-dir=/usr/local --with-jpeg-dir=/usr/local --with-curl=/usr/lib --with-freetype-dir=/usr/include/freetype2/freetype/ --enable-bcmath --enable-maintainer-zts
# 注:其中最后一个参数--enable-maintainer-zts在安装PHP5.4最新版本时必须添加(5.3貌似不需要),表示打开PHP进程安全Thread Safe,默认不添加为NON Thread Safe,开启apache服务会报错.....(此处被坑了一下午...#$%^&*)
# make
# make install
安装结束后:
#添加php配置文件,需要CP 一个源码里面的php.ini-development或php.ini-production 到/usr/local/php/lib 为 php.ini
#cd ~/php-5.4.13
#cp php.ini-development /usr/local/php5/lib/php.ini
然后配置APACHE的PHP模块:
编辑Apache配置文件,即/usr/local/apache2/conf/httpd.conf,
并添加以下一行:AddType application/x-httpd-php .php
并在 DirectoryIndex 一行后面加上 index.php
如果PHP无法加载,检查httpd.conf配置下是否有该行配置(默认安装PHP后会自动将该行写入)
LoadModule php5_module modules/libphp5.so
注: 编译安装的apache 网页存放路径跟rpm包安装的不一样。编译安装的路径在/usr/local/apache2/htdocs/
可添加以下内容info.php到/usr/local/apache2/htdocs/下,测试php模块是否加载.
# vi /usr/local/apache2/htdocs/info.php
---------------
<?php
echo phpinfo();
?>
---------------
检查80端口是否打开
# lsof -i:80
浏览器输入 http://localhost/info.php
显示有如下测试页面内容,PHP模块即在APACHE下加载成功
测试本地mysql是否正常连接
# vi /usr/local/apache2/htdocs/index.php
---------------------
<html><body><h1>The page to test mysql local connection.</h1></body></html>
<?php
$conn=mysql_connect('localhost','root','123456');
if ($conn)
echo "<h2>Success...</h2>";
else
echo "<h2>Failure...</h2>";
?>
---------------------
浏览器输入 http://localhost
显示如下页面,表示连接成功.
PHP配置: (修改php.ini,默认目录/usr/local/php5/lib/php.ini)
vi /usr/local/php5/lib/php.ini
-------------------------
# 隐藏PHP版本信息:
expose_php = Off
# 关闭警告及错误信息,爆路径:
display_errors = Off
# 调整时区,防止phpinfo()函数报错.
date.timezone =PRC
# 开启php错误日志并设置路径.
log_errors = On
error_log = /usr/local/apache2/logs/php_error.log
--------------大功告成O(∩_∩)O~-----------------
觉得不错,请大家回帖支持下....