Java Web项目部署(四)-Redis、Nginx

Redis:Key-Value性数据库常作为缓存使用。

Nginx:高性能HTTP、反向代理web服务器,能支持高达5万并发连接,也常用于负载均衡服务使用。

1、Redis部署

(1)解压

tar -zxvf redis-5.0.7.tar.gz

(2)编译、安装

cd redis-5.0.7
mkdir logs
make
make install PREFIX=/home/redis-5.0.7

(3)修改redis配置

vi /home/redis-5.0.7/redis.conf

修改内容:

1)注释 bind 127.0.0.1(如果只有一台服务器,后端使用127.0.0.1连接也可以不注释)

2)允许后台静默运行,将:daemonize no改成:daemonize yes

3)修改日志路径

logfile "/home/redis-5.0.7/logs/6379.log"

注意:要创建logs文件夹

4)连接密码,将: requirepass foobared改为:requirepass xxx

(4)配置redis服务

vi /etc/systemd/system/redis.service

输入内容:

[Unit]
Description=Redis 6379
After=syslog.target network.target

[Service]
Type=forking
PrivateTmp=yes
# Restart=always
ExecStart=/home/redis-5.0.7/bin/redis-server /home/redis-5.0.7/redis.conf
ExecStop=/home/redis-5.0.7/bin/redis-cli -h 127.0.0.1 -p 6379 -a jcon shutdown User=root Group=root LimitCORE=infinity LimitNOFILE=100000 LimitNPROC=100000 

[Install] 
WantedBy=multi-user.target

(5)重载服务

systemctl daemon-reload

(6)启动Redis

systemctl start redis

开机启动

systemctl enable redis

(7)测试

cd /home/redis-5.0.7/bin
./redis-cli -h 127.0.0.1 -p 6379
auth XXX #输入密码
set aa 1
get aa
del aa
flushall

这里Redis就安装完成。

2、Nginx部署

(1)安装依赖

yum install gcc-c++ # 安装 gcc 的环境
yum install -y pcre pcre-devel # 安装 pcre 库
yum install -y zlib zlib-devel # 安装 zlib 库
yum install -y openssl openssl-devel # 安装 openssl 库

(2)解压

tar -zxvf nginx-1.18.0.tar.gz

(3)配置、编译、安装

--prefix=/home/nginx为Nginx安装路劲,如果在nginx-1.18.0同目录下安装会报错

cd nginx-1.18.0/
./configure --prefix=/home/nginx
make && make install

(4)配置服务

vi /etc/systemd/system/nginx.service

输入内容:

[Unit]
Description=nginx service
After=network.target

[Service]
Type=forking
ExecStart=/home/nginx/sbin/nginx -c /home/nginx/conf/nginx.conf
ExecReload=/home/nginx/sbin/nginx -s reload
ExecStop=/home/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

(5)重载服务

systemctl daemon-reload

(6)启动nginx

systemctl start nginx

开机启动

systemctl enable nginx

(7)nginx配置

#开启进程数 <=CPU数
worker_processes  1;

#全局错误日志保存位置
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#进程号保存文件
pid     ./logs/nginx.pid;


events {
    #每个进程最大连接数(最大连接=连接数x进程数)
    worker_connections  1024;
}

#设定http服务器
http {
    #文件扩展名与文件类型映射表
    include       mime.types;

    #默认文件类型
    default_type  application/octet-stream;

    #默认编码
    charset  utf-8;

    #日志文件输出格式,这个位置相于全局设置
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "$http_x_forwarded_for"';
    #定义本虚拟主机的访问日志
    #access_log  logs/access.log  main;

    #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,
	#如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。
	#注意:如果图片显示不正常把这个改成off。
    sendfile        on;

    #防止网络阻塞
    #tcp_nopush     on;

    #长连接超时时间,单位是秒
    keepalive_timeout  65;

    #开启gzip压缩输出
    gzip  on;
    gzip_proxied any;
    #最小压缩文件大小
    gzip_min_length 1k;
    #压缩缓冲区
    gzip_buffers 4 16k;
    #压缩版本(默认1.1,前端如果是squid2.5请使用1.0
    #gzip_http_version 1.0;
    #压缩等级
    gzip_comp_level 6;
	#压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary on;
    gzip_disable "MSIE [1-6].";

    #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。
    fastcgi_connect_timeout 600;
    fastcgi_send_timeout 600;
    fastcgi_read_timeout 600;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 16 128k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    fastcgi_cache_valid 200 302 1h;
    fastcgi_cache_valid 301 1d;
    fastcgi_cache_valid any 1m;
    fastcgi_cache_min_uses 1;
    fastcgi_cache_use_stale error timeout invalid_header http_500;

    #反向代理配置
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
	#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #允许客户端请求的最大单文件字节数
    client_max_body_size 100m;
    #缓冲区代理缓冲用户端请求的最大字节数
    client_body_buffer_size 256k;
    #nginx跟后端服务器连接超时时间(代理连接超时)
    proxy_connect_timeout 600;
    #后端服务器数据回传时间(代理发送超时)
    proxy_send_timeout 180;
    #连接成功后,后端服务器响应时间(代理接收超时)
    proxy_read_timeout 600;
    #设置代理服务器(nginx)保存用户头信息的缓冲区大小
    proxy_buffer_size 16k;
    #proxy_buffers缓冲区,网页平均在32k以下的设置
    proxy_buffers 4 32k;
    #高负荷下缓冲大小(proxy_buffers*2)
    proxy_busy_buffers_size 64k;
    #设定缓存文件夹大小,大于这个值,将从upstream服务器传
    proxy_temp_file_write_size 128k;
    proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
    proxy_max_temp_file_size 256m;
    add_header Vary Accept-Encoding;

    #设定负载均衡的服务器列表
    upstream tomcat8080 {
        server 127.0.0.1:8080 weight=1;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        # location / {
        #     root   html;
        #     index  index.html index.htm;
        # }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ .php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /.ht {
        #    deny  all;
        #}

        location / {
            proxy_pass http://tomcat8080;
			
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            client_max_body_size       100m;
        }
    }


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

修改配置后,重载nginx配置

systemctl reload nginx

直接通过80端口,可以访问Tomcat了

到这里,Java Web项目部署涉及的环境基本部署好了。下期,我们创建一个简易的Java Web项目,部署到当前的环境中。

展开阅读全文

页面更新:2024-04-20

标签:缓冲区   负载   缓存   进程   大小   环境   服务器   时间   项目   内容   日志

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top