Java项⽬部署到服务器-Nginx
(欢迎访问我的个⼈博客,现开放注册)
Java项⽬部署到服务器-Nginx
打包
设置l中bulid标签的finalName,设置为⼯程名,eg:<finalName>comi_single_blog</finalName>
使⽤idea的侧边栏的maven快捷命令1.maven-clean 2.maven-install
随后在⼯程⽂件下下出现target⽂件夹,其中*.war就是我们已经打包好了的程序
服务器配置(为了⽅便使⽤宝塔⾯板)
1. 安装nginx(为什么使⽤nginx?由于我的其他项⽬是由nginx来配置的,如laravel博客,⼩程序后台,所以不可能为了⼀个java项
⽬⽽去重新配置另外的项⽬,最好能兼容。)
2. 安装tomcat(本地是tomcat8,所以服务器也是tomcat8)
⾮宝塔安装(tomcat8配置(⽆需修改8080端⼝))
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener SSLEngine="on" className="org.AprLifecycleListener" />
<Listener className="org.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFac tory" name="UserDatabase" pathname="l" type="org.apache.catalina.UserDatabase" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine defaultHost="localhost" name="Catalina">
<Realm className="org.alm.LockOutRealm">
<Realm className="org.alm.UserDatabaseRealm" resourceName="UserDatabase" />
</Realm>
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_ac cess_log" suffix=".txt" />
</Host>
<Host appBase="/www/server/tomcat/webapps/comi_single_blog" autoDeploy="true" name="myBlog.12zw.club" unpackWARs="true" xmlNamespaceA ware="false" xmlValidation="false">
<Context crossContext="true" docBase="/www/server/tomcat/webapps/comi_single_blog" path="" reloadable="true" />
</Host>
</Engine>
</Service>
</Server>
修改完配置⽂件后,重启tomcat服务
上传war包⾄tomcat-webapps下,tomcat便会多出⼀个与war⼀样的⽂件夹,此时不要删除war⽂件,否者⽆法访问站点。
3.修改⽹站⽬录为webapp下的war⽂件夹
4.添加站点后,在站点设置页⾯设置⽹站⽬录和运⾏⽬录(WEB-INF)
5.启动tomcat
⾮宝塔配置
nginx配置⽂件
server
{
listen 80;
listen 80;
listen 443 ssl http2;
server_name www.baidu;  <<<<------
index index.jsp index.html index.htm default.php default.htm default.html;
root /www/server/tomcat/webapps/baidu/WEB-INF;
#SSL-START SSL相关配置,请勿删除或修改下⼀⾏带注释的404规则
#error_page 404/404.html;
ssl_certificate    /etc/letsencrypt/live/www.baidub/fullchain.pem;
ssl_certificate_key    /etc/letsencrypt/live/www.baidu/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;    ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
error_page 497  $host$request_uri;
#SSL-END
#ERROR-PAGE-START  错误页配置,可以注释、删除或修改
error_page 404 /404.html;
error_page 502 /502.html;
#ERROR-PAGE-END
#PHP-INFO-START  PHP引⽤配置,可以注释或修改
#TOMCAT-START
location /
{
proxy_pass "www.baidu:8080";
proxy_set_header Host www.baidu;
nginx部署前端项目proxy_set_header X-Forwarded-For $remote_addr;
}
location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
expires      12h;
}
location ~ .*\.war$
{
return 404;
}
#TOMCAT-END
f;
#PHP-INFO-END
#REWRITE-START URL重写规则引⽤,修改后将导致⾯板设置的伪静态规则失效    include /www/server/panel/vhost/rewrite/f;
#REWRITE-END
#禁⽌访问的⽂件或⽬录
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
{
return 404;
}
#⼀键申请SSL证书验证⽬录相关设置
location ~ \.well-known{
allow all;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires      30d;
error_log off;
access_log off;
}
}
location ~ .*\.(js|css)?$
{
expires      12h;
error_log off;
access_log off;
}
access_log  /www/wwwlogs/www.baidu.log;
error_log  /www/wwwlogs/log; }
接下来便可访问站点 END

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。