Ubuntu下Nginx做负载实现高性能WEB服务器
Start:
nginx can be used as a reverse proxy to load balance HTTP requests among back-end servers.
(Pardon!!!You do not know english!!!You are kidding!!!)
OK,Go on.
1.Network topology
photograph 1.1 Network architecture 1
photograph 1.2 Network architecture 2
两种架构最主要区别在数据库方面,架构2把DB server 独立出来,具有很好的可扩展性,但是代价较高;架构1的实现较为简单,代价较小,且已经可以满足相当高的需求了,此处为了以后做HA、双机热备、mysql主从复制等,采用架构1来实现,对于架构2后续也会做实现。
相关阅读:
2.Environment
2.1 Key points
External network switch 外网交换机
Internal network switch 内网交换机
Proxy server 代理服务器
Web server 1 Web服务器1
Web server 2 Web服务器2
2.2 Explanations
网络拓扑图中虚的外网网线表示可有可无,建议部署环境时保留,待稳定运行后撤下。另因为使用了VPN,故此处外网地址为192.168.80.X 内网地址为192.168.1.X。
Proxy server :
Name:ubuntu
OS: ubuntu10.10 x86
NIC1: eth0 with public IP 192.168.80.8
NIC2: eth1 with internal IP 192.168.1.8
Runs nginx listening on 192.168.80.8:80
Web server 1:
Name:ubuntu2
OS: ubuntu10.10 x86
NIC1: eth0 with public IP 192.168.80.9(will be disabled after this deployment)
NIC2: eth1 with internal IP 192.168.1.9
Runs any http server ( Eg: apache2 ) listening on 192.168.1.9:80
Runs mysql server listening on 192.168.1.9:3306
Web server 2:
Name:ubuntu3
OS: ubuntu10.10 x86
NIC1: eth0 with public IP 192.168.80.10 ( will be disabled after this deployment )
NIC2: eth1 with internal IP 192.168.1.10
Runs any http server ( Eg: apache2 ) listening on 192.168.1.10:80
Runs mysql server listening on 192.168.1.10:3306
3.Configuration
3.1 Install nginx on the Proxy server :
You can use any way to install nginx. Here we will use the most simple method to install nginx.( Can view http://nginx.org/en/download.html )
Configure network address
edit /etc/network/interfaces like
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.80.8
netmask 255.255.255.0
network 192.168.80.0
broadcast 192.168.80.255
gateway 192.168.80.1
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 210.22.70.3
auto eth1
iface eth1 inet static
address 192.168.1.8
netmask 255.255.255.0
Or run the following commands:
root@ubuntu:~# ifconfig eth0 192.168.80.8 netmask 255.255.255.0 up
root@ubuntu:~# ifconfig eth1 192.168.1.8 netmask 255.255.255.0 up
root@ubuntu:~# route add default gw 192.168.80.1
root@ubuntu:~# sed -i '1 i\nameserver 210.22.70.3' /etc/resolv.conf
root@ubuntu:~# sed -i '1 i\nameserver 210.22.84.3' /etc/resolv.conf
Install nginx. For ubuntu 10.10:
root@ubuntu:~# wget http://nginx.org/keys/nginx_signing.key
root@ubuntu:~# apt-key add nginx_signing.key
root@ubuntu:~# vi /etc/apt/sources.list
Append the following to the end of the /etc/apt/sources.list file
deb http://nginx.org/packages/ubuntu/ lucid nginx
deb-src http://nginx.org/packages/ubuntu/ lucid nginx
Then run the following commands:
root@ubuntu:~# apt-get update
root@ubuntu:~# apt-get install nginx
Installation directory
root@ubuntu:~# find / -name nginx
/usr/sbin/nginx
/usr/share/doc/nginx
/usr/share/nginx
/var/log/nginx
/var/cache/nginx
/var/lib/update-rc.d/nginx
/etc/nginx
/etc/logrotate.d/nginx
/etc/init.d/nginx
/etc/default/nginx
root@ubuntu:~#
Install System-V style init script links(add system startup item)
root@ubuntu:~# update-rc.d nginx defaults
3.2 Install apache2 on the Web server 1 and 2:(You must run the same commands on ubuntu1 and ubuntu2.)
You can use any way to install LAMP. Here we will use the most simple method to install LAMP.( hahaha...)
Configure network address
Web 1 eth0:192.168.80.9 eth1:192.168.1.9
Web 2 eth0:192.168.80.10 eth1:192.168.1.10
DNS:210.22.70.3/210.22.84.3
Install LAMP.For ubuntu 10.10 run the following commands.(You must run the same commands on the Web server 1 and 2.)
root@ubuntu:~# apt-get update
root@ubuntu:~# apt-get install apache2 mysql-server mysql-client php5 php5-gd php5-mysql libapache2-mod-php5 phpmyadmin
Install System-V style init script links(add system startup item) (so…)
root@ubuntu:~# update-rc.d apache2 defaults
root@ubuntu:~# update-rc.d mysql defaults