【创建个人服务器】五.项目部署

目录

【创建个人服务器】一.安装软件-数据库
【创建个人服务器】二.创建一个Springboot项目
【创建个人服务器】三.部署springboot
【创建个人服务器】四.第一个应用:文档合并
【创建个人服务器】五.项目部署

1.前端部署

安装Nginx

1)下载安装

打开服务器终端,在终端中输入以下命令,下载nginx安装包。

wget http://nginx.org/download/nginx-1.20.2.tar.gz

其中nginx版本可以自己选择,具体版本可查看此链接:http://nginx.org/
将下载的压缩包解压,输入指令:

tar -zvxf nginx-1.20.2.tar.gz
cd nginx-1.20.2

2)安装nginx

./configure --with-http_ssl_module --with-http_gzip_static_module

【错误】错误为:./configure: error: the HTTP rewrite module requires the PCRE library.
解决方案:安装 pcre-devel

 yum -y install pcre-devel

【错误】如果之前没有安装openssl,需要在执行./configure命令之前安装

yum -y install openssl openssl-devel make zlib zlib-devel gcc gcc-c++ libtool    pcre pcre-devel
make
make install

3)启动程序

进入目录,启动nginx

cd /usr/local/nginx/sbin/

./nginx

4)其他命令

查看nginx运行状态:
ps aux | grep nginx

停止运行:
./nginx -s stop

查看版本:
./nginx -v

检查配置文件:
./nginx -t

5)配置nginx

在将dist文件传输至服务器以后,需要对nginx进行配置。
在服务器中找到/usr/local/nginx/conf/nginx.conf文件,打开nginx.conf文件修改以下内容:

1.修改服务器端口
listen默认端口为8080,因为个人需求所以改为了8082,server_name填写localhost即可。

server {
    listen       8081;
    server_name  localhost;

2.修改dist存放路径
root改为dist文件存放的路径,添加一行try_files $uri $uri/ @router;,防止刷新页面出现404。

location / {
    root   /home/xj/dist;
    index  index.html index.htm;
    try_files $uri $uri/ @router;
}

3.完整配置文件
nginx.conf完整内容如下:

#user  root;
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 {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8082;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /home/xj/dist;
            index  index.html index.htm;
            try_files $uri $uri/ @router;
        }
        location @router {
	    rewrite ^.*$ /index.html last;
	}

        #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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # 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;
    #    }
    #}

}

6)进入界面和项目更新

1.进入界面
配置完成后需要重新启动以下nginx,可以用如下指令:

nginx -s stop
cd /usr/local/nginx/sbin/
./nginx

#或者

nginx -s reload

重启完以后在浏览器中输入服务器IP:端口即可访问界面。

2.项目更新
如果后续前端项目文件需要更新,则再次生成dist文件,将新的dist文件替换至服务器文件夹相同位置中,无需重启nginx。
若发现更新后前端界面无变化,可重启nginx后再次进入界面查看。

posted @ 2022-09-23 23:23  Raiyon  阅读(54)  评论(0编辑  收藏  举报