1. 程式人生 > 實用技巧 >路飛專案十

路飛專案十

1 阿里雲購買

# 1 寫好的專案,在本地跑,別問訪問不到,沒有公網ip
# 2 遠端連線阿里雲的伺服器47.103.156.13(xshell,finalShell)
# 3 ssh連線 
# 4 配置前後端專案
# 5 編譯前端專案,要把前端專案傳到伺服器(可以用軟體)
	scp -r dist [email protected]:~
# 6 

2 雲伺服器安裝redis

1)前往使用者根目錄
>: cd ~

2)下載redis-5.0.5
>: wget http://download.redis.io/releases/redis-5.0.5.tar.gz
>: scp -r C:\Users\dell\Desktop\pkg\redis-5.0.5.tar.gz [email protected]:~

3)解壓安裝包
>: tar -xf redis-5.0.5.tar.gz

4)進入目標檔案
>: cd redis-5.0.5

5)編譯環境
>: make

6)複製環境到指定路徑完成安裝
>: cp -r ~/redis-5.0.5 /usr/local/redis


9)建立軟連線
>: ln -s /usr/local/redis/src/redis-server /usr/bin/redis-server
>: ln -s /usr/local/redis/src/redis-cli /usr/bin/redis-cli

10)後臺執行redis
>: cd /usr/local/redis
>: redis-server &

ctrl + c 停止

11)測試redis環境
>: redis-cli
ctrl + c

12)關閉redis服務
>: pkill -f redis -9

3 雲伺服器安裝mysql

1)前往使用者根目錄
>: cd ~

2)下載mysql57
>: wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

也可以本地上傳,這條命令要在本地終端上執行
>: scp -r C:mysql57-community-release-el7-10.noarch.rpm [email protected]:~

3)安裝mysql57
>: yum -y install mysql57-community-release-el7-10.noarch.rpm
>: yum -y install mysql-community-server

4)啟動mysql57並檢視啟動狀態
>: systemctl start mysqld.service
>: systemctl status mysqld.service

5)檢視預設密碼並登入
>: grep "password" /var/log/mysqld.log
>: mysql -uroot -p

6)修改密碼
>: ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';

4 雲伺服器安裝python

1)前往使用者根目錄
>: cd ~

2)下載 或 上傳 Python3.6.7
# 伺服器終端
>: wget https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tar.xz

# 本地終端,給伺服器上傳
>: scp -r 本地Python-3.6.7.tar.xz ssh [email protected]:伺服器路徑
>: scp -r C:\Users\dell\Desktop\pkg\Python-3.6.7.tar.xz ssh [email protected]~

3)解壓安裝包
>: tar -xf Python-3.6.7.tar.xz

4)進入目標檔案
>: cd Python-3.6.7

5)配置安裝路徑:/usr/local/python3
>: ./configure --prefix=/usr/local/python3

6)編譯並安裝
>: make && sudo make install

7)建立軟連線:終端命令 python3,pip3
>: ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
>: ln -s /usr/local/python3/bin/pip3.6 /usr/bin/pip3

8)刪除安裝包與檔案:
>: rm -rf Python-3.6.7
>: rm -rf Python-3.6.7.tar.xz

5 雲伺服器安裝nginx

1)前往使用者根目錄
>: cd ~

2)下載nginx1.13.7
>: wget http://nginx.org/download/nginx-1.13.7.tar.gz

3)解壓安裝包
>: tar -xf nginx-1.13.7.tar.gz

4)進入目標檔案
>: cd nginx-1.13.7

5)配置安裝路徑:/usr/local/nginx
>: ./configure --prefix=/usr/local/nginx

6)編譯並安裝
>: make && sudo make install

7)建立軟連線:終端命令 nginx
>: ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

8)刪除安裝包與檔案:
>: cd ~
>: rm -rf nginx-1.13.7
>: rm -rf nginx-1.13.7.tar.xz

9)測試Nginx環境,伺服器執行nginx,本地訪問伺服器ip
>: nginx
>: 伺服器繫結的域名 或 ip:80


#Nginx命令
1)啟動
>: nginx

2)關閉nginx
>: nginx -s stop

