centos 解压安装nginx,并配置多个tomcat
环境说明:
VMware虚拟机,部署免安装版的nginx-1.18.0.tar.gz
项目 |
版本 |
说明 |
操作系统 |
CentOS Linux release 7.5.1804 (Core) |
最小化安装 |
nginx |
nginx-1.18.0.tar.gz |
压缩包 |
首先要安装几个依赖环境
1.gcc
nginx是C语言开发,建议在linux上运行。
安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc
[root@localhost ~]# yum install gcc-c++
2.PCRE
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
#注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。
[root@localhost ~]# yum install -y pcre pcre-devel
3.zlib
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
[root@localhost ~]# yum install -y zlib zlib-devel
4.openssl
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
[root@localhost ~]# yum install -y openssl openssl-devel
二、下载解压
1、官网地址:http://nginx.org/en/download.html
2、上传解压
下载之后,用ssh工具上传到centos目录
#进入到当前文件夹解压
[root@localhost nginx]# tar -zxvf nginx-1.18.0.tar.gz
这是我这里的目录,我这里已经解压完毕了
三、安装
cd进入解压nginx之后目录,也就是nginx-1.18.0
1、configure
###复制下面的./configure一直到最后,所有的,包括 \,执行即可
#–prefix 指定安装路径,【注意修改】,这是要安装在哪个文件夹下
#–with-http_stub_status_module 允许查看nginx状态的模块
# –with-http_ssl_module 支持https的模块
我这里的安装目录,根据情况修改自己的路径
默认用
/root/nginx
[root@localhost nginx-1.18.0]# ./configure \ --prefix=/root/nginx \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/temp/nginx/client \ --http-proxy-temp-path=/var/temp/nginx/proxy \ --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \ --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \ --http-scgi-temp-path=/var/temp/nginx/scgi \ --with-http_stub_status_module \ --with-http_ssl_module
自己用
./configure \ --prefix=/root/nginx \ --pid-path=/root/nginx/nginx.pid \ --lock-path=/root/nginx/nginx.lock \ --error-log-path=/root/nginx/logs/error.log \ --http-log-path=/root/nginx/logs/access.log \ --with-http_gzip_static_module \ --http-client-body-temp-path=/root/nginx/temp/client \ --http-proxy-temp-path=/root/nginx/temp/proxy \ --http-fastcgi-temp-path=/root/nginx/temp/fastcgi \ --http-uwsgi-temp-path=/root/nginx/temp/uwsgi \ --http-scgi-temp-path=/root/nginx/temp/scgi \ --with-http_stub_status_module \ --with-http_ssl_module
这里执行上面的提示bash: ./configure: 权限不够
-bash: ./configure: 权限不够 [root@localhost nginx-1.18.0]# chmod +x configure
2、编译并安装
(都是在解压之后的目录执行)
[root@centos8 nginx-1.18.0]# make && make install
3、启动
1、修改配置:
/root/nginx/conf/nginx.conf
①找到pid和error_log,将注释放开,并修改为:
error_log /root/nginx/logs/error.log;
pid /root/nginx/logs/nginx.pid;
②在 /usr/local/nginx 目录下创建 logs 目录:
[root@localhost sbin]# mkdir /root/nginx/logs
③启动
#下面第四行启动不起来,是因为少了文件夹,第五行命令创建就可以了,创建,要对应自己的路径
make[1]: 离开目录“/root/nginx/nginx-1.18.0/nginx-1.18.0” [root@localhost nginx-1.18.0]# cd /root/nginx/sbin [root@localhost sbin]# ./nginx nginx: [emerg] mkdir() "/root/nginx/temp/client" failed (2: No such file or directory) [root@localhost sbin]# sudo mkdir -p /root/nginx/temp [root@localhost sbin]# ./nginx [root@localhost sbin]#
4、访问
http://ip:80/
5、nginx命令
#查询nginx主进程号 ps -ef | grep nginx #停止进程 kill -QUIT 主进程号 #快速停止 kill -TERM 主进程号 #强制停止 pkill -9 nginx #修改配置文件后重新加载nginx,免重启 ./nginx -s reload
四、配置多个tomcat
1、启动tomcat
首先分别启动两个或者多个tomcat,我这里的端口号为8081和8082,这里不讲解,自行百度
2、修改nginx配置文件
路径为 /root/nginx/conf/nginx.conf
添加1:
在http里面添加
upstream servers { ip_hash; #配置多个tomcat地址和端口 server 127.0.0.1:8081;#服务器地址1 server 127.0.0.1:8082;#服务器地址2 }
添加二:
location / { root html; index index.html index.htm; proxy_pass http://servers; #配置方向代理地址,对应upstream后面的名称 }
如图:
3、访问
http://192.168.12.72:8080/street/
后面这个street是项目名,到现在的话,就可以根据不同的客户端,根据ip_hash访问到不同的tomcat了
五、windows配置Nginx
关于windows配置Nginx文章:https://www.cnblogs.com/w-yu-chen/p/13093135.html
原文链接:https://www.cnblogs.com/w-yu-chen/p/13053813.html
本文来源 互联网收集,文章内容系作者个人观点,不代表 本站 对观点赞同或支持。如需转载,请注明文章来源,如您发现有涉嫌抄袭侵权的内容,请联系本站核实处理。