基於LNMP實現動靜分離,PHP+Memcached實現會話保持

基於LNMP實現動靜分離,PHP+Memcached實現會話保持php




1、Nginx+PHP+Mysql+Memcachehtml

  拓撲圖:mysql

wKiom1dEcA6zokR-AADHTwi1enM801.png


環境搭建:linux

Nginx代理:172.18.123.10    nginx-1.8.0nginx

Memcached:172.18.123.50    memcached-1.4.15git

Nginx web:172.18.123.20    nginx-1.8.0github

PHP:      172.18.123.21    php-5.4.26  xcache-3.2.0  php擴展memcache-3.0.8web

Mysql:    172.18.123.100   mariadb-10.1.8sql


2、編譯安裝數據庫

 

1.Nginx代理服務器:172.18.123.10 編譯安裝Nginx

1)安裝開發環境及解決依賴關係

[root@localhost ~]# yum groupinstall -y "Development Tools" "Development Libraries"

[root@localhost ~]# yum install -y pcre-devel opensll-devel

2)安裝

添加nginx用戶及組,管理nginx服務進程

[root@localhost ~]# groupadd -r nginx

[root@localhost ~]# useradd -r -g nginx nginx

下載軟件包

[root@localhost ~]# wget http://nginx.org/download/nginx-1.8.0.tar.gz

[root@localhost ~]# tar xf nginx-1.8.0.tar.gz

[root@localhost ~]# cd nginx-1.8.0/

[root@localhost nginx-1.8.0]# ./configure \

  --prefix=/usr/local/nginx \

  --sbin-path=/usr/local/nginx/sbin/nginx \

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

  --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \

  --http-scgi-temp-path=/var/tmp/nginx/scgi \

  --with-pcre

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

配置nginx.conf

wKiom1dEcUzyARr-AAAoaCkpqWw278.png

設置環境變量

[root@localhost ~]# vim /etc/profile.d/nginx.sh 添加以下行

export PATH=$PATH:/usr/local/nginx/sbin/

[root@localhost ~]# source /etc/profile.d/nginx.sh         

啓動服務,查看端口是否啓用

2.Memcached服務器:172.18.123.50

1下載依賴包libevent,編譯安裝。

[root@localhost~]#wget   https://cloud.github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz

[root@localhost ~]# tar xf  libevent-2.0.21-stable.tar.gz

[root@localhost ~]# cd libevent-2.0.21-stable/

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

[root@localhost libevent-2.0.21-stable]# make &&make install

2)下載memcached

[root@localhost ~]# tar xf memcached-1.4.15.tar.gz

[root@localhost ~]# cd memcached-1.4.15/

[root@localhostmemcached-1.4.15]#./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent

[root@localhost memcached-1.4.15]# make && make install

3)啓動memcached

[root@localhost memcached-1.4.15]# /usr/local/memcached/bin/memcached -d -u root -P /tmp/memcached.pid

查看端口

3.Nginx web服務器上,編譯安裝nginx,修改配置文件,啓動Nginx

wKiom1dEcZ3y-5uhAAAI3EOCvtk220.png

1)建立主頁目錄,設置主頁目錄

mkdir /www/web  -pv

echo 「<h1> web server  172.18.123.20  </h1> > /www/web/index.html 

wKioL1dEcrGxg97GAABM69GpZz8440.png

4.PHP:172.18.123.21 編譯安裝

# yum groupinstall "Development Tools" "Server Platform Development"

# yum install -y libmcrypt-devel libxml2-devel bzip2-devel openssl-devel

# yum install -y php-mysql

1)下載 php-5.4.26.tar.bz2 源碼包

[root@localhost ~]# tar xf php-5.4.26.tar.bz2

[root@localhost ~]# cd php-5.4.26/

[root@localhostphp-5.4.26]# ./configure --prefix=/usr/local/php --with-config-file-path=/etc --with-config-scan-dir=/etc/php.d --with-libxml-dir=/usr --with-openssl --with-zlib --with-bz2 --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-fpm --enable-mbstring --with-mcrypt --enable-sockets --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd

[root@localhost ~]# make && make install

2)php配置文件

#cp php.ini.production /etc/php.ini

爲php-fpm配置一個啓動腳本

#cp sapi/fpm/init.d.php-pfm /etc/rc.d/init.d/php-fpm

加入到系統服務

#chkconfig  --add php-fpm

#chkconfig  php-fpm on

#chkconfig  --list php-fpm

配置php-fpm

#cd /usr/local/php/etc

#cp php-fpm.conf.default php-fpm.conf

更改配置文件

#vim php-fpm.conf

wKioL1dEdnDh-7zLAAAOvMmh_WU842.png

