本章我们来介绍下源码安装LNMP 和 PHP的缓存加速xcache3.0

LNMP代表的就是: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/

[[email protected] ~]# tar -zxvf mysql-5.5.15-linux2.6-i686.tar.gz -C /usr/local/

我们为了方便访问MySQL目录 为这个目录做一个软连接

[[email protected] ~]# cd /usr/local/

[[email protected] local]# ln -s mysql-5.5.15-linux2.6-i686/ mysql

源码安装LNMP+PHP缓存加速xcache3.0

[[email protected] local]# cd mysql

[[email protected] mysql]# less INSTALL-BINARY    在这个文件里面有配置的步骤

源码安装LNMP+PHP缓存加速xcache3.0

下面我们照着上图进行配置

[[email protected] ~]# groupadd -r mysql 新建一个系统组

[[email protected] ~]# useradd -r -g mysql mysql -M 新建一个系统帐号

[[email protected] ~]# cd /usr/local/mysql

[[email protected] mysql]# chown -R mysql .      改变当前目录所有者的为mysql

[[email protected] mysql]# chgrp -R mysql .       改变当前目录所属组为mysql

[[email protected] mysql]# scripts/mysql_install_db --user=mysql     执行初始化脚本

[[email protected] mysql]# chown -R root .        将当前目录所有者改回root

[[email protected] mysql]# chown -R mysql data      data的目录所有者依然为mysql

[[email protected] mysql]# cp  support-files/my-medium.cnf  /etc/my.cnf

 

拷贝MySQL配置文件到/etc/my.cnf

[[email protected] mysql]# cp support-files/mysql.server   /etc/init.d/mysqld

 

生成MySQL服务的控制文件到 /etc/init.d/mysqld

[[email protected] mysql]# chmod a+x /etc/init.d/mysqld   给mysqld加上可执行的权限

[[email protected] mysql]# chkconfig --add mysqld    加入开机启动

[[email protected] mysql]# chkconfig --list |grep mysql   查看一下,已经加入开机启动

mysqld          0:off 1:off 2:on 3:on 4:on 5:on 6:off

 

下面我们启动MySQL

[[email protected] mysql]# service mysqld start

源码安装LNMP+PHP缓存加速xcache3.0

[[email protected] bin]# vim /etc/profile    编辑下环境变量文件,更改mysql的搜索路径

源码安装LNMP+PHP缓存加速xcache3.0

[[email protected] bin]# .  /etc/profile    执行下profile

[[email protected] bin]# mysql

源码安装LNMP+PHP缓存加速xcache3.0

[[email protected] ~]# mysqladmin -u root -p password '123'    设置mysql的帐号密码

头文件库文件导入到环境里面 让系统的应用可以访问到

 

库文件

[[email protected] mysql]# vim /etc/ld.so.conf.d/mysql.conf

源码安装LNMP+PHP缓存加速xcache3.0

[[email protected] mysql]# ldconfig  刷新缓存文件

源码安装LNMP+PHP缓存加速xcache3.0

上图可以看到缓存已经更新过了

 

头文件

[[email protected] mysql]# ln -s include  /usr/include/mysql

 

接下来我们配置Nginx

[[email protected] ~]# mkdir /mnt/cdrom/

[[email protected] ~]# mount /dev/cdrom  /mnt/cdrom/     光盘挂载到/mnt/cdrom/

[[email protected] ~]# cd /mnt/cdrom/Server/   切换到当前目录

[[email protected] Server]# rpm -ivh pcre-devel-6.6-2.el5_1.7.i386.rpm   安装pcre

 

源码安装libevent

[[email protected] ~]# tar -zxvf libevent-2.0.16-stable.tar.gz  -C /usr/local/src/

[[email protected] ~]# cd /usr/local/src/libevent-2.0.16-stable/

[[email protected] libevent-2.0.16-stable]# ./configure --prefix=/usr/local/libevnet

[[email protected] libevent-2.0.16-stable]# make

[[email protected] libevent-2.0.16-stable]# make install

[[email protected] libevent-2.0.16-stable]# cd /usr/local/libevnet/

