Linux-基礎學習(四)-部署圖書管理系統項目

部署圖書管理項目須要如下軟件html

  • 項目文件(django項目文件夾)
  • 數據庫文件(django項目對應的數據庫文件)
  • centos7(linux本體)
  • nginx(反向代理以及靜態文件收集)
  • uWSGI(代理服務器與後端的application應用服務器之間的中間件)
  • virtualenv(虛擬環境)
  • supervisor(自動守護)

1.項目部署

1.1 項目文件上傳

上傳圖書管理系統項目到linux服務器上python

  • Lrzsz工具進行上傳
  • xftp工具進行上傳(推薦方式)
  • scp命令

在/opt/目錄下傳輸項目文件,數據庫文件一樣存放在這個文件夾下mysql

1.2 虛擬環境配置virtualenv

經過virtualenvwrapper工具的配置,解決虛擬環境問題linux

1554254767286

建立虛擬環境mkvirtualenv book_manage_envnginx

1.3 數據庫安裝

在centos7本體下安裝配置mariadb數據庫,且建立數據庫數據,遷移導入圖書管理系統數據sql

  • 安裝mariadb數據庫(和mysql如出一轍只是爲了免費)
  • 導出開發過程當中的book_manage項目的數據
    • 第一種方法:使用navicat導出,轉儲SQL文件,(若是須要數據則使用結構與數據,反之則使用結構)
    • 第二種方法,使用mysqldump命令, 此命令適用於windows和linux下
  • 將數據導入到服務器mariadb數據庫中
    • 進入數據庫,先建立一個數據庫book_manage   (create database book_manage)
    • 進入此數據庫   use book_manage
    • 導入數據庫  source /opt/book_manage.sql
  • 容許root用戶遠程登錄數據庫
    • 第一種方法, 在初始化數據庫的時候,就容許root用戶遠程登陸
    • 第二種方法 使用SQL語句進行受權(grant all privileges on *.* to root@"%" identified  by "你的密碼";)

1554255785718

1554255835509

1554255870779

-測試使用linux的python解釋器去運行項目 切換到 項目中運行(注意要解決解釋器的模塊問題,才能正常運轉項目)數據庫

1.4 uWSGI配置

完成uWSGI命令學習,使用uWSGI啓動圖書管理系統項目,支持多進程django

1554258779888

1554258763697

1.4.1 安裝Uwsgi

(1)使用pip安裝uwsgi模塊vim

pip3 install uwsgi

(2)建立一個測試文件testuwsgi.py, 運行簡單的uWSGI站點windows

第一步vim /opt/book_homework/testuwsgi.py 寫入如下文件

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]

第二步 使用uwsgi命令啓動此文件

uwsgi --http :9000 --file testuwsgi.py

第三步 在瀏覽器經過IP加端口進行訪問

(3)使用uwsgi.ini文件,簡單的命令就能夠將uwsgi啓動起來

cd /opt/book_manage
vim uwsgi.ini

寫入配置文件

[uwsgi]
# Django-related settings
# the base directory (full path)
# 寫上項目的絕對路徑
chdir           = /opt/book_manage
# Django's wsgi file
# 填寫找到django的wsgi文件,填寫相對路徑,以chdir參數爲相對路徑
module          = book_manage.wsgi
# the virtualenv (full path)
# 填寫虛擬環境的絕對路徑
home            = /root/Envs/book_manage_env/
# process-related settings
# master
#啓動uwsgi主進程
master          = true
# maximum number of worker processes
processes       = 1
# the socket (use the full path to be safe
#若是你使用了nginx,作反向代理,必須填寫socket連接,而不是http參數
socket          = 0.0.0.0:8000
#若是你不用nginx,直接使用uwsgi,運行一個http服務端,就用這個http參數
http = 0.0.0.0:8000
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true

使用uwsgi命令指定配置文件啓動,單個項目是能夠這麼啓動

uwsgi -ini /opt/book_homework/uwsgi.ini

1.5 nginx配置

1.5.1 收集django靜態文件

收集django靜態文件

vim /opt/book_homework/book_homework/settings.py

加入一行配置,靜態文件

STATIC_ROOT='/opt/static'

回到django項目主目錄下(有manage.py文件的目錄), 輸入命令進行收集靜態文件

python3 manage.py collectstatic

1.5.2 配置nginx與uwsgi結合

下圖爲/opt/nginx112/conf/uwsgi_params文件

1554262298366

1.5.3 修改nginx的配置文件/opt/nginx112/conf/nginx.conf

