nginx+apache实现负载均衡+动静分离配置(编译安装)

一、编译安装nginx

cd /usr/local/src

wget http://nginx.org/download/nginx-1.6.3.tar.gz

tar -zxvf nginx-1.6.3.tar.gz

cd nginx-1.6.3

./configure --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_image_filter_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_degradation_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-pcre

make && make install

二、安装apache的httpd服务器

wget apache.fayea.com/apr/apr-1.5.2.tar.gz

wget apache.fayea.com/apr/apr-util-1.5.4.tar.gz

wget apache.fayea.com/httpd/httpd-2.4.16.tar.gz

#安装apache的时候必须先安装apr,在安装apr-util,最后安装httpd

(1)安装apr

tar -zxvf apr-1.5.2.tar.gz

cd apr-1.5.2

./cponfigure --prefix=/usr/local/apr

make && make install

(2)安装apr-util

tar -zxvf apr-util-1.5.4.tar.gz

cd apr-util-1.5.4

./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/

make && make install

(3)安装httpd

tar -zxvf httpd-2.4.16.tar.gz

cd httpd-2.4.16

./configure --prefix=/usr/local/http --sysconfdir=/etc/httpd --enable-rewrite --enable-so --enable-ssl --enable-cgi --enable-cgid --enable-modules=most --enable-mods-shared=most --enable-mpms-shared=all --with-apr=/usr/local/apr --with-apr-utils=/usr/local/apr-util/

make && make install

#安装完成之后为httpd提供启动脚本

vim /etc/init.d/httpd

#!/bin/bash

#

# httpd                 Startup script for the Apache HTTP Server

#

# chkconfig: - 85 15

# description: Apache is a World Wide Web server.   It is used to serve \

#          HTML files and CGI.

# processname: httpd

# config: /etc/httpd/conf/httpd.conf

# config: /etc/sysconfig/httpd

# pidfile: /var/run/httpd.pid

# Source function library.

. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then

. /etc/sysconfig/httpd

fi

# Start httpd in the C locale by default.

HTTPD_LANG=${HTTPD_LANG-"c"}

# This will prevent initlog from swallowing up a pass-phrase prompt if

# mod_ssl needs a pass-phrase from the user.

INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server

# with the thread-based "worker" MPM; BE WARNED that some modules may not

# work correctly with a thread-based MPM; notably PHP will refuse to start.

# Path to the apachectl script, server binary, and short-form for messages.

apachectl=/usr/local/http/bin/apachectl

httpd=${HTTPD-/usr/local/http/bin/httpd}

prog=httpd

pidfile=${PIDFILE-/var/run/httpd.pid}

lockfile=${LOCKFILE-/var/lock/subsys/httpd}

RETVAL=0

start() {

echo -n $"Starting $prog: "

LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS

RETVAL=$?

echo

[ $RETVAL = 0 ] && touch ${lockfile}

return $RETVAL

}

stop() {

echo -n $"Stopping $prog: "

killproc -p ${pidfile} -d 10 $httpd

RETVAL=$?

echo

[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}

}

reload() {

echo -n $"Reloading $prog: "

if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then

RETVAL=$?

echo $"not reloading due to configuration systax error"

failure $"not reloading $httpd due to configuration syntax error"

else

killproc -p ${pidfile} $httpd -HUP

RETVAL=$?

fi

echo

}

# See how we were called.

case "$1" in

start)

start

;;

stop)

stop

;;

status)

status -p ${pidfile} $httpd

RETVAL=$?

;;

restart)

stop

start

;;

condrestart)

if [ -f ${pidfile} ] ; then

stop

start

fi

;;

reload)

reload

;;

graceful|help|configtest|fullstatus)

$apachectl [email protected]

RETVAL=$?

;;

*)

echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"

exit 1

esac

exit $RETVAL

chmod +x /etc/init.d/httpd   #赋予可执行权限

vim /etc/profile.d/httpd.sh  #开机加入path变量httpd的安装路径

三、安装php

wget mirrors.sohu.com/php/php-5.3.29.tar.gz

tar -zxvf php-5.3.29.tar.gz

cd php-5.3.29

./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/usr/local/http/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts --with-mhash --with-gd --with-iconv-dir --enable-magic-quotes --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-gd-native-ttf --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-exif --enable-dom                        #重要的是with-apxs2=/usr/local/http/bin/apxs 选项,代表把php编译成httpd的模块使用

make && make install

cp /usr/local/src/php-5.3.29/php.ini-production /etc/php.ini

vim /etc/httpd/httpd.conf   #修改httpd配置文件,在大概158行左右去掉注释,使apache加载php的模块

LoadModule php5_module        modules/libphp5.so

#在http配置文件大概385行左右的位置加入下面的配置让httpd支持php

AddType application/x-httpd-php .php

AddType image/x-icon .ico

AddType application/x-http-php-source .phps

保存退出

重启httpd

service httpd restart

apachectl -M 查看是否加载了php

