Nginx安装在公网IP为x.x.x.x的服务器上
Tomcat安装在公网IP为y.y.y.y的服务器上
Redis安装在公网IP为z.z.z.z的服务器上
nginx安装
第一步,安装编译工具及库文件。
命令:yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel
第二步,安装Nginx。
# 下载Nginx安装包
命令:cd /usr/local/src/
wget http://nginx.org/download/nginx-1.20.1.tar.gz
# 解压Nginx安装包
命令:tar zxvf nginx-1.20.1.tar.gz
# 编译配置Nginx
命令:cd nginx-1.20.1
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-http_v2_module \
--with-threads \
--with-stream \
--with-stream_ssl_module
# 编译安装Nginx
命令:make && make install
# 查看nginx版本
命令:/usr/sbin/nginx -v
或 /usr/sbin/nginx -V
若结果显示“nginx version: nginx-1.20.1”,则nginx安装完成。
nginx配置
第一步,创建 Nginx 运行使用的用户nginx。
命令:useradd -s /sbin/nologin -M nginx
( Nginx 服务的默认用户是 nobody ,为了安全更改为 nginx,在配置文件中启用user nginx nginx;)
第二步,修改nginx.conf配置文件。
nginx.conf路径为/etc/nginx/nginx.conf。nginx.conf内容如下:
user nginx nginx; #用户名设置为刚刚创建的用户名
worker_processes 4; #允许生成的进程数,默认为1
worker_cpu_affinity 0001 0010 0100 1000;
error_log /var/log/nginx/error.log info; #日志位置和级别
pid /var/run/nginx.pid; #指定nginx进程运行文件存放地址
worker_rlimit_nofile 102400; #最大连接数,默认为512
events {
use epoll; #事件驱动模型
worker_connections 102400; #最大连接数,默认为512
accept_mutex off; #设置网路连接序列化,防止惊群现象发生,默认为on
multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
}
http {
server_tokens off;
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型,默认为text/plain
log_format main escape=json '$remote_addr $time_iso8601 $status $request_time $http_x_forwarded_for $http_host $request $request_body $http_referer'; #自定义格式
access_log /var/run/access.log main; #combined为日志格式的默认值
sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
#keepalive_timeout 0;
keepalive_timeout 300s; #连接超时时间,默认为75s,可以在http,server,location块。
send_timeout 300s;
gzip on;
server {
listen 80; #监听端口
server_name localhost; #域名,当前IP地址
index index.html index.htm index.php;
...
}
server {
listen 80; #监听端口
listen 443 ssl;
server_name xxxxxxx.com; #域名,当前IP地址
ssl_certificate /etc/nginx/cert/xxxxxxx.com.pem;
ssl_certificate_key /etc/nginx/cert/xxxxxxx.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://xxx;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
第三步,检查配置文件nginx.conf的正确性。
命令:/usr/sbin/nginx -t
若结果显示“nginx: [emerg] mkdir() "/var/tmp/nginx/client" failed (2: No such file or directory) nginx: configuration file /etc/nginx/nginx.conf test failed”,则说明服务无法启动。可以使用命令“mkdir -p /var/tmp/nginx”创建目录,然后再次运行命令“/usr/sbin/nginx -t”就可以了。
若结果显示“nginx: configuration file /etc/nginx/nginx.conf test is successful”,则说明nginx安装和配置成功。
nginx启动和访问站点
第一步,启动nginx。
命令:/usr/sbin/nginx
第二步,检查是否已经启动。(查看是否有进程)
命令:ps -ef | grep nginx
结果的第一行显示“nginx:master process”,nginx已经启动。
注意:nginx:master process后面有一个路径,这就是nginx的安装路径。
第三步,访问站点。
从浏览器访问已经配置好的站点IP,如果页面显示“Welcome to nginx!”,则说明Nginx已经安装及配置好了。
nginx关闭和重启
第一步,关闭nginx。
命令:/usr/sbin/nginx -s stop
第二步,配置文件修改后,需要指定配置文件进行重启。
如果nginx服务已经停止,那就需要把nginx服务启动。
命令:/usr/sbin/nginx -c /etc/nginx/nginx.conf
重启nginx服务必须是在nginx服务已经启动的情况下进行,因为这时,/var/run中存在nginx.pid文件。
命令:/usr/sbin/nginx -s reload
不进入nginx根目录即可进行相应的操作
第一种方法:
第一步,找到nginx所在的安装目录/usr/local/nginx/sbin,这个目录下有一个名为nginx的文件。
第二步,创建一个软链接放在全局目录中。相当于在全局环境中设置了一个文件指向依赖的环境目录中。
命令:cd /usr/local/bin/
ln -s /usr/sbin/nginx nginx
现在不进入nginx根目录输入命令,不会再提示command not foun

本文详细介绍了如何在Linux环境下部署Nginx、Tomcat和Redis集群,并配置它们之间的协同工作,包括安装步骤、配置文件修改、启动与关闭命令等。
1443

被折叠的 条评论
为什么被折叠?



