ubuntu中利用nginx部署vue項目的完整步驟

1.安裝nginx

更新源列表

apt-get update

安裝nginx

apt-get install nginx

檢查nginx是否安裝,輸入如下命令後若出現版本號則安裝成功

nginx -v

啟動nginx

server nginx restart

在瀏覽器輸入ip地址,若出現如下頁面則啟動成功

2. 打包上傳vue項目到服務器

打包

我的項目使用的是vs code,在終端輸入如下命令進行打包

npm run build

上傳

打包完成後會有dist文件,該文件為打包完成後的項目,該文件中有index.html和static兩個內容。

將該dist文件上傳到服務器的某個位置即可

我上傳到/home/ubuntu文件中

配置nginx

修改nginx.conf

安裝nginx後,nginx的默認目錄是/etc/nginx

在該目錄中有nginx.conf文件,輸入如下命令,使用vi打開該文件

vi nginx.conf

我的nginx.conf文件原內容如下

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模塊中加入如下內容,表示配置文件要引用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;
#	}
#}

創建*.host文件

在/etc/nginx中創建hosts文件夾

mkdir hosts

在host文件中創建syt.host文件,文件名隨便命名

在文件中添加如下內容

server {
        listen       8080;#自己設置端口號
        server_name  syt;#自己設置項目名稱
        #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即可進行訪問

常見錯誤

瀏覽器訪問時顯示403

這個問題有多種原因,我當時遇到的原因是該項目所在的文件沒有權限訪問。我的項目所在文件是/home/ububtu/dist

使用如下命令保證可以訪問(比較暴力qaq)

chmod -R 777 home
chmod -R 777 ubuntu
chmod -R 777 dist

總結

到此這篇關於ubuntu中利用nginx部署vue項目的文章就介紹到這瞭,更多相關ubuntu用nginx部署vue項目內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: