源码搭建LNMP及xcache加速

源码搭建LNMP及xcache加速

    LNMPLinux系统下Nginx+MySQL+PHP这种网站服务器架构。

Nginx是一个小巧而高效的Linux下的Web服务器软件,与Apache相比,消耗资源更少,支持的并发连接,更高的效率,反向代理功能效率高、静态文件处理快等,但动态页面处理能力不如Apache等老牌软件成熟。单独使用Nginx处理大量动态页面时容易产生频繁的502错误。

 

环境:

Centos6.4 64位操作系统

libevent-2.0.16-stable.tar.gz

mysql-5.6.15-linux-glibc2.5-x86_64.tar.gz

nginx-1.0.11.tar.gz

php-5.5.8.tar.bz2

xcache-3.1.0.tar.gz

安装前准备

 

关闭selinux和防火墙

安装开发工具

Development tools  

Additional Development

Server Platform Development

 

 

安装前环境搭建及准备

[root@localhost src]# yum install pcre-devel    //为了更好的支持正则表达式

安装libevent//这是一个事件库。因为nginx采用的是epoll机制,为了让事件触发机制更有效,与nginx配合使用。

[root@localhost ~]# tar -zxvf /libevent- 2.0.16-stable.tar.gz  -C /usr/local/src  

[root@localhost ~]# cd /usr/local/src/libevent-2.0.16-stable/

[root@localhost libevent-2.0.16-stable]# ./configure --prefix=/usr/local/libeven t            

[root@localhost ~]# cd /usr/local/libevent/lib/        //这是他的库文件所在,我们要把它输出出来

[root@localhost ~]# echo "/usr/local/libevent/lib/" >>/etc/ld.so.conf.d/libe ven t.conf

[root@localhost ~]# ldconfig    //刷新一下缓存

[root@localhost ~]# ldconfig -pv |grep libevent         //我们可以看到他已经生效

libevent_pthreads-2.0.so.5 (libc6,x86-64) => /usr/local/libevent/lib/libevent_pthreads-2.0.so.5

libevent_openssl-2.0.so.5 (libc6,x86-64) => /usr/local/libevent/lib/libevent_openssl-2.0.so.5

libevent_extra-2.0.so.5 (libc6,x86-64) => /usr/local/libevent/lib/libevent_extra-2.0.so.5

 

 

LNMP搭建:

安装nginx

[root@localhost ~]# tar -zxvf nginx-1.0.11.tar.gz -C /usr/local/src 

[root@localhost ~]# cd /usr/local/src/nginx-1.0.11/          

[root@localhost nginx-1.0.11]#  ./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 \

> --lock-path=/var/lock/nginx.lock \

> --user=nginx \

> --group=nginx \

> --with-http_ssl_module \

> --with-http_flv_module \

> --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/ \

> --with-pcre               //执行config

[root@localhost nginx-1.0.11]# make && make install    

 

配置关于nginx的环境变量

[root@localhost ~]# cd /usr/local/nginx/

[root@localhost nginx]# ll

total 8

drwxr-xr-x. 2 root root 4096 Apr 10 06:22 html

drwxr-xr-x. 2 root root 4096 Apr 10 06:22 sbin

 

[root@localhost nginx]# vim /etc/profile

wKiom1ORdUzg5tf5AACCvjSTOU8504.jpg

[root@localhost nginx]# . /etc/profile

[root@localhost nginx]# nginx -t      //启动nginx,进行语法检查

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: [emerg] mkdir() "/var/tmp/nginx/client/" failed (2: No such file or directory)

nginx: configuration file /etc/nginx/nginx.conf test failed

发现有语法错误,解决办法

[root@localhost nginx]# mkdir -pv /var/tmp/nginx/client/

 

再次进行语法检查,正确

[root@localhost nginx]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

 

[root@localhost nginx]# nginx     //启动nginx

[root@localhost nginx]# netstat -tupln |grep  80      

tcp       0         0 0.0.0.0:80        0.0.0.0:*          LISTEN      10298/nginx 

//nginx已经启动起来了,并且只有nginx,没有apache

 

