Cenos/Redhat 6下nginx的安装以及部署 与 代理/反向代理

 

第一步:下载nginx压缩包
Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个

wget -c https://nginx.org/download/nginx-1.12.0.tar.gz

 

第二步:配置nginx安装所需的环境

1. 安装gcc
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境。安装指令如下:
yum install -y gcc gcc-c++

2. 安装PCRE pcre-devel
Nginx的Rewrite模块和HTTP核心模块会使用到PCRE正则表达式语法。这里需要安装两个安装包pcre和pcre-devel。第一个安装包提供编译版本的库,而第二个提供
开发阶段的头文件和编译项目的源代码。安装指令如下:
yum install -y pcre pcre-devel

3.安装zlib
zlib库提供了开发人员的压缩算法,在Nginx的各种模块中需要使用gzip压缩。安装指令如下:
yum install -y zlib zlib-devel

4.安装Open SSL
nginx不仅支持 http协议,还支持 https(即在 ssl 协议上传输 http),如果使用了 https,需要安装 OpenSSL 库。安装指令如下:
yum install -y openssl openssl-devel


第三步:解压nginx压缩包并安装
tar -zxvf nginx-1.12.0.tar.gz

配置,编译安装 开启nginx状态监测功能
./configure --prefix=/usr/local/nginx/
make
make install


第四步:启动nginx
进入/usr/local/nginx/sbin目录,输入./nginx即可启动nginx
./nginx

--------------添加防火墙允许端口访问-----------------

iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 80 -j ACCEPT

在其它电脑使用浏览器访问 http://ip 进行测试

关闭nginx
./nginx -s quit 或者 ./nginx -s stop

重启nginx
./nginx -s reload

查看nginx进程
ps aux|grep nginx

 

此外,nginx的配置文件位置 ->/usr/local/nginx/conf/nginx.conf

如修改域名、端口、反向代理,都在这里面使用vim进行配置修改

 

e.g:使用nginx 代理 tomcat部署的web项目

vim /usr/local/nginx/conf/nginx.conf

最简单的部署方案

 server {
    listen 80;
    server_name www.xxxx.com;
    location / {
    proxy_pass http://127.0.0.1:8080/web项目路径/;
    }

 

通过nginx反向代理tomcat

 在http{  } 中添加如下内容

    upstream xxxx{
        server 192.168.0.1:8080;
        server 192.168.0.2:8080;
    }

在server{location /{}}中添加如下内容 

 server {
    listen 80;
    server_name www.xxxx.com;
    #charset koi8-r;
    #access_log  logs/host.access.log  main;

    location / {
    root    html;
    index    index.html index.htm;
    proxy_pass http://xxxx;
    }

反向代理完成,通过外部浏览器访问 http://192.168.0.1/xxxx/ 与 http://192.168.0.2/xxxx/  即可

 

 


-----------------------将Nginx注册为Linux服务---------------------------------
1.在/etc/init.d目录下新建文件,命名为nginx
对nginx文件进行编辑,执行

vim /etc/init.d/nginx

2.粘贴以下内容,并修改路径为自己实际的路径

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf

nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0

# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1

fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?

echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}

# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}

# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "

#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL

 

3.设置nginx的文件属性,把nginx 修改为可运行的文件

#chmod a+x nginx

4.设置服务运行级别

#chkconfig --add nginx

5.服务就添加成功了

然后用 chkconfig --list 查看,在服务列表里就会出现自定义的服务了

# chkconfig --list

6.测试

service nginx start

service nginx stop

service nginx restart

service nginx status

 

至此,nginx的安装便到此结束!

通过外部浏览器访问 http://ip 测试后台页面

 

posted @ 2019-08-30 10:18  ∴9527  阅读(662)  评论(0编辑  收藏  举报