本章我们来介绍下源码安装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/ [iyunv@localhost ~]# tar -zxvf mysql-5.5.15-linux2.6-i686.tar.gz -C /usr/local/ 我们为了方便访问MySQL目录 为这个目录做一个软连接 [iyunv@localhost ~]# cd /usr/local/ [iyunv@localhost local]# ln -s mysql-5.5.15-linux2.6-i686/ mysql
[iyunv@localhost local]# cd mysql [iyunv@localhost mysql]# less INSTALL-BINARY 在这个文件里面有配置的步骤
下面我们照着上图进行配置 [iyunv@localhost ~]# groupadd -r mysql 新建一个系统组 [iyunv@localhost ~]# useradd -r -g mysql mysql -M 新建一个系统帐号 [iyunv@localhost ~]# cd /usr/local/mysql [iyunv@localhost mysql]# chown -R mysql . 改变当前目录所有者的为mysql [iyunv@localhost mysql]# chgrp -R mysql . 改变当前目录所属组为mysql [iyunv@localhost mysql]# scripts/mysql_install_db --user=mysql 执行初始化脚本 [iyunv@localhost mysql]# chown -R root . 将当前目录所有者改回root [iyunv@localhost mysql]# chown -R mysql data data的目录所有者依然为mysql [iyunv@localhost mysql]# cp support-files/my-medium.cnf /etc/my.cnf
拷贝MySQL配置文件到/etc/my.cnf [iyunv@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
生成MySQL服务的控制文件到 /etc/init.d/mysqld [iyunv@localhost mysql]# chmod a+x /etc/init.d/mysqld 给mysqld加上可执行的权限 [iyunv@localhost mysql]# chkconfig --add mysqld 加入开机启动 [iyunv@localhost mysql]# chkconfig --list |grep mysql 查看一下,已经加入开机启动 mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
下面我们启动MySQL [iyunv@localhost mysql]# service mysqld start
[iyunv@localhost bin]# vim /etc/profile 编辑下环境变量文件,更改mysql的搜索路径
[iyunv@localhost bin]# . /etc/profile 执行下profile [iyunv@localhost bin]# mysql
[iyunv@localhost ~]# mysqladmin -u root -p password '123' 设置mysql的帐号密码 头文件库文件导入到环境里面 让系统的应用可以访问到
库文件 [iyunv@localhost mysql]# vim /etc/ld.so.conf.d/mysql.conf
[iyunv@localhost mysql]# ldconfig 刷新缓存文件
上图可以看到缓存已经更新过了
头文件 [iyunv@localhost mysql]# ln -s include /usr/include/mysql
接下来我们配置Nginx [iyunv@localhost ~]# mkdir /mnt/cdrom/ [iyunv@localhost ~]# mount /dev/cdrom /mnt/cdrom/ 光盘挂载到/mnt/cdrom/ [iyunv@localhost ~]# cd /mnt/cdrom/Server/ 切换到当前目录 [iyunv@localhost Server]# rpm -ivh pcre-devel-6.6-2.el5_1.7.i386.rpm 安装pcre
源码安装libevent [iyunv@localhost ~]# tar -zxvf libevent-2.0.16-stable.tar.gz -C /usr/local/src/ [iyunv@localhost ~]# cd /usr/local/src/libevent-2.0.16-stable/ [iyunv@localhost libevent-2.0.16-stable]# ./configure --prefix=/usr/local/libevnet [iyunv@localhost libevent-2.0.16-stable]# make [iyunv@localhost libevent-2.0.16-stable]# make install [iyunv@localhost libevent-2.0.16-stable]# cd /usr/local/libevnet/
把库文件和头文件做相应的输出 库文件 [iyunv@localhost libevnet]# vim /etc/ld.so.conf.d/libevnet.conf
头文件 [iyunv@localhost libevnet]# ln -s /usr/local/libevnet/include/ /usr/include/libevnet 将头文件软连接到/usr/include/libevnet 命名为libevnet 这样系统就会找到头文件
源码安装nginx [iyunv@localhost ~]# tar -zxvf nginx-1.0.11.tar.gz -C /usr/local/src/ 解压到/usr/local/src/ [iyunv@localhost nginx-1.0.11]# cd /usr/local/src/nginx-1.0.11/ [iyunv@localhost nginx-1.0.11]# groupadd -r nginx 创建nginx组 [iyunv@localhost 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
[iyunv@localhost nginx-1.0.11]# make 接下来执行make [iyunv@localhost nginx-1.0.11]# make install 完成以可以在下列两个目录里面看到存放的文件 [iyunv@localhost nginx]# ll /etc/nginx/ [iyunv@localhost nginx]# ll /usr/local/nginx/ [iyunv@localhost sbin]# cd /usr/local/nginx/sbin/ -rwxr-xr-x 1 root root 3911134 Apr 26 12:43 nginx 我们直接执行这个文件就可启动
现在我们来编写一个控制脚本方便我们的使用 [iyunv@localhost ~]# vim /etc/init.d/nginx
脚本在文章的最后会给出的 [iyunv@localhost ~]# chmod a+x /etc/init.d/nginx 给脚本可执行权限 [iyunv@localhost ~]# chkconfig --add nginx 更新nginx 系统服务 [iyunv@localhost ~]# chkconfig --list |grep nginx nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off 查看启动级别,这里可以看到刚刚编写的脚本生效了 下面我们启动一下 发现报错了 没有/var/tmp/nginx/client/ 我们手工创建一个
[iyunv@localhost ~]# mkdir -pv /var/tmp/nginx/client 下面再试一下 [iyunv@localhost ~]# service nginx start starting nginx......ok [iyunv@localhost ~]# service nginx stop okoping nginx........ ok
Nginx 我们已经搭建好了 下面我们来测试下 在浏览器打开http://192.168.1.100
接下来源码安装PHP [iyunv@localhost ~]# tar -jxvf php-5.4.13.tar.bz2 -C /usr/local/src/ [iyunv@localhost 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 [iyunv@localhost php-5.4.13]# make [iyunv@localhost php-5.4.13]# make install [iyunv@localhost php-5.4.13]# mkdir /etc/php /etc/php5.d [iyunv@localhost php-5.4.13]# cp php.ini-production /etc/php [iyunv@localhost php-5.4.13]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm [iyunv@localhost php-5.4.13]# chmod a+x /etc/init.d/php-fpm [iyunv@localhost php-5.4.13]# cd /usr/local/php/etc/ [iyunv@localhost etc]# cp php-fpm.conf.default php-fpm.conf [iyunv@localhost etc]# chkconfig --add php-fpm 加入开机启动 [iyunv@localhost etc]# chkconfig --list |grep fpm php-fpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off 已经加入开机启动 [iyunv@localhost etc]# service php-fpm start Starting php-fpm done 启动成功 [iyunv@localhost 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相关的请求 [iyunv@localhost etc]# vim /etc/nginx/nginx.conf nginx的配置文件
[iyunv@localhost etc]# cd /usr/local/nginx/html/ 切换到nginx的 [iyunv@localhost html]# mv index.html index.php [iyunv@localhost html]# vim index.php
我们重启一下服务测试PHP和nginx的结合 [iyunv@localhost html]# service nginx restart [iyunv@localhost html]# service php-fpm restart
用浏览器打开http://192.168.1.100 可以看到nginx和PHP的结合已经做好了
接下来测试nginx和mysql的结合 [iyunv@localhost html]# vim index.php
用浏览器打开http://192.168.1.100 可以看到nginx和mysql的结合已经做好了
[iyunv@localhost html]# service mysqld stop 停止下mysql 看下出现的情况
至此我们的LNMP已经做好了
为了提高PHP的加速我们可以使用xcache加速 先介绍下xcache XCache 是一个又快又稳定的 PHP opcode 缓存器.这意味着他能够提高您服务器上的 PHP 性能. 他通过把编译 PHP 后的数据缓冲到共享内存从而避免重复的编译过程, 能够直接使用缓冲区已编译的代码从而提高速度. 通常能够提高您的页面生成速率 2 到5 倍, 降低服务器负载. [iyunv@localhost ~]# tar -zxvf xcache-3.0.1.tar.gz -C /usr/local/src/ 解压 [iyunv@localhost xcache-3.0.1]# cd /usr/local/src/xcache-3.0.1/ 切换到当期目录 在这里我们并未看到configure 这里我们需要提一下,我们在安装PHP的时候有一个phpize我们运行一下就会生成configure [iyunv@localhost xcache-3.0.1]# /usr/local/php/bin/phpize [iyunv@localhost xcache-3.0.1]# ls
[iyunv@localhost xcache-3.0.1]# ./configure \ > --enable-xcache > --with-php-config=/usr/local/php/bin/php-config [iyunv@localhost xcache-3.0.1]# make [iyunv@localhost xcache-3.0.1]# make install Xcache和PHP的结合是一个模块,这个模块在 /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/ 目录下
[iyunv@localhost xcache-3.0.1]# cp xcache.ini /etc/php5.d/ xcache和PHP的结合 /etc/php5.d/ 是额外功能的目录 我们把xcache.ini 拷贝到这个目录 [iyunv@localhost xcache-3.0.1]# vim /etc/php5.d/xcache.ini 编辑这个文件
更改一下测试的网页 [iyunv@localhost xcache-3.0.1]# cd /usr/local/nginx/html/ [iyunv@localhost html]# vim index.php
[iyunv@localhost xcache-3.0.1]# service php-fpm restart 重启一下fastcgi 浏览器访问http://192.168.1.100 可以看到已经测试成功 xcache和PHP已经完美结合了
附上脚本 #!/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
|