1. 程式人生 > 實用技巧 >前端專案部署到伺服器

前端專案部署到伺服器

1.安裝編譯工具及庫檔案

# yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre-devel

gcc、gcc-c++ # 主要用來進行編譯相關使用

openssl、openssl-devel # 一般當配置https服務的時候就需要這個了

zlib、zlib-devel # 主要用於檔案的解壓縮

pcre、pcre-devel # Nginx的rewrite模組和HTTP核心模組會用到PCRE正則表示式語法

make # 遍歷

make install # 安裝

2.建立nginx目錄

1)進入/usr/local目錄下

# cd /usr/local

2)建立nginx檔案

# mkdir nginx

3.下載並解壓nginx

1)進入nginx檔案下

#cd /usr/local/nginx

2)下載nginx安裝包

# wget https://nginx.org/download/nginx-1.18.0.tar.gz

3)解壓

# tar -zxvf nginx-1.18.0.tar.gz

4.進入安裝包目錄

# cd /usr/local/nginx-1.18.0

5.編譯安裝nginx,預設安裝到 /usr/local/nginx中

./configure

make && make install

6.進入到/usr/local/nginx/sbin目錄,啟動nginx

# ./nginx

7.檢視啟動的 nginx 程序

ps -ef|grep nginx

8、檢視是否可以訪問

# curl localhost:80

出現html指令碼代表可以訪問

9.對專案進行打包

1)在vscode中開啟專案路徑下的終端,輸入npm run build

2)打包完成後,會在專案路徑生產一個dist的檔案,將dist檔案重新命名為dist-atp(命名可自定義)

3)將dist-atp檔案上傳到伺服器的/usr/local/nginx/html路徑下

10.配置nginx.conf

cd /usr/local/nginx/conf

vim nginx.conf

對紅色部分的內容做修改

  1 #user  nobody;
  2 #啟動的程序數量
  3 worker_processes  1;
  4 
  5 #error_log  logs/error.log;
  6 #error_log  logs/error.log  notice;
  7 #error_log  logs/error.log  info;
  8 
  9 #pid        logs/nginx.pid;
 10 
 11 
 12 events {
 13 #單個程序併發量
 14     worker_connections  1024;#總併發量=單個程序併發量*啟動的程序數量
 15 }
 16 
 17 
 18 http {
 19     include       mime.types;
 20     default_type  application/octet-stream;
 21 
 22     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 23     #                  '$status $body_bytes_sent "$http_referer" '
 24     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 25 
 26     #access_log  logs/access.log  main;
 27 
 28     sendfile        on;
 29     #tcp_nopush     on;
 30 
 31     #keepalive_timeout  0;
 32     keepalive_timeout  65;#連線伺服器超時時長65s
 33 
 34     #gzip  on;
 35     
 36     #虛擬主機配置
 37 
 38     server {#一個虛擬主機配置,多個虛擬機器就配置多個
 39         listen       8888;
 40         server_name  123.57.1.35; #域名解析
 41 
 42         #charset koi8-r;
 43 
 44         #access_log  logs/host.access.log  main;
 45 
 46         location / {#配置預設訪問頁
 47            # root   html;
 48            #try_files $uri $uri/ @router;
 49             root   /usr/local/nginx/html/dist-atp;
 50             index  index.html index.htm;#訪問的首頁
 51         }
 52         location ./ {
 53            # 代理轉發到後臺服務介面,注意後面英文分號;不要少了
 54            proxy_pass http://123.57.1.35:8090/jeecg-boot/swagger-ui.html;
 55         }
 56 
 57     location @router{
 58     rewrite ^.*/index.html last;
 59     }
 60 
 61         #error_page  404              /404.html;
 62 
 63         # redirect server error pages to the static page /50x.html
 64         #
 65         error_page   500 502 503 504  /50x.html;
 66         location = /50x.html {
 67             root   html;
 68         }
 69 
 70         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 71         #
 72         #location ~ \.php$ {
 73         #    proxy_pass   http://127.0.0.1;
 74         #}
 75 
 76         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 77         #
 78         #location ~ \.php$ {
 79         #    root           html;
 80         #    fastcgi_pass   127.0.0.1:9000;
 81         #    fastcgi_index  index.php;
 82         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 83         #    include        fastcgi_params;
 84         #}
 85 
 86         # deny access to .htaccess files, if Apache's document root
 87         # concurs with nginx's one
 88         #
 89         #location ~ /\.ht {
 90         #    deny  all;
 91         #}
 92     }
 93 
 94 
 95     # another virtual host using mix of IP-, name-, and port-based configuration
 96     #
 97     #server {
 98     #    listen       8000;
 99     #    listen       somename:8080;
100     #    server_name  somename  alias  another.alias;
101 
102     #    location / {
103     #        root   html;
104     #        index  index.html index.htm;
105     #    }
106     #}
107 
108 
109     # HTTPS server
110     #
111     #server {
112     #    listen       443 ssl;
113     #    server_name  localhost;
114 
115     #    ssl_certificate      cert.pem;
116     #    ssl_certificate_key  cert.key;
117 
118     #    ssl_session_cache    shared:SSL:1m;
119     #    ssl_session_timeout  5m;
120 
121     #    ssl_ciphers  HIGH:!aNULL:!MD5;
122     #    ssl_prefer_server_ciphers  on;
123 
124     #    location / {
125     #        root   html;
126     #        index  index.html index.htm;
127     #    }
128     #}
129 
130 }

儲存檔案

按esc

:wq

11.檢視nginx.conf配置是否正確

/usr/local/nginx/sbin/nginx -t

12.重啟nginx

# ./nginx -s reload

13.在瀏覽器輸入域名:埠進行訪問

附件內容如下:

啟動,重啟,停止nginx

cd /usr/local/nginx/sbin/

./nginx #啟動
./nginx -s stop #停止
./nginx -s quit #退出
./nginx -s reload #重啟 修改配置後重新載入生效<br><br>./nginx -s reopen :重新開啟日誌檔案<br>

./nginx -s quit:此方式停止步驟是待nginx程序處理任務完畢進行停止。
./nginx -s stop:此方式相當於先查出nginx程序id再使用kill命令強制殺掉程序。

啟動方法二

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

停止方法二

ps -ef|grep nginx #查詢程序號
kill -QUIT 主程序號 #從容停止
kill -TERM 主程序號 #快速停止
kill -9 主程序號 #強制停止

注:訪問外網ip(注意如果是阿里雲伺服器需要先配置安全組規則)