主要实现功能是:
1、动态页面与静态页面分离;
2、nginx既可以做静态网站的负载均衡,同时还可做动态网站的负载均衡
3、在后续工作中可以为后端服务器跟据需要进行拓展
这是咱们这次的实验拓扑图:
![QQ截图20140403175503.jpg wKioL1M9MpejRfzAAACQCd3gv-8918.jpg](https://i-blog.csdnimg.cn/blog_migrate/d7c85329f501638221c767ea21669cc1.jpeg)
system: redhat 6.4_x64
nginx: 192.168.100.11
apache: 192.168.100.31
php: 192.168.100.21
mysql: 192.168.100.41
software:
apr-1.4.6.tar.bz2
apr-util-1.5.2.tar.bz2
pcre-8.32.tar.gz
httpd-2.4.4.tar.bz2
mysql-5.6.10-linux-glibc2.5-x86_64.tar.gz
libmcrypt-2.5.8-9.el6.x86_64.rpm
libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
php-5.4.13.tar.bz2
1) 解决依赖关系
1> 编译安装apr
# tar xf apr-1.4.6.tar.bz2
# cd apr-1.4.6
# ./configure --prefix=/usr/local/apr
# make && make install
2> 编译安装apr-util
# tar xf apr-util-1.5.2.tar.bz2
# cd apr-util-1.5.2
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
# make && make install
3> 编译安装pcre
#tar xf pcre-8.32.tar.gz
#cd pcre-8.32
# ./configure --prefix=/usr/local/pcre
# make &&make install
2) 编译安装apache
# tar xf httpd-2.4.4.tar.bz2
# cd httpd-2.4.4
# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre=/usr/local/pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all --with-mpm=event
# make && make install
3)设置其Pid文件的路径
编辑/etc/httpd/httpd.conf,添加如下行即可:
PidFile "/var/run/httpd.pid"
4)提供SysV服务脚本/etc/rc.d/init.d/httpd
在源码httpd-2.x.x/build/rpm中存在httpd.init拷贝命令如下:
#cp httpd.init/etc/init.d/httpd
拷贝之后,注意其中有三处主要的地方需要修改下的:
httpd=${HTTPD-/usr/local/apache/bin/httpd}
pidfile=${PIDFILE-/usr/local/apache/logs/${prog}.pid}
CONFFILE=/etc/httpd/httpd.conf
请根据自己的实际情况更改相应的路径!
然后运行如下命令:
#chmod +x/etc/init.d/httpd
#chkconfig--add httpd
#chkconfig --level2345 httpd on
这样一来,启动、停止、重启Apache就可以用以下方式了:
#/etc/init.d/httpdstart
#/etc/init.d/httpdstop
#/etc/init.d/httpdrestart
5)输出PATH变量
#vim /etc/profile.d/httpd.sh
export PATH=$PATH:/usr/local/apache/bin
1) 新建用户以安全方式运行进程:
# groupadd -r mysql
# useradd -g mysql -r -s /sbin/nologin mysql
2) 二进制安装mysql5.6:
# tar xf mysql-5.6.10-linux-glibc2.5-x86_64.tar.gz -C /usr/local
# cd /usr/local/
# ln -s mysql-5.6.10-linux-glibc2.5-x86_64.tar.gz mysql
# cd mysql
# chown -R root:mysql .
# chown -R mysql.mysql ./data
3) 初始化数据库:
# scripts/mysql_install_db --user=mysql
# chown -R root .
4) 编辑mysql主配置文件:
# vim /usr/local/mysql/my.cnf
datadir = /mydata/data
port = 3306
server_id = 1
socket = /var/lib/mysql/mysql.sock
log-bin = master-bin
innodb_file_per_table = 1
由于默认没有socket文件存放目录,所以还需要创建该目录,权限为mysql属主,属组
# mkdir /var/lib/mysql
# chown -R mysql.mysql /var/lib/mysql
4) 为mysql提供sysv服务脚本:
# cd /usr/local/mysql
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod +x /etc/rc.d/init.d/mysqld
添加至服务列表:
# chkconfig --add mysqld
启动服务
# service mysqld start
5) 为了使用mysql的安装符合系统使用规范,并将其开发组件导出给系统使用,这里还需要进行如下步骤:
输出mysql的man手册至man命令的查找路径:
编辑/etc/man.config,添加如下行即可:
MANPATH /usr/local/mysql/man
输出mysql的头文件至系统头文件路径/usr/include:
这可以通过简单的创建链接实现:
# ln -sv /usr/local/mysql/include /usr/include/mysql
输出mysql的库文件给系统库查找路径:
# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
而后让系统重新载入系统库:
# ldconfig -v
修改PATH环境变量,让系统可以直接使用mysql的相关命令
# vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
1) 解决依赖关系:
如果想让编译的php支持mcrypt扩展,可以到http://www.rpmfind.net下载
libmcrypt-2.5.8-9.el6.x86_64.rpm
libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
mysql-5.6.10-linux-glibc2.5-x86_64.tar.gz
2) 由于下一步php需要mysql,然后本人也试过指定mysql的头文件,但编译php时总是提示找不到mysql的头文件,所以还是用二进制安装算了,反正这里也不需启动mysql只需它的客户端工具与库文件及头文件
二进制安装mysql5.6:
# tar xf mysql-5.6.10-linux-glibc2.5-x86_64.tar.gz -C /usr/local
# cd /usr/local/
# ln -s mysql-5.6.10-linux-glibc2.5-x86_64.tar.gz mysql
修改PATH环境变量,让系统可以直接使用mysql的相关命令
# vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
3) 编译安装php-5.4.13
# tar xf php-5.4.13.tar.bz2
# cd php-5.4.13
# ./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 --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts
注:
1>这里为了支持apache的worker或event这两个MPM,编译时使用了--enable-maintainer-zts选项,如果我们使用prefork,则不需要该项。
2>如果使用PHP5.3以上版本,为了链接MySQL数据库,可以指定mysqlnd,这样在本机就不需要先安装MySQL或MySQL开发包了。mysqlnd从php 5.3开始可用,可以编译时绑定到它(而不用和具体的MySQL客户端库绑定形成依赖),但从PHP 5.4开始它就是默认设置了。
# ./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd
# make
# make intall
4) 为php提供配置文件:
# cp php.ini-production /etc/php.ini
5) 为php-fpm提供Sysv init脚本,并将其添加至服务列表:
# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
# chmod +x /etc/rc.d/init.d/php-fpm
# chkconfig --add php-fpm
# chkconfig php-fpm on
6) 为php-fpm提供配置文件:
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
编辑php-fpm的配置文件:
# vim /usr/local/php/etc/php-fpm.conf
配置fpm的相关选项为你所需要的值,并启用pid文件(如下最后一行):
pm.max_children = 150
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pid = /usr/local/php/var/run/php-fpm.pid
# service php-fpm start
使用如下命令来验证(如果此命令输出有中几个php-fpm进程就说明启动成功了):
# ps aux | grep php-fpm
1) 编译安装:
# tar xvf nginx-1.4.7.tar.gz
# cd nginx-1.4.7
# groupadd -r nginx
# useradd -r -g nginx -s /sbin/nologin nginx
./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--with-pcre
# make && make install
2) 为nginx提供服务脚本
vim /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
-
整合nginx和php5
1) # vim /etc/nginx/nginx.conf,启用如下选项:
upstream static {
server 192.168.100.31 max_fails=2 fail_timeout=1;
server 192.168.100.11 backup;
}
server {
listen 80;
server_name localhost;
location / {
root /web/htdocs;
proxy_pass http://static;
index index.html index.php;
}
location ~ \.php$ {
root /web/php;
fastcgi_pass 192.168.100.21:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
2) # vim /etc/nginx/fastcgi_params,将其内容更改为如下内容:
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
如果要在SSL中使用php,需要在php的location中添加此选项:
fastcgi_param HTTPS on;
并在所支持的主页面格式中添加php格式的主页,类似如下:
location / {
root /web/htdocs;
index index.php index.html index.htm;
}
# vim /web/htdocs/index.php
<?php
phpinfo();
?>
而后重新载入nginx的配置文件:
# service nginx reload
apache服务器上:
# service httpd restart
# vim /usr/local/apache/htdocs/index.html
<h1>This is Apache Server! </h1>
nginx服务器上:
# mkdir /web/htdocs
# vim /web/htdocs/index.html
<h1>This is Nginx Server! </h1>
php服务器上:
# mkdir /web/php
# vim /web/php/index.php
<?php
phpinfo();
?>
测试mysql的连接是否成功
<?php
$link=mysql_connect('192.168.100.41','root','asdasd');
if(!$link)
echo "connect failed !!!! ";
else
echo "success ....";
mysql_close();
?>