1.编译安装介绍

在工作中,基于各种生产环境下,可能yum安装不能满足我们的需求。这个时候我们就得自己动手编译安装软件包,当然只要掌握了技巧,其实编译安装也非常的简单。本文将介绍如何在CentOS 6.7系统下编译安装:httpd-2.4.6 +php-5.6.4 +xcache-3.2.0 +mariadb-5.5.43,编译完成后基于LAMP环境搭建一个Wordpress 和phpMyAdmin。


2.编译安装 2.1mariadb-5.5.43

准备数据存放的文件系统

新建一个逻辑卷,并将其挂载至特定目录,一般数据库应存放于单独的文件系统之中。

[root@ch~]#mkdir/data/mydata/-pv##创建一个用于存放数据库的目录


新建用户以安全方式运行进程

[root@ch~]#groupadd-rmysql##新建mysql组[root@ch~]#useradd-gmysql-r-s/sbin/nologin-M-d/data/mydata/mysql##新建mysql系统用户[root@ch~]#chown-Rmysql.mysql/data/mydata/##修改目录的属主、属组为mysql
安装并初始化mysql-5.5.43

下载对应的mysql版本至本地,官方下载站点:https://mariadb.org/download/这里是64为平台,因此对应的版本是 mariadb-5.5.43-linux-x86_64.tar.gz

注意此为二进制版本。

[root@ch~]#tarxfmariadb-5.5.43-linux-x86_64.tar.gz-C/usr/local/##执行解压缩[root@ch~]#cd/usr/local/[root@chlocal]#ln-smariadb-5.5.43-linux-x86_64mysql##创建符号链接[root@chlocal]#cdmysql/[root@chmysql]#chown-Rmysql.mysql.##修改mysql目录下的所有文件属主和属组均为mysql[root@chmysql]#scripts/mysql_install_db--user=mysql--datadir=/data/mydata##执行数据库初始化[root@chmysql]#cpsupport-files/my-large.cnf/etc/my.cnf##为mysql提供配置文件[root@chmysql]#vim/etc/my.cnf##编辑配置文件thread_concurrency=2##此处为CPU的个数*2datadir=/data/mydata##添加行指定mysql数据存放位置


为mysql提供sysv服务脚本

[root@chmysql]#cpsupport-files/mysql.server/etc/rc.d/init.d/mysqld[root@chmysql]#chmod+x/etc/rc.d/init.d/mysqld[root@chmysql]#chkconfig--addmysqld##添加至服务列表[root@chmysql]#chkconfigmysqlon##设置开机自启


启动并配置数据库

[root@chmysql]#servicemysqldstart##启动服务[root@chmysql]#ss-tan##查看3306是否处于监听状态[root@chmysql]#exportPATH=/usr/local/mysql/bin/:/$PATH[root@chmysql]#vim/etc/profile.d/mysql.sh##修改PATH环境变量,让系统可以直接使用mysql命令exportPATH=/usr/local/mysql/bin/:/$PATH[root@chmysql]#vim/etc/man.config##输出mysql的man手册至man命令的查找路径MANPATH/usr/local/mysql/man[root@chmysql]#ln-sv/usr/local/mysql/include/usr/include/mysql##输出mysql的头文件至系统头文件路径/usr/include[root@chmysql]#echo'/usr/local/mysql/lib'>/etc/ld.so.conf.d/mysql.conf##输出mysql的库文件给系统库查找路径[root@chmysql]#ldconfig##让系统重新载入系统库[root@chmysql]#mysqladmin-urootpassword'123.com'##修改数据库root用户密码[root@chmysql]#mysql-uroot-p'123.com'##登录数据库MariaDB[(none)]>createdatabasewp_DB;##创建数据库MariaDB[(none)]>grantallonwp_DB.*to'chen'@'172.18.20.%'identifiedby'123.com';##授权用户登录数据库

2.2httpd-2.4.6

httpd-2.4 以上版本需要较新版本的apr和apr-util,因此需要事先对其进行升级。升级方式有两种,一种是通过源代码编译安装,一种是直接升级rpm包。这里通过源代码编译安装进行全部演示。官方下载地址:http://httpd.apache.org/


编译安装apr-1.5.0

[root@chtmp]#tarxfapr-1.5.0.tar.bz2[root@chtmp]#cdapr-1.5.0[root@chapr-1.5.0]#./configure--prefix=/usr/local/apr[root@chapr-1.5.0]#make-j4&&makeinstall
编译安装apr-util-1.5.2