到httpd的网页目录下创建一个index.php文件,在浏览器打开。如果可以访问就是可以解析php程序了。

#index.php文件内容:

<?php

phpinfo();

?>

到此安装结束。下面是配置负载均衡和动静分离

一、nginx配置

先说说架构,前端一个nginx(192.168.10.100)实现轮询算法的负载均衡,所有请求由前端的nginx接受根据轮询的方式交给后端的应用服务器(192.168.10.233、192.168.10.234),静态请求由后端的nginx响应,而动态请求通过转发至后端的httpd服务器进行处理之后在返回给nginx。由nginx把结果返回给客户。我用两个虚拟主机,分别是a.pl.com和img.pl.com

(1)前端负载nginx配置(192.168.10.100)定义后端的应用服务器集群和负载方式

vim /usr/local/nginx/conf/nginx.conf

upstream a.pl.com {

server 192.168.10.233:80 weight=50 max_fails=5 fail_timeout=20s;

server 192.168.10.234:80 weight=50 max_fails=5 fail_timeout=20s;

}

upstream b.pl.com {

server 192.168.10.233:80 weight=50 max_fails=5 fail_timeout=20s;

server 192.168.10.234:80 weight=50 max_fails=5 fail_timeout=20s;

}

___________________________________________________________________________________________

(2)后端nginx服务器配置(192.168.10.233)

#定义两个虚拟主机,在每个server段中配置一个location,所有以.php结尾的页面全部交给apache来处理

server {

listen       80;

server_name a.pl.com;

index index.html index.php;

root  /data/wwwroot/polyguide;

access_log  /var/log/nginx/access_cd.polyguide.com.cn.log;

location / {

if (!-e $request_filename)

{

rewrite ^/(.*\.(js|ico|gif|jpg|png|css|bmp|html|xls)$) /$1 last;

rewrite ^/(.*) /index.php?$1 last;

rewrite ^.*$ /index.php last;

}

}

location ~ .*\.php$ {

                proxy_set_header Host $host;

                proxy_set_header X-Real-IP $remote_addr;

                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                proxy_pass  http://127.0.0.1:8080;

        }

}

server {

listen       80;

server_name  img.pl.com;

access_log  /var/log/nginx/img_access.log;

location / {

root   /data/fastdfs;

index  index.html index.htm;

}

location ~* /group1/M00/ {

root /data/fastdfs;

ngx_fastdfs_module;

}

location ~ .*\.php$ {

                proxy_set_header Host $host;

                proxy_set_header X-Real-IP $remote_addr;

                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                proxy_pass  http://127.0.0.1:8080;

        }

}

二、后端apache应用服务器(192.168.10.233)

(1)编辑httpd主配置文件

vim /etc/httpd/httpd.conf

___________________________________________________________________________________________

大概224行左右,注释掉DocumentRoot

DocumentRoot "/data/wwwroot/polyguide"

大概477行左右,去掉注释,使apache开启虚拟主机功能

Include /etc/httpd/extra/httpd-vhosts.conf

(2)编辑httpd虚拟主机配置文件

vim /etc/httpd/extra/httpd-vhosts.conf

<VirtualHost 127.0.0.1:8080>            #设置虚拟主机的监听地址和端口,由于httpd不直接向外通信,只是接收nginx转发的动态请求,所以监听在本地地址。

ServerName a.pl.com                 #基于域名的虚拟主机

DocumentRoot "/data/wwwroot/polyguide"

<Directory "/data/wwwroot/polyguide">

Options none

AllowOverride none

Require all granted                 #允许所有用户访问

</Directory>

<IfModule mod_rewrite.c>

RewriteEngine on

RewriteRule ^/static/(.*) /static/$1 [L]

RewriteRule ^/uploadtmp/(.*) /uploadtmp/$1 [L]

RewriteRule ^/ueditor/(.*) /ueditor/$1 [L]

RewriteRule ^/crontab/(.*) /crontab/$1 [L]

RewriteRule ^/robots/(.*) /robots/$1 [L]

#RewriteRule ^(.*)$ /uploadtmp/$1 [L]            #should use this

RewriteRule ^(.*)$ /uploadtmp/index2.php [L]     #this is test rewrite rules

</IfModule>

ErrorLog "/var/log/http/a.pl-error_log"            #httpd错误日志的位置

CustomLog "/var/log/http/a.pl-access_log" combined    #访问日志位置

</VirtualHost>

<VirtualHost 127.0.0.1:8080>                #第二个虚拟主机

ServerName img.pl.com

DocumentRoot "/data/fastdfs"            #网页文件的主目录

<Directory "/data/fastdfs">

Options none

AllowOverride none

Require all granted

</Directory>

<Location /M00>

sethandler fastdfs                       #这个是FastDFS的设置

</Location>

ErrorLog "/var/log/http/img.pl-error_log"

CustomLog "/var/log/http/img.pl-access_log" combined

</VirtualHost>

