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.1 mariadb-5.5.43
准备数据存放的文件系统
新建一个逻辑卷,并将其挂载至特定目录,一般数据库应存放于单独的文件系统之中。[[email protected] ~]# mkdir /data/mydata/ -pv ## 创建一个用于存放数据库的目录新建用户以安全方式运行进程
[[email protected] ~]# groupadd -r mysql ## 新建mysql组 [[email protected] ~]# useradd -g mysql -r -s /sbin/nologin -M -d /data/mydata/ mysql ## 新建mysql系统用户 [[email protected] ~]# chown -R mysql.mysql /data/mydata/ ## 修改目录的属主、属组为mysql安装并初始化mysql-5.5.43
下载对应的mysql版本至本地,官方下载站点:https://mariadb.org/download/ 这里是64为平台,因此对应的版本是 mariadb-5.5.43-linux-x86_64.tar.gz
注意此为二进制版本。
[[email protected] ~]# tar xf mariadb-5.5.43-linux-x86_64.tar.gz -C /usr/local/ ## 执行解压缩 [[email protected] ~]# cd /usr/local/[[email protected] local]# ln -s mariadb-5.5.43-linux-x86_64 mysql ## 创建符号链接 [[email protected] local]# cd mysql/[[email protected] mysql]# chown -R mysql.mysql . ## 修改 mysql目录下的所有文件属主和属组均为mysql [[email protected] mysql]# scripts/mysql_install_db --user=mysql --datadir=/data/mydata ## 执行数据库初始化 [[email protected] mysql]# cp support-files/my-large.cnf /etc/my.cnf ## 为mysql提供配置文件 [[email protected] mysql]# vim /etc/my.cnf ## 编辑配置文件 thread_concurrency = 2 ## 此处为CPU的个数*2 datadir = /data/mydata ## 添加行指定mysql数据存放位置为mysql提供sysv服务脚本
[[email protected] mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld [[email protected] mysql]# chmod +x /etc/rc.d/init.d/mysqld [[email protected] mysql]# chkconfig --add mysqld ## 添加至服务列表 [[email protected] mysql]# chkconfig mysql on ## 设置开机自启启动并配置数据库
[[email protected] mysql]# service mysqld start ## 启动服务 [[email protected] mysql]# ss -tan ## 查看3306是否处于监听状态 [[email protected] mysql]# export PATH=/usr/local/mysql/bin/:/$PATH [[email protected] mysql]# vim /etc/profile.d/mysql.sh ## 修改PATH环境变量,让系统可以直接使用mysql命令 export PATH=/usr/local/mysql/bin/:/$PATH [[email protected] mysql]# vim /etc/man.config ## 输出mysql的man手册至man命令的查找路径 MANPATH /usr/local/mysql/man [[email protected] mysql]# ln -sv /usr/local/mysql/include /usr/include/mysql ## 输出mysql的头文件至系统头文件路径/usr/include [[email protected] mysql]# echo ‘/usr/local/mysql/lib‘ > /etc/ld.so.conf.d/mysql.conf ## 输出mysql的库文件给系统库查找路径 [[email protected] mysql]# ldconfig ## 让系统重新载入系统库 [[email protected] mysql]# mysqladmin -uroot password ‘123.com‘ ## 修改数据库root用户密码 [[email protected] mysql]# mysql -uroot -p‘123.com‘ ## 登录数据库 MariaDB [(none)]> create database wp_DB; ## 创建数据库 MariaDB [(none)]> grant all on wp_DB.* to ‘chen‘@‘172.18.20.%‘ identified by ‘123.com‘; ## 授权用户登录数据库
2.2 httpd-2.4.6
httpd-2.4 以上版本需要较新版本的apr和apr-util,因此需要事先对其进行升级。升级方式有两种,一种是通过源代码编译安装,一种是直接升级rpm包。这里通过源代码编译安装进行全部演示。官方下载地址:http://httpd.apache.org/编译安装apr-1.5.0
[[email protected] tmp]# tar xf apr-1.5.0.tar.bz2 [[email protected] tmp]# cd apr-1.5.0 [[email protected] apr-1.5.0]# ./configure --prefix=/usr/local/apr [[email protected] apr-1.5.0]# make -j 4 && make install编译安装apr-util-1.5.2
[[email protected] tmp]# tar xf apr-util-1.5.2.tar.bz2 [[email protected] tmp]# cd apr-util-1.5.2 [[email protected] apr-util-1.5.2]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr [[email protected] apr-util-1.5.2]# make -j 4 && make install编译安装http-2.4.6
[[email protected] httpd-2.4.6]# yum -y install pcre-devel [[email protected] tmp]# tar xf httpd-2.4.6.tar.bz2 [[email protected] tmp]# cd httpd-2.4.6 [[email protected] httpd-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 [[email protected] httpd-2.4.6]# make -j 4 && make install-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文件的路径
[[email protected] tmp]# vim /etc/httpd/httpd.confPidFile "/var/run/httpd.pid" ## 在配置文件中添加这一行即可 LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so ## 启用fcgi模块提供SysV服务脚本
[[email protected] tmp]# vim /etc/rc.d/init.d/httpd ## 内容如下 #!/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 httpd=${HTTPD-/usr/local/apache/bin/httpd} prog=httpd pidfile=${PIDFILE-/var/run/httpd.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 [email protected] RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}" exit 1 esac exit $RETVAL[[email protected] tmp]# chmod +x /etc/rc.d/init.d/httpd ## 赋予脚本执行权限 [[email protected] tmp]# chkconfig --add httpd ## 加入服务列表 [[email protected] tmp]# service httpd start ## 启动httpd服务 [[email protected] tmp]# netstat -tan ## 查看80端口是否监听至于库文件、man文档、系统命令、头文件依旧按照编译安装mariadb 第5节配置即可
提供服务器状态页面
[[email protected] httpd]# vim httpd.conf ## 编辑主配置文件 <Location /server-status> SetHandler server-status Require ip 172.18.20.1 ## 仅允许172.18.20.1 主机查看 </Location>2.3 php-5.6.4
解决依赖关系
[[email protected] tmp]# yum -y groupinstall "X Software Development"如果想让编译的php支持mcrypt扩展,此处还需安装
[[email protected] tmp]# yum -y install libmcrypt libmcrypt-devel mhash mhash-devel bzip2-devel编译安装php-5.6.4
[[email protected] tmp]# tar xf php-5.6.4.tar.xz [[email protected] tmp]# cd php-5.6.4 [[email protected] php-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 [[email protected] php-5.6.4]# make -j 4 && make install如果使用PHP5.3以上版本,为了链接MySQL数据库,可以指定mysqlnd,这样在本机就不需要先安装MySQL或MySQL开发包了。mysqlnd从php 5.3开始可用,可以编译时绑定到它(而不用和具体的MySQL客户端库绑定形成依赖),但从PHP 5.4开始它就是默认设置了。
配置php-fpm
[[email protected] php-5.6.4]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm ## 为php-fpm提供SysV init脚本 [[email protected] php-5.6.4]# chmod +x /etc/rc.d/init.d/php-fpm [[email protected] php-5.6.4]# chkconfig --add php-fpm ## 添加至服务 [[email protected] php-5.6.4]# chkconfig php-fpm on ## 设置开机自启 [[email protected] php-5.6.4]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf ## 提供php-fpm配置文件 [[email protected] php-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.pid pm.max_children = 5 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3 [[email protected] php]# mkdir /var/lib/php/session [[email protected] php]# chown apache:apache /var/lib/php/session ##创建session目录,并确保运行php-fpm进程的用户对此目录有读写权限 [[email protected] php-5.6.4]# service php-fpm start ## 启动服务 [[email protected] php-5.6.4]# netstat -tanlp | grep php-fpm ## 验证是否监听9000端口编译安装xcache,为php加速
[[email protected] tmp]# tar xf xcache-3.2.0.tar.bz2 [[email protected] tmp]# cd xcache-3.2.0 [[email protected] xcache-3.2.0]# /usr/local/php/bin/phpize [[email protected] xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config [[email protected] xcache-3.2.0]# make -j 4 && make install安装完成后,会出现 Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/
编辑php.ini,整合php和xcache
[[email protected] xcache-3.2.0]# mkdir /etc/php.d [[email protected] xcache-3.2.0]# cp xcache.ini /etc/php.d/ [[email protected] xcache-3.2.0]# vim /etc/php.d/xcache.ini zend_extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/xcache.so
3.整合测试
3.1 配置http
[[email protected] ~]# vim /etc/httpd/httpd.conf User apache Group apache ## 修改运行httpd服务的属组和属组为apache AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps ## 让apache能识别php格式的页面,并支持php格式的主页 <Directory /> AllowOverride none Require all granted ## 修改为允许所有人访问 </Directory> #DocumentRoot "/usr/local/apache/htdocs" ## 禁用中心主机 DirectoryIndex index.html index.php ## 添加一项为index.php <VirtualHost 172.18.20.20:80> ## 添加虚拟主机 DocumentRoot "/www/ch1" ServerName www.ch.com ProxyRequests Off ## 关闭正向代理 ProxyPassMatch ^/(.*\.php)$ fcgi://172.18.20.21:9000/www/ch2/$1 ## 把以.php结尾的文件请求发送到php-fpm服务器 <Directory "/www/ch1"> Options none AllowOverride none Require all granted </Directory> </VirtualHost> [[email protected] ~]# useradd -r -s /sbin/nologin apache ## 新建apache系统用户 [[email protected] ~]# mkdir -pv /www/ch1 ## 创建网页所需目录 [[email protected] ~]# vim /www/ch1/index.html ## 创建html页面 <p1> 172.18.20.20 </p1> ## 编辑测试页面 [[email protected] ~]# /usr/local/apache/bin/httpd -t ## 检查配置文件 [[email protected] ~]# service httpd reload ## 重载配置文件用客户端浏览器访问 http://172.18.20.20/index.html 确保http正常工作
3.2 配置php-fpm
[[email protected] ~]# mkdir -pv /www/ch2 ## 创建网站所需目录 [[email protected] ~]# vim /www/ch2/index.php ## 新建php测试页面 <?php phpinfo(); ?>用浏览器访问 http://172.18.20.20/index.php 确保http+php工作是正常的,查看phpinfo信息能看到xcache信息,确保xcache配置安装也是没有问题的
编辑php测试页面,确保php连接mysql没有问题
[[email protected] fpm]# vim /www/ch2/index.php <?php $con = mysql_connect(‘172.18.20.22‘,‘chen‘,‘123.com‘); if ($con) echo "OK"; else echo "Failure"; ?>用浏览器访问 http://172.18.20.20/index.php 出现OK代表连接数据库正常
4.安装网站程序
4.1 安装wordpress
[[email protected] ~]# cd /www/ch2/ [[email protected] ch2]# unzip wordpress-4.3.1-zh_CN.zip [[email protected] ch2]# chown -R apache:apache wordpress [[email protected] ch2]# chmod -R g+w wordpress [[email protected] ch2]# ln -s wordpress wp以上需在http主机和php-fpm主机上做同样的操作,或者可以搭建一个nfs服务器,基于文件系统共享的方式进行设置。
访问 http://172.18.20.20/wp 配置正确的数据库信息
配置网站信息
接下来wordpress就安装完成了。
4.1 安装phpMyAdmin
[[email protected] ch2]# yum -y install php-common php-mbstring [[email protected] tmp]# unzip phpMyAdmin-4.3.5-all-languages.zip [[email protected] tmp]# chown -R apache:apache phpMyAdmin-4.3.5-all-languages [[email protected] tmp]# chmod -R g+w phpMyAdmin-4.3.5-all-languages [[email protected] tmp]# ln -s phpMyAdmin-4.3.5-all-languages php [[email protected] php]# cp config.sample.inc.php config.ini.php ## 复制phpmyadmin的配置文件 [[email protected] php]# vim config.inc.php ## 编辑配置文件 $cfg[‘Servers‘][$i][‘host‘] = ‘172.18.20.22‘; ## 将这一项改为mariadb数据库的IP地址此处也是php-fpm和httpd主机都需这样配置
在httpd主机上配置如下:
<VirtualHost 172.18.20.20:8080> DocumentRoot "/www/ch2/php" ServerName www.ch.com ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://172.18.20.21:9000/www/ch2/php/$1 <Directory "/www/ch2/php"> Options FollowSymLinks AllowOverride none Require all granted </Directory></VirtualHost>打开客户端浏览器,访问 http://172.18.20:8080登录数据,出现以下界面
5.遇到的问题
打开phpmyadmin发现网站页面显示不了图片
随便点击一张图片出现下图,可以看出是html资源路径出现问题。
检查静态页面配置,发现路径存在问题,解决之后就OK了。