wKiom1ORdW3ymPgXAACy6z8q_CM587.jpg

Nginx可以成功访问

 

接下来配置nginx的配置文件

[root@localhost ~]# cd /etc/nginx/

[root@localhost nginx]# ll

。。。。。。。。。。。

-rw-r--r--. 1 root root 2685 Apr 10 06:22 nginx.conf

。。。。。。。。。。。

 

wKioL1ORdU-w5YaSAACiVXldjVw844.jpg

我们需要给nginx编写一个控制脚本

[root@localhost ~]# touch /etc/init.d/nginx

[root@localhost ~]# chmod a+x /etc/init.d/nginx 

#!/bin/bash

# chkconfig: 2345 88 44

# descriptions: the nginx web server

prog=/usr/local/nginx/sbin/nginx

lockfile=/var/lock/nginx.lock

start () {

        if [ -e $lockfile ];then

           echo "the nginx server is startd"

        else

        echo -n "the nginx is starting..."

        sleep 1

        $prog && echo "OK" && touch $lockfile ||echo "fail"

        fi

 

}

stop () {

        if [ ! -e $lockfile ];then

            echo "the nginx server is stoped"

        else

        echo -n "the nginx server is stopd..."

        sleep 1

        $prog -s stop && echo "OK" && rm -rf $lockfile || echo "fail"

        fi

 

}

 

 

case "$1" in

start)

        start

        ;;

stop)

        stop

        ;;

restart)

        stop

        start

        ;;

configtest)

        $prog  -t

        ;;

*)

   echo "USAGE:start|stop|restart|configtest"

        ;;

esac

 

[root@localhost ~]# service nginx restart

the nginx server is stoping...OK

the nginx is starting...OK

[root@localhost ~]# netstat -tupln |grep 80

tcp     0    0 0.0.0.0:80      0.0.0.0:*     LISTEN      1654/nginx 

[root@localhost ~]# chkconfig  --add nginx

[root@localhost ~]# chkconfig  nginx on

nginx配置完成。   

 

 

安装mysql

 

在安装顺序上,因为我们要把php与mysql结合,在安装php时我们需要指定mysql的位置,所以我们要先安装mysql

安装前我们先把系统自带的mysql***,否则安装会出现问题

[root@localhost src]# yum remove mysql

解压:

[root@localhost ~]# tar -zxvf mysql-5.6.15-linux-glibc2.5-x86_64.tar.gz -C /usr/local/     //这是个绿色软件包,所以我们只需解压在/usr/local/目录下即可

 

[root@localhost local]# ln -s mysql-5.6.15-linux-glibc2.5-x86_64/  mysql     //创建一个软连接,因为它名字太长了,不方便后期操作

 

[root@localhost local]# cd mysql

[root@localhost mysql]# vim INSTALL-BINARY     //这是安装mysql的介绍,我们按着执行

 

 

76    To install and use a MySQL binary distribution, the basic command

  77    sequence looks like this:

  78 shell> groupadd mysql

  79 shell> useradd -r -g mysql mysql

  80 shell> cd /usr/local

  81 shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz

  82 shell> ln -s full-path-to-mysql-VERSION-OS mysql

  83 shell> cd mysql

  84 shell> chown -R mysql .

  85 shell> chgrp -R mysql .

  86 shell> scripts/mysql_install_db --user=mysql

  87 shell> chown -R root .

  88 shell> chown -R mysql data

  89 shell> bin/mysqld_safe --user=mysql &

  90 # Next command is optional

  91 shell> cp support-files/mysql.server /etc/init.d/mysql.server

 

[root@localhost mysql]# groupadd mysql

[root@localhost mysql]# useradd -r -g mysql mysql

[root@localhost mysql]# chown -R mysql .

[root@localhost mysql]# chgrp -R mysql .

[root@localhost mysql]# scripts/mysql_install_db --user=mysql    //在执行这一步时出错,解决办法安装libaio-devel这个库文件   yum install libaio-devel

Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

 

[root@localhost mysql]# chown -R root .     //修改所有文件的拥有者为root

[root@localhost mysql]# chown -R mysql data   //修改data文件的拥有者为mysql

