nginx+tomcat

CentOS7 Nginx源码安装

版本信息

CentOS 7.6.1810
Nginx 1.14.2

源码安装

1
2
3
4
5
6
7
8
9
10
$ tar zxf nginx-1.14.2.tar.gz
$ cd nginx-1.14.2
$ ./configure --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module
$ sudo make && sudo make install
# 报错 configure: error: You need a C++ compiler for C++ support
$ yum -y install gcc-c++
# 报错 configure: error: the HTTP rewrite module requires the PCRE library
$ yum -y install pcre-devel.x86_64
# 报configure: error: SSL modules require the OpenSSL library.
$ yum -y install openssl openssl-devel

配置nginx参数

1
2
3
$ vim /etc/nginx/nginx.conf  # (yum安装)

$ vim /usr/local/nginx/conf/nginx.conf #(源码方式安装)
1
2
3
4
5
6
7
8
9
# /usr/local/nginx/conf/nginx.conf
user root;
worker_processes 8;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

log_format loitlogs '"$http_x_forwarded_for" - $remote_user [$time_local]' '"$request_method ' '$host' '$request_uri' ' $server_protocol"' ' $status $body_bytes_sent "$http_referer" ' '"$http_user_agent"';

access_log /var/log/nginx/loit_access_log loitlogs
1
2
3
4
# 测试配置文件
$ /usr/sbin/nginx -t #(yum方式安装)

$ /usr/local/nginx/sbin/nginx -t #(源码方式安装)

源码方式安装添加系统服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# vim /lib/systemd/system/nginx.service

[Unit]
Description=nginx
After=network.target

[Service]
Type=oneshot
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s quit
ExecReload=/usr/local/nginx/sbin/nginx -s reload
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
1
2
# 开机自启
$ systemctl enable nginx

Tomcat安装配置

下载Tomcat压缩包
版本信息: apache-tomcat-7.0.93.zip

设置Tomcat系统服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# vim /lib/systemd/system/tomcat.service
[Unit]
Description=tomcat
After=network.target

[Service]
Type=oneshot
# tomcat 目录
ExecStart=/home/apache-tomcat-7.0.93/bin/startup.sh
ExecStop=/home/apache-tomcat-7.0.93/bin/shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

设置服务

1
2
3
4
5
6
7
$ systemctl start tomcat
# 查看服务状态
$ systemctl status tomcat
$ systemctl restart tomcat
# 开机启动
$ systemctl enable tomcat
$ systemctl stop tomcat

配置发布方案

nginx 站点配置

1
2
3
$ vim /etc/nginx/conf.d/default.conf 

$ vim /usr/local/nginx/conf/nginx.conf
1
2
3
4
5
6
# /usr/local/nginx/conf/nginx.conf

location / {
root /data/site/html; # 站点根目录
index index.html index.htm;
}

Tomcat 站点配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!--
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
-->
<!--
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs" prefix="localhost_access_log."
suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
</Host>
-->

# 添加:
# appBase -> /data/site 该目录下的子目录将自动被部署为应用 和 这个目录下的war文件将被自动解压并部署为应用
# docBase -> www 表示指向了某个应用的目录和appBase没有关系
<Host name="localhost" appBase="/data/site/" unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="www"></Context>
</Host>

Tomcatserver.xml配置appBase与docBase的用法

nginx转发请求至tomcat

1
2
3
4
5
location / {
root /data/site/html;
index index.html index.htm;
proxy_pass http://localhost:8080/;
}