Nginx+apache Load balancer + static and dynamic separation configuration (compile and install)

Source: Internet
Author: User
Tags nginx server

First, compile and install 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


Second, install Apache's httpd server

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, install the Apr-util, and finally install the httpd


(1) Install Apr

TAR-ZXVF apr-1.5.2.tar.gz

CD apr-1.5.2

./cponfigure--PREFIX=/USR/LOCAL/APR

Make && make install


(2) Installation 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) Installation 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's 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 would 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 this some modules may not

# work correctly with a thread-based MPM; Notably PHP would 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 $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 "$" 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的安装路径



Third, install 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/loca The L/HTTP/BIN/APXS option, which represents the module used to compile PHP into httpd

Make && make install

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

vim/etc/httpd/httpd.conf #修改httpd配置文件, remove comments around about 158 lines so that Apache loads PHP modules


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

Save exit


Restart httpd

Service httpd Restart


Apachectl-m to see if PHP was loaded

Create a index.php file in the HTTPD Web directory and open it in the browser. If you can access it, you can parse the PHP program.

#index. php File Contents:

<?php

Phpinfo ();

?>


To the end of this installation. Here is the configuration of load balancing and static and dynamic separation


First, nginx configuration

First of all, the architecture, the front-end of an nginx (192.168.10.100) to implement the polling algorithm load balancing, all requests by the front-end Nginx accept based on the polling to the backend application server (192.168.10.233, 192.168.10.234), The static request is answered by the back-end nginx, and the dynamic request is returned to Nginx after it is processed by forwarding to the backend's httpd server. The results are returned to the customer by Nginx. I use two virtual hosts, a.pl.com and img.pl.com, respectively.

(1) Front-end load Nginx configuration (192.168.10.100) defines the backend application server cluster and load mode

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) Back-end nginx server configuration (192.168.10.233)

#定义两个虚拟主机, configure a location in each server segment, and all pages ending in. php are given to Apache for processing.



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;

}


}



Second, back-end Apache application Server (192.168.10.233)


(1) Edit httpd master configuration file

Vim/etc/httpd/httpd.conf

___________________________________________________________________________________________

About 224 lines, comment out DocumentRoot

DocumentRoot "/data/wwwroot/polyguide"


About 477 lines, remove comments, enable Apache to turn on the virtual host function

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


(2) Edit httpd virtual Host configuration file

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

<virtualhost 127.0.0.1:8080> #设置虚拟主机的监听地址和端口, because httpd not directly outward communication, just receive nginx forwarding dynamic request, so listen on the local address.

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>


Another configuration is not written, to this, the configuration is complete, now access the static page on Nginx and Apache on the dynamic page test. Beginner ~ Bad writing. Please do not spray the big God, what errors and needs to be corrected please leave a message. Thank you ~

This article from "Beginner's mind, always" blog, declined reprint!

Nginx+apache Load balancer + static and dynamic separation configuration (compile and install)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.