本次编译安装LAMP环境版本(Apache2.4.23+MySQL5.6.33+php5.6.9+Xcache3.2.0):
Linux:Centos6x-el6.x86_64
Apache:2.4.23
Mysql:5.6.33 (rpm,通用二进制,源码)
PHP:5.6.9+FastCGI
Xcache:3.2.0
安装顺序:Aapche-->Mysql-->PHP(fastcgi)-->Xcache
首先关闭selinux
#vim /etc/sysconfig/selinux
把里边的一行改为
SELINUX=disabled //重启生效
#setenforce 0 临时生效
一、编译安装httpd2.4.23
1、安装 Development tools和 Server Platform Development开发环境。
#yum groupinstall -y Development Tools Server Platform Development
2、安装apr 然后安装apr-util,默认已经安装,可以不用安装
#wget http://mirrors.cnnic.cn/apache/apr/apr-1.5.2.tar.bz2
#tar -jxf apr-1.5.2.tar.bz2
#cd apr-1.5.2
#./configure --prefix=/usr/local/apr
#make
#make install
#wget http://mirrors.cnnic.cn/apache/apr/apr-util-1.5.4.tar.bz2
#tar -jxf apr-util-1.5.4.tar.bz2
#cd apr-util-1.5.4
#./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
#make
#make install
3、编译安装httpd2.4.23
#yum install -y pcre-devel
#yum install openssl-devel
#wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.4.23.tar.bz2
#./configure --prefix=/usr/local/httpd -sysc --enable-so --enable-rewrite --enable-ssl --enable--cgi --enable-cgid --enable-modules=most --enable-mods-shared=most --enable-mpms-shared=all --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr-util/ --with-ssl=/etc/pki/tls
#make
#make install
补充:
(1)构建MPM为静态模块
在全部平台中,MPM都可以构建为静态模块。在构建时选择一种MPM,链接到服务器中。如果要改变MPM,必须重新构建。为了使用指定的MPM,请在执行configure脚本 时,使用参数 --with-mpm=NAME。NAME是指定的MPM名称。编译完成后,可以使用 ./httpd -l 来确定选择的MPM。 此命令会列出编译到服务器程序中的所有模块,包括 MPM。
(2)构建 MPM 为动态模块
在Unix或类似平台中,MPM可以构建为动态模块,与其它动态模块一样在运行时加载。 构建 MPM 为动态模块允许通过修改LoadModule指令内容来改变MPM,而不用重新构建服务器程序。在执行configure脚本时,使用--enable-mpms-shared选项即可启用此特性。当给出的参数为all时,所有此平台支持的MPM模块都会被安装。还可以在参数中给出模块列表。默认MPM,可以自动选择或者在执行configure脚本时通过--with-mpm选项来指定,然后出现在生成的服务器配置文件中。编辑LoadModule指令内容可以选择不同的MPM。
4、配置httpd2.4.23
#vim /etc/httpd/httpd.conf
在ServerRoot "/usr/local/httpd"后面添加一行PidFile "/var/run/httpd.pid"
#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/httpd/bin/apachectl
httpd=${HTTPD-/usr/local/httpd/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 $@
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
exit 1
esac
exit $RETVAL
#chmod +x /etc/init.d/httpd 为此脚本赋予执行权限:
#chkconfig --add httpd //添加服务到开机启动
#chkconfig --level 35 httpd on //设定启动级别
#vim /etc/profile.d/httpd.sh //添加httpd环境变量
添加export PATH=$PATH:/usr/local/httpd/bin
#vim /etc/sysconfig/iptables
添加 -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
#/etc/init.d/iptables restart
5、启用虚拟主机
#vim /etc/httpd/httpd.conf
找到DocumentRoot "/usr/local/httpd/htdocs"
改成#DocumentRoot "/usr/local/httpd/htdocs"
找到#Include /etc/httpd/extra/httpd-vhosts.conf
改成Include /etc/httpd/extra/httpd-vhosts.conf
#/etc/init.d/httpd restart
添加站点,编辑 /etc/httpd/extra/httpd-vhosts.conf文件即可,新建站点格式如下:
<VirtualHost *:80>
ServerAdmin admin@amd5.cn
DocumentRoot "/www/wwwroot/test"
ServerName amd5.cn
ServerAlias www.amd5.cn
ErrorLog "/var/log/httpd/amd5.cn-error_log"
CustomLog "/var/log/httpd/amd5.cn-access_log" combined
</VirtualHost>
说明:重启httpd如果提示如下错误:
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using xxxx. Set the 'ServerName' directive globally to suppress this message
可以vim /etc/httpd/httpd.conf 找到
#ServerName www.example.com:80
更改为:
ServerName localhost:80
6、启用服务器状态(可省略)
mod_status模块可以让管理员查看服务器的执行状态,它通过一个HTML页面展示了当前服务器的统计数据。这些数据通常包括但不限于:
(1) 处于工作状态的worker进程数;
(2) 空闲状态的worker进程数;
(3) 每个worker的状态,包括此worker已经响应的请求数,及由此worker发送的内容的字节数;
(4) 当前服务器总共发送的字节数;
(5) 服务器自上次启动或重启以来至当前的时长;
(6) 平均每秒钟响应的请求数、平均每秒钟发送的字节数、平均每个请求所请求内容的字节数;
启用状态页面的方法很简单,只需要在主配置文件中添加如下内容即可:
<Location /server-status>
SetHandler server-status
Require all granted
</Location>
需要提醒的是,这里的状态信息不应该被所有人随意访问,因此,应该限制仅允许某些特定地址的客户端查看。比如使用Require ip 172.16.0.0/16来限制仅允许指定网段的主机查看此页面。
7、启用openssl
#vim /etc/init.d/httpd restart
找到#LoadModule ssl_module modules/mod_ssl.so
改成LoadModule ssl_module modules/mod_ssl.so
找到#Include /etc/httpd/extra/httpd-ssl.conf
改为Include /etc/httpd/extra/httpd-ssl.conf
#/etc/init.d/httpd restart
编辑/etc/httpd/extra/httpd-ssl.conf可以对openssl进行配置。
openssl详细配置可以参考阿汤博客教程 Centos Apache基于openssl的https服务配置
8、profork、worker、event参数设置
#vim /etc/httpd/httpd.conf
找到#Include /etc/httpd/extra/httpd-mpm.conf
改为Include /etc/httpd/extra/httpd-mpm.conf
编辑/etc/httpd/extra/httpd-mpm.conf可对相应参数做调整
#/etc/init.d/httpd restart
profork、worker、event三种工作模式比较可以参考阿汤博客文章 Apache的mpm prefork、worker、event三种工作模式比较
二、mysql5.6.33通用二进制安装配置:
1、下载解压,并添加mysql系统用户和mysql系统组
#wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz
#tar -zxf mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
#cd /usr/local/
#ln -sv mysql-5.6.33-linux-glibc2.5-x86_64 mysql
#groupadd -r -g 306 mysql
#useradd -g 306 -r -u 306 mysql
#chown -R mysql:mysql /usr/local/mysql/*
2、创建一个逻辑卷存放数据库数据,方便以后扩展,也可以mkdir一个目录,存放。(数据量不大,可以省略此步骤,直接在现有磁盘新建目录存放)
#fdisk /dev/vdb //创建一个20G的分区 分区id 为8e
#partprobe /dev/vdb //让内核识别分区 cat /proc/partitions 查看,如果命令无法识别,重启服务器
#pvcreate /dev/vdb2 //在创建的分区上面创建pv
#vgcreate mysqlvg/dev/vdb2 //创建vg
#lvcreate -n mysqllv -L 20G mysqlvg //创建lv
#mkfs.ext4 /dev/mysqlvg/mysqllv
#mkdir /mysql_data/
#mount /dev/mysqlvg/mysqllv /mysql_data/
#vim /etc/fstab
添加/dev/mapper/mysqlvg-mysqllv /mysql_data ext4 defaults 0 0
3、配置初始化安装mysql
#mkdir /mysql_data/data
#chmod o-rx /mysql_data/data/ //取消其他用户组权限
#chown -R mysql.mysql /mysql_data/data/ //设置数据存放路径 用户和组
#/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/mysql_data/data/ --keep-my-cnf
#chown -R root /usr/local/mysql/*
#cp support-files/mysql.server /etc/init.d/mysqld //为mysql提供sysv服务脚本
#chmod +x /etc/rc.d/init.d/mysqld
#chkconfig --add mysqld //设置开机启动,默认2345级别启动
#cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
#vim /etc/my.cnf
添加下列两行指定安装路径和数据库存放路径
basedir = /usr/local/mysql
datadir = /mysql_data/data
4、环境变量、头文件、库文件、帮助手册设置(可省略)
#vim /etc/profile.d/mysql.sh //设置环境变量
添加export PATH=$PATH:/usr/local/mysql/bin
#vim /etc/man.config //设置帮助手册
添加MANPATH /usr/local/mysql/man
#vim /etc/ld.so.conf.d/mysql.conf //导出库文件
添加/usr/local/mysql/lib
#ldconfig -v //重新建立库文件缓存
#ln -sv /usr/local/mysql/include/ /usr/include/mysql //导出头文件
5、设置mysql密码
#mysql
>set password for 'root'@'localhost' =password('12344321'); //默认密码为空,这里更改为12344321
>quit
三、编译安装php-5.6.9
1、安装依赖库文件,解决依赖关系
#yum install -y libxml2 libxml2-devel bzip2 bzip2-devel
如果想让编译的php支持mcrypt扩展,此处还需要下载如下两个rpm包并安装(如果不需要此扩展,省略 --with-mcrypt即可 ):
#wget ftp://rpmfind.net/linux/dag/redhat/el6/en/x86_64/dag/RPMS/libmcrypt-2.5.7-1.2.el6.rf.x86_64.rpm
#wget ftp://rpmfind.net/linux/dag/redhat/el6/en/x86_64/dag/RPMS/libmcrypt-devel-2.5.7-1.2.el6.rf.x86_64.rpm
#rpm -ivh libmcrypt-2.5.7-1.2.el6.rf.x86_64.rpm libmcrypt-devel-2.5.7-1.2.el6.rf.x86_64.rpm
2、编译安装php5.6.9
#wget http://mirrors.sohu.com/php/php-5.6.9.tar.bz2
#tar jxf php-5.6.9.tar.bz2
#cd php-5.6.9
#./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 --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2
说明:
1、如果使用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
# make
# make intall
3、配置php-fpm
为php-fpm提供Sysv init脚本,并将其添加至服务列表:
# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
# chmod +x /etc/rc.d/init.d/php-fpm
# chkconfig --add php-fpm
# chkconfig php-fpm on
为php-fpm提供配置文件:
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
编辑php-fpm的配置文件:
# vim /usr/local/php/etc/php-fpm.conf
配置fpm的相关选项为你所需要的值,并启用pid文件(如下最后一行):
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
pid = /usr/local/php/var/run/php-fpm.pid
接下来就可以启动php-fpm了:
# service php-fpm start
使用如下命令来验正(如果此命令输出有中几个php-fpm进程就说明启动成功了):
# ps aux | grep php-fpm
默认情况下,fpm监听在127.0.0.1的9000端口,也可以使用如下命令验正其是否已经监听在相应的套接字。
# netstat -tnlp | grep php-fpm
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 689/php-fpm
四、配置httpd支持php+fastcgi
1、启用httpd的相关模块
在Apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,它其实是作为mod_proxy.so模块的扩充,因此,这两个模块都要加载
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
2、配置虚拟主机支持使用fcgi
在相应的虚拟主机中添加类似如下两行。
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/PATH/TO/DOCUMENT_ROOT/$1
例如:
<VirtualHost *:80>
DocumentRoot "/www/wwwroot/test"
ServerName amd5.cn
ServerAlias www.amd5.cn
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/wwwroot/test/$1
<Directory "/www/wwwroot/test">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
ProxyRequests Off:关闭正向代理
ProxyPassMatch:把以.php结尾的文件请求发送到php-fpm进程,php-fpm至少需要知道运行的目录和URI,所以这里直接在fcgi://127.0.0.1:9000后指明了这两个参数,其它的参数的传递已经被mod_proxy_fcgi.so进行了封装,不需要手动指定。
3、编辑apache配置文件httpd.conf,让apache能识别php格式的页面,并支持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
补充:Apache httpd 2.4以前的版本中,要么把PHP作为Apache的模块运行,要么添加一个第三方模块支持PHP-FPM实现。
五、编译安装Xcache-3.2.0
#wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz
#tar -zxf xcache-3.2.0.tar.gz
#cd xcache-3.2.0
#./configure --enable-xcache --with-php-c>
#make
#make install
#mkdir /etc/php.d
#cp xcache.ini /etc/php.d/
#vim /etc/php.d/xcache.ini
找到xcache.count = 改为你当前服务器cpu个数(cat /proc/cpuinfo |grep -c processor查看个数)
#/etc/init.d/httpd restart