源码安装LNMP+PHP缓存加速xcache3.0
本章我们来介绍下源码安装LNMP 和 PHP的缓存加速xcache3.0LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。
Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
Mysql是一个小型关系型数据库管理系统。
PHP是一种在服务器端执行的嵌入HTML文档的脚本语言。
这四种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统。
下面我们来介绍一下Nginx
Nginx是一个小巧而高效的Linux下的Web服务器软件,是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler 站点开发的,已经在一些俄罗斯的大型网站上运行多年,相当的稳定。
Nginx性能稳定、功能丰富、运维简单、处理静态文件速度快且消耗系统资源极少。
我们这次实验的环境
redhat linux 5.4
Nginx(源码) nginx-1.0.11
MySQL(绿色软件包,二进制)mysql-5.5.15
PHP(fastcgi) php-5.4.13.tar.bz2
PHP缓存加速 xcache-3.0.1.tar.gz
首先安装MySQL
解压MySQL到/usr/local/
# tar -zxvf mysql-5.5.15-linux2.6-i686.tar.gz -C /usr/local/
我们为了方便访问MySQL目录 为这个目录做一个软连接
# cd /usr/local/
# ln -s mysql-5.5.15-linux2.6-i686/ mysql
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903297uYfI.png
# cd mysql
# less INSTALL-BINARY 在这个文件里面有配置的步骤
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903311JIwL.png
下面我们照着上图进行配置
# groupadd -r mysql 新建一个系统组
# useradd -r -g mysql mysql -M 新建一个系统帐号
# cd /usr/local/mysql
# chown -R mysql . 改变当前目录所有者的为mysql
# chgrp -R mysql . 改变当前目录所属组为mysql
# scripts/mysql_install_db --user=mysql 执行初始化脚本
# chown -R root . 将当前目录所有者改回root
# chown -R mysql data data的目录所有者依然为mysql
# cpsupport-files/my-medium.cnf/etc/my.cnf
拷贝MySQL配置文件到/etc/my.cnf
# cp support-files/mysql.server /etc/init.d/mysqld
生成MySQL服务的控制文件到 /etc/init.d/mysqld
# chmod a+x /etc/init.d/mysqld 给mysqld加上可执行的权限
# chkconfig --add mysqld 加入开机启动
# chkconfig --list |grep mysql 查看一下,已经加入开机启动
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
下面我们启动MySQL
# service mysqld start
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903315kYve.png
# vim /etc/profile 编辑下环境变量文件,更改mysql的搜索路径
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903316XJ8j.png
# ./etc/profile 执行下profile
# mysql
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903325kR6D.png
# mysqladmin -u root -p password '123' 设置mysql的帐号密码
头文件库文件导入到环境里面 让系统的应用可以访问到
库文件
# vim /etc/ld.so.conf.d/mysql.conf
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903326E7u9.png
# ldconfig刷新缓存文件
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903330ClhC.png
上图可以看到缓存已经更新过了
头文件
# ln -s include/usr/include/mysql
接下来我们配置Nginx
# mkdir /mnt/cdrom/
# mount /dev/cdrom/mnt/cdrom/ 光盘挂载到/mnt/cdrom/
# cd /mnt/cdrom/Server/ 切换到当前目录
# rpm -ivh pcre-devel-6.6-2.el5_1.7.i386.rpm 安装pcre
源码安装libevent
# tar -zxvf libevent-2.0.16-stable.tar.gz-C /usr/local/src/
# cd /usr/local/src/libevent-2.0.16-stable/
# ./configure --prefix=/usr/local/libevnet
# make
# make install
# cd /usr/local/libevnet/
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_13679033322qRc.png
把库文件和头文件做相应的输出
库文件
# vim /etc/ld.so.conf.d/libevnet.conf
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903332sZYa.png
头文件
# ln -s /usr/local/libevnet/include//usr/include/libevnet
将头文件软连接到/usr/include/libevnet 命名为libevnet这样系统就会找到头文件
源码安装nginx
# tar -zxvf nginx-1.0.11.tar.gz -C /usr/local/src/解压到/usr/local/src/
# cd /usr/local/src/nginx-1.0.11/
# groupadd -r nginx创建nginx组
# useradd -r -g nginx nginx 将nginx帐号加入nginx组
把下列的参数复制进去
./configure \
--conf-path=/etc/nginx/nginx.conf \ 配置文件路径(全称,必须写到下面的具体文件名)
--error-log-path=/var/log/nginx/error.log \ 错误日志存放的位置
--http-log-path=/var/log/nginx/access.log \成功日日志存放的位置
--pid-path=/var/run/nginx/nginx.pid \ 进程ID号保存到nginx.pid文件
--lock-path=/var/lock/nginx.lock \ 锁机制文件是nginx.lock
--user=nginx \ 运行者
--group=nginx \ 运行组
--with-http_ssl_module \ 支持SSL
--with-http_flv_module \ 支持FLV流媒体
--with-http_stub_status_module \ 本地信息输出
--with-http_gzip_static_module \ 压缩
--http-client-body-temp-path=/var/tmp/nginx/client/ \ 临时目录,客户端访问的目录
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \ 反向代理加速的目录
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ 支持fastcgi的目录
--with-pcre
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903335LY3G.png
# make 接下来执行make
# make install
完成以可以在下列两个目录里面看到存放的文件
# ll/etc/nginx/
# ll/usr/local/nginx/
# cd /usr/local/nginx/sbin/
-rwxr-xr-x 1 root root 3911134 Apr 26 12:43 nginx我们直接执行这个文件就可启动
现在我们来编写一个控制脚本方便我们的使用
# vim /etc/init.d/nginx
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_13679033454ezt.png
脚本在文章的最后会给出的
# chmod a+x /etc/init.d/nginx 给脚本可执行权限
# chkconfig --add nginx 更新nginx 系统服务
# chkconfig --list |grep nginx
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
查看启动级别,这里可以看到刚刚编写的脚本生效了
下面我们启动一下 发现报错了 没有/var/tmp/nginx/client/我们手工创建一个
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903348q5Hx.png
# mkdir-pv/var/tmp/nginx/client
下面再试一下
# service nginx start
starting nginx......ok
# service nginx stop
okoping nginx........ ok
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903356XVQ7.png
Nginx 我们已经搭建好了 下面我们来测试下 在浏览器打开http://192.168.1.100
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903364Jj6I.png
接下来源码安装PHP
# tar -jxvf php-5.4.13.tar.bz2-C/usr/local/src/
# ./configure \
> --prefix=/usr/local/php \ 安装到/usr/local/php目录下
> --enable-fpm \ 支持fastcgi
> --enable-sockets\ fastcgi的sockets服务
> --with-mysql=/usr/local/mysql \ mysql的安装目录
> --with-mysqli=/usr/local/mysql/bin/mysql_config\mysql接口文件
> --enable-mbstring\
> --enable-xml\
> --enable-xml\
> --with-png-dir \
> --with-png\
> --with-jpeg-dir\
> --with-zlib\
> --with-freetype-dir \
> --with-config-file-path=/etc/php \
> --with-config-file-scan-dir=/etc/php5.d
# make
# make install
# mkdir /etc/php /etc/php5.d
# cpphp.ini-production/etc/php
# cpsapi/fpm/init.d.php-fpm/etc/init.d/php-fpm
# chmoda+x/etc/init.d/php-fpm
# cd/usr/local/php/etc/
# cpphp-fpm.conf.defaultphp-fpm.conf
# chkconfig--add php-fpm 加入开机启动
# chkconfig --list |grep fpm
php-fpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off 已经加入开机启动
# service php-fpm start
Starting php-fpmdone 启动成功
# netstat -tupln |grep 9000
tcp0 0 127.0.0.1:9000 0.0.0.0:* LISTEN7260/php-fpm.conf 启动成功
当一个用户访问nginx 如果要访问你的是PHP页面的话nginx会发给-----》fastcgi-----》php
下面我们要更改下nginx的配置文件 让nginx可以处理PHP相关的请求
# vim /etc/nginx/nginx.conf nginx的配置文件
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903371j64m.png
# cd /usr/local/nginx/html/切换到nginx的
# mvindex.htmlindex.php
# vim index.php
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903372uaL9.png
我们重启一下服务测试PHP和nginx的结合
# service nginx restart
# service php-fpm restart
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903373lOsU.png
用浏览器打开http://192.168.1.100
可以看到nginx和PHP的结合已经做好了
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903385aa0o.png
接下来测试nginx和mysql的结合
# vim index.php
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903386S5mk.png
用浏览器打开http://192.168.1.100
可以看到nginx和mysql的结合已经做好了
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903390UCFM.png
# service mysqld stop 停止下mysql看下出现的情况
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903395kVlD.png
至此我们的LNMP已经做好了
为了提高PHP的加速我们可以使用xcache加速
先介绍下xcache
XCache 是一个又快又稳定的 PHP opcode 缓存器.这意味着他能够提高您服务器上的 PHP 性能. 他通过把编译 PHP 后的数据缓冲到共享内存从而避免重复的编译过程, 能够直接使用缓冲区已编译的代码从而提高速度. 通常能够提高您的页面生成速率 2 到5 倍, 降低服务器负载.
# tar-zxvf xcache-3.0.1.tar.gz-C/usr/local/src/解压
# cd /usr/local/src/xcache-3.0.1/ 切换到当期目录
在这里我们并未看到configure 这里我们需要提一下,我们在安装PHP的时候有一个phpize我们运行一下就会生成configure
# /usr/local/php/bin/phpize
# ls
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903400tHKB.png
# ./configure \
>--enable-xcache
>--with-php-config=/usr/local/php/bin/php-config
# make
# make install
Xcache和PHP的结合是一个模块,这个模块在
/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/目录下
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903402yhPD.png
# cpxcache.ini /etc/php5.d/ xcache和PHP的结合
/etc/php5.d/ 是额外功能的目录 我们把xcache.ini拷贝到这个目录
# vim /etc/php5.d/xcache.ini编辑这个文件
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_13679034077wuK.png
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903408zikE.png
更改一下测试的网页
# cd/usr/local/nginx/html/
# vim index.php
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903409NDVk.png
# service php-fpm restart 重启一下fastcgi
浏览器访问http://192.168.1.100
可以看到已经测试成功 xcache和PHP已经完美结合了
http://renlaifeng.blog.运维网.com/attachment/201305/7/6491370_1367903418dCDd.png
附上脚本
#!/bin/sh
#set -x
NGINXD='/usr/local/nginx/sbin/nginx'
#description:nginx web server
#chkconfig: 2345 88 60
if [ -f /etc/init.d/functions ];then
. /etc/init.d/functions
fi
start(){
[ -f /var/lock/subsys/nginx ]&& echo "nginx is started" && exit
echo -n"starting nginx......"
sleep1
$NGINXD&& RETVAL=0 ||RETVAL=1
[ $RETVAL -eq 0 ]&& touch /var/lock/subsys/nginx && echo "ok" || echo "fail"
}
stop(){
[ ! -f/var/lock/subsys/nginx ] && echo "nginx is started..." && exit
echo -n "stoping nginx........"
sleep1
killproc nginx&& RETVAL=0 ||RETVAL=1
[ $RETVAL -eq 0 ] && rm -rf /var/lock/subsys/nginx && echo "ok" || echo "fail"
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "start|stop|restart"
;;
Esac
页:
[1]