前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用CentOS,Nginx部署前端项目

使用CentOS,Nginx部署前端项目

原创
作者头像
用户6833524
修改2021-07-15 14:26:58
1.3K0
修改2021-07-15 14:26:58
举报
文章被收录于专栏:前端练习前端练习前端练习

登录服务器

在 git-bash 中:

$ ssh root@云服务器公网ip地址
$ # 输入密码

一些常用的 linux 命令:

$ cd # 改变目录
$ ls -la # 列出目录中的资源
$ pwd # 列出当前目录的完整路径
$ rm -rf node_modules/ # 删除指定资源
$ cat filename # 查看文件内容
$ vi filename # 编辑文件

安装软件:

nginx、nodejs、git

$ yum install git # 需要手动选择 y 继续安装
$ yum install nginx -y
$ yum install nodejs -y

克隆中央仓库代码到云服务器

$ git clone xxxx/repo.git

安装依赖

$ npm i

开发环境测试

$ npm run serve

此时云服务器中运行了 webpack-dev-server 的服务器,默认访问 http://公网ip:8080 可查看浏览 器中的效果。注意, 8080 端口需要到云服务器实例的安全组中开放该端口的访问。

生产环境测试

$ npm run build # 打包,生成在生产环境中使用的 dist 目录

进入 dist 目录,可结合 pm2 加 http-server 来发布部署。

nginx 使用

测试配置文件是否正确,启动服务器

$ nginx -t # 测试配置文件是否正确
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$ nginx -c /etc/nginx/nginx.conf # 设置配置文件
$ nginx -s reload # 重启

在 /etc/nginx 目录下是 nginx 服务器相关的资源, nginx.conf 是默认的配置文件:

# 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 / {
}
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 {
# }
# }
}

通常将自己项目部署的配置文件放置到 conf.d 目录下:

$ vi xxx.conf

新建一个 任意名称.conf 的文件,在打开的界面中,按 i 键插入代码:

server {
listen 8080; # 监听的端口,改成自己的端口
root /web/dist; # 部署项目的根目录,改成自己的目录
location / { # vue-router 使用 history 模式时需要添加
try_files $uri $uri/ /index.html;
}
location /xiongmao/ { # 反向代理,相当于在 vue.config.js 中代理的前缀,根据需要修改
rewrite ^/xiongmao/(.*)$ /$1 break; # 相当于在 vue.config.js 中代理的pathRewrite
proxy_pass https://xiongmaoyouxuan.com; # 相当于在 vue.config.js 中代理的 target
}
}

配置文件编写好之后,按 esc 退出编辑模式,再按 : 后输入 wq 保存并退出文件

继续:

$ nginx -c /etc/nginx/nginx.conf

如果此时提示端口被占用,可先停止 nginx 服务器,再重启:

$ nginx -s stop # 停止服务器
$ nginx -c /etc/nginx/nginx.conf
$ nginx -s reload

再访问 http://公网ip:8080 即可

部署过程中遇到的问题

以上流程可以将服务部署到8080端口,但是如果想要部署到80端口,直接使用域名访问,需要在自建的.conf文件中监听80端口。

但是如果直接填写80端口,会遇到两种情况:

  • 部署无报错,但是直接访问ip依然显示的是nginx默认页面
  • nginx报错nginx: [error] open() “/run/nginx.pid” failed (2: No such file or directory)

解决方式

运行nginx -c /etc/nginx/nginx.conf,再运行nginx -s reload

注意必须要使用绝对位置(etc前加/),不然还是会报错。

解决了上面的报错之后,访问ip依然是nginx默认页面,这时候可以进入/etc/nginx目录,使用vi 文件名 命令编辑nginx.conf文件,将serverl处的listen 80修改为其他值,两处都要修改。修改完成之后保存退出,重启服务器即可。

以上两个步骤完成之后,如尝试ip直接访问无问题,DNS解析域名到对于IP,就可以直接使用域名来访问前端页面了。 VUEdemo地址:http://hd.bycat.cn

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 登录服务器
    • 在 git-bash 中:
      • 一些常用的 linux 命令:
      • 安装软件:
        • nginx、nodejs、git
          • 克隆中央仓库代码到云服务器
            • 安装依赖
              • 开发环境测试
                • 生产环境测试
                • nginx 使用
                  • 测试配置文件是否正确,启动服务器
                    • 通常将自己项目部署的配置文件放置到 conf.d 目录下:
                      • 新建一个 任意名称.conf 的文件,在打开的界面中,按 i 键插入代码:
                        • 继续:
                        • 部署过程中遇到的问题
                          • 解决方式
                          领券
                          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档