[root@chtmp]#tarxfapr-util-1.5.2.tar.bz2[root@chtmp]#cdapr-util-1.5.2[root@chapr-util-1.5.2]#./configure--prefix=/usr/local/apr-util--with-apr=/usr/local/apr[root@chapr-util-1.5.2]#make-j4&&makeinstall
编译安装http-2.4.6

[root@chhttpd-2.4.6]#yum-yinstallpcre-devel[root@chtmp]#tarxfhttpd-2.4.6.tar.bz2[root@chtmp]#cdhttpd-2.4.6[root@chhttpd-2.4.6]#./configure--prefix=/usr/local/apache--sysconfdir=/etc/httpd--enable-so--enable-ssl--enable-fcgi--enable-rewrite--with-zlib--with-pcre--with-apr=/usr/local/apr--with-apr-util=/usr/local/apr-util--enable-modules=most--enable-mpms-shared=all--enable-mpm=worker[root@chhttpd-2.4.6]#make-j4&&makeinstall

-enable-so 支持动态模块机制

-enable-ssl 支持ssl功能

-enable-fcgi -enable-rewrite 支持fcgi和url重写

-enable-modules=all 启用大多数常用模块

-enable-mpms-shared=all 编译prefork/worker/event 3个模块

-with-mpm-worker 设置默认模块为worker

-with-zlib 支持压缩

-with-pcre 支持pre扩展的正则表达式引擎


修改httpd的主配置文件,设置其Pid文件的路径

[root@chtmp]#vim/etc/httpd/httpd.confPidFile"/var/run/httpd.pid"##在配置文件中添加这一行即可LoadModuleproxy_modulemodules/mod_proxy.soLoadModuleproxy_fcgi_modulemodules/mod_proxy_fcgi.so##启用fcgi模块


提供SysV服务脚本

[root@chtmp]#vim/etc/rc.d/init.d/httpd##内容如下#!/bin/bash##httpdStartupscriptfortheApacheHTTPServer##chkconfig:-8515#description:ApacheisaWorldWideWebserver.Itisusedtoserve\#HTMLfilesandCGI.#processname:httpd#config:/etc/httpd/conf/httpd.conf#config:/etc/sysconfig/httpd#pidfile:/var/run/httpd.pid#Sourcefunctionlibrary../etc/rc.d/init.d/functionsif[-f/etc/sysconfig/httpd];then./etc/sysconfig/httpdfi#StarthttpdintheClocalebydefault.HTTPD_LANG=${HTTPD_LANG-"C"}#Thiswillpreventinitlogfromswallowingupapass-phrasepromptif#mod_sslneedsapass-phrasefromtheuser.INITLOG_ARGS=""#SetHTTPD=/usr/sbin/httpd.workerin/etc/sysconfig/httpdtouseaserver#withthethread-based"worker"MPM;BEWARNEDthatsomemodulesmaynot#workcorrectlywithathread-basedMPM;notablyPHPwillrefusetostart.#Pathtotheapachectlscript,serverbinary,andshort-formformessages.apachectl=/usr/local/apache/bin/apachectlhttpd=${HTTPD-/usr/local/apache/bin/httpd}prog=httpdpidfile=${PIDFILE-/var/run/httpd.pid}lockfile=${LOCKFILE-/var/lock/subsys/httpd}RETVAL=0start(){echo-n$"Starting$prog:"LANG=$HTTPD_LANGdaemon--pidfile=${pidfile}$httpd$OPTIONSRETVAL=$?echo[$RETVAL=0]&&touch${lockfile}return$RETVAL}stop(){echo-n$"Stopping$prog:"killproc-p${pidfile}-d10$httpdRETVAL=$?echo[$RETVAL=0]&&rm-f${lockfile}${pidfile}}reload(){echo-n$"Reloading$prog:"if!LANG=$HTTPD_LANG$httpd$OPTIONS-t>&/dev/null;thenRETVAL=$?echo$"notreloadingduetoconfigurationsyntaxerror"failure$"notreloading$httpdduetoconfigurationsyntaxerror"elsekillproc-p${pidfile}$httpd-HUPRETVAL=$?fiecho}#Seehowwewerecalled.case"$1"instart)start;;stop)stop;;status)status-p${pidfile}$httpdRETVAL=$?;;restart)stopstart;;condrestart)if[-f${pidfile}];thenstopstartfi;;reload)reload;;graceful|help|configtest|fullstatus)$apachectl$@RETVAL=$?;;*)echo$"Usage:$prog{start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"exit1esacexit$RETVAL


