Fork me on GitHub

Centos7 安装 Nginx TCP HTTP

nginx 正常安装 仅支持 http和https的负载均衡和反向代理 相关文档:

由于现在需要使用TCP的负载均衡和反向代理功能,所以特别记录了这篇安装文档,本文中 TCP 模块添加在其中

CentOS 7 下安装 Nginx

查看是否安装Nginx

安装之前,最好检查一下是否已经安装有nginx
[root@CENTOS7-TEST1 sbin]# find -name nginx
如果系统已经安装了nginx,那么就先卸载如果系统已经安装了nginx,那么就先卸载
[root@CENTOS7-TEST1 /]# yum remove nginx

安装所需环境

Nginx 是 C语言 开发,建议在 Linux 上运行,当然,也可以安装 Windows 版本,本篇则使用 CentOS 7 作为安装环境。

gcc 安装

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

1
[root@CENTOS7-TEST1 nginx]# yum install gcc-c++

PCRE pcre-devel 安装

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

1
[root@CENTOS7-TEST1 nginx]# yum install -y pcre pcre-devel

zlib 安装

zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

1
[root@CENTOS7-TEST1 nginx]# yum install -y zlib zlib-devel

OpenSSL 安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

1
[root@CENTOS7-TEST1 nginx]# yum install -y openssl openssl-devel

注:yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel ## 一次性安装以上依赖包

安装Nginx

使用wget命令下载(推荐)。

1
[root@CENTOS7-TEST1 nginx]# wget https://nginx.org/download/nginx-1.13.9.tar.gz

我下载的是1.13.9版本,这个是目前的稳定版。

解压

1
[root@CENTOS7-TEST1 nginx]# tar -zxvf nginx-1.13.9.tar.gz  && cd nginx-1.13.9

配置安装 a、b两种选择其一即可

a 配置 默认安装(只支持http和https)

1
[root@CENTOS7-TEST1 nginx-1.13.9]# ./configure

b 配置 添加额外的 TCP 模块

1
[root@CENTOS7-TEST1 nginx-1.13.9]# ./configure --with-stream --with-stream_ssl_module --with-http_stub_status_module

编译安装

1
2
[root@CENTOS7-TEST1 nginx]# make
[root@CENTOS7-TEST1 nginx]# make install

查找安装路径:

1
2
[root@CENTOS7-TEST1 nginx-1.13.9]# whereis nginx
nginx: /usr/local/nginx

配置文件 对应上面配置安装来配置文件

a 默认安装不支持TCP的配置文件不需要修改,原配置文件即可

b 支持TCP的配置文件配置,添加TCP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
stream {                  ## stream模块,就跟http模块一样  
         server {             ## 里面乐意有多个监听服务
           listen 12345;  
           proxy_pass 192.168.1.2:3306;  
         }  
          server {  
           listen 12346;  
           proxy_pass 192.168.9.1.3:3306;  
         }  
         ## 在tcp请求代理中,也是可以使用负载均衡的upstream的  
         upstream mysql{  
             server 192.168.9.3:3306;  
             server 192.168.9.4:3306;  
         }  
         server {  
              listen 33333;  
              proxy_pass myqsl;  
         }  
}

启动、停止nginx

1
2
3
4
5
6
7
8
9
10
11
[root@CENTOS7-TEST1 nginx-1.13.9]# cd /usr/local/nginx/sbin/
[root@CENTOS7-TEST1 sbin]# ./nginx
[root@CENTOS7-TEST1 sbin]# ./nginx -s stop #此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
[root@CENTOS7-TEST1 sbin]# ./nginx
[root@CENTOS7-TEST1 sbin]# ./nginx -s quit #此方式停止步骤是待nginx进程处理任务完毕进行停止
[root@CENTOS7-TEST1 sbin]# ./nginx
[root@CENTOS7-TEST1 sbin]# ./nginx -s reload
[root@CENTOS7-TEST1 sbin]# ps aux|grep nginx #查询nginx进程
root 3848 0.0 0.0 20496 1308 ? Ss 11:19 0:00 nginx: master process ./nginx
nobody 3851 0.0 0.0 20948 1440 ? S 11:19 0:00 nginx: worker process
root 3853 0.0 0.0 112660 972 pts/0 S+ 11:20 0:00 grep --color=auto nginx

开机自启动

即在rc.local增加启动代码就可以了。

1
2
[root@CENTOS7-TEST1 sbin]# vi /etc/rc.local
增加一行 /usr/local/nginx/sbin/nginx

设置执行权限:[根据需求]

[root@CENTOS7-TEST1 sbin]# chmod 755 rc.local

添加Nginx到系统服务

创建nginx启动命令脚本

1
[root@CENTOS7-TEST1 /]# vi /etc/init.d/nginx

插入以下内容, 注意修改PATH和NAME字段, 匹配自己的安装路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#! /bin/bash
# chkconfig: - 85 15
PATH=/usr/local/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$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() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || 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

设置执行权限

1
[root@CENTOS7-TEST1 /]# chmod a+x /etc/init.d/nginx

注册成服务

1
[root@CENTOS7-TEST1 /]# chkconfig --add nginx

设置开机启动

1
[root@CENTOS7-TEST1 /]# chkconfig nginx on

重启, 查看nginx服务是否自动启动

1
2
[root@CENTOS7-TEST1 /]# shutdown -h 0 -rn
[root@CENTOS7-TEST1 /]# netstat -apn|grep nginx

对nginx服务执行停止/启动/重新读取配置文件操作

1
2
3
4
5
6
7
8
#启动nginx服务
systemctl start nginx.service
#停止nginx服务
systemctl stop nginx.service
#重启nginx服务
systemctl restart nginx.service
#重新读取nginx配置(这个最常用, 不用停止nginx服务就能使修改的配置生效)
systemctl reload nginx.service
------本文结束 感谢阅读------
鲁顺德 wechat
欢迎您扫一扫上面的微信公众号,订阅我的分享资源!
坚持原创技术分享,您的支持将鼓励我继续创作!