1. 程式人生 > >Linux專案部署釋出

Linux專案部署釋出

Linux專案部署釋出

1.部署環境準備,準備python3和虛擬環境直譯器,virtualenvwrapper
pip3 install -i https://pypi.douban.com/simple virtualenvwrapper

2.修改python3的環境變數,寫入到/etc/profile中
PATH=/opt/python36/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/ruby/bin/:/root/bin
3.修改~/.bashrc
寫入變數

4.新建一個虛擬環境  s15vuedrf
mkvirtualenv  s15vuedrf

5.準備前後端程式碼
wget https://files.cnblogs.com/files/pyyu/luffy_boy.zip
wget https://files.cnblogs.com/files/pyyu/07-luffy_project_01.zip
如果程式碼在本地,傳到伺服器 使用 lrzsz 和xftp工具

(lfxc) [

[email protected] lfxc]# yum install unzip


6.解壓縮程式碼
unzip luffy_boy.zip
unzip 07-luffy_project_01.zip


7.從vue前端程式碼搞起
    1.準備node打包環境
    wget https://nodejs.org/download/release/v8.6.0/node-v8.6.0-linux-x64.tar.gz
    
    2.解壓縮node包,配置環境變數,使用npm和node命令
    (lfxc) [
[email protected]
lfxc]# tar -zxvf node-v8.6.0-linux-x64.tar.gz
    
    進入node資料夾
    (lfxc) [[email protected] lfxc]# cd node-v8.6.0-linux-x64
    
    列出node資料夾內容
    (lfxc) [[email protected] node-v8.6.0-linux-x64]# ls
    bin  CHANGELOG.md  include  lib  LICENSE  README.md  share

    切換到bin目錄下面
    (lfxc) [
[email protected]
node-v8.6.0-linux-x64]# cd bin
    (lfxc) [[email protected] bin]# ls
    node  npm  npx
    
    新增環境變數(看結尾處)
    vi /etc/profile
    PATH=/opt/python37/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/opt/enn/lfxc/node-v8.6.0-linux-x64/bin
    
    更新配置檔案,讓其生效
    source /etc/profile
    
    
    檢測node和npm
    (lfxc) [[email protected] node-v8.6.0-linux-x64]# cd bin
    (lfxc) [[email protected] bin]# ls
    node  npm  npx
    (lfxc) [[email protected] bin]# node -v
    v8.6.0
    (lfxc) [[email protected] bin]# npm -v
    5.3.0

    
    4.安裝vue專案所需的包
    切換到專案前臺目錄檔案下
    cd /opt/s15vuedrf/07-luffy_project_01
    執行npm install  
    建立了資料夾node_modules
    執行npm run build
    建立了資料夾dist
    這兩條都正確配置之後,就會生成一個 dist 靜態檔案目錄,整個專案的前端內容和index.html都在這裡了
    
    5.等待nginx載入這個 dist資料夾
    

    
    
    
    

    
8.部署後端程式碼所需的環境
1.啟用虛擬環境
workon s15vuedrf

2.通過一條命令,匯出本地的所有軟體包依賴
pip3 freeze >  requirements.txt

3.將這個requirements.txt 傳至到伺服器,在伺服器的新虛擬環境中,安裝這個檔案,就能安裝所有的軟體包了
pip3 install -r requirements.txt   
這個檔案內容如下:專案所需的軟體包都在這裡了
[[email protected] opt]# cat requirements.txt
certifi==2018.11.29
chardet==3.0.4
crypto==1.4.1
Django==2.1.4
django-redis==4.10.0
django-rest-framework==0.1.0
djangorestframework==3.9.0
idna==2.8
Naked==0.1.31
pycrypto==2.6.1
pytz==2018.7
PyYAML==3.13
redis==3.0.1
requests==2.21.0
shellescape==3.4.1
urllib3==1.24.1
uWSGI==2.0.17.1
        
4.準備uwsgi 支援高併發的啟動python專案(注意uwsgi不支援靜態檔案的解析,必須用nginx去處理靜態檔案)
1.安裝uwsgi
pip3 install -i https://pypi.douban.com/simple uwsgi

2.學習uwsgi的使用方法
通過uwsgi啟動一個python檔案,例如:test.py
--http 指定http協議
--wsgi-file 指定一個python檔案
uwsgi --http :8000 --wsgi-file test.py
        

通過uwsgi啟動django專案,並且支援熱載入專案,不重啟專案,自動生效新的後端程式碼
--module 指定找到django專案的wsgi.py檔案
uwsgi --http :8000 --module s15drf.wsgi --py-autoreload=1
        
5.使用uwsgi的配置檔案,啟動專案
1.建立一個uwsgi.ini配置檔案,寫入引數資訊,這個檔案建議和Django專案中的manage.py放在同一檔案下,方便查詢管理。
touch uwsgi.ini

(lfxc) [[email protected] myweb11]# tree
.
├── db.sqlite3
├── manage.py
├── myweb11
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-37.pyc
│   │   ├── settings.cpython-37.pyc
│   │   ├── urls.cpython-37.pyc
│   │   └── wsgi.cpython-37.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── uwsgi.ini
2 directories, 11 files
                                
[uwsgi]
# Django-related settings
# 指定專案的絕對路徑的第一層路徑
chdir = /opt/enn/lfxc/myweb11

# 指定專案的 wsgi.py檔案
# 寫入相對路徑即可,這個引數是以chdir引數為相對路徑
module = myweb11.wsgi

