登哥总冠军 發表於 2021-7-3 21:56:00

怎样在Linux服务器上部署自己的项目?(超详细)

<p># 系列文章目录</p>
<p>Linux操作系统笔记【超详细】<br>
<font color="black" face="微软雅黑" size="3"><br>
本篇文章主要从准备篇、项目开发、项目打包、项目部署四个部分去介绍如何把前后端分离的项目部署到阿里云服务器,在服务器上去玩自己的项目。<br>
项目最终效果图:输入ip即可访问!!!<br>
<img src="https://img-blog.csdnimg.cn/20210609162049908.png"></font></p><font color="black" face="微软雅黑" size="3">
<hr style="border: solid; width: 100px; height: 1px" color="#000000" size="1&quot;">
<p>@</p><div class="toc"><div class="toc-container-header">目录</div><ul><li>前言</li><li>一、准备篇</li><li>二、项目开发及调试</li><li>三、项目打包</li><li>四、项目部署</li><li>测试</li></ul></div><br>
</font><p></p>
<hr style="border: solid; width: 100px; height: 1px" color="#000000" size="1&quot;">
<h1 id="前言">前言</h1>
<p><font color="black" face="微软雅黑" size="3">部署的大概的步骤流程如下图:<br>
<img src="https://img-blog.csdnimg.cn/20210609161524142.png"></font></p><font color="black" face="微软雅黑" size="3">
<hr style="border: solid; width: 100px; height: 1px" color="#000000" size="1&quot;">
<h1 id="一准备篇">一、准备篇</h1>
<p><font color="black" face="微软雅黑" size="2">1、阿里云服务器(LinuxCentOS 7.3 64bit)<br>
2、安装Xshell、Xftp连接工具并成功连接服务器。<br>
3、在服务器上安装JDK、Mysql、Redis、Tomcat、Nginx等环境,并确保安装成功!<br>
4、Mysql安装成功后,使用Navicat连接工具连接服务器上的Mysql。<br>
5、Redis安装成功后,使用RedisDesktopManager连接工具连接服务器上的Reids。</font></p><font color="black" face="微软雅黑" size="2">
<h1 id="二项目开发及调试">二、项目开发及调试</h1>
<p><font color="black" face="微软雅黑" size="2">部署的项目是之前做的一个小项目,Vue+SpringBoot前后端分离的项目。<br>
1、执行SQL脚本,在服务器mysql上创建表。<br>
2、修改Mysql连接配置。修改配置文件application.yml中Mysql数据库url、username、password为你实际服务器上数据库配置。<br>
3、修改Redis的连接配置。修改Redis缓存的 host 、 password 等连接信息为你实际服务器上Redis配置。<br>
4、本地运行测试。启动前端、后端项目确保项目成功运行。</font></p><font color="black" face="微软雅黑" size="2">
<h1 id="三项目打包">三、项目打包</h1>
<p><font color="black" face="微软雅黑" size="2">1、前端项目构建打包。切换到项目根目录下,执行下面命令。</font></p><font color="black" face="微软雅黑" size="2">
<pre><code class="language-c">npm run build:prod
</code></pre>
<p><font color="black" face="微软雅黑" size="2">注:构建打包成功之后,会在根⽬录⽣成 <code>dist</code> ⽂件夹,⾥⾯就是构建打包好的前端项⽬⽂件!<br>
<font color="black" face="微软雅黑" size="2">2、后端项目构建打包。为了方便起见,Spring Boot 由于自带 Tomcat 应用服务器,项目默认会打包为可执行的 jar 包。<br>
切换到项目的根目录,执行 <code>mvn package</code> 命令即可构建打包。构建打包完成并可执行的 <code>jar 包</code>位于<code>target</code>文件夹。</font></font></p><font color="black" face="微软雅黑" size="2"><font color="black" face="微软雅黑" size="2">
<h1 id="四项目部署">四、项目部署</h1>
<p><font color="black" face="微软雅黑" size="2">1、前端部署。使用Xftp工具将前端打包完成的dist文件夹,上传至服务器的<code>/usr/local/web</code>文件夹下。<br>
修改Nginx的配置文件<code>nginx.conf</code>。位于目录<code>/etc/nginx</code><br>
<img src="https://img-blog.csdnimg.cn/20210609203552741.png"><br>
修改配置如下:</font></p><font color="black" face="微软雅黑" size="2">
<pre><code class="language-java"># For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid/run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log/var/log/nginx/access.logmain;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type      application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
      listen       80 default_server;
      listen       [::]:80 default_server;
      server_name118.31.187.5;
   #root         /usr/share/nginx/html;

      # Load configuration files for the default server block.
      include /etc/nginx/default.d/*.conf;

      location / {
         root   /usr/local/web/dist;
         try_files $uri $uri/ /index.html;
         indexindex.html;
      }
      location /prod-api/ {# 反向代理到后端工程
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://localhost:8088/;
      }


   #   error_page 404 /404.html;
    #   location = /404.html {
    #    }

      error_page 500 502 503 504 /50x.html;
      location = /50x.html {
            root html;
      }
    }

# Settings for a TLS enabled server.
#
#    server {
#      listen       443 ssl http2 default_server;
#      listen       [::]:443 ssl http2 default_server;
#      server_name_;
#      root         /usr/share/nginx/html;
#
#      ssl_certificate "/etc/pki/nginx/server.crt";
#      ssl_certificate_key "/etc/pki/nginx/private/server.key";
#      ssl_session_cache shared:SSL:1m;
#      ssl_session_timeout10m;
#      ssl_ciphers HIGH:!aNULL:!MD5;
#      ssl_prefer_server_ciphers on;
#
#      # Load configuration files for the default server block.
#      include /etc/nginx/default.d/*.conf;
#
#      location / {
#      }
#
#      error_page 404 /404.html;
#      location = /404.html {
#      }
#
#      error_page 500 502 503 504 /50x.html;
#      location = /50x.html {
#      }
#    }

}

</code></pre>
<p><font color="black" face="微软雅黑" size="2">改动的地方如下:</font></p><font color="black" face="微软雅黑" size="2">
<pre><code class="language-c">location / {
         root   /usr/local/web/dist;
         try_files $uri $uri/ /index.html;
         indexindex.html;
      }
</code></pre>
<pre><code class="language-c"> location /prod-api/ {# 反向代理到后端工程
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://localhost:8088/;
      }
</code></pre>
<p><font color="black" face="微软雅黑" size="2">执行如下命令,重新加载 Nginx 使其生效。</font></p><font color="black" face="微软雅黑" size="2">
<pre><code class="language-c">nginx -s reload
</code></pre>
<p><font color="black" face="微软雅黑" size="2">2、后端部署。使用Xftp工具将打包完成的<code>jar包</code>,上传至服务器的<code>/usr/local/web</code>目录下面。<br>
使用后台的方式启动后端工程。</font></p><font color="black" face="微软雅黑" size="2">
<pre><code class="language-c">nohup java -jar xx_web.jar &gt;/dev/null 2&gt;&amp;1 &amp;
</code></pre>
<p><font color="black" face="微软雅黑" size="2">注:阿里云服务器需要配置安全组,并支持端口访问;比如80,8080,3306、6379端口等。</font></p><font color="black" face="微软雅黑" size="2">
<hr style="border: solid; width: 100px; height: 1px" color="#000000" size="1&quot;">
<h1 id="测试">测试</h1>
<p><font color="black" face="微软雅黑" size="2">在浏览器的地址栏中,访问IP, 即可进入后台管理系统!!!</font></p><font color="black" face="微软雅黑" size="2">
</font></font></font></font></font></font></font></font></font></font></font></font><br><br>
来源:https://www.cnblogs.com/xxkfz/p/14967705.html
頁: [1]
查看完整版本: 怎样在Linux服务器上部署自己的项目?(超详细)