oiuy 发表于 2014-5-28 08:39:26

源码包搭建LNMP+Memcached平台

                      此实验在一台服务器上完成,IP为192.168.100.1

一.用源码包搭建LNMP网站运行平台
1.安装依赖包软件,停止rpm包apache和mysql服务
# yum -y groupinstall "Development libraries" "Development tools" "X Software Development"
# yum -y install gcc gcc-c++ make
# yum -y install openssl openssl-devel pcre pcre-devel
# service httpd stop;chkconfig httpd off
# service mysqld stop;chkconfig mysqld off

2.安装Nginx
# useradd -M -s /sbin/nologin nginx
# tar -zxvf nginx-1.2.0.tar.gz
# cd nginx-1.2.0
# ./configure \
--prefix=/usr/local/nginx \
--pid-path=/usr/local/nginx/nginx.pid \
--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=/usr/local/nginx/client \
--http-proxy-temp-path=/usr/local/nginx/proxy \
--http-fastcgi-temp-path=/usr/local/nginx/fcgi \
--http-uwsgi-temp-path=/usr/local/nginx/uwsgi \
--http-scgi-temp-path=/usr/local/nginx/scgi \
--with-pcre

# make
# make install

3.启动nginx 服务
# /usr/local/nginx/sbin/nginx-t
# /usr/local/nginx/sbin/nginx
# netstat -tunalp | grep :80
tcp 0 0 0.0.0.0:800.0.0.0:*LISTEN32428/nginx

4.在本机测试是否能访问
elinks --dump http://localhost

5.安装mysql(选用5.5.13版本的源码包安装,此版本需要cmake编译工具)

5.1安装cmake 编译工具
# tar -zxvf cmake-2.8.10.2.tar.gz
# cd cmake-2.8.10.2
# ./bootstrap --prefix=/usr/local/cmake
# make
# makeinstall
# /usr/local/cmake/bin/cmake --version
ncmake version 2.8.10.2

5.2安装源码包mysql
# mv /etc/my.cnf /etc/my.cnf.old
# useradd -M -s /sbin/nlogin mysql
# tar -zxvf mysql-5.5.13.tar.gz
# cd mysql-5.5.13
# /usr/local/cmake/bin/cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc -DMYSQL_DATADIR=/usr/local/mysql/data \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_UNIX_ADDR=/tmp/mysqld.sock \
-DMYSQL_USER=mysql -DEXTRA_CHARSETS=all \
-DWITH_READLINE=1 -DWITH_SSL=system \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1

# make
# make install

5.3初始化mysql授权库
# chown -R mysql:mysql /usr/local/mysql/
# cd /usr/local/mysql/
# ./scripts/mysql_install_db --user=mysql   //初始化mysql授权库         
# ls /usr/local/mysql/data/mysql             //产生授权库,则初始化成功

5.4创建主配置文件
# cd mysql-5.5.13
# cp support-files/my-medium.cnf /etc/my.cnf   

5.5添加为系统服务并启动源码数据库服务
# cd mysql-5.5.13
# cp support-files/mysql.server /etc/init.d/mysqld
# chmod +x /etc/init.d/mysqld
# chkconfig --add mysqld
# service mysqld start

5.6把源码mysql命令所在的路径添加到系统环境变量PATH里
# PATH=/usr/local/mysql/bin/:$PATH
# vim /etc/bashrc
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin/
export PATH

5.7设置数据库管理员从本机登录的密码
# mysqladmin -uroot password "123456"   

5.8测试用数据库管理员root在本机登录
# mysql -uroot -p123456
# mysql>

5.9指定mysql库文件的位置   
# vim /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/mysql/lib/             //添加库文件的位置
# ldconfig -v                   //加载库文件

6.安装源码php
6.1安装php的扩展软件(mhash libiconv libmcrypt libltdl)
# tar -zxvf mhash-0.9.9.9.tar.gz   //哈希函数库
# cd mhash-0.9.9.9
# ./configure
# make
# make install

# tar -zxvf libiconv-1.13.tar.gz      //处理中文各种编码之间的转换
# cd libiconv-1.13
# ./configure
# make
# make install

# tar -zxvf libmcrypt-2.5.8.tar.gz   //提供加密功能的库文件
# cd libmcrypt-2.5.8
# ./configure
# make
# make install
# cd libltdl
# ./configure --with-gmetad --enable-gexec --enable-ltdl-install
# make
# make install


6.2指定扩展包库文件的位置
# ln -sv /usr/local/lib/libmcrypt* /usr/lib/
# ln -sv /usr/local/lib/libmhash.* /usr/lib/
# ldconfig -v

6.3安装源码包php
# tar -zxvf php-5.4.9.tar.gz
# cd php-5.4.9
# ./configure \
--prefix=/usr/local/php5nginx \
--with-config-file-path=/usr/local/php5nginx/etc \
--with-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-iconv-dir=/usr/local \
--with-freetype-dir --with-jpeg-dir \
--with-png-dir --with-zlib --with-libxml-dir=/usr \
--enable-xml --disable-rpath --enable-bcmath \
--enable-shmop --enable-sysvsem \
--enable-inline-optimization --with-curl --with-curlwrappers
--enable-mbregex --enable-fpm --enable-mbstring
--with-mcrypt --with-gd --enable-gd-native-ttf \
--with-openssl --with-mhash --enable-pcntl \
--enable-sockets --with-ldap --with-ldap-sasl \
--with-xmlrpc --enable-zip --enable-soap \

