前戲
假設你這邊已經寫好了一個vue的項目,並且本地運行也是沒有問題的,現在我們就可以部署到服務器上了,我們使用nginx進行部署
Nginx 是一個高性能的 Web 和反向代理服務器, 它具有有很多非常優越的特性:
作為 Web 服務器:相比 Apache,Nginx 使用更少的資源,支持更多的並發連接,體現更高的效率,這點使Nginx 尤其受到虛擬主機提供商的歡迎。能夠支持高達 50,000 個並發連接數的響應. 更好的處理靜態文件Nginx 安裝非常的簡單,配置文件 非常簡潔,Bug非常少的服務器: Nginx 啟動特別容易,並且幾乎可以做到7*24不間斷運行,即使運行數個月也不需要重新啟動
這是我開發的項目目錄結構
打包
在項目根目錄下,執行打包命令
npm run build
打包完成會在項目根目錄下生成一個dist的目錄,我們可以命名為zz-mms
打包完成之后把zz-mms上傳到服務器
我是上傳在/opt目錄下
安裝啟動nginx,如何安裝和啟動,請移駕我nginx的博客
打開nginx的配置文件進行修改
[root@HH conf]# vim /opt/nginx1-16/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 localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root /opt/zz-mms; try_files $uri $uri/ /index.html; # 解決刷新報404的問題 index index.html index.htm; } location /pro-api{ # 解決跨域問題 proxy_pass http://mengxuegu.com:7300/mock/5db437d92aa750460d4fce18; } #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; # } #} }
其中的/pro-api是我們在項目的.env.production里面的變量
更改完重新加載配置文件
[root@HH sbin]# ./nginx -s reload
默認端口是80,訪問雲服務器的ip地址,如果訪問成功,則配置成功
如果沒有訪問成功,檢查雲服務器的端口是否開放