Centos+Nginx部署Vue项目

 1.项目打包生成dist文件夹

在项目根目录下打开cmd窗口,输入命令

npm run build
//生成dist文件夹

2.将dist文件夹上传到centos7

使用scp命令或者用远程连接工具将打包好的dist文件夹上传到服务器或者虚拟机的某个位置。

打开cmd命令输入:
scp -r 22 E:\\dist root@192.168.3.5:/home/dist
输入root 用户密码,等待上传成功即可
​
E:\dist 为dist所在目录
root 为虚拟机root用户
192.168.3.5 为虚拟机ip
/home/ 为将dist文件上传虚拟机上home目录

3.安装nginx

推荐链接:
https://www.cnblogs.com/kaid/p/7640723.html

4.nginx的几个常用命令

//进入  /sbin目录下
cd /usr/local/nginx/sbin
//启动nginx
./nginx
//检查更改的配置
./nginx -t
//重新加载
./nginx -s reload
//停止nginx
./nginx -s stop

5.修改nginx的conf配置

//进入conf
cd /usr/local/nginx/conf
vim nginx.conf

nginx.conf这个是全部的配置文件,可以复制下来参考。

#user  nobody;
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       80;
        server_name  192.168.111.128;
        index  index.html index.htm;
        root   /app/fusionwork_deployment/web;
        #charset koi8-r;
​
        #access_log  logs/host.access.log  main;
​
        #location / {
        #    root   html;
        #    index  index.html index.htm;
        #}
​
        #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.将端口的防火墙关掉

//8088,自己指定端口
firewall-cmd --permanent --add-port=8880/tcp --zone=public  
//重新加载
firewall-cmd --reload

7.重新加载配置,启动nginx

//检查配置是否正确
./nginx -t
//启动nginx
./nginx

8.浏览器访问本机虚拟机ip

http://192.168.111.128

9.路径重写rewrite

可以看下这个,写的比较详细
https://blog.csdn.net/hotlinhao/article/details/88355125
http://www.pianshen.com/article/9593257962/
原文地址:https://www.cnblogs.com/cherylgi/p/13343400.html