- 这篇文章只涉及到前端页面的部署,不涉及后端部署及域名映射等。
- 本文虽然涉及到部分linux指令,但是并不难,基本照着操作就能部署成功,涉及到代码的我也会贴出来,供大家复制粘贴。
- 云服务器推荐购买阿里云,腾讯云或者京东云,新用户首次购买都会有免费试用的机会,一般为一周或一个月。
- 我这里试用的是腾讯云,新用户可以免费试用云服务器7天。
一. 预装镜像选择 centOs 7.6,其它的不变
二. 购买完成之后,重置密码(密码一定要记住,远程服务器的时候会用到)
三. 新建安全组,模板选择“放通22,80,443,3389端口和ICMP协议”,这样后面才能通过SSH远程连接到服务器和正常访问你的前端页面
四. 回到你的实例页面,点击配置安全组
**
**
**
**
五. 现在可以通过SSH连接到你的服务器啦,这里推荐使用FinalShell软件连接,方便上传你的前端文件,
打开FinalShell,点击新建SSH连接。
六. 连接SSH: 名称随便起,主机填你服务器的公网ip,端口默认是22不用管,用户名填root密码就是你购买服务器后重置的密码;
**
**
**
**
**
**
七. 安装nginx
1. 输入以下指令,然后按回车。(使用vim编辑器编辑nginx.repo)
vim /etc/yum.repos.d/nginx.repo
2. 按i进入编辑模式,底部出现“插入”则说明成功进入编辑模式
3. 复制以下代码,粘贴进去(注意别用ctrl+v粘贴,在finalshell里点击鼠标右键,然后选择粘贴)
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
4. 注意每行开头不能有空格,否则可能会报错(vim编辑器不适用左键改变光标位置,可以通过上下左右控制)
5. 复制完确认没问题后,点击键盘上的esc退出编辑模式,然后输入:wq保存并退出 Vim 编辑器(注意输入法一定要是英文模式)
6. 输入指令安装nginx
yum install -y nginx
7. 安装完毕之后查看nginx状态
systemctl status nginx
8. 上面这个说明nginx已经安装成功,但是未启动,现在启动一下nginx,启动后再次查看nginx状态,出现active (running)说明启动成功
systemctl start nginx
9. 查看80端口是否被nginx占用,然后在浏览器地址栏直接访问你的公网ip地址,如果出现nginx提示,则说明nginx已经配置OK。
10. 在你的电脑上新建一个文本文档,重命名为nginx.conf,用记事本打开,然后把以下内容粘贴进去
重点部分就是
location / {
root /usr/local/dist;
index index.html;
}
root配置根目录,index配置入口文件(打包好的文件需要放到usr下的local文件夹内,我这边前端打包生成的文件是dist,所以root配置为/usr/local/dist)
# 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 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_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root /usr/local/dist;
index index.html;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.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_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# 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 = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
11. 进入/etc/nginx目录
cd /etc/nginx
12. 将目录下原有的nginx.conf拷贝一份(注意刷新一下才会显示)
cp nginx.conf nginxCopy.conf
13. 删除目录下的nginx.conf,然后将本地电脑上的nginx.conf上传到目录下。
rm -f nginx.conf
八. 将前端文件打包并放到服务器
npm run build打包生成dist文件。
cd /usr/local
进入/usr/local后将dist拖入
重新加载nginx配置文件
/usr/sbin/nginx -s reload
然后再次访问服务器的公网ip,就可以访问到前端页面啦!