django+uwsgi+nginx部署web項目

系統需求

centos7 minimal
python2.7

部署前的準備工做

centos7 minimal是精簡版本,須要手動去配置一些設置。
1. 配置網絡,設置固定ip
    ip能夠自動獲取,我這裏設置成固定ip
    
2. 更新yum源
    [root@localhost /]# cd 
    [root@localhost ~]# wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    [root@localhost ~]# rpm -ivh epel-release-latest-7.noarch.rpm
    檢查是否添加到源列表:
    [root@localhost ~]# yum repolist

3. 安裝vim
    [root@localhost ~]# yum install vim
   
4. 設置防火牆開啓80,3306等須要的端口
    高版本的centos系統防火牆默認是firewall,經過命令能夠永久開放端口
    [root@localhost ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
5. kill命令的擴展
    [root@localhost ~]# yum install psmisc

須要安裝的軟件

1. mariadb或者mysql,這裏選擇mariadb
    [root@localhost /]# yum install mariadb mariadb-server
    [root@localhost /]# yum install mysql-devel
    
2. nginx
    安裝依賴包:
    [root@localhost /]#  yum install gcc-c++

    安裝nginx:
    [root@localhost /]#  yum install nginx
    
3. python的一些組件
    安裝pip:
    [root@localhost /]#  yum install python-pip
    更新pip:
    [root@localhost /]#  pip install --upgrade pip
    安裝虛擬環境:
    [root@localhost /]#  pip install virtualenv
    安裝uwsgi:
    [root@localhost /]#  yum install python-devel
    [root@localhost /]#  pip install uwsgi
    安裝mysql連接工具:
    [root@localhost /]#  pip install MySQL-python

4. 安裝git
    [root@localhost /]#  yum install git

開始部署項目django+uwsgi+nginx

1. 建立項目目錄
    [root@localhost /]#  mkdir /www/
    
2. 從遠程倉庫git
    [root@localhost /]#  cd /www/
    [root@localhost www]# git clone https://example.git
    
3. 配置數據庫
    [root@localhost www]# systemclt start mariadb
    [root@localhost www]# mysql_secure_installation
    
    登陸mysql
    [root@localhost www] mysql -uroot -p
    建立數據庫
    MariaDB [(none)]> create database db_name default character set utf8;
    
4. 建立虛擬環境並激活
    [root@localhost /]#  cd /www/
    [root@localhost www]#  virtualenv env --system-site-packages (這裏虛擬環境要繼承全局)
    [root@localhost www]#  source env/bin/activate

5. 配置django環境
    在虛擬環境下,安裝所需的packages, 而後配置數據庫等開發須要的內容。
    
6. 配置uwsgi
    在manage.py文件同級目錄下新建文件django_socket.ini爲uwsgi配置文件。內容以下:
    [uwsgi]
    chdir = /www/site/  # 項目根目錄路徑
    wsgi-file = /www/site/site/wsgi.py  # wsgi文件路徑
    master = true
    vhost = true
    pythonpath = /www/env/lib/python2.7/site-packages  # 虛擬環境路徑
    socket = 127.0.0.1:8077
    processes = 4
    threads = 2
    log-maxsize = 500000
    buffer-size = 32768
    daemonize = /www/logs/uwsgi.log  # log文件路徑
    
7. 配置nginx
    在/etc/nginx/conf.d/目錄下新建nginx配置文件,site.conf(必須是.conf的後綴名),配置文件內容以下:
    server {
        listen 80;
        server_name 192.168.1.20;  # 域名或者ip地址
        root /www/site;

        location / {
                uwsgi_pass 127.0.0.1:8077;
                include uwsgi_params;
                uwsgi_param REMOTE_ADDR $HTTP_X_REAL_IP;
        }

        location = /50x.html {
                root html;
        }

        location /static/ {
                root /www/site;  # 靜態資源路徑
        }

        location /uploads/ {
                root /www/site;
        }
    }
    
    以上配置基本完成了,接下來運行以下命令:
        [root@localhost site]#  uwsgi --ini django_socket.ini
        [root@localhost site]#  nginx -c /etc/nginx/nginx.conf
        [root@localhost site]#  nginx -s reload
        若是沒有報錯,就說明配置成功,在瀏覽器輸入ip地址或者域名就能訪問了。
        
8. 腳本啓動服務器
    方便起見,咱們能夠設置腳本啓動重啓服務器,在/www/目錄下新建shell腳本, 命名爲update.sh, 內容以下:
    #!/bin/bash
            
    nginx_process=`ps aux|grep nginx|grep worker`
    if [ "$nginx_process" != "" ]; then
        killall -9 nginx
    fi
    
    uwsgi_process=`ps aux|grep uwsgi|grep django_socket`
    if [ "$uwsgi_process" != "" ]; then
        killall -9 uwsgi
    fi
    
    cd /www/site
    #/usr/bin/python manage.py collectstatic 
    /usr/bin/uwsgi --ini django_socket.ini
    
    /usr/sbin/nginx -c /etc/nginx/nginx.conf
    /usr/sbin/nginx -s reload
    
    修改文件權限:
        [root@localhost www]# chmod 755 update.sh
        
    腳本啓動配置完成,若是發佈新版本以後記得執行該腳本才能生效。(若是報錯爲:nginx502那就再執行一次 - -)
    
    
9. 自動化配置
    配置mysql開機啓動:
        [root@localhost www]#  systemclt enable mariadb

    配置uwsgi+nginx開機啓動:
        因爲已經設置經過腳本啓動uwsgi和nginx,因此只須要將/www/update.sh這個腳本設置爲開機啓動就OK啦。配置以下:
        
        打開/etc/rc.local,在最後一行添加:
            sh /www/update.sh
            
        保存關閉。重啓服務器以後有可能腳本沒有被自動執行,緣由是/etc/rc.local是/etc/rc.d/rc.local的連接文件,所以,須要修改/etc/rc.d/rc.local的權限:
            [root@localhost root]# chmod 755 /etc/rc.d/rc.local

至此,整個部署就完成了。若有須要改進的地方,還請各位指出,互相學習交流。html

相關文章
相關標籤/搜索