LNMP系列(1)centos 6.0 nginx安装
#!/bin/bash
#安装依赖
yum -y install gcc gcc-c++ autoconf automake yum -y install zlib zlib-devel openssl openssl-devel pcre-devel #zlib:nginx提供gzip模块,需要zlib库支持 #openssl:nginx提供ssl功能 #pcre:支持地址重写rewrite功能
#创建用户
groupadd -r nginx useradd -s /sbin/nologin -g nginx -r nginx id nginx
#创建log 目录
mkdir /var/log/nginx
#其他目录
mkdir -pv /var/tmp/nginx/{client,proxy,fastcgi}
#准备make
./configure \ --prefix=/usr/local/nginx \ --conf-path=/etc/nginx/nginx.conf \ --user=nginx \ --group=nginx \ --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 \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-http_flv_module \ --with-http_mp4_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/fastcgi #make && make install make && make install
#检查路径
ls /usr/local/nginx/ ls /usr/local/nginx/sbin/ ls /etc/nginx/
#写path
echo "export PATH=/usr/local/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh
#使ngnix 环境生效
source /etc/profile.d/nginx.sh
#查看nginx 版本
nginx -v
#测试nginx 文件
nginx -t
#配置服务
vim /etc/rc.d/init.d/nginx # 没有下面两行则不能添加 --list # chkconfig: - 85 15 # description: nginx is a World Wide Web server. It is used to serve #! /bin/sh # chkconfig: - 85 15 # description: nginx is a World Wide Web server. It is used to serve # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: starts the nginx web server PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DESC="nginx daemon" NAME=nginx DAEMON=/usr/local/nginx/sbin/$NAME CONFIGFILE=/etc/nginx/$NAME.conf PIDFILE=/var/run/nginx/$NAME.pid SCRIPTNAME=/etc/init.d/$NAME set -e [ -x "$DAEMON" ] || exit 0 do_start() { $DAEMON -c $CONFIGFILE || echo -n "nginx already running" } do_stop() { kill -INT `cat $PIDFILE` || echo -n "nginx not running" } do_reload() { kill -HUP `cat $PIDFILE` || echo -n "nginx can't reload" } case "$1" in start) echo -n "Starting $DESC: $NAME" do_start echo "." ;; stop) echo -n "Stopping $DESC: $NAME" do_stop echo "." ;; reload|graceful) echo -n "Reloading $DESC configuration..." do_reload echo "." ;; restart) echo -n "Restarting $DESC: $NAME" do_stop do_start echo "." ;; *) echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2 exit 3 ;; esac exit 0
#检查开机设置
chkconfig --list nginx chkconfig --add nginx chkconfig nginx on
#设置脚本权限
chmod +x /etc/rc.d/init.d/nginx
#启动:
/usr/local/nginx/sbin/nginx
#重启
/usr/local/nginx/sbin/nginx -s reload
#相关操作
#查询nginx主进程号 ps -ef | grep nginx #停止进程 kill -QUIT 主进程号 #快速停止 kill -TERM 主进程号 #强制停止 pkill -9 nginx #测试端口 netstat –na|grep 80 #浏览器中测试 http://ip:80
#映射
location / { proxy_pass http://localhost:8080/; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #root html; #index index.html; } location /v1/ { proxy_pass http://localhost:8080/v1/; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #root html; #index index.html; }
---------------------------------------------------------------------------------
Kind Regards,
HaiTao Niu
JavaScript developer :)
ORACLE Certified Database Administrator - 10g
SUN Certified Applocation Developer - Java 6
Ring Building,No.28,ZhongGuanCun Software Park, No.8 Dong Bei Wang West Road, Haidian District, Beijing P.R.China 100193
E-mail: 1648500@qq.com