[root@localhost mysql]# cp my.cnf /etc/       // mysql server  配置文件

[root@localhost mysql]# cp support-files/mysql.server  /etc/init.d/mysqld

   //产生mysql server 控制文件

[root@localhost mysql]# chmod a+x /etc/init.d/mysqld

[root@localhost mysql]# service mysqld start     //启动mysql

Starting MySQL SUCCESS! 

[root@localhost mysql]# chkconfig --add mysqld   

[root@localhost mysql]# chkconfig mysqld on         //设置为开机自启动

[root@localhost mysql]# vim /etc/profile

wKiom1ORdZWRAd21AABrJPdugso389.jpg

 PATH=$PATH:/usr/local/mysql/bin           //添加一条命令修改环境变量注意这个环境变量添加在export之前!

 

[root@localhost mysql]# . /etc/profile

[root@localhost mysql]#echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf   

  //mysql库文件的输出

[root@localhost mysql]# ldconfig    //重新加载配置文件

[root@localhost mysql]# echo "MANPATH /usr/local/mysql/man">>/etc/man.conf   

//配置手册 

 

启动MySQL

[root@localhost ~]# mysql

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql> 

 

[root@localhost mysql]# mysqladmin -u root -p password '123'   //mysql数据库设置口令

 

测试成功,mysql安装完毕。

 

 

安装php

先查询系统中有没有安装php,如果有的话一定要先卸载了

[root@localhost ~]# rpm -qa |grep php

解压

[root@localhost ~]# tar -jxvf php-5.5.8.tar.bz2 -C /usr/local/src/     

[root@localhost ~]# cd /usr/local/src/php-5.5.8/

[root@localhost php-5.5.8]# ./configure    \

--prefix=/usr/local/php \                

--enable-fpm   \                 //开启fastcgi

--enable-sockets  \

--with-mysql=/usr/local/mysql \        //关联mysql数据库

--with-mysqli=/usr/local/mysql/bin/mysql_config  \    //mysqlimysql的高级功能

--enable-mbstring  \

--enable-xml  \

--with-png-dir \

--with-jpeg-dir  \

--with-zlib  \

--with-freetype-dir \

--with-config-file-path=/etc/php \

--with-config-file-scan-dir=/etc/php5.d

 

[root@localhost php-5.5.8]# make && make install

 

 

[root@localhost php-5.5.8]# mkdir /etc/php   /etc/php5.d

[root@localhost php-5.5.8]# cp php.ini-development  /etc/php/php.ini

[root@localhost php-5.5.8]# cd sapi/    

[root@localhost sapi]# cd fpm/

[root@localhost fpm]# cp init.d.php-fpm /etc/init.d/php-fpm      //php-fpm的控制文件

[root@localhost fpm]# chmod a+x /etc/init.d/php-fpm 

 

[root@localhost fpm]# cd /usr/local/php/etc/

[root@localhost etc]# ll

total 20

-rw-r--r--. 1 root root  1152 Apr 10 18:02 pear.conf

-rw-r--r--. 1 root root 15212 Apr 10 18:02 php-fpm.conf.default     //php-fpm的配置文件