源码安装LNMP+PHP缓存加速xcache3.0

把库文件和头文件做相应的输出

库文件

[[email protected] libevnet]# vim /etc/ld.so.conf.d/libevnet.conf

源码安装LNMP+PHP缓存加速xcache3.0

头文件

[[email protected] libevnet]# ln -s /usr/local/libevnet/include/  /usr/include/libevnet

将头文件软连接到/usr/include/libevnet   命名为libevnet  这样系统就会找到头文件

 

源码安装nginx

[[email protected] ~]# tar -zxvf nginx-1.0.11.tar.gz -C /usr/local/src/  解压到/usr/local/src/

[[email protected] nginx-1.0.11]# cd /usr/local/src/nginx-1.0.11/

[[email protected] nginx-1.0.11]# groupadd -r nginx  创建nginx组

[[email protected] nginx-1.0.11]# 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

源码安装LNMP+PHP缓存加速xcache3.0

[[email protected] nginx-1.0.11]# make   接下来执行make

[[email protected] nginx-1.0.11]# make install

完成以可以在下列两个目录里面看到存放的文件

[[email protected] nginx]# ll  /etc/nginx/

[[email protected] nginx]# ll  /usr/local/nginx/

[[email protected] sbin]# cd /usr/local/nginx/sbin/

-rwxr-xr-x 1 root root 3911134 Apr 26 12:43 nginx  我们直接执行这个文件就可启动

 

现在我们来编写一个控制脚本方便我们的使用

[[email protected] ~]# vim /etc/init.d/nginx

源码安装LNMP+PHP缓存加速xcache3.0

脚本在文章的最后会给出的

[[email protected] ~]# chmod a+x /etc/init.d/nginx   给脚本可执行权限

[[email protected] ~]# chkconfig --add nginx   更新nginx 系统服务

[[email protected] ~]# chkconfig --list |grep nginx

nginx           0:off 1:off 2:on 3:on 4:on 5:on 6:off

查看启动级别,这里可以看到刚刚编写的脚本生效了

下面我们启动一下 发现报错了 没有/var/tmp/nginx/client/  我们手工创建一个

源码安装LNMP+PHP缓存加速xcache3.0

[[email protected] ~]# mkdir  -pv  /var/tmp/nginx/client

下面再试一下

[[email protected] ~]# service nginx start

starting nginx......ok

[[email protected] ~]# service nginx stop

okoping nginx........ ok

源码安装LNMP+PHP缓存加速xcache3.0

Nginx 我们已经搭建好了 下面我们来测试下 在浏览器打开http://192.168.1.100

源码安装LNMP+PHP缓存加速xcache3.0

 

 

接下来源码安装PHP

[[email protected] ~]# tar -jxvf php-5.4.13.tar.bz2  -C  /usr/local/src/

[[email protected] php-5.4.13]# ./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

[[email protected] php-5.4.13]# make

[[email protected] php-5.4.13]# make install

[[email protected] php-5.4.13]# mkdir /etc/php    /etc/php5.d

[[email protected] php-5.4.13]# cp  php.ini-production  /etc/php

[[email protected] php-5.4.13]# cp  sapi/fpm/init.d.php-fpm  /etc/init.d/php-fpm

[[email protected] php-5.4.13]# chmod  a+x  /etc/init.d/php-fpm

[[email protected] php-5.4.13]# cd  /usr/local/php/etc/

[[email protected] etc]# cp  php-fpm.conf.default  php-fpm.conf

[[email protected] etc]# chkconfig  --add php-fpm   加入开机启动

[[email protected] etc]# chkconfig --list |grep fpm

php-fpm         0:off 1:off 2:on 3:on 4:on 5:on 6:off    已经加入开机启动

[[email protected] etc]# service php-fpm start

Starting php-fpm  done    启动成功

[[email protected] etc]# netstat -tupln |grep 9000

tcp  0   0 127.0.0.1:9000    0.0.0.0:*   LISTEN  7260/php-fpm.conf   启动成功

当一个用户访问nginx 如果要访问你的是PHP页面的话  nginx会发给-----》fastcgi-----》php