# 寫入虛擬環境直譯器的絕對路徑
# (lfxc) [[email protected] myweb11]# cdvirtualenv
# (lfxc) [[email protected] lfxc]# pwd
# /opt/enn/lfxc
home = /opt/enn/lfxc


# process-related settings
# master
master = true

# 指定uwsgi啟動的程序個數                
processes = 1

# socket指的是,uwsgi啟動一個socket連線,當你使用nginx+uwsgi的時候,使用socket引數.
# 沒用Nginx時,使用socket引數會報錯,錯誤如下:
# invalid request block size: 21573 (max 4096)...skip
socket = 0.0.0.0:8000
# 這個引數是uwsgi啟動一個http連線,當你不用nginx只用uwsgi時,使用這個引數.
#http = 0.0.0.0:8000

# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
        
6.使用uwsgi配置檔案啟動專案
uwsgi --ini  uwsgi.ini
        














        
supervisor程序管理工具

1.將linux程序執行在後臺的方法有哪些
第一個,命令後面加上 &  符號
python  manage.py  runserver &
第二個 使用nohup命令
第三個使用程序管理工具


2.安裝supervisor,使用python2的包管理工具 easy_install. 注意,此時要退出虛擬環境.
執行命令安裝easy_install
yum install python-setuptools
執行命令安裝supervisor
easy_install supervisor


3.通過命令,生成一個配置檔案,這個檔案就是寫入你要管理的程序任務
echo_supervisord_conf > /etc/supervisor.conf


4.編輯這個配置檔案,寫入操作  django專案的 命令
vim /etc/supervisor.conf  
直接到最底行,寫入以下配置
[program:s15luffy]
command=/opt/enn/lfxc/bin/uwsgi --ini /opt/enn/lfxc/luffy_boy/uwsgi.ini
/opt/enn/lfxc/bin/uwsgi


5.啟動supervisord服務端,指定配置檔案啟動
5.1檢查服務端是否啟動
[[email protected] ~]# ps -ef |grep super
root       1646   1444  0 19:52 pts/0    00:00:00 grep --color=auto super
5.2啟動服務端
[[email protected] ~]# supervisord -c /etc/supervisor.conf
Unlinking stale socket /tmp/supervisor.sock


6.通過supervisorctl管理任務
[[email protected] ~]# supervisorctl -c /etc/supervisor.conf
s15luffy    RUNNING    pid 1833, uptime 0:01:42


7.supervisor管理django程序的命令如下
supervisorctl直接輸入命令會進入互動式的操作介面
supervisor> status s15luffy
s15luffy    STOPPED   Jan 05 08:16 PM

supervisor> start s15luffy
s15luffy: started

supervisor> status s15luffy
s15luffy    RUNNING   pid 1871, uptime 0:00:06

supervisor> stop all
s15luffy: stopped

supervisor>



8.啟動luffy的後端程式碼




配置nginx步驟如下
# 檢視uwsgi/supervisor程序是否有在執行
[[email protected] myweb11]# ps -ef |grep uwsgi
root  1951  1783  0  20:32 pts/0  00:00:00 grep --color=auto uwsgi
[[email protected] myweb11]# ps -ef |grep super
root  1953    1783  0  20:32 pts/0  00:00:00 grep --color=auto super


# 啟動supervisord服務端,指定配置檔案啟動
[[email protected] myweb11]# supervisord -c /etc/supervisor.conf
Unlinking stale socket /tmp/supervisor.sock


# 啟動supervisorctl管理任務,指定配置檔案啟動
[[email protected] myweb11]# supervisorctl -c /etc/supervisor.conf
s15luffy    RUNNING   pid 1984, uptime 0:00:35
supervisor>


# 網路工具沒有安裝,會有以下報錯!!!
[[email protected] myweb11]# netstat -tunlp
-bash: netstat: command not found
[[email protected] myweb11]# yum install net-tools


# 檢查uwsgi是否執行在9000埠上
[[email protected] myweb11]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:9000            0.0.0.0:*               LISTEN      2067/uwsgi          
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      961/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1182/master         
tcp6       0      0 :::22                   :::*                    LISTEN      961/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1182/master         
udp        0      0 127.0.0.1:323           0.0.0.0:*                           698/chronyd         
udp        0      0 0.0.0.0:68              0.0.0.0:*                           766/dhclient        
udp6       0      0 ::1:323                 :::*                                698/chronyd   



1.編譯安裝nginx
2.nginx.conf配置如下

#第一個server虛擬主機是為了找到vue的dist檔案, 找到路飛的index.html
server {
    listen 80;
    server_name 192.168.142.133;
        #當請求來自於 192.168.13.79/的時候,直接進入以下location,然後找到vue的dist/index.html
        location / {
            root   /opt/enn/lfxc/07-luffy_project_01/dist;
            index  index.html;
        }
    }
    
#由於vue傳送的介面資料地址是192.168.142.133:8000我們還得再準備一個入口server
server {
    listen 8000;
    server_name  192.168.142.133;
    #當接收到介面資料時,請求url是192.168.142.133:8000就進入如下location
    location /  {
        #這裡是nginx將請求轉發給uwsgi啟動的9000埠
        uwsgi_pass 192.168.142.133:9000;
        # include就是一個“引入的作用”,就是將外部一個檔案的引數,匯入到當前的nginx.conf中生效
        include /opt/nginx1616/conf/uwsgi_params;
    }
}

3.啟動nginx
./sbin/nginx  直接啟動
此時可以訪問 192.168.13.79  檢視頁面結果