wKiom1dEdaHDSKb0AABGgmqGIUU679.png

定位到listen,ip地址更改成別人能夠訪問的地址

wKioL1dEd-iRSTCdAAAca_XvVfI883.png

啓動服務,查看相應端口

wKioL1dEdv7xTuFNAAAQlixkbTk354.png

創建一個目錄做爲虛擬主機的目錄:

# mkdir -pv /www/web

3)安裝xcache加速php

下載xcache-3.2.0.tar.bz2源碼包

[root@localhost ~]# tar xf xcache-3.2.0.tar.bz2

[root@localhost ~]# cd xcache-3.2.0/

[root@localhost ~]# phpize

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

[root@localhost ~]# make && make install

安裝完成會有以下信息:

wKiom1dEdi_RXrqJAAAQpBZI66E714.png

配置php

[root@localhost ~]#vim /etc/php.ini

找到extension開頭的行,修改成以下行:

extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xcache.so

重啓php-fpm,

wKioL1dEd53w8dyCAABISn0z-ag934.png

4)安裝php-memcached擴展,

下載memcache-3.0.8.tgz,編譯安裝

[root@localhost ~]# tar xf memcache-3.0.8.tgz

[root@localhost ~]# cd memcache-3.0.8/

[root@localhost memcache-3.0.8]# phpize123

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

[root@localhost memcache-3.0.8]# make && make install

一樣會生成如下信息

wKiom1dEdtHRQFIcAAAQpBZI66E287.png

配置php

[root@localhost ~]#vim /etc/php.ini

找到extension開頭的行,修改成以下行:

extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so

定位到seession.save添加下列兩行:

session.save_handler = memcache

session.save_path = "tcp://172.18.123.50:11211?persisent=1&weight=1&timeout=1&retry_interval=15"

5)編寫測試腳本測試:

[root@localhost ~]# vim /www/web/test.php 

#www.magedu.com

<?php

$mem = new Memcache;

$mem->connect("172.18.123.50", 11211)  or die("Could not connect");

 

$version = $mem->getVersion();

echo "Server's version: ".$version."<br/>\n";

 

$mem->set('hellokey', 'Hello World', 0, 600) or die("Failed to save data at the memcached server");

echo "Store data in the cache (data will expire in 600 seconds)<br/>\n";

 

$get_result = $mem->get('hellokey');

echo "$get_result is from memcached server.";

?>

wKioL1dEeEmwTz4zAABCvCgzVgI825.png

下載memadmin-1.0.12.tar.gz,解壓到/www/web/目錄下,複製一樣一份到Nginx web服務器/www/web/目錄下

[root@localhost ~]# useradd -r www

wKiom1dEd4izEaHoAABo4yxEaHU657.png

5.Mysql:172.18.123.100,二進制包安裝

1)安裝

下載mariadb-5.5.46-linux-x86_64.tar.gz

[root@MYSQL ~]# groupadd -r -g 306 mysql

[root@MYSQL ~]# useradd -r -g mysql mysql

[root@MYSQL ~]# mkdir /mydata/data -pv

[root@MYSQL ~]# chown -R mysql.mysql /mydata/data

[root@MYSQL ~]# tar xf mariadb-5.5.46-linux-x86_64.tar.gz -C /usr/local/

[root@MYSQL ~]# cd /usr/local

[root@MYSQL local]# ln -sv mariadb-5.5.46-linux-x86_64 mysql

[root@MYSQL local]# cd /mysql

[root@MYSQL mysql]# chown -R root:mysql ./*

[root@MYSQL mysql]# mkdir -pv /etc/mysql

[root@MYSQL mysql]# cp support-files/my-large.cnf /etc/mysql/my.cnf

[root@MYSQL mysql]# vim /etc/my.cnf

wKioL1dEeKOBWJ6PAAANkyRkRns209.png

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

[root@MYSQL mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data

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

[root@MYSQL mysql]# service mysqld start

Starting MySQL... SUCCESS!

2)受權用戶

wKioL1dEeLqwxfvFAABW7CVvDLw594.png

3)測試數據

wKiom1dEd_GQkNO4AABFofm6wqA630.png

6.部署應用

在PHP主機上

下載phpMyAdmin-4.0.5-all-languages.zip

解壓到/www/web/目錄下,一樣的文件複製一份到Nginx web服務器/www/web下

修改配置文件

# cp config.sample.inc.php config.inc.php

# vim config.inc.php 

wKiom1dEeBfS7HK7AAAxaaDITn4730.png

wKioL1dEeR7RJVdpAADi05cZono986.png

同理部署phpwind

wKioL1dEeUvBNdsKAAB5xdGFCqs534.png

相關文章
相關標籤/搜索