nginx的编译安装以及启动脚本编写

Nginx的编译安装和启动脚本的编写

Nginxd的功能强大,可以实现代理、负载均衡等企业常用的功能。下面介绍一下nginx的编译安装方法:

1. 下载

  官方下载地址:http://nginx.org/en/download.html;下面以nginx-1.8.0版本为例:

# cd /usr/local/src/
# wget http://nginx.org/download/nginx-1.8.0.tar.gz
# tar zxvf nginx-1.8.0.tar.gz

2. 配置

# cd nginx-1.8.0
# ./configure \
--prefix=/usr/local/nginx \
--with-http_realip_module \
--with-http_sub_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-pcre

  若出现以下错误:

./configure: error: the HTTP rewrite module requires the PCRE library.
# yum -y install pcre-devel

3. 编译、安装

# make && make install

4. 启动Nginx

# /usr/local/nginx/sbin/nginx   //启动nginx
# ps aux |grep nginx   //查看是否启动成功

5. 启动脚本编写

  编写的启动脚本,可实现功能如【start|stop|reload|restart|configtest】即【启动|关闭|重新加载|重启|配置排错】

# vim /etc/init.d/nginx    //写入以下内容
#!/bin/bash # chkconfig: - 30 21 # description: http service. # Source Function Library . /etc/init.d/functions # Nginx Settings NGINX_SBIN="/usr/local/nginx/sbin/nginx" NGINX_CONF="/usr/local/nginx/conf/nginx.conf" NGINX_P RETVAL=0 prog="Nginx" start() { echo -n $"Starting $prog: " mkdir -p /dev/shm/nginx_temp daemon $NGINX_SBIN -c $NGINX_CONF RETVAL=$? echo return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p $NGINX_PID $NGINX_SBIN -TERM rm -rf /dev/shm/nginx_temp RETVAL=$? echo return $RETVAL } reload(){ echo -n $"Reloading $prog: " killproc -p $NGINX_PID $NGINX_SBIN -HUP RETVAL=$? echo return $RETVAL } restart(){ stop start } configtest(){ $NGINX_SBIN -c $NGINX_CONF -t return 0 } case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) restart ;; configtest) configtest ;; *) echo $"Usage: $0 {start|stop|reload|restart|configtest}" RETVAL=1 esac exit $RETVAL

  保存后,修改权限,因为必须要有执行权限:

# chmod 755 /etc/init.d/nginx
# chkconfig --add nginx  //加入开机启动列表
# chkconfig nginx on     //启动开机启动

  开机启动选项,可以根据自己的需求来添加和开启。