序言
一篇简单的介绍部署前端项目的过程
正文
首先我们需要进行环境搭建,主要就是服务器和nginx配置。
- 服务器
如果有服务器的直接可以通过远程工具远程到服务器,可以用 xshell 软件进行远程连接。
若没有服务器的可以通过 VMWare软件 搭建一个虚拟机来实现,下载完之后还需要一个镜像文件,我一般下载centos(推荐一个镜像iso文件下载地址:阿里云官方镜像站:http://mirrors.aliyun.com)。进行网络配置,将打包好的前端资源部署到服务器。
虚拟机指通过软件模拟的具有完整硬件系统功能的、运行在一个完全隔离环境中的完整计算机系统,在实体计算机中能够完成的工作在虚拟机中都能够实现。(注:记得用户名和密码)
- nginx配置
执行以下命令安装nginx
sudo yum install epel-release
sudo yum install nginx
启动nginx
sudo systemctl start nginx
启动完之后我们需要更改配置文件,配置文件路径在 etc/nginx/nginx.conf,主要配置是location,路径配置对应的前端项目路径,我们需要将其映射到我们的前端资源
# 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_format main '$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.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
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;
listen [::]:80;
server_name localhost;
location / {
root /var/www/test/;
index index.html index.htm;
}
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
修改完之后重启nginx
sudo systemctl restart nginx
最后主机通过浏览器就能访问到虚拟机ip
还有很多部署方案,比如用docker安装nginx镜像,部署的更快,也不用安装虚拟机这么大,或者将项目构建成镜像,用docker直接运行。大家也可以了解一下。
在其他大佬评论区看见的一段话,共勉。
一般生产环境,起码要解决几个问题:
1、单点故障,就是我一台物理机突然崩掉,如何让应用还能正常跑(k8s的deployment了解一下)
2、热更新,比如我要update我的应用,如何做到用户无感知更新。(k8s的deployment的热更新了解一下)
3、时光回溯,比如上的新版本出问题了,如何回退
4、负载均衡(用的nginx,没啥问题)
5、自动扩缩容,比如流量突然增大,如何自动扩容
6、数据库部署,数据库部署不建议上k8,本地部署个比如mysql集群,或者直接上云,所以还需要具备基本的数据运维能力,比如日志收集(ELK,EFK),性能监控等等
简单说上面几个才能上生产,多说一嘴,自动化pipeline,也就是jenkins,gitlab,k8s。。。那一套组合拳搞好了,不用多么厉害,中级水平,基本上在中小型公司横着走了