源碼包搭建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:80  0.0.0.0:*  LISTEN  32428/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

# make  install

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

# cd  memcache-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  -s  stop

# ./sbin/nginx 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章