ubuntu 主机分离lamp搭建

 

服务器配置

网络拓扑

clip_image002

原理

需要4台主机,一个公网ip 10.5.0.226

Web服务器处理php代码,picture server处理静态文件的访问。上传图片先由web服务器处理,再通过nfs从web服务器存入picture server。

用户访问代理服务器test.com。代理服务器根据请求文件的后缀判断,并将动态文件请求转发给web服务器,静态文件请求转发给picture server  
   
由于nginx处理静态文件更有优势,所以静态picture服务器使用的是nginx服务器。    
测试代码,提供单页无刷新的图片上传,展示和删除测试。

IP配置

本机操作

1.修改配置文件

vi /etc/network/interfaces

clip_image003

2.重启网卡

ifdown eth0

ifup eth0

3查看网络配置信息

ifconfig

clip_image004

4.检查是否可以联网

ping www.baidu.com

安装web服务器

主机ip:10.5.0.223

Apache

1.安装apache

apt-get install apache2

输入ip:10.5.0.223测试

clip_image006

PHP

1.安装php

apt-get install php5

apt-get install libapache2-mod-php5

2.输入测试代码phpinfo();

vi /var/www/html/test.php

3.测试

http://10.5.0.223/test.php

clip_image008

Mysql客户端

apt-get install mysql-client

安装mysql服务器

主机ip:10.5.0.224

1.安装

apt-get install mysql-server

2.检查安装是否成功

mysql �Cu root -p

3.开启外部访问权限

grant all privileges on *.* to root@"%" identified by "root";

flush privileges

4. 允许外联:

vi /etc/mysql/my.cnf

注释bind-address=127.0.0.1这行

5.重启mysql

service mysql restart

让apache、php支持 mysql

主机ip:10.5.0.223

apt-get install libapache2-mod-auth-mysql

apt-get install php5-mysql

测试:

service apache2 restart

clip_image010

连接mysql服务器:

vi /var/www/html/test.php

<?php

$link = mysql_connect('10.5.0.224','root','root');

var_dump($link);

clip_image011

安装phpmyadmin

主机ip:10.5.0.223

安装

apt-get install phpmyadmin

ln -s /usr/share/phpmyadmin /var/www/html/

配置phpmyadmin连接的mysql主机地址

$cfg['Servers'][$i]['host'] = 'localhost';改为$cfg['Servers'][$i]['host'] = '10.5.0.224';

连接登陆

http://10.5.0.223/phpmyadmin/index.php

安装图片服务器

主机ip:10.5.0.225

1.安装nginx

apt-get install nginx

查看10.5.0.225

clip_image013

3.添加需要共享的文件夹

mkdir /usr/share/nginx/html/image

4.修改该文件夹权限

配置文件夹所属用户为任意普通用户

chown wangrui.wangrui image/

5.安装NFS

apt-get install nfs-kernel-server

6.配置共享文件夹

vi /etc/exports

在末尾加入

/usr/share/nginx/html/image 10.5.0.203(rw,sync,anonuid=1000,anongid=1000)

10.5.0.223用户登入时,用户权限和uid为1000的权限一样,该用户为拥有/usr/share/nginx/html/image读写权限的普通用户

7.重启nfs

service nfs-kernel-server restart

8.查看共享文件夹

showmount -e

安装nfs客户端

主机ip:10.5.0.223

apt-get install nfs-common

1.添加需要挂载的文件夹

mkdir /var/www/html/image

chmod 777 /var/www/html/image

2.挂载到图片服务器的共享文件夹

mount -t nfs 10.5.0.225:/usr/share/nginx/html/image /var/www/html/image/

3.测试:

在10.5.0.223 /var/www/html/image增删文件,实际是操作10.5.0.225 /usr/share/nginx/html/image

上传图片到

10.5.0.225 的/usr/share/nginx/html/image目录

可以通过http://10.5.0.223/image/1.jpg访问到

安装haproxy

主机ip:10.5.0.226

1.安装

apt-get install haproxy

2.配置

vi /etc/haproxy/haproxy.cfg

#---------------------------------------------------------------------

# main frontend which proxys to the backends

#---------------------------------------------------------------------

frontend proxy *:80 #前端代理

acl url_static path_end -i .jpg .gif .png

acl dynamic_content path_end -i .php .html .css .js

use_backend static if url_static

default_backend dynamic

#---------------------------------------------------------------------

# static backend for serving up images, stylesheets and such

#---------------------------------------------------------------------

backend static #后端静态服务器

server web1 10.5.0.225:80 inter 3000 rise 2 fall 3 check maxconn 5000

#---------------------------------------------------------------------

# round robin balancing between the various backends

#---------------------------------------------------------------------

backend dynamic #后端动态服务器

server web2 10.5.0.223:80 inter 3000 rise 2 fall 3 check maxconn 5000

综合测试

1.创建数据库

create database if not exists test charset utf8;

use test;

create table if not exists test_image(

id int primary key auto_increment,

name varchar(32) not null comment 'name of image',

path varchar(100) not null comment 'path of image',

create_time int not null comment 'file create time'

)charset utf8;

2.上传代码到web服务器10.5.0.223

clip_image015

Config.php为数据库配置文件DB_HOST地址为mysql服务器地址

clip_image017

3.配置 Host文件

10.5.0.226 test.com

4访问测试

访问地址test.com/test.html

所有响应来自test.com

clip_image019

数据库:

clip_image021

图片服务器:10.5.0.225

clip_image023

如果关闭nginx不能访问到图片,说明请求图片文件时,haproxy是请求的nginx

你可能感兴趣的:(ubuntu 主机分离lamp搭建)