[root@chtmp]#chmod+x/etc/rc.d/init.d/httpd##赋予脚本执行权限[root@chtmp]#chkconfig--addhttpd##加入服务列表[root@chtmp]#servicehttpdstart##启动httpd服务[root@chtmp]#netstat-tan##查看80端口是否监听

至于库文件、man文档、系统命令、头文件依旧按照编译安装mariadb 第5节配置即可


提供服务器状态页面

[root@chhttpd]#vimhttpd.conf##编辑主配置文件<Location/server-status>SetHandlerserver-statusRequireip172.18.20.1##仅允许172.18.20.1主机查看</Location>


2.3 php-5.6.4

解决依赖关系

[root@chtmp]#yum-ygroupinstall"XSoftwareDevelopment"

如果想让编译的php支持mcrypt扩展,此处还需安装

[root@chtmp]#yum-yinstalllibmcryptlibmcrypt-develmhashmhash-develbzip2-devel
编译安装php-5.6.4

[root@chtmp]#tarxfphp-5.6.4.tar.xz[root@chtmp]#cdphp-5.6.4[root@chphp-5.6.4]#./configure--prefix=/usr/local/php--with-mysql=mysqlnd--with-pdo-mysql=mysqlnd--with-mysqli=mysqlnd--with-openssl--enable-mbstring--with-freetype-dir--with-jpeg-dir--with-png-dir--with-zlib--with-libxml-dir=/usr--enable-xml--enable-sockets--enable-fpm--with-mcrypt--with-config-file-path=/etc--with-config-file-scan-dir=/etc/php.d--with-bz2[root@chphp-5.6.4]#make-j4&&makeinstall

如果使用PHP5.3以上版本,为了链接MySQL数据库,可以指定mysqlnd,这样在本机就不需要先安装MySQL或MySQL开发包了。mysqlnd从php 5.3开始可用,可以编译时绑定到它(而不用和具体的MySQL客户端库绑定形成依赖),但从PHP 5.4开始它就是默认设置了。


配置php-fpm

[root@chphp-5.6.4]#cpsapi/fpm/init.d.php-fpm/etc/rc.d/init.d/php-fpm##为php-fpm提供SysVinit脚本[root@chphp-5.6.4]#chmod+x/etc/rc.d/init.d/php-fpm[root@chphp-5.6.4]#chkconfig--addphp-fpm##添加至服务[root@chphp-5.6.4]#chkconfigphp-fpmon##设置开机自启[root@chphp-5.6.4]#cp/usr/local/php/etc/php-fpm.conf.default/usr/local/php/etc/php-fpm.conf##提供php-fpm配置文件[root@chphp-5.6.4]#vim/usr/local/php/etc/php-fpm.conf##编辑配置文件listen=172.18.20.21:9000##服务监听的地址listen.allowed_clients=172.18.20.20##允许为哪个客户端响应转发请求pid=/usr/local/php/var/run/php-fpm.pidpm.max_children=5pm.start_servers=2pm.min_spare_servers=1pm.max_spare_servers=3[root@chphp]#mkdir/var/lib/php/session[root@chphp]#chownapache:apache/var/lib/php/session##创建session目录,并确保运行php-fpm进程的用户对此目录有读写权限[root@chphp-5.6.4]#servicephp-fpmstart##启动服务[root@chphp-5.6.4]#netstat-tanlp|grepphp-fpm##验证是否监听9000端口


编译安装xcache,为php加速

[root@chtmp]#tarxfxcache-3.2.0.tar.bz2[root@chtmp]#cdxcache-3.2.0[root@chxcache-3.2.0]#/usr/local/php/bin/phpize[root@chxcache-3.2.0]#./configure--enable-xcache--with-php-config=/usr/local/php/bin/php-config[root@chxcache-3.2.0]#make-j4&&makeinstall

安装完成后,会出现Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/


编辑php.ini,整合php和xcache

[root@chxcache-3.2.0]#mkdir/etc/php.d[root@chxcache-3.2.0]#cpxcache.ini/etc/php.d/[root@chxcache-3.2.0]#vim/etc/php.d/xcache.inizend_extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/xcache.so


