案例(一)LAMP apache、php(为apahce的模块)、mysql在同一台主机上编译安装
编译版本:
httpd-2.4.9
mariadb-5.5.36
PHP-5.4.26
一、httpd编译安装和虚拟主机的配置
httpd-2.4编译步骤:
1、安装编译适用于httpd-2.4的apr
# rpm -qa |grep apr-------检查当前主机的apr版本信息
#yum check-update
下载包
lftp 172.16.0.1:/pub/Sources/sources/httpd> mget apr-1.5.0.tar.bz2 apr-util-1.5.3.tar.bz2 httpd-2.4.9.tar.bz2
(1)编译安装apr
# tar xf apr-1.5.0.tar.bz2 ----------解压包
#cd apr-1.5.0------------到这个目录下
# ./configure --prefix=/usr/local/apr-----检查编译环境(指定一个目录因为原来的2.2安装在了/usr/local下,所以我们指定了个安装路径)
#make && make install ------------------编译安装
# ls /usr/local/apr------------查看安装结果
bin build-1 include lib
[root@ning apr]# ls lib
apr.exp libapr-1.la libapr-1.so.0 pkgconfig
libapr-1.a libapr-1.so libapr-1.so.0.5.0-------1.5.0库文件
(2)编译安装apr-util
# tar xf apr-util-1.5.3.tar.bz2 ---------解压包
# cd apr-util-1.5.3
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/----检查编译环境
命令解释:--prefix-----指定apr-util的安装路径
--with-apr-----指定apr的安装目录(由于apr-util是apr的插件应用,所以必须的指明安装路径)
#make && make install ------------------编译安装
2、安装编译apache
# tar xf httpd-2.4.9.tar.bz2 ------解压包
# cd httpd-2.4.9
# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util -enable-mpms-shared=all --with-mpm=event --enable-modules=most
命令解释:
--prefix=/usr/local/apache-----指定安装目录
--sysconfdir=/etc/httpd-----指定配置文件
--enable-so------------------是否支持DSO的机制
--enable-ssl---------------开启openssl功能(必须有这个包没有安装上openssl-devel-1.0.1e-15.el6.x86_64)
--enable-cgi--------安装cgi,安装了默认是启用状态
--enable-rewrite-----支持url重写(就是比如说用户访问的是:http://www.ning.com,可以指定为https://www.ning.com)网站的迁移的时候可用
--with-zlib--------支持压缩(压缩库)
--with-pcre----这是支持重写的插件(必须安装# yum -y install pcre-devel)
--with-apr=/usr/local/apr-----指定apr路径
--with-apr-util=/usr/local/apr-util------指定apr-util
-enable-mpms-shared=all-------把所有的编译模块都编辑过来,并编程成共享模块
--with-mpm=event -------指定mpm模块
--enable-modules=most|all----(most的是安装大多数常用模块;all是安装全部模块)
#make && make install------编译安装
3、启动apache服务
(1)因为没有启动脚本只能用apachectl来启动服务
# ls /usr/local/apache/bin/
ab checkgid envvars-std htdbm httpd rotatelogs
apachectl dbmmanage fcgistarter htdigest httxt2dbm
apxs envvars htcacheclean htpasswd logresolve
(2)加入到搜索变量中来
#vim /etc/profile.d/httpd24.sh
export PATH=/usr/local/apache/bin:$PATH
#source /etc/profile.d/httpd24.sh
#apachectl start-----启动服务
4、测试就可以了。
5、编译安装注意:
把所有的文件都安装在/usr/local/apache目录下
[root@ning apache]# ls
bin build cgi-bin error htdocs icons include logs man manual modules
bin---各种二进制程序的目录
cgi-bin----cgi脚本存放位置
icons-----图标文件
include-----头文件和开放任意相关(ln -sv /usr/local/apache/include/ /usr/include/httpd24)
error------一些错误的页面文件(客户端访问的提示信息,,403报错信息)
logs------所有的日志文件和pid文件
man----帮助文件(vim /etc/man.config中添加 MANPATH /usr/local/apache/man )就可以了#man httpd
manual-------手册
modules----模块
网页文件存放的位置是在:/usr/local/apache/htdocs------安装在htdocs目录下
6、配置文件:
[root@ning httpd24]# ls /etc/httpd24
extra httpd.conf magic mime.types original-----主配置文件httpd.conf是调用extra的配置文件,()
[root@ning httpd24]# ls extra/----------查看extra
httpd-autoindex.conf httpd-languages.conf httpd-ssl.conf
httpd-dav.conf httpd-manual.conf httpd-userdir.conf
httpd-default.conf httpd-mpm.conf httpd-vhosts.conf
httpd-info.conf httpd-multilang-errordoc.conf proxy-html.conf
修改mpm
#vim /etc/httpd24/httpd.conf中修改模块加载信息把event修改成prefork
LoadModule mpm_event_module modules/mod_mpm_event.so修改成LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
修改后测试下
/usr/local/apache/bin/httpd -t
启用mpm修改配置信息
#vim /etc/httpd24/httpd.conf
Include /etc/httpd24/extra/httpd-mpm.conf----去掉注释即可
#vim /etc/httpd24/extra/httpd-mpm.conf
7、服务脚本:其实复制修改路径即可。。
[root@ning init.d]# cat /etc/rc.d/init.d/httpd24
#!/bin/bash
#
# httpd Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server. It is used to serve \
# HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid
# Source function library.
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi
# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}
# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""
# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.
# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache/bin/apachectl-------程序apachectl的路径
httpd=${HTTPD-/usr/local/apache/bin/httpd}------主程序的路径
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}-------pid路径
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
start() {
echo -n $"Starting $prog: "
LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d 10 $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
echo -n $"Reloading $prog: "
if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
RETVAL=$?
echo $"not reloading due to configuration syntax error"
failure $"not reloading $httpd due to configuration syntax error"
else
killproc -p ${pidfile} $httpd -HUP
RETVAL=$?
fi
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart)
if [ -f ${pidfile} ] ; then
stop
start
fi
;;
reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
exit 1
esac
exit $RETVAL
httpd-2.4上做虚拟主机
1、#vim /etc/httpd24/httpd.conf
(1)注释#DocumentRoot "/usr/local/apache/htdocs"
开启虚拟机功能
Include /etc/httpd24/extra/httpd-vhosts.conf
由于2.4版本权限有严格限制,必须得给权限
<Directory ~ "/usr/local/host">
Options -Indexes -FollowSymLinks
AllowOverride None
Require all granted
</Directory>
(2)虚拟主机不再需要NameVirtualHost指令
在 #vim /etc/httpd24/extra/httpd-vhosts.conf
<VirtualHOST 172.16.3.1:80>
documentroot "/usr/local/host1"
servername www.centos.com
ErrorLog /usr/local/host1/error_log
CustomLog /usr/local/host1/access_log common
<Directory "/usr/local/host1">
Require all granted
</Directory>
</VirtUalHOST>
<VirtualHOST 172.16.3.1:80>
documentroot "/usr/local/host2"
servername www.apache.com
ErrorLog /usr/local/host2/error_log
CustomLog /usr/local/host2/access_log common
<Directory "/usr/local/host2">
Require all granted
</Directory>
</VirtUalHOST>
<VirtualHOST 172.16.3.1:80>
documentroot "/usr/local/host3"
servername www.redhat.com
ErrorLog /usr/local/host3/error_log
CustomLog /usr/local/host3/access_log common
<Directory "/usr/local/host3">
Require all granted
</Directory>
</VirtUalHOST>
至此完成
二、mariadb编译安装
mariadb-5.5.36:
通过二进制格式安装:
编译好的二进制程序:(安装平台是有关系的,32位的操作系统,只能安装32位数据库)
1、下载安装包,展开程序
mariadb-5.5.36-linux-x86_64.tar.gz
(1)mariadb安装目录必须是mysqld9(否则一些默认的程序无法运行)
#tar xf mariadb-5.5.36-linux-x86_64.tar.gz -C /usr/local------指定解压目录
(2)查看软件包提示怎么安装的
#ls /usr/local/mariadb-5.5.36-linux-x86_64
INSTALL-BINARY---------这个文件当中说明了怎么安装
(3)数据库的数据我们一般存储在和系统不在同一个磁盘上指定的LVM的单独逻辑卷中(方便以后管理扩展、快照备份等),以保重数据不丢失。
创建个逻辑卷
#fdisk /dev/sda (创建/dev/sda3,类型为8e)
#kpartx -af /dev/ada----加载磁盘分区信息
#partx -a /dev/sda
#pvcreate /dev/sda3
#vgcreate myvg /dev/sda3
#lvcreate -L 3G -n mydata myvg
#mke2fs -t ext4 -L MYDATA -b 4096 -m 3 /dev/myvg/mydata
#mkdir mydata
#echo "LABEL=MYDATA /mydata ext4 defaults 0 0 >> /etc/fstab
#mount -a---立即生效挂载分区
#mkdir /mydata/data----创建个子目录来存放数据
(4)创建数据库的用户和组为mysql用户
#groupadd -r mysql
#useradd -r -g mysql mysql
(5)设置存放数据目录的属组和属主
#chown -R mysql.mysql /mydata/data
2、安装mariadb
# ln -sv mariadb-5.5.36-linux-x86_64/ mysql---方便以后升级安装重名问题,我们在这里创建链接,下次在安装的时候就直接删除连接即可。
#cd /usr/local/mysql
#chown -R root.mysql ./*
(1)初始化修改配置文件
配置文件:/usr/local/mysql/support-files
在这里我们说下配置文件:
my-small.cnf--可用内存的不同的配置文件(小)
my-large.cnf----(大)
my-huge.cnf ------(巨大)
由于这里提供的配置文件与物理机配置不同,选择不同的配置文件。
我们这里使用的是虚拟机做实验,用my-large.cnf足够了
复制配置文件,以my-large.cnf为模版
#mkdir /etc/mysql(默认情况下有rpm -ql mysql-libs查看下,默认提供mysql客户端包)
#cp /usr/local/mysql/support-files/my-large.cnf /etc/mysql/my.cnf
#vim /etc/mysql/my.cnf
在配置文件中必须加上指明数据存储的目录datadir = /mydata/data这个目录必须放定义mysqld属性下
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock
# Here follows entries for some specific programs
# The MariaDB server
[mysqld]
port = 3306
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 8
datadir = /mydata/data-----在这个配置文件 中只需要添加这条信息即可指定数据的存储目录
(2)执行初始化脚本;
由于mysql_install_db需要/usr/local/mysql/目录下的bin文件的信息,指明路径执行
#scripts/mysql_install_db --datadir=/mydata/data --user=mysql
命令解释:--datadir=/mydata/data:指定数据库存放的目录
--user=mysql:必须用mysql用户运行
(3)查看初始化是否成功
# ls /mydata/data(主要查看我们存放数据的目录下是否生成数据库信息。)
aria_log.00000001 mysql mysql-bin.000002 performance_schema
aria_log_control mysql-bin.000001 mysql-bin.index test
(4)启动服务这里需要启动脚本
在安装目录下/usr/local/mysql/support-files/
#ls /usr/local/mysql/support-files/
[root@ning mysql]# ls support-files/(中的mysql.server)
binary-configure magic my-medium.cnf mysql.server
config.huge.ini my-huge.cnf my-small.cnf ndb-config-2-node.ini
config.medium.ini my-innodb-heavy-4G.cnf mysqld_multi.server SELinux
config.small.ini my-large.cnf mysql-log-rotate solaris
只要是安装目录没有改动(/usr/local/mysql下),复制过去就能启动
# cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld(注意下,要是你的虚拟机上还装有不同版本的mysql,在启动脚本下有服务启动脚本,你只需要复制过去叫,mysqld55起个名字即可,在以后使用时记得即可)
#chkconfig --add mysqld
#chkcofnig --list
#service mysqld start
(5)测试:安装的结果,由于是我们编译安装还需要提供,启动命令的修改
导出:命令
# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH----指定路径
# source /etc/profile.d/mysql.sh
导出:帮助信息
#vim /etc/man.config
MANPATH /usr/local/mysql/man
导出:头信息文件
#ln -sv /usr/local/mysql/include/ /usr/include/mysql
导出:库文件
#ldconfig -v |grep mysql- --查看以下库文件版本
# vim /etc/ld.so.conf.d/mysql.conf--创建配置文件并添加以下内容即可
usr/local/mysql/lib
成功的结果:
[root@ning mysql]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.36-MariaDB-log MariaDB Server-----查看版本是否正确
Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> Ctrl-C -- exit!
Aborted
[root@ning mysql]# vim /etc/profile.d/mysql.sh
[root@ning mysql]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.36-MariaDB-log MariaDB Server
Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
三、PHP编译安装
1、编译安装PHP-5.4.26
(1)编译环境
# yum -y groupinstall "Desktop Platform Development"
# yum -y install bzip2-devel libmcrypt-devel (PHP编译安装时会用到,压缩库)
(2)解压编译安装
# tar xf php-5.4.26.tar.bz2
# cd php-5.4.26
#./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts
命令解释
--prefix=/usr/local/php ----------------------PHP安装路径
--with-mysql=/usr/local/mysql -----------------指定mysql的位置,(编译的mysql安装需要指明安装的路径,如果是rpm包安装的--with-mysql会找mysql-devel包安装的位置,需要单独安装)
--with-openssl ---------------------------------支持openssl
--with-mysqli=/usr/local/mysql/bin/mysql_config -----mysql的另外一种接口,有些PHP是通过这个接口来连接的。
--enable-mbstring ------------------------------支持多字节字串(支持中文)
--with-freetype-dir ----------------------------字体处理工具
--with-jpeg-dir --------------------------------直接生成jpeg图片的
--with-png-dir ---------------------------------直接生成png图片
--with-zlib ------------------------------------压缩库
--with-libxml-dir=/usr -------------------------扩展标记语言(跨平台交互的)指明库文件的位置在/usr下(不指定默认去/usr/local找)
--enable-xml ----------------------------------启用xml功能
--enable-sockets -------------------------------允许PHP基于sockets通信
--with-apxs2=/usr/local/apache/bin/apxs --------编译第三方模块的,指定编译模块的位置,在/usr/local/apache/bin/apxs
--with-mcrypt-----------------------------------支持使用加密库
--with-config-file-path=/etc -------------------php配置文件存放位置(php.ini)
--with-config-file-scan-dir=/etc/php.d ---------php复制配置文件的存放位置(以.ini结尾的文件)
--with-bz2 ------------------------------------支持bz2压缩
--enable-maintainer-zts-------------------------mpm为worker和event必须加上
说明:
1、这里为了支持apache的worker或event这两个MPM,编译时使用了--enable-maintainer-zts选项。
2、如果使用PHP5.3以上版本,为了链接MySQL数据库,可以指定mysqlnd,这样在本机就不需要先安装MySQL或MySQL开发包了。mysqlnd从php 5.3开始可用,可以编译时绑定到它(而不用和具体的MySQL客户端库绑定形成依赖),但从PHP 5.4开始它就是默认设置了。
# ./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd
-----------------------------------------------------------------------------
一个小错误编译时检查编译环境时出现:checking for xml2-config path...
configure: error: xml2-config not found. Please check your libxml2 installation.
解决方法:
# yum install libxml2-devel
----------------------------------------------------------------------
#make && make install
2、为php提供配置文件:
# cp php.ini-production /etc/php.ini (如果是开放环境就需要提供cp php.ini-development /etc/php.ini)
3、 编辑apache配置文件httpd.conf,以apache支持php
# vim /etc/httpd/httpd.conf
(1)、添加如下二行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
(2)、定位至DirectoryIndex index.html
修改为:
DirectoryIndex index.php index.html
4、测试:
首先启动服务
#service httpd24 restart
#service mysqld55 restart
(1)、测试php是否正常使用
#vim /usr/local/host1/index.php
<html>
<?php
phpinfo();
?>
</html>
~
测试结果有图有真相
(2)测试数据库是否能够连同
#vim /usr/local/host1/index.php
<html> <body><h>ninghongliang </h>
<?php
$link = mysql_connect('localhost','liang','ning');
if ($link)
echo "Success...";
else
echo "Failure";
?>
</body></html>
测试结果(有图有真相)
数据库授权账户命令:
#mysql
>GRANT ALL ON mydb.* TO 'root'@'localhost' IDENTIFIED BY 'ning';(授权帐号为root,密码为ning)
在apache上安装phpMyAdmin:
#cd /usr/local/host2
#tar xf phpMyAdmin-3.5.1-all-languages.tar.bz2
#mv phpMyAdmin-3.5.1-all-languages php
浏览器测试:
http://www.apache.com/php 登入数据库授权的帐号即可
5、压力测试:
安装xcache,为php加速:
(1)编译安装xcache-3.10
#tar xf tar xf xcache-3.1.0.tar.bz2
# cd xcache-3.0.3
# /usr/local/php/bin/phpize----是个应用程序来生成configure(用来和当前的php版本来整合生成configure)
# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
命令解释:--enable-xcache--------------启用xcache模块
--with-php-config=/usr/local/php/bin/php-config----指定php中的配置文件
# make && make install
安装结束时,会出现类似如下行:
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20100525/---下游xcache.so的模块
(2)编译完成之后要给PHP提供配置文件
#ls /root/xcache-3.1.0源码目录下有 xcache.ini配置文件
首先将xcache提供的样例配置导入php.ini
# mkdir /etc/php.d-----PHP会读取配置文件
#cd /root/xcache-3.1.0
# cp xcache.ini /etc/php.d
# vim /etc/php.d/xcache.ini
接下来编辑/etc/php.d/xcache.ini,找到extension开头的行,修改为如下行:
extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so
注意:如果php.ini文件中有多条extension指令行,要确保此新增的行排在第一位。
没有装xcache前后的测试结果:
# ab -n 2000 -c 1000 http://www.apache.com/
Requests per second: 229.73 [#/sec] (mean)
Time per request: 4353.017 [ms] (mean)
Time per request: 4.353 [ms] (mean, across all concurrent requests)
Transfer rate: 139.76 [Kbytes/sec] received
Requests per second: 95.09 [#/sec] (mean)
Time per request: 10516.734 [ms] (mean)
Time per request: 10.517 [ms] (mean, across all concurrent requests)
Transfer rate: 57.53 [Kbytes/sec] received