一、使用的程序包名称及版本:
httpd:2.4.29
mariadb-server:10.2.9
PHP:5.6.32
备注:httpd-2.4.x依赖于apr-1.4+以及apr-util-1.4+,而CentOS6.9 yum安装的apr和apr-util的版本均为1.3.9,httpd的版本为2.2.15,所以如果要在CentOS 6.9中使用httpd-2.4.x,只能重新源码编译安装
apr:Apache Portable Runtime,Apache运行时环境,可以理解为Apache虚拟机,主要为上层的应用程序提供一个可以跨越多操作系统平台使用的底层支持接口库。
二、准备工作:
1、下载相关程序包:
httpd-2.4.29,http://mirror.bit.edu.cn/apache/httpd/
mariadb-10.2.9,https://downloads.mariadb.org/
PHPMyAdmin-4.7.5-all-languages.zip,https://www.PHPmyadmin.net/downloads/
xcache-3.2.0,http://xcache.lighttpd.net/wiki/ReleaseArchive
2、关闭iptables和SELinux
3、服务器时间校对
4、配置epel源
5、安装开发环境程序包组:# yum -y groupinstall "Development Tools" "Server PlatformDevelopment"
6、安装依赖程序包:# yum -y install pcre-devel expat-devel libxml2-devel bzip2-devel libmcrypt-devel curl-devel libpng-devel gmp-devel libc-client-devel recode-devel net-snmp-devel libtidy-devel libjpeg-devel freetype-devel readline-devel libicu-devel unzip
7、配置相关库文件:# ln -sv /usr/lib64/libc-client.so /usr/lib/libc-client.so # ln -sv /usr/lib64/libldap* /usr/lib/
三、安装配置httpd-2.4.29:
1、安装配置apr-1.6.2:
# tar -xf apr-1.6.2.tar.gz -C /usr/src
# cd /usr/src/apr-1.6.2
# vim configure --> 用#号注释第30951行代码$RM"$cfgfile"
# ./configure --prefix=/usr/local/apr16 --enable-threads --enable-posix-shm --enable-pool-concurrency-check --enable-other-child --disable-ipv6 --with-sendfile
# make && make install
# vim /etc/ld.so.conf.d/apr16.conf --> /usr/local/apr16/lib
# ldconfig
2、安装配置apr-util-1.6.0:
# tar -xf apr-util-1.6.0.tar.gz -C /usr/src
# cd /usr/src/apr-util-1.6.0
# ./configure --prefix=/usr/local/apr-util16 --with-apr=/usr/local/apr16 --with-crypto
# vim /etc/ld.so.conf.d/apr-util16.conf--> /usr/local/apr-util16/lib
3、编译安装httpd-2.4.29:
# groupadd -r apache
# useradd -r -g apache apache
# id apache
# tar -xf httpd-2.4.29.tar.gz -C /usr/src
# cd /usr/src/httpd-2.4.29
# ./configure --prefix=/usr/local/apache24 --enable-modules=all --enable-mods-shared=all --enable-authn-dbm --enable-authn-anon --enable-authn-dbd --enable-authn-socache --enable-authz-dbm --enable-authz-owner --enable-authz-dbd --enable-authnz-fcgi --enable-auth-form --enable-auth-digest --enable-allowmethods --enable-isapi --enable-file-cache --enable-cache --enable-cache-disk --enable-cache-socache --enable-socache-shmcb --enable-socache-dbm --enable-socache-memcache --enable-so --enable-watchdog --enable-macro --enable-dbd --enable-dumpio --enable-echo --enable-case-filter --enable-case-filter-in --enable-buffer --enable-data --enable-ratelimit --enable-ext-filter --enable-request --enable-include --enable-reflector --enable-substitute --enable-sed --enable-deflate --enable-http --enable-log-forensic --enable-logio --enable-luajit --enable-mime-magic --enable-cern-Meta --enable-expires --enable-ident --enable-usertrack --enable-unique-id --enable-remoteip --enable-proxy --enable-proxy-connect --enable-proxy-ftp --enable-proxy-http --enable-proxy-fcgi --enable-proxy-scgi --enable-proxy-fdpass --enable-proxy-wstunnel --enable-proxy-ajp --enable-proxy-balancer --enable-proxy-express --enable-proxy-hcheck --enable-session --enable-session-cookie --enable-session-crypto --enable-session-dbd --enable-slotmem-shm --enable-slotmem-plain --enable-ssl --enable-dialup --enable-lbmethod-byrequests --enable-lbmethod-bytraffic --enable-lbmethod-bybusyness --enable-lbmethod-heartbeat --enable-mpms-shared=all --enable-heartbeat --enable-heartmonitor --enable-dav --enable-asis --enable-info --enable-suexec --enable-cgid --enable-cgi --enable-dav-fs --enable-dav-lock --enable-vhost-alias --enable-negotiation --enable-imagemap --enable-actions --enable-speling --enable-userdir --enable-rewrite --with-apr=/usr/local/apr16 --with-apr-util=/usr/local/apr-util16 --with-pcre --with-z --with-ssl --with-mpm=event
# make -j 4 && make install
# ls /usr/local/apache24
备注:.so扩展名的文件为模块文件,静态加载的模块不会显示在modules目录下,静态加载的模块会内嵌在系统里。动态加载的模块都会显示在modules目录下,启用这些模块,只需要在主配置文件中使用LoadModule命令装载这些模块即可。
(1)导出二进制程序目录至PATH环境变量:
# vim /etc/profile.d/httpd24.sh --> export PATH=/usr/local/apache24/bin:$PATH
# . /etc/profile.d/httpd24.sh
# echo $PATH
(2)导出头文件:# ln -sv /usr/local/apache24/include /usr/include/httpd24
(3)导出man帮助文档:# vim /etc/man.config--> MANPATH /usr/local/apache24/man
(4)配置使用service命令控制httpd服务:# cp /usr/local/apache24/bin/apachectl /etc/init.d/httpd24
备注:为了不和yum方式安装的httpd冲突,所以服务脚本名称叫httpd24,如果仍命名为httpd,以后使用命令# yum -y install httpd后,服务脚本会被httpd覆盖,不过此httpd24只支持start| restart | stop
# cd /usr/local/apache24/conf
# cp httpd.conf httpd.conf.bak
# vim httpd.conf
新增代码:ServerName localhost:80
147行LoadModulesession_cookie_module modules/mod_session_cookie.so
148行LoadModulesession_crypto_module modules/mod_session_crypto.so
149行LoadModulesession_dbd_module modules/mod_session_dbd.so
# httpd -t
(6)配置httpd开机自启:
# vim /etc/init.d/httpd24,在首行#!/bin/sh下,新增如下2行代码:
# chkconfig: 2345 95 60
# description: Apache HTTP Server 2.4.29
# chkconfig --add httpd24
# chkconfig httpd24 on
# chkconfig --list httpd24
5、启动httpd服务,并在浏览器中输入192.168.1.144:
# service httpd24 start
# ss -tnl | grep :80
# ps -ef | grep httpd
# httpd -M | grep mpm --> mpm_event_module (shared)
四、安装配置二进制格式的mariadb-10.2.9:
1、虚机添加新硬盘,创建LVM逻辑卷,并创建用于存放mariadb数据的目录:
(1)虚机新硬盘添加完成后,需要重启服务器,新硬盘才能被识别
(2)创建LVM分区:
# partx -a /dev/sdb --> 忽略错误提示
# cat /proc/partitions
(3)创建pv:# pvcreate /dev/sdb1 # pvs
(4)创建vg:# vgcreate MysqLvg /dev/sdb1# vgs
(5)创建lv:# lvcreate -l +100%FREE MysqLvg -n MysqLdata # lvs
(6)格式化LVM分区:# mke2fs -t ext4 /dev/MysqLvg/MysqLdata
(7)创建挂载点:# mkdir -pv /data
(8)开机自动挂载:# vim /etc/fstab --> /dev/MysqLvg/MysqLdata /dataext4 defaults 0 0
(9)挂载分区并查看分区信息:# mount -a # mount # df -Th
(10)创建用于存放mariadb数据的目录:# mkdir -pv /data/MysqLdata
(11)创建MysqL用户和组:# groupadd -r MysqL # useradd -r -g MysqL MysqL # id MysqL
(12)设置数据目录权限:# cd /data # chown -R MysqL.MysqL MysqLdata # chmod -R o-rx MysqLdata
2、安装mariadb-10.2.9:
# tar -xf mariadb-10.2.9-linux-x86_64.tar.gz -C /usr/local
# cd /usr/local
# mv mariadb-10.2.9-linux-x86_64/ MysqL
# cd MysqL
3、配置PATH环境变量中:
# vim /etc/profile.d/mariadb.sh --> export PATH=/usr/local/MysqL/bin:$PATH
# . /etc/profile.d/mariadb.sh
# MysqL --version
# ln -sv /usr/local/MysqL/include/MysqL /usr/include/MysqL
(3)导出库文件:# vim /etc/ld.so.conf.d/mariadb.conf--> /usr/local/MysqL/lib # ldconfig
(4)导出# vim /etc/man.config--> MANPATH /usr/local/MysqL/man
# find / -name my.cnf
# mv /etc/my.cnf /etc/my.cnf.bak //如果系统中事先已存在my.cnf,则将其重命名备份
# cp /usr/local/MysqL/support-files/my-innodb-heavy-4G.cnf /etc/my.cnf
备注:MariaDB的配置文件my.cnf是集中式的配置文件,可以为多个程序提供配置,包括client、MysqL、MysqLd等等。查找配置文件的顺序依次为:/etc/my.cnf --> /etc/MysqL/my.cnf --> $BASEDIR/my.cnf--> --defaults-extra-file=/PATH/TO/CONF_FILE --> ~/.my.cnf。如果发现多个my.cnf配置文件,那最后一个被读到的配置文件将会最终生效,即如果存在~/.my.cnf配置文件,那么MariaDB服务器的配置以此文件为准。
# vim/etc/my.cnf
basedir = /usr/local/MysqL
datadir = /data/MysqLdata
user = MysqL
innodb_file_per_table = on
skip_name_resolve = on
注释thread_concurrency = 8
注释innodb_additional_mem_pool_size = 16M
(6)初始化mariadb-server:
# ls /data/MysqLdata //确认数据存放目录必须为空
# /usr/local/MysqL/scripts/MysqL_install_db --user=MysqL --basedir=/usr/local/MysqL --datadir=/data/MysqLdata --defaults-file=/etc/my.cnf --skip-name-resolve
# ls /data/MysqLdata
(7)使用mariadb-server,并设置开机自启:
# cp /usr/local/MysqL/support-files/MysqL.server /etc/init.d/MysqLd
# vim /etc/init.d/MysqLd,修改如下2行内容:
# chkconfig --add MysqLd
# chkconfig MysqLd on
# chkconfig --list MysqLd
4、启动# service MysqLd start
# ss -tnl | grep :3306
# ps aux | grep MysqLd
5、mariadb-server安全设置:# MysqL_secure_installation
备注:用于设置root用户密码、删除匿名用户、是否允许root用户远程登录、删除测试数据库、重载授权表
6、允许远程主机访问mariadb-server:
出于安全考虑,mariadb-server默认是不允许远程主机直接访问的
# MysqL -uroot -p //输入root用户密码
MariaDB [(none)]> grant all privileges on *.* to 'root'@'%' identified by'123456' with grant option;
MariaDB [(none)]> flush privileges;
五、安装配置PHP-5.6.32:
1、编译安装PHP-5.6.32:
# find / -name PHP.ini
# mv /etc/PHP.ini /etc/PHP.ini.bak //如果系统中事先已存在PHP.ini,则将其重命名备份
# tar -xf PHP-5.6.32.tar.xz -C /usr/src
# cd /usr/src/PHP-5.6.32
# ./configure --prefix=/usr/local/PHP56 --with-apxs2=/usr/local/apache24/bin/apxs --with-config-file-path=/etc --with-config-file-scan-dir=/etc/PHP56.d --with-MysqL=MysqLnd --with-MysqL-sock=/tmp/MysqL.sock --with-MysqLi=MysqLnd --with-pdo-MysqL=MysqLnd --with-libxml-dir --with-openssl --with-kerberos --with-pcre-regex --with-pcre-dir --with-zlib --with-zlib-dir --with-bz2 --with-curl --with-ldap --with-ldap-sasl --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-gettext --with-mhash --with-mcrypt --with-snmp --with-readline --with-recode --with-tidy --with-xmlrpc --with-iconv-dir --with-pear --enable-sysvmsg --enable-sysvsem --enable-sysvshm --disable-ipv6 --enable-MysqLnd --enable-fpm --enable-PHPdbg --enable-bcmath --enable-calendar --enable-exif --enable-ftp --enable-gd-native-ttf --enable-intl --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-wddx --enable-zip --enable-zend-signals
2、配置(1)创建并修改PHP.ini配置文件:# cp /usr/src/PHP-5.6.32/PHP.ini-production /etc/PHP.ini
(2)导出二进制程序目录至# vim /etc/profile.d/PHP56.sh --> export PATH=/usr/local/PHP56/sbin:/usr/local/PHP56/bin:$PATH
# . /etc/profile.d/PHP56.sh
# echo $PATH
六、配置httpd-2.4.29支持PHP-5.6.32(以下两种方法二选一):
1、把PHP编译成httpd的模块:
(1)修改主配置文件# vim /usr/local/apache24/conf/httpd.conf
DirectoryIndex index.PHPindex.html
# service httpd24 restart
# httpd -M | grep PHP --> PHP5_module (shared)
(2)创建测试页:
# vim /usr/local/apache24/htdocs/index.PHP
<?PHP
$conn=MysqLi_connect("127.0.0.1","root","123456");
if ($conn)
@H_280_1502@echo "Success";else
@H_280_1502@echo "Failure";MysqLi_close();
PHPinfo();
?>
备注:如果在新版本PHP中使用旧版本PHP的MysqL_connect()函数连接MariaDB,会提示undefined function MysqL_connect()。从PHP 5.5开始,MariaDB就不推荐使用MysqL_connect()函数,属于废弃函数,PHP 7中已经彻底不支持了,其增加了MysqLi的函数功能。从某种意义上讲,MysqLi是MysqL系统函数的增强版,更稳定、更高效、更安全,属于面向对象,用对象的方式操作驱动MariaDB数据库。
浏览器中输入192.168.1.144/index.PHP:
未加载XCache相关模块
2、通过fpm(FastCGI)方式,将PHP作为独立的服务运行:
# cd /usr/local/PHP56/etc
# cp PHP-fpm.conf.default PHP-fpm.conf
# vim PHP-fpm.conf,修改如下参数的值(参考):
pm.max_children= 1024
pm.start_servers= 16
pm.min_spare_servers= 5
pm.max_spare_servers= 20
(2)为PHP-fpm提供SysV init脚本,并将其添加至服务列表:
# cp /usr/src/PHP-5.6.32/sapi/fpm/init.d.PHP-fpm /etc/init.d/PHP-fpm
# chmod +x /etc/init.d/PHP-fpm
# chkconfig --add PHP-fpm
# chkconfig PHP-fpm on
# chkconfig --list PHP-fpm
(3)启动PHP-fpm:
# service PHP-fpm start
# ss -tnlp | grep PHP-fpm //默认情况下,fpm监听127.0.0.1的9000端口
# ps aux | grep PHP-fpm
(4)修改主配置文件AddType application/x-httpd-PHP .PHP
装载proxy模块:LoadModule proxy_module modules/mod_proxy.so
装载proxy_fcgi模块:LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
配置反向代理(也可在虚拟主机中配置),在末尾新增如下2行代码:
ProxyRequestsOff //关闭正向代理
ProxyPassMatch^/(.*\.PHP)$ fcgi://127.0.0.1:9000/usr/local/apache24/htdocs/$1
未加载# service MysqLd stop --> Failure,表示PHP与mariadb-server通信正常
七、安装配置PHPMyAdmin:
PHPMyAdmin是一个以PHP为基础,以Web-Base方式架构在网站主机上的MariaDB的数据库管理工具,让管理员可用WebGUI接口管理MariaDB数据库。
# unzip -q PHPMyAdmin-4.7.5-all-languages.zip
# cp -a PHPMyAdmin-4.7.5-all-languages/ /usr/local/apache24/htdocs
# cd /usr/local/apache24/htdocs
# mv PHPMyAdmin-4.7.5-all-languages/ pma
# cd pma
# cp config.sample.inc.PHP config.inc.PHP
# openssl rand -base64 8 --> uRYO7/xG88k=
# vim config.inc.PHP --> $cfg['blowfish_secret'] = 'uRYO7/xG88k=';
# service httpd24 restart
浏览器中输入192.168.1.144/pma:
八、使用XCache优化性能:
XCache是一个又快又稳定的PHP Opcode缓存器,它能够提高PHP的性能。通过把编译PHP后的数据缓冲到共享内存,从而避免重复的编译过程,能够直接使用缓冲区已编译的代码从而提高速度,降低服务器负载。
1、未加载XCache模块时测试性能:# ab -c 20 -n 1000 http://192.168.1.144/pma/index.PHP
2、编译安装XCache:
# tar -xf xcache-3.2.0.tar.gz -C /usr/src
# cd /usr/src/xcache-3.2.0
# PHPize //出现configure脚本
# ./configure --enable-xcache --with-PHP-config=/usr/local/PHP56/bin/PHP-config
3、整合PHP和# mkdir -pv /etc/PHP56.d
# cp /usr/src/xcache-3.2.0/xcache.ini /etc/PHP56.d
# vim /etc/PHP56.d/xcache.ini
若把PHP编译成httpd的模块,则:# service httpd24 restart
刷新页面,已加载XCache相关模块:
性能提升了近5倍