# make ZEND_EXTRA_LIBS='-liconv'
# make install

6.4创建php的主配置文件php.ini
# cd php-5.4.9
# cp php.ini-production /usr/local/php5nginx/etc/php.ini


二.整合Nginx和Fast-cgi               
1.配置Fast-cgi
# cd /usr/local/php5nginx/etc
# cp php-fpm.conf.default php-fpm.conf   //生成配置文件
# vim php-fpm.conf                  //根据需求编辑配置文件
……
listen = 127.0.0.1:9000      //默认监听9000端口
pm = dynamic
pm.max_children = 5      
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
……

2.启动php-fpm服务
# cd php-5.4.9/sapi/fpm/
# cp init.d.php-fpm /etc/init.d/php-fpm      //创建启动脚本
# chmod +x /etc/init.d/php-fpm
# chkconfig --add php-fpm
# service php-fpm start
# netstat -utnlap | grep :9000

3.编辑nginx.conf文件
# vim /usr/local/nginx/conf/nginx.conf
……
    location / {
    root html;
    index index.php index.html index.htm;
    }
    location ~ \.php$ {
    root html;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    include fastcgi_params;
    }
……

4.编辑fastcgi_params文件
# vim /usr/local/nginx/conf/fastcgi_params
……
fastcgi_param SCRIPT_FILENAME$document_root$fastcgi_script_name;
……

5.重启nginx服务
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s stop
# /usr/local/nginx/sbin/nginx

6.测试nginx能否识别php网页
# vim /usr/local/nginx/html/test.php   //制作测试网页
<?php
    phpinfo( );
?>

浏览器访问 http://192.168.100.1/test.php      


7.测试php能否连接mysql数据库服务
# cd /usr/local/php5nginx/bin/
./php -m | grep --color mysql

# vim /usr/local/nginx/html/linkdb.php      //制作测试网页
<?php
    $link=mysql_connect("192.168.100.1","root","123456");
    if($link){
      echo "db link ok";
    }else{
       echo "db link no";
    }
?>

客户端访问 http://192.168.100.1/linkdb.php


三.LNMP+Memcached
1.在当前服务器上搭建memcached服务
1.1安装事件库程序
# tar -zxvf libevent-2.0.21-stable.tar.gz       //事件库
# cd libevent-2.0.21-stable
# ./configure
# make
# make install

1.2指定库文件位置
# echo /usr/local/lib/ > /etc/ld.so.conf.d/libevent.conf
# ldconfig -v

1.3安装memcached
# tar -zxvf memcached-1.4.5.tar.gz
# cd memcached-1.4.5
# ./configure
# make
# make install

1.4启动memcached服务
# /usr/local/bin/memcached -l 192.168.100.1 -u root -m 200 -c 200 -n 10 -f 2 -d -vvv
# netstat -tulnp | grep :11211


2.安装php连接memcached服务器的工具
2.1安装memcache
# tar -zxvf memcache-2.2.5.tgz
# cdmemcache-2.2.5
# /usr/local/php5nginx/bin/phpize
# ./configure --with-php-config=/usr/local/php5nginx/bin/php-config --enable-memcache
# make
# make   install
Installing shared extensions:
/usr/local/php5nginx/lib/php/extensions/no-debug-non-zts-20100525/         //提示模块存放目录


2.2编辑php程序的配置文件,指定模块的位置
# vim /usr/local/php5nginx/etc/php.ini
……
extension_dir = "/usr/local/php5nginx/lib/php/extensions/no-debug-non-zts-20100525/"
extension = memcache.so                      //加载模块         
……
2.3重启php-fpm
# service php-fpm restart

3.查看php是否支持memcached
# /usr/local/php5nginx/bin/php -m | grep --color memcache

4.测试php能从memcahed里存取数据。
# vim /usr/local/nginx/html/mem.php         //制作测试网页
<?php
    $memcache=new Memcache;                        //创建memcache对象
    $memcache->connect('192.168.100.1',11211) or die ('could not connect!!'); //连接    memcached服务器
    $memcache->set('key','jin');                      //定义变量
    $get_values=$memcache->get('key');               //获取变量值
    echo $get_values;
?>

客户端访问 http://192.168.100.1/mem.php   在网页上显示jin则表示测试成功

5.Nginx整合Memcached
当nginx服务器接收到访问.php文件时,先访问memcached服务器,在memcached服务器里没找到时,在到网站服务器上去找。

# vim /usr/local/nginx/conf/nginx.conf
Server {
server_name www.jinjianjun.com;                        //nginx服务器主机名
    location / {
......
    set $memcached_key $uri;                  //用uri路径定义变量
    memcached_pass 127.0.0.1:11211;         //把请求发给memcached服务器
    default_type text/html;
    error_page 404 @fallback;               //请求跳转标记
    }
    location @fallback {
    proxy_pass http://servergroup;       //服务器组名,若转给某台nginx服务器,如本机也可写成proxy_pass http://192.168.100.1:80
    }
}

6.重启nginx服务
# ./sbin/nginx-t
# ./sbin/nginx-sstop
# ./sbin/nginx

                   

页: [1]
查看完整版本: 源码包搭建LNMP+Memcached平台