如何部署一個本地的web項目到服務器-搭建linux環境(2)?


前面我們已經把linux環境部署好了,在這里我們可以把我們需要的上線的html文件或者其他發布到服務器上。

先說一下linux基礎命令:ls,cd,mkdir,vim,等等

具體參考:這位大神的博客

然后先創建一個文件夾放置我們需要上線的html文件;

mkdir test

這里我們事先准備好我們要上線的文件,比如react,vue經過webpack打包后的文件都可以;

使用Transmit進行文件傳輸,

首先查看服務器ip,sftp傳輸文件需要ip

使用Tansmit軟件新建一個SFTP服務,

具體設置如下:

成功鏈接就會出現下面的頁面,這樣我們就可以進行文件拷貝了,離成功很快了;

接下來就是nginx相關的配置,有一些比較惡心的配置,新手可能會遇到,

例如

  • nginx啟動不了
  • server配置錯誤
  • 防火牆沒有關閉,linux和centos都需要關閉
  • 沒有給與文件夾讀取權限

首先我們先說一下nginx的配置:

  • ps -ef | grep nginx      查看nginx安裝目錄
  • nginx -t                 查看nginx.conf配置文件目錄(每個人配置nginx路徑可能不一樣)
 1 # For more information on conf configuretion, see;
 2 #   * Official English Documention: http://nginx.org/en/docs
 3 #   * Official Russian Documention: http://nginx.org/ru/docs
 4 
 5 user root;                  //操作賬戶
 6 worker_processes auto;      
 7 error_log  /var/log/nginx/error.log;
 8 pid /run/nginx.pid;
 9 
10 # Load dynamic modules. See /usr/share/nginx/rename.dynamic
11 include /usr/share/nginx/module/*.conf
12 
13 events {
14     worker_connentions 1024;
15 }
16 
17 http {
18     log_format main     '$remote_addr - $remote_user [$time_local] "$request"'
19                         '$status $body_bytes_sent "$http_referer"'
20                         '"http_user_agent" "$http_x_forwarded_for"'
21 
22     access_log          /var/log/nginx/access.log   main;
23 
24     sendfile            on;
25     tcp_nopush          on;
26     tcp_nodelay         on;
27     keepalive_timeout   65;
28     types_hash_max_size 2048;
29     include             /etc/nginx/mine.types;
30     default_type        application/octet-stream;
31 
32     # Load modular configureation files from the /etc/nginx/conf.d directory.
33     # See http://nginx.org/en/docs/ngx_core_module.html#include
34     # for more information
35 
36     server {
37         listen      80 default_server;
38         server_name ifeng.lego.com;
39         root        /root/test/build;
40         # Load configuration files the dafault server block;
41         include     /etc/nginx/default.d/*.conf;
42 
43         location = / {
44             root /root/test/build/;         //指定目錄存放位置
45             index index.htmkl index.html    //指定打開文件
         try_files $uri $uri/ /index.html;
46 } 47 48 error_page 404 /404.html; 49 location = /40x.html{ 50 51 } 52 53 error_page 500 502 503 504 /50x.html 54 location = /50x.html{ 55 56 } 57 } 58 }

配置完之后,我么需要關閉linxu防火牆和centos防火牆

centons后使用systemctl代替service

具體如下,

  • systemctl start firewalld     開啟防火牆
  • systemctl stop firewalld       關閉防火牆
  • systemctl status firewalld     檢查防火牆狀態

安裝iptables-services

  • systemctl enable iptables      設置開機啟動
  • systemctl [start|restart|stop]  開啟,重啟,關閉防火牆
  • service iptables save            保存設置

接下來我們需要給我們訪問的目錄"/root/test/build"授予訪問權限,也是出現403的原因

  • chmod -R 777 /root/test/build  授予web目錄文件夾操作權限

然后設置nginx的SELinux設置為disabled

  • vim /etc/selinux/config 
  • SELINUX = disabled

然后啟動nginx服務,訪問index.html,

  • systemctl [start|restart|stop]  nginx.service    啟動,重啟,停止nginx服務
  • systemctl reload nginx.service                 重啟nginx服務

講道理到這來我們就可能訪問我們之前打包好的靜態文件了,

*后續我們需要的mysql,node等服務跟前面server的配置大致相同,這里不做過多闡述,根據不同情況不同分析,

最后看效果

以上就是搭建一個基本的服務的全部過程,對於新手學習linux,nginx有一定幫助,如有疑問請提出。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM