手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

CentOS 5.8部署Nginx+Keepalived实现负载均衡

[日期:2012-05-30] 来源:51cto博客  作者:andyxu [字体:大 中 小]

4 、系统约定

软件源代码包存放位置:/usr/local/src

源码包编译安装位置:/usr/local/软件名字

默认系统就会加载/dev/shm ,它就是所谓的tmpfs,有人说跟ramdisk(虚拟磁盘),但不一样。象虚拟磁盘一样,tmpfs 可以使用您的 RAM,但它也可以使用您的交换分区来存储。而且传统的虚拟磁盘是个块设备,并需要一个 mkfs 之类的命令才能真正地使用它,tmpfs 是一个文件系统,而不是块设备;您只是安装它,它就可以使用了。

tmpfs有以下优势:

1)动态文件系统的大小,

2)tmpfs 的另一个主要的好处是它闪电般的速度。因为典型的 tmpfs 文件系统会完全驻留在 RAM 中,读写几乎可以是瞬间的。

3)tmpfs 数据在重新启动之后不会保留,因为虚拟内存本质上就是易失的。所以有必要做一些脚本做诸如加载,绑定的操作。

首先在/dev/shm建个tmp文件夹,然后与实际/tmp绑定

mkdir /dev/shm/tmp

chmod 777 /dev/shm/tmp

mount --bind /dev/shm/tmp /tmp

5、下载软件

cd /usr/local/src  #进入目录

(1)、下载nginx

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

(2)、下载pcre  (支持nginx伪静态)

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.30.tar.gz

(3)、下载ngx_cache_purge(清除指定URL缓存)

wget http://labs.frickle.com/files/ngx_cache_purge-1.5.tar.gz

(4)、下载keepalived

wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz

6、安装pcre

tar  zxvf pcre-8.21.tar.gz

cd pcre-8.21

./configure

make

make install

7、安装nginx

groupadd  www   #添加www组

useradd -g www www -s /bin/false   #创建nginx运行账户www并加入到www组,不允许www用户直接登录系统   

cd /usr/local/src

tar  zxvf  ngx_cache_purge-1.5.tar.gz

tar  zxvf nginx-1.3.0.tar.gz

cd nginx-1.3.0

./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-openssl=/usr/include/openssl --with-pcre=/usr/local/src/pcre-8.30 --add-module=/usr/local/src/ngx_cache_purge-1.5 --with-http_gzip_static_module

# 注意:--with-pcre指向的是源码包解压的路径,而不是安装的路径,否则编译会报错

make

make install

/usr/local/nginx/sbin/nginx    #启动nginx

chown www.www -R /usr/local/nginx/html   #设置目录所有者

vi /etc/rc.d/init.d/nginx    # 设置nginx开启启动,编辑启动文件添加下面内容

  1. #!/bin/bash 
  2. # nginx Startup script for the Nginx HTTP Server 
  3. # it is v.1.3.0 version. 
  4. # chkconfig: - 85 15 
  5. # description: Nginx is a high-performance web and proxy server. 
  6. #              It has a lot of features, but it's not for everyone. 
  7. # processname: nginx 
  8. # pidfile: /var/run/nginx.pid 
  9. # config: /usr/local/nginx/conf/nginx.conf 
  10. nginxd=/usr/local/nginx/sbin/nginx 
  11. nginx_config=/usr/local/nginx/conf/nginx.conf 
  12. nginx_pid=/usr/local/nginx/logs/nginx.pid 
  13. RETVAL=0 
  14. prog="nginx" 
  15. # Source function library. 
  16. .  /etc/rc.d/init.d/functions 
  17. # Source networking configuration. 
  18. .  /etc/sysconfig/network 
  19. # Check that networking is up. 
  20. [ ${NETWORKING} = "no" ] && exit 0 
  21. [ -x $nginxd ] || exit 0 
  22. # Start nginx daemons functions. 
  23. start() { 
  24. if [ -e $nginx_pid ];then 
  25.    echo "nginx already running...." 
  26.    exit 1 
  27. fi 
  28.    echo -n $"Starting $prog: " 
  29.    daemon $nginxd -c ${nginx_config} 
  30.    RETVAL=$? 
  31.    echo 
  32.    [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx 
  33.    return $RETVAL 
  34. } 
  35. # Stop nginx daemons functions. 
  36. stop() { 
  37.         echo -n $"Stopping $prog: " 
  38.         killproc $nginxd 
  39.         RETVAL=$? 
  40.         echo 
  41.         [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid 
  42. } 
  43. reload() { 
  44.     echo -n $"Reloading $prog: " 
  45.     #kill -HUP `cat ${nginx_pid}` 
  46.     killproc $nginxd -HUP 
  47.     RETVAL=$? 
  48.     echo 
  49. } 
  50. # See how we were called. 
  51. case "$1" in 
  52. start) 
  53.         start 
  54.         ;; 
  55. stop) 
  56.         stop 
  57.         ;; 
  58. reload) 
  59.         reload 
  60.         ;; 
  61. restart) 
  62.         stop 
  63.         start 
  64.         ;; 
  65.  
  66. status) 
  67.         status $prog 
  68.         RETVAL=$? 
  69.         ;; 
  70. *) 
  71.         echo $"Usage: $prog {start|stop|restart|reload|status|help}" 
  72.         exit 1 
  73. esac 
  74. exit $RETVAL

chmod +x /etc/rc.d/init.d/nginx   # 赋予执行权限

chkconfig nginx on   #设置开机启动

service nginx stop   #关闭nginx

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款