下面我们要更改下nginx的配置文件   让nginx可以处理PHP相关的请求

[[email protected] etc]# vim /etc/nginx/nginx.conf      nginx的配置文件

源码安装LNMP+PHP缓存加速xcache3.0

[[email protected] etc]# cd /usr/local/nginx/html/  切换到nginx的

[[email protected] html]# mv  index.html  index.php

[[email protected] html]# vim index.php

源码安装LNMP+PHP缓存加速xcache3.0

我们重启一下服务测试PHP和nginx的结合

[[email protected] html]# service nginx restart

[[email protected] html]# service php-fpm restart

源码安装LNMP+PHP缓存加速xcache3.0

用浏览器打开http://192.168.1.100

可以看到nginx和PHP的结合已经做好了

源码安装LNMP+PHP缓存加速xcache3.0

 

 

接下来测试nginx和mysql的结合

[[email protected] html]# vim index.php

源码安装LNMP+PHP缓存加速xcache3.0

用浏览器打开http://192.168.1.100

可以看到nginx和mysql的结合已经做好了

源码安装LNMP+PHP缓存加速xcache3.0

[[email protected] html]# service mysqld stop   停止下mysql  看下出现的情况

源码安装LNMP+PHP缓存加速xcache3.0

至此我们的LNMP已经做好了

 

为了提高PHP的加速我们可以使用xcache加速

先介绍下xcache

XCache 是一个又快又稳定的 PHP opcode 缓存器.这意味着他能够提高您服务器上的 PHP 性能. 他通过把编译 PHP 后的数据缓冲到共享内存从而避免重复的编译过程, 能够直接使用缓冲区已编译的代码从而提高速度. 通常能够提高您的页面生成速率 2 到5 倍, 降低服务器负载.

[[email protected] ~]# tar  -zxvf xcache-3.0.1.tar.gz  -C  /usr/local/src/  解压

[[email protected] xcache-3.0.1]# cd /usr/local/src/xcache-3.0.1/   切换到当期目录

在这里我们并未看到configure   这里我们需要提一下,我们在安装PHP的时候有一个phpize我们运行一下就会生成configure

[[email protected] xcache-3.0.1]# /usr/local/php/bin/phpize

[[email protected] xcache-3.0.1]# ls

源码安装LNMP+PHP缓存加速xcache3.0

[[email protected] xcache-3.0.1]# ./configure   \

>  --enable-xcache

>  --with-php-config=/usr/local/php/bin/php-config

[[email protected] xcache-3.0.1]# make

[[email protected] xcache-3.0.1]# make install

Xcache和PHP的结合是一个模块,这个模块在

/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/  目录下

源码安装LNMP+PHP缓存加速xcache3.0

[[email protected] xcache-3.0.1]# cp  xcache.ini   /etc/php5.d/   xcache和PHP的结合

  /etc/php5.d/   是额外功能的目录   我们把xcache.ini  拷贝到这个目录

[[email protected] xcache-3.0.1]# vim /etc/php5.d/xcache.ini  编辑这个文件

源码安装LNMP+PHP缓存加速xcache3.0

源码安装LNMP+PHP缓存加速xcache3.0

更改一下测试的网页

[[email protected] xcache-3.0.1]# cd  /usr/local/nginx/html/

[[email protected] html]# vim index.php

源码安装LNMP+PHP缓存加速xcache3.0

[[email protected] xcache-3.0.1]# service php-fpm restart 重启一下fastcgi

浏览器访问http://192.168.1.100

可以看到已经测试成功   xcache和PHP已经完美结合了

源码安装LNMP+PHP缓存加速xcache3.0

附上脚本

#!/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......"

        sleep  1

     $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........"

        sleep  1

     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

转载于:https://blog.51cto.com/renlaifeng/1194745

相关文章:

  • 2021-09-13
  • 2021-12-15
  • 2021-08-23
  • 2022-01-10
  • 2021-09-27
猜你喜欢
  • 2022-02-24
  • 2021-08-22
  • 2021-11-16
  • 2021-12-18
  • 2022-12-23
  • 2021-04-02
  • 2021-09-19
相关资源
相似解决方案