3.整合测试
3.1 配置http

[root@ch~]#vim/etc/httpd/httpd.confUserapacheGroupapache##修改运行httpd服务的属组和属组为apacheAddTypeapplication/x-httpd-php.phpAddTypeapplication/x-httpd-php-source.phps##让apache能识别php格式的页面,并支持php格式的主页<Directory/>AllowOverridenoneRequireallgranted##修改为允许所有人访问</Directory>#DocumentRoot"/usr/local/apache/htdocs"##禁用中心主机DirectoryIndexindex.htmlindex.php##添加一项为index.php<VirtualHost172.18.20.20:80>##添加虚拟主机DocumentRoot"/www/ch2"ServerNamewww.ch.comProxyRequestsOff##关闭正向代理ProxyPassMatch^/(.*\.php)$fcgi://172.18.20.21:9000/www/ch3/$1##把以.php结尾的文件请求发送到php-fpm服务器<Directory"/www/ch2">OptionsnoneAllowOverridenoneRequireallgranted</Directory></VirtualHost>[root@ch~]#useradd-r-s/sbin/nologinapache##新建apache系统用户[root@ch~]#mkdir-pv/www/ch2##创建网页所需目录[root@ch~]#vim/www/ch2/index.html##创建html页面<p1>172.18.20.20</p1>##编辑测试页面[root@ch~]#/usr/local/apache/bin/httpd-t##检查配置文件[root@ch~]#servicehttpdreload##重载配置文件


用客户端浏览器访问http://172.18.20.20/index.html确保http正常工作



3.2 配置php-fpm

[root@ch~]#mkdir-pv/www/ch3##创建网站所需目录[root@ch~]#vim/www/ch3/index.php##新建php测试页面<?phpphpinfo();?>

用浏览器访问http://172.18.20.20/index.php确保http+php工作是正常的,查看phpinfo信息能看到xcache信息,确保xcache配置安装也是没有问题的



编辑php测试页面,确保php连接mysql没有问题

[root@chfpm]#vim/www/ch3/index.php<?php$con=mysql_connect('172.18.20.22','chen','123.com');if($con)echo"OK";elseecho"Failure";?>

用浏览器访问http://172.18.20.20/index.php出现OK代表连接数据库正常


4.安装网站程序
4.1 安装wordpress

[root@ch~]#cd/www/ch3/[root@chch3]#unzipwordpress-4.3.1-zh_CN.zip[root@chch3]#chown-Rapache:apachewordpress[root@chch3]#chmod-Rg+wwordpress[root@chch3]#ln-swordpresswp

以上需在http主机和php-fpm主机上做同样的操作,或者可以搭建一个nfs服务器,基于文件系统共享的方式进行设置。

访问http://172.18.20.20/wp 配置正确的数据库信息

配置网站信息

接下来wordpress就安装完成了。
4.1 安装phpMyAdmin

[root@chch3]#yum-yinstallphp-commonphp-mbstring[root@chtmp]#unzipphpMyAdmin-4.3.5-all-languages.zip[root@chtmp]#chown-Rapache:apachephpMyAdmin-4.3.5-all-languages[root@chtmp]#chmod-Rg+wphpMyAdmin-4.3.5-all-languages[root@chtmp]#ln-sphpMyAdmin-4.3.5-all-languagesphp[root@chphp]#cpconfig.sample.inc.phpconfig.ini.php##复制phpmyadmin的配置文件[root@chphp]#vimconfig.inc.php##编辑配置文件$cfg['Servers'][$i]['host']='172.18.20.22';##将这一项改为mariadb数据库的IP地址

此处也是php-fpm和httpd主机都需这样配置


在httpd主机上配置如下:

<VirtualHost172.18.20.20:8080>DocumentRoot"/www/ch3/php"ServerNamewww.ch.comProxyRequestsOffProxyPassMatch^/(.*\.php)$fcgi://172.18.20.21:9000/www/ch3/php/$1<Directory"/www/ch3/php">OptionsFollowSymLinksAllowOverridenoneRequireallgranted</Directory></VirtualHost>


打开客户端浏览器,访问http://172.18.20:8080登录数据,出现以下界面


5.遇到的问题

打开phpmyadmin发现网站页面显示不了图片


随便点击一张图片出现下图,可以看出是html资源路径出现问题。

检查静态页面配置,发现路径存在问题,解决之后就OK了。