3)重啟nginx
>: nginx -s reload

4)檢視埠,強行關閉
>: ps -aux|grep nginx
>: kill <pid:程序編號>
 

# 
	Nginx (engine x) 是一個高效能的HTTP和反向代理web伺服器
	Apache和Nginx最核心的區別在於 apache 是同步多程序模型,一個連線對應一個程序;而 nginx 是非同步的,多個連線(萬級別)可以對應一個程序
    
   http請求轉發
   反向代理伺服器
   負載均衡
   動靜分離

6 nginx轉發靜態檔案(前端專案)

# 1 mv ~/dist /home/html
# 2 cd /usr/local/nginx/conf
# 3 vim nginx.conf
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    server {
        listen 80;
        server_name  127.0.0.1; # 改為自己的域名,沒域名修改為127.0.0.1:80
        charset utf-8;
        location / {
            root /home/html; # html訪問路徑
            index index.html; # html檔名稱
            try_files $uri $uri/ /index.html; # 解決單頁面應用重新整理404問題
        }
    }
}   

esc
:wq   # 儲存並推出
    
# 重啟nginx
nginx -s reload

# 前端專案就有了

7 雲伺服器轉發動態請求(uwsgi+django專案)

# 1 mkdir /home/project
# 2 cd /home/project
# 3 git clone xxxx
# 4 安裝虛擬環境,建立虛擬環境,安裝專案依賴
pip install -r requirements.txt  (在指定的目錄下)
# 5 pip install uwsgi (虛擬和真實環境都需要安裝)
# 6 進行uwsgi服務配置,內容如下
vim /home/project/luffyapi/luffyapi.xml

<uwsgi>    
   <socket>127.0.0.1:8808</socket> <!-- 內部埠,自定義 --> 
   <chdir>/home/project/luffyapi/</chdir> <!-- 專案路徑 -->            
   <module>luffyapi.wsgi</module>  <!-- luffyapi為wsgi.py所在目錄名--> 
   <processes>4</processes> <!-- 程序數 -->     
   <daemonize>uwsgi.log</daemonize> <!-- 日誌檔案 -->
</uwsgi>

# 7 去向Nginx配置目錄,備份配置,完全更新配置:填入下方內容
>: vim /usr/local/nginx/conf/nginx.conf

events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    server {
        listen 80;
        server_name  127.0.0.1; # 改為自己的域名,沒域名修改為127.0.0.1:80
        charset utf-8;
        location / {
            root /home/html; # html訪問路徑
            index index.html; # html檔名稱
            try_files $uri $uri/ /index.html; # 解決單頁面應用重新整理404問題
        }
    }
    # 新增的server
    server {
        listen 8000;
        server_name  127.0.0.1; # 改為自己的域名,沒域名修改為127.0.0.1:80
        charset utf-8;
        location / {
           include uwsgi_params;
           uwsgi_pass 127.0.0.1:8808;  # 埠要和uwsgi裡配置的一樣
           uwsgi_param UWSGI_SCRIPT luffyapi.wsgi;  #wsgi.py所在的目錄名+.wsgi
           uwsgi_param UWSGI_CHDIR /home/project/luffyapi/; # 專案路徑
        }
    }
} 

        
#9 mysql的root使用者,登入,建立表luffyapi,建立使用者,授權
    1)管理員連線資料庫
    >: mysql -uroot -p

    2)建立資料庫
    >: create database luffyapi default charset=utf8;

    3)設定許可權賬號密碼:賬號密碼要與專案中配置的一致
    >: grant all privileges on luffyapi.* to 'luffyapi'@'%' identified by 'Luffy123?';
    >: grant all privileges on luffyapi.* to 'luffyapi'@'localhost' identified by 'Luffy123?';
    >: flush privileges;

    4)退出mysql
    >: quit;
# 10 資料遷移建立超級使用者
python manage_pro.py makemigraions
python manage_pro.py migrate
python manage_pro.py createsuperuser


#11 啟動uwsgi和nginx(後端專案就可以了)
    1)啟動uwsgi
    >: uwsgi -x /home/project/luffyapi/luffyapi.xml
    2)重啟nginx
    >: nginx -s reload