[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf      //给它重命名为php-fpm

 

[root@localhost php-5.5.8]# service php-fpm start

Starting php-fpm  done

[root@localhost etc]# netstat -tupln |grep php

tcp   0  0 127.0.0.1:9000     0.0.0.0:*       LISTEN      35953/php-fpm 

 

[root@localhost php-5.5.8]# chkconfig --add php-fpm     //加入开机启动项

[root@localhost php-5.5.8]# chkconfig --list |grep fpm

php-fpm        0:off1:off2:on3:on4:on5:on6:off

php-fpm安装成功。

 

接下来我们就要把nginxphp以及xcache进行结合。

 

修改nginx的配置文件

[root@localhost ~]# vim /etc/nginx/nginx.conf

 wKioL1ORdYSC9Qs9AAFtaWYb-nY029.jpg

[root@localhost ~]# service nginx restart    //重启nginx

 

接下来我们可以测试一下nginxphp的结合

 

[root@localhost ~]# vim /usr/local/nginx/html/index.php     //添加一个网页

wKiom1ORdbHjTHcQAAAf6VmBXXE173.jpg

wKioL1ORdkCDOfXPAALVMQe1PsQ829.jpg


 

测试phpmysql的连接性

 

[root@localhost html]# cd /usr/local/nginx/html/

[root@localhost html]# vim index.php 

wKiom1ORdpSwn1wxAABtOiW2yqY811.jpg

wKioL1ORdmqjyan-AAChgSDb78g562.jpg


连通性测性成功

 

 

 

LNMP环境已经构搭建完成。

 

我们通过xcache来对web服务器进行加速。

 

php是一种解释性语言,每次访问网站时都要重新对php解释一次,太浪费时间。所以我们通过xcache来做对php网页一个缓存,对其进行加速。这样当再次访问时就不用再进行解释从而加快网站的访问速度。

 

安装xcache

[root@localhost ~]# cd /usr/local/src

[root@localhost ~]# tar -zxvf xcache-3.1.0.tar.gz -C /usr/local/src/xcache-3.1.0/

[root@localhost xcache-3.1.0]# vim INSTALL 

wKioL1ORdoKT5HxrAAC914bUlrw849.jpg

Phpize---------------》这是xcache源码中没有configure文件,我们要通过这个phpize来生成一个这样的文件。这个phpize是由php提供的,在/usr/local/php/bin/phpize目录下

[root@localhost xcache-3.1.0]# /usr/local/php/bin/phpize 

[root@localhost xcache-3.1.0]# ll     

:::::::::::::::

-rwxr-xr-x. 1 root root 463336 Apr 11 01:28 configure          //configure文件已经产生

:::::::::::::::

 

[root@localhost xcache-3.1.0]# ./configure  --enable-xcache  --with-php-config=/usr/local/php/bin/php-config

::::::::::::::

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/

 

最后一行说明了xcache的加速模块的目录

[root@localhost xcache-3.1.0]# cd /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/

[root@localhost no-debug-non-zts-20121212]# ll

total 2196

-rwxr-xr-x. 1 root root 1016532 Apr 10 23:15 opcache.a

-rwxr-xr-x. 1 root root  532648 Apr 10 23:15 opcache.so

-rwxr-xr-x. 1 root root  690332 Apr 11 01:32 xcache.so     //xcache的加速模块

 

把这个模块拷贝到/etc/php5.d/ 目录下

[root@localhost no-debug-non-zts-20121212]# cp xcache.so /etc/php5.d/     

[root@localhost no-debug-non-zts-20121212]# cd /usr/local/src/xcache-3.1.0/

[root@localhost xcache-3.1.0]# vim INSTALL  

wKiom1ORdtbQYQFZAABAtc_swVw543.jpg

 

[root@localhost xcache-3.1.0]# cp xcache.ini /etc/php5.d/

[root@localhost xcache-3.1.0]# vim /etc/php5.d/xcache.ini     //这个就是xcache的配置文件了


用浏览器访问192.168.3.100

wKioL1ORds2zDUhMAAH452RPwV8740.jpg

 

我们可以通过web界面来管理xcache

 

[root@localhost xcache-3.1.0]# ll htdocs/          //这个就是xcacheweb界面管理的文件

[root@localhost xcache-3.1.0]# cp -r htdocs  /usr/local/nginx/html/admin

[root@localhost ~]# vim /etc/php5.d/xcache.ini 

wKioL1ORdxSguCGFAAEsvPKCeGs213.jpg

生成MD5密文的两种方法

wKiom1ORd1LRat0_AADj2sBPyHA371.jpg

 

 

我们得把nginx打开的默认页面设为index.php,否则打不开xcache的管理页面[root@localhost admin]# vim /etc/nginx/nginx.conf

 

wKiom1ORd2HiVcotAABG63nylBM822.jpg

[root@localhost ~]# service php-fpm restart

[root@localhost admin]# service nginx restart

 

wKioL1ORd0TwDJkJAAJAxJYq8hw607.jpg


你可能感兴趣的:(fastcgi,网站服务器,xcache,lnmp源码安装)