react项目部署到linux服务器

该文章详细记录了React项目从打包到服务器部署的过程,包括使用yarn打包,将build文件上传至服务器的指定目录,配置Nginx代理以处理HTTP和HTTPS请求,设置SSL证书确保安全连接,并重启nginx服务以使改动生效。
摘要由CSDN通过智能技术生成

react部署笔记

1.1打包项目

#安装yarn
npm install -g yarn
#打包项目
yarn build

1.2 将打包好的build 推到服务器

# cd到前端web目录
cd /usr/local/web/lwh
#build文件夹放到这路径下

1.3配置Nginx代理配置

修改nginx.conf文件

#文件nginx.conf
# nginx的配置文件位置
cd /usr/local/nginx/conf
# 编辑nginx文件
vim nginx.conf
#添加如下内容,xxx为你的项目名字
include confile/xxx.conf;

创建xxx.conf 对应项目nginx配置文件

cd confile
#创建xxx项目nginx配置文件
mkdir xxx.conf
# 编辑xx.conf配置文件
vim xxx.conf

xxx.conf文件内容

server{
    listen 80;
    server_name lwhtimepill.leiweihao.ltd;
    rewrite ^(.*)$ https://${server_name}$1 permanent;
}
server{
    listen 443;
    server_name lwhtimepill.leiweihao.ltd;
    ssl     on;
    ssl_certificate /usr/local/nginx/conf/cert/lwhtimepill.leiweihao.ltd_nginx/lwhtimepill.leiweihao.ltd_bundle.pem;
    ssl_certificate_key /usr/local/nginx/conf/cert/lwhtimepill.leiweihao.ltd_nginx/lwhtimepill.leiweihao.ltd.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    location / {
        proxy_pass http://127.0.0.1:21010; 
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host                    $http_host;
    }
}
server{
    listen 21010; 
    location /{
        root /usr/local/web/lwh/timepill/build;
        try_files $uri /index.html;
    }
    error_page  404              /404.html;
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
    location ~/api/(.*){
        proxy_pass http://localhost:21011;
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
        add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
        proxy_set_header           Host $host;
        proxy_set_header           X-Real-IP $remote_addr;
        proxy_set_header           X-Forwarded-For $proxy_add_x_forwarded_for;
        rewrite  ^/api/(.*)  /$1 break;
    }
}

1.4域名申请ssl证书,下载到服务器

>>> cd /usr/local/nginx/conf/cert/lwhtimepill.leiweihao.ltd_nginx
>>> ls
lwhtimepill.leiweihao.ltd_bundle.crt  lwhtimepill.leiweihao.ltd_bundle.pem  lwhtimepill.leiweihao.ltd.csr  lwhtimepill.leiweihao.ltd.key

1.5重启nginx

#重启nginx
nginx -s reload

1.6配置完成,网站正常访问

https://lwhtimepill.leiweihao.ltd/home

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
依前后分离版Linux部署是一种将前端和后端的服务分开部署的方式。通常情况下,前端指的是用户界面,可以是网页、移动应用或者桌面应用,而后端则是用户界面背后的服务器端应用程序。 在这种部署方式中,前端和后端可以独立部署和运行,它们之间通过网络通信进行交互。这种分离的部署方式更容易实现各自的扩展和升级,有效降低了系统的耦合度,提高了系统的可维护性和灵活性。 首先,我们需要选择合适的前端技术,比如React.js、Vue.js等,根据项目的需求和规模来选择合适的技术栈。 接着,我们可以使用Nginx或者Apache等web服务器部署前端服务,同时配置反向代理将请求转发到后端服务的API接口。 对于后端服务,我们可以选择合适的后端框架进行开发,如Spring Boot、Express.js等,根据业务需求来构建相应的后端逻辑。 最后,我们需要将后端服务部署到独立的服务器上,并且配置数据库、缓存等其他相关的服务。通过这种方式,我们可以实现前后端分离的部署,使得系统更加灵活和可维护。 在整个部署过程中,我们还需要考虑系统的监控、日志记录、错误处理等一系列运维工作,确保系统的稳定和安全。通过前后分离版Linux部署,我们可以更好地实现系统的模块化和分布式部署,提高系统的性能和可扩展性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值