另一台配置就不写了,到此,配置完毕,现在分别访问在nginx上的静态页面和apache上的动态页面测试。新手~写的不好。请各位大神勿喷,有什么错误和需要改正的请留言。谢谢~

时间: 2024-12-22 13:57:25

nginx+apache实现负载均衡+动静分离配置(编译安装)的相关文章

Nginx+Tomcat+Keepalived+Memcache 负载均衡动静分离技术

一.概述 Nginx 作负载均衡器的优点许多,简单概括为: ①实现了可弹性化的架构,在压力增大的时候可以临时添加Tomcat服务器添加到这个架构里面去; ②upstream具有负载均衡能力,可以自动判断下面的机器,并且自动踢出不能正常提供服务的机器: Keepalived 可实现 Nginx负载均衡器双机互备,任意一台机器发生故障,对方都能够将虚拟IP接管过去. Memcache可以实现Tomcat服务器的Sission共享整个拓补如下: 注意: 1.由于服务器有限,IP相同的为同一台机.只是端

HAproxy负载均衡动静分离实现及配置详解

 HAproxy负载均衡动静分离实现及配置详解 HAproxy的介绍 HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理.HAProxy运行在时下的硬件上,完全可以支持数以万计的并发连接.并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上. HAProxy实现了一种事件驱动.单一进程

Nginx安装-反向代理-负载均衡-动静分离

安装 1.需要素材 后两个用命令下载安装 openssl-1.0.1t.tar.gzzlib -1.2.8.tar.gz 2:在/usr/src/ 下吧 " nginx-1.16.1.tar.gz " "pcre-8.37.tar.gz" 这两个文件放进去并且解压然后在pcre-8.37这个文件下先 : ./configure 在敲 make && make install pcre-conffig --verison 查看版本 下面安装nginx

nginx+tomcat负载均衡+动静分离

1.服务器A安装ng,服务器B.C安装tomcat: 2.服务器A建立/data/www目录,用于发布静态文件: 3.ng无动静分离配置: user root root; worker_processes 8; pid /usr/local/nginx/nginx.pid; worker_rlimit_nofile 102400; events { use epoll; worker_connections 102400; } http { include mime.types; default

Nginx+Tomcat配置负载均衡-动静分离(二)

配置动静分离的时候遇到了一些问题,一个是配置nginx配置文件有问题导致访问不到服务器,另一个问题是配置静态资源的路径和实际的资源目录不匹配导致404,502等错误 结合上一篇的基础,在此将动静分离的配置文件主要部分做一说明,为日后参考. 1 #user nobody; 2 worker_processes 1; 3 4 #error_log logs/error.log; 5 #error_log logs/error.log notice; 6 #error_log logs/error.l

nginx配置优化+负载均衡+动静分离详解

nginx配置如下: #指定nginx进程运行用户以及用户组user www www;#nginx要开启的进程数为8worker_processes 8;#全局错误日志文件#debug输出日志最为详细,而crit输出日志最少/var/log目录是linux下的日志存放目录error_log /var/log/nginx/nginx_error.log crit;#指定进程id的存储位置pid /var/run/nginx.pid;#绑定worker进程和CPU,linux内核2.4以上可用wor

[转] nginx配置优化+负载均衡+动静分离(附带参数解析)

#指定nginx进程运行用户以及用户组user www www;#nginx要开启的进程数为8worker_processes  8;#全局错误日志文件#debug输出日志最为详细,而crit输出日志最少/var/log目录是linux下的日志存放目录error_log /var/log/nginx/nginx_error.log crit;#指定进程id的存储位置pid   /var/run/nginx.pid;#绑定worker进程和CPU,linux内核2.4以上可用worker_rlim

【Linux运维-集群技术进阶】Nginx+Keepalived+Tomcat搭建高可用/负载均衡/动静分离的Webserver集群

额.博客名字有点长.. . 前言 最终到这篇文章了,心情是有点激动的. 由于这篇文章会集中曾经博客讲到的全部Nginx功能点.包含主要的负载均衡,还有动静分离技术再加上这篇文章的重点.通过Keepalived实现的HA(High Available).为什么要实现高可用呢?曾经在搭建的时候仅仅用了一台Nginxserver,这种话假设Nginxserver宕机了,那么整个站点就会挂掉.所以要实现Nginx的高可用,一台挂掉还会有还有一台顶上去.从而保证站点能够持续的提供服务. 关于负载均衡和动静

Haproxy负载均衡/动静分离(haproxy各选项详细解释)

在前端领域做负载均衡,动静分离的程序有很多,比较常用的是nginx和Haproxy,今天就说一下 Haproxy在这两方面的表现,文章参考很多网文写成,再加上自己的实验成果,文中所有解释都经过实际环境验证. 环境介绍: Centos 6.5 Haproxy 1.7.9 前端 192.168.6.10 后端 192.168.6.20(web1)  192.168.6.21(web2) 图片服务器 192.168.6.22(img01) 1.安装Haproxy wget http://www.hap