添加如下配置

location / {
            # nginx自帶ngx_http_uwsgi_module模塊,起到nginx和uwsgi交互做用
            # 經過uwsgi_pass設置服務器地址和協議,將動態請求轉發給uwsgi處理
            include  /opt/nginx112/conf/uwsgi_params;
            uwsgi_pass 0.0.0.0:8000;
            root   html;
            index  index.html index.htm;
        }

*************************************************************************************************************
location /static {
            alias /opt/static;
        }

如圖配置

1554264465020

nginx.conf所有配置文件以下

worker_processes  1;
error_log  logs/error.log;
pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
  # nginx反向代理uwsgi
    server {
        listen       80;
        server_name  qishijd.com;
        location / {
            # nginx自帶ngx_http_uwsgi_module模塊,起到nginx和uwsgi交互做用
            # 經過uwsgi_pass設置服務器地址和協議,將動態請求轉發給uwsgi處理
            include  /opt/nginx112/conf/uwsgi_params;
            uwsgi_pass 0.0.0.0:8000;
            root   html;
            index  index.html index.htm;
        }
        # nginx處理靜態頁面資源
        # 當用戶請求是qishijd.com/static/的時候, 就會進入這個location匹配
        # 經過alias參數進行路徑別名,讓nginx去/opt/static底下去找靜態資源
     location /static {
            alias /opt/static;
        }
     # nginx處理媒體資源
     location /media{
            alias /opt/nginx112/media;  
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

1.5.4 平滑重啓nginx

/opt/nginx112/sbin/nginx -s reload

1.5.5 確保uwsgi已經正常啓動

這個主要是用於守護進程

/root/Envs/book_manage/bin/uwsgi --ini /opt/book_manage/uwsgi.ini
ps -ef | grep uwsgi

     在瀏覽器訪問http://192.168.1.44已經能夠正常訪問,靜態文件也能夠正常加載了,修改數據也沒有問題, 說明數據庫鏈接正常

image

2.安裝配置supervisor

supervisor 是基於 python 的任務管理工具,用來自動運行各類後臺任務,固然你也能直接利用 nohup 命令使任務自動後臺運行,但若是要重啓任務,每次都本身手動 kill 掉任務進程,這樣很繁瑣,並且一旦程序錯誤致使進程退出的話,系統也沒法自動重載任務。

這裏咱們要配置基於virtualenv的supervisor,可是請注意:因爲supervisor在python3下沒法使用,所以只能用python2去下載!!!!!!

2.1 安裝suprvisor

安裝suprvisor

easy_install supervisor  # 這個是python2下面的安裝模塊命令,等同於python3下面的pip

若是沒有easy_install這個命令,就須要安裝setuptools工具

yum install python-setuptools

2.2 使用命令生成supervisor.conf配置文件

echo_supervisord_conf > /etc/supervisord.conf

2.3 在/etc/supervisord.conf末尾添加上以下代碼

[program:book_manage]
command=/root/Envs/book_manage_env/bin/uwsgi /opt/book_manage/uwsgi.ini
stopasgroup=true  # 若是發現關閉supervisor進程後,結束uwsgi進
killasgroup=true  # 程無效,就須要加上這兩個參數

如圖所示

1554263627344

2.4 啓動supervisor, 完成uWSGI啓動django,nginx反向代理

supervisord -c /etc/supervisord.conf

2.5 upervisor啓動命令參數有兩種

# 任務管理命令以下:有兩種,一個是參數形式, 一個是交互式
# 參數形式  
supervisorctl -c /etc/supervisor.conf stop/start/restart   all
supervisorctl -c /etc/supervisor.conf start crm_knight

# 交互式形式
supervisorctl -c /etc/supervisor.conf

2.6 從新加載supervisor(瞭解便可)

1、添加好配置文件後

2、更新新的配置到supervisord    

supervisorctl update
3、從新啓動配置中的全部程序

supervisorctl reload
4、啓動某個進程(program_name=你配置中寫的程序名稱)

supervisorctl start program_name
5、查看正在守候的進程

supervisorctl
6、中止某一進程 (program_name=你配置中寫的程序名稱)

pervisorctl stop program_name
7、重啓某一進程 (program_name=你配置中寫的程序名稱)

supervisorctl restart program_name
8、中止所有進程

supervisorctl stop all
注意:顯示用stop中止掉的進程,用reload或者update都不會自動重啓。

 

a

相關文章
相關標籤/搜索