ubuntu中利用nginx部署vue項目的完整步驟
1.安裝nginx
更新源列表
apt-get update
安裝nginx
apt-get install nginx
檢查nginx是否安裝,輸入如下命令后若出現(xiàn)版本號則安裝成功
nginx -v
啟動nginx
server nginx restart
在瀏覽器輸入ip地址,若出現(xiàn)如下頁面則啟動成功
2. 打包上傳vue項目到服務(wù)器
打包
我的項目使用的是vs code,在終端輸入如下命令進(jìn)行打包
npm run build
上傳
打包完成后會有dist文件,該文件為打包完成后的項目,該文件中有index.html和static兩個內(nèi)容。
將該dist文件上傳到服務(wù)器的某個位置即可
我上傳到/home/ubuntu文件中
配置nginx
修改nginx.conf
安裝nginx后,nginx的默認(rèn)目錄是/etc/nginx
在該目錄中有nginx.conf文件,輸入如下命令,使用vi打開該文件
vi nginx.conf
我的nginx.conf文件原內(nèi)容如下
user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; events { worker_connections 768; # multi_accept on; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # SSL Settings ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } #mail { # # See sample authentication script at: # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript # # # auth_http localhost/auth.php; # # pop3_capabilities "TOP" "USER"; # # imap_capabilities "IMAP4rev1" "UIDPLUS"; # # server { # listen localhost:110; # protocol pop3; # proxy on; # } # # server { # listen localhost:143; # protocol imap; # proxy on; # } #}
在http模塊中加入如下內(nèi)容,表示配置文件要引用hosts文件夾下的host后綴的文件。該host后綴文件就是用來配置vue項目的,一個host文件配置一個vue項目
include /etc/nginx/hosts/*.host;
修改后文件如下
user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; events { worker_connections 768; # multi_accept on; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # SSL Settings ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; include /etc/nginx/hosts/*.host;#新添加的一行 } #mail { # # See sample authentication script at: # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript # # # auth_http localhost/auth.php; # # pop3_capabilities "TOP" "USER"; # # imap_capabilities "IMAP4rev1" "UIDPLUS"; # # server { # listen localhost:110; # protocol pop3; # proxy on; # } # # server { # listen localhost:143; # protocol imap; # proxy on; # } #}
創(chuàng)建*.host文件
在/etc/nginx中創(chuàng)建hosts文件夾
mkdir hosts
在host文件中創(chuàng)建syt.host文件,文件名隨便命名
在文件中添加如下內(nèi)容
server { listen 8080;#自己設(shè)置端口號 server_name syt;#自己設(shè)置項目名稱 #access_log logs/host.access.log main; location / { root /home/ubuntu/dist;#這里寫vue項目的所在地址 index index.html;#這里是vue項目的首頁,需要保證dist中有index.html文件 } error_page 500 502 503 504 /50x.html;#錯誤頁面 }
重啟nginx
nginx -s reload
訪問vue項目
ip:port/index.html即可進(jìn)行訪問
常見錯誤
瀏覽器訪問時顯示403
這個問題有多種原因,我當(dāng)時遇到的原因是該項目所在的文件沒有權(quán)限訪問。我的項目所在文件是/home/ububtu/dist
使用如下命令保證可以訪問(比較暴力qaq)
chmod -R 777 home
chmod -R 777 ubuntu
chmod -R 777 dist
總結(jié)
到此這篇關(guān)于ubuntu中利用nginx部署vue項目的文章就介紹到這了,更多相關(guān)ubuntu用nginx部署vue項目內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue使用ElemenUI對table的指定列進(jìn)行合算的方法
這篇文章主要介紹了Vue使用ElemenUI對table的指定列進(jìn)行合算的方法,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03vue+elementUI動態(tài)增加表單項并添加驗證的代碼詳解
這篇文章主要介紹了vue+elementUI動態(tài)增加表單項并添加驗證的代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12vue項目前端微信JSAPI與外部H5支付相關(guān)實(shí)現(xiàn)過程及常見問題
這篇文章主要介紹了vue項目前端微信JSAPI與外部H5支付相關(guān)實(shí)現(xiàn)過程及常見問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Vue 引入AMap高德地圖的實(shí)現(xiàn)代碼
這篇文章主要介紹了Vue 引入AMap高德地圖的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04vue3配置全局參數(shù)(掛載全局方法)以及組件的使用
這篇文章主要介紹了vue3配置全局參數(shù)(掛載全局方法)以及組件的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07