天天看點

[學習筆記]Linux + Nginx環境下部署 Web 站點

​部署後端程式,請閱讀:

[學習筆記]Linux環境下部署 .Net5 程式 - 林曉lx - 部落格園 (cnblogs.com)

打包項目

以Vue項目為例,首先打包站點,前往項目的根目錄并鍵入:

yarn build           
[學習筆記]Linux + Nginx環境下部署 Web 站點

 等待打包結束,成功後将在根目錄下生成dist檔案夾

[學習筆記]Linux + Nginx環境下部署 Web 站點
[學習筆記]Linux + Nginx環境下部署 Web 站點

 用Zip壓縮dist檔案夾至dist.zip

[學習筆記]Linux + Nginx環境下部署 Web 站點
[學習筆記]Linux + Nginx環境下部署 Web 站點

拷貝dist.zip到目标伺服器的檔案夾下,這裡以 ~/下載下傳 為目标

将站點檔案移動至/var/www/{站點名稱}/

unzip ~/下載下傳/dist.zip -d /var/www/{站點名稱}/           
[學習筆記]Linux + Nginx環境下部署 Web 站點

 配置權限

sudo chmod 777 -R /var/www/abp-mp-auth/           
[學習筆記]Linux + Nginx環境下部署 Web 站點

配置防火牆和SELunix規則:

sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
sudo firewall-cmd --reload           
[學習筆記]Linux + Nginx環境下部署 Web 站點
sudo semanage port -a -t http_port_t  -p tcp 8080           
[學習筆記]Linux + Nginx環境下部署 Web 站點

 配置Nginx

首先更改server區域内容,root為你的站點根目錄,注意不要指定任何index入口,因為這是目錄

server {
    listen        8080 ssl http2;
    server_name   www.matoapp.net;
    root          /var/www/{站點名稱};
 }           
[學習筆記]Linux + Nginx環境下部署 Web 站點

Nginx配置SSL證書,請閱讀:

[學習筆記] Linux 環境下搭建基于Ngnix的反向代理服務 - 林曉lx - 部落格園 (cnblogs.com)

若出現無法通路,網站報403或者404,請檢視nginx日志

sudo cat /var/log/nginx/error.log           
[學習筆記]Linux + Nginx環境下部署 Web 站點

部分錯誤可參照 Troubleshooting解決

Troubleshooting

若在日志顯示:

"/home/xamarin/web/{站點名稱}/index.html" is forbidden (13: Permission denied)           
[學習筆記]Linux + Nginx環境下部署 Web 站點

則有可能是SELinux政策問題,鍵入下列指令添加規則,建議不要直接禁用SELinux:

chcon -R -t httpd_sys_content_t /var/www/{站點名稱}/dist/           
[學習筆記]Linux + Nginx環境下部署 Web 站點

出現“blocked by CORS policy”, 未将網站域名添加至允許跨域通路設定

[學習筆記]Linux + Nginx環境下部署 Web 站點
[學習筆記]Linux + Nginx環境下部署 Web 站點

 在服務端config中,或者docker run指令參數中,添加站點位址至允許跨域通路設定:

docker run -p 44311:44311 --net="host" -e App:CorsOrigins="https://{站點位址}:8080"           
[學習筆記]Linux + Nginx環境下部署 Web 站點

繼續閱讀