一、编译安装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_p_w_picpath_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-util=/usr/local/apr-util/

make && make install

编译httd报错,在文档写好后的两个星期,搭建另一个httpd服务器遇到的,error message:exports.c:1131:note: previous definition of 'ap_hack_apr_version_string' was here

make[2]: *** [exports.lo] 错误1

make[1]:*** [all-recursive] 错误1

原因:with-apr-utils写错了,应该是with-apr-util,本文已经更正。。。百度了一番没有解决,在这里卡了几个小时。郁闷。。。

#安装完成之后为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 $@

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 p_w_picpath/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上的动态页面测试。新手~写的不好。请各位大神勿喷,有什么错误和需要改正的请留言。谢谢~

转载于:https://blog.51cto.com/gouyc/1695036

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

  1. ngixn+tomcat负载均衡 动静分离配置 (nginx反向代理)

    文章目录 Tomcat主要配置文件 Nginx负载均衡实现原理 实验环境 动静分离配置 创建静态页面 配置动态页面 图片动静分离 Tomcat主要配置文件 bin:存放启动和关闭Tomcat脚本 co ...

  2. Nginx 负载均衡动静分离配置

    nginx.conf 1.通过目录进行分离. #静态服务器 upstream static_pools {           server 10.0.0.9:80 weight=5 max_fail ...

  3. Nginx 反向代理 负载均衡 动静分离 高可用 原理

    1.Nginx简介 1.1 概述 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理服务器,特点是占有内存少,并发能力强,能经受高负载的考验,有报告表明能 ...

  4. Nginx 方向代理/负载均衡/动静分离/高可用

    一,反向代理 正向代理:正向代理服务器位于客户端和服务器之间,为了向服务器获取数据,客户端要向代理服务器发送一个请求,并指定目标服务器,代理服务器将目标服务器返回的数据转交给客户端.这里客户端是要进行 ...

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

     HAproxy负载均衡动静分离实现及配置详解 HAproxy的介绍 HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAP ...

  6. Nginx快速入门(安装 负载均衡 动静分离 主备 原理)

    什么是Nginx? Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务.Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Ra ...

  7. 四层负载均衡 动静分离和资源分离 Rewrite rewrite伪静态实例

    文章目录 四层负载均衡 四层负载均衡特点 查看四层负载均衡语法 四层负载均衡配置 优化配置文件 四层负载均衡日志配置 nginx的TCP负载均衡---端口转发 动静分离 单台的动静分离 多台机器动静分 ...

  8. Nginx的动态代理,负载均衡,动静分离的简单使用

    nginx 一 Nginx介绍 1.1 为什么要学习Nginx 问题1: 客户端到底要将请求发送那台服务器 问题2:所有客户端的请求都发送给了服务器1 问题3:客户端发送的请求可能是申请静态资源的,也 ...

  9. Weblogic加Apache的负载均衡以及群集配置

    Weblogic的负载均衡可以通过软件和硬件的方式实现.硬件可以使用F5等设备,软件一般使用apache来实现.下面说明使用apache的具体方法. Weblogic的负载均衡可以通过软件和硬件的方式 ...

最新文章

  1. 【二级java】操作题知识点积累
  2. 谈谈@@IDENTITY 和 SCOPE_IDENTITY()的区别
  3. WIN10 常用bug解决办法
  4. 《左手MongoDB右手Redis》第3章笔记-robo3t上进行增删改查
  5. 有限元笔记04:二维实体单元
  6. 邓公数据结构C++语言版学习笔记1
  7. 黑客到底可以厉害到什么程度
  8. hive获取mysql里的文件_apache – 如何在hive中获取数据库用户名和密码
  9. 深度学习TF—10.循环神经网络RNN及其变体LSTM、GRU实战
  10. 什么造就一个伟大的站点
  11. at android.widget.Toast$TN.handleShow(Toast.java:501)自定义布局异常
  12. Rational License Key Error的永久解决办法
  13. 看漫画学电子,非常精彩!有些概念以前模糊现在真的懂了
  14. 蓝牙音箱延迟测试软件,首发漫步者Lolli Pods使用感受及延迟测试
  15. 湖南省银河计算机专修学院,湖南计算机专修学院
  16. 浅谈智能家居安防监控的重要性
  17. SASS实现颜色卡动画
  18. C#程序设计--任何一个自然数m的立方均可写成m个连续奇数之和
  19. 重庆华侨城携手T.M.D PCP 发财潮流文化节2.0国庆重磅来袭
  20. 电梯控制算法(5)单电梯场景——屏蔽较近楼层进梯请求

热门文章

  1. CSS3j背景渐变,字体颜色渐变,以及兼容IE写法
  2. 2018-10-05学习笔记
  3. 在Centos7上搭建Mysql数据库
  4. D1net阅闻:Google发布了能理解人类语言的云服务
  5. 数字证书转换cer---pem
  6. C语言 · 9-1九宫格
  7. web pack的使用事项
  8. 定制操作(传递函数或lambda表达式)
  9. 通过命令管理Windows7桌面防火墙
  10. 递归处理vue菜单数据