1. 程式人生 > >ngnix+uwsgi+django+python+mysql環境部署

ngnix+uwsgi+django+python+mysql環境部署

configure 軟連接 環境 pre django com nal 追加 部署

django
一個python的web框架
flask和django

uwsgi
一個Python的容器

nginx
一個web服務器 可以做web容器的前端

client--->nginx(80)--->uwsgi(開端口5000)--->web頁面


virtualenv  virtualenvwrapper
    python開發虛擬環境

安裝python3:
    下載python3.6
        去官網下載
        #xz -d  壓縮包.xz
    準備環境:      
        #yum install zlib* openssl* readline* sqlite* -y

    安裝python3
        # cd Python-3.6.4
        # ./configure --prefix=/usr/local/python3.6  && make && make install 
        # rm -rf  /usr/bin/python
        # ln -s /usr/local/python3.6/bin/python3  /usr/bin/python

     處理yum(python升級之後yum就不能用了,他默認用的2.7)
        # vim /usr/bin/yum    把解釋器改成2.7
        # !/usr/bin/python2.7

    給pip做軟連接:    
    # ln -s /usr/local/python3.6/bin/pip3  /usr/bin/pip

    # vim ~/.bash_profile 
        PATH=$PATH:$HOME/bin:/usr/local/python3.6/bin/
     #source ~/.bash_profile

安裝django        
    # pip install django==1.10

創建django項目:firstsite
    #cd ~
    #django-admin.py startproject firstsite
    #cd ~/firstsite

同步數據庫:
    #./manage.py migrate
    #./manage.py createsuperuser

設置靜態文件目錄:
    # vim firstsite/settings.py   //先找到STATIC關鍵字,追加下面一行  
    STATIC_ROOT = os.path.join(BASE_DIR, "static/") 
    #./manage.py collectstatic

測試django是否可用:    
    #./manage.py runserver 0.0.0.0:443
    瀏覽器訪問:   
    http://xiangmu.buy360.xyz:443

    瀏覽器訪問:    
    http://xiangmu.buy360.xyz:443/admin

安裝uwsgi:    
    #pip install uwsgi

測試uwsgi:
    # uwsgi --http :443 --chdir /root/firstsite -w firstsite.wsgi
     --http  :端口
     --chdri  項目目錄
     -w   wsgi模塊

創建uwsgi配置文件:
#mkdir -p /etc/uwsgi/sites
# vim /etc/uwsgi/sites/firstsite.ini 
[uwsgi]
project = firstsite      //項目名稱
username = root      //運行用戶
base = /%(username)/         //項目目錄的上級目錄
chdir = %(base)/%(project)         //項目目錄
module = %(project).wsgi:application     
master = true
processes = 5
uid = %(username)
socket = /run/uwsgi/%(project).sock
chown-socket = %(username):nginx
chmod-socket = 660
vacuum = true

創建uwsgi啟動配置文件:
# vim /etc/systemd/system/uwsgi.service
[Unit]
Description=uWSGI Emperor service

[Service]
ExecStartPre=/usr/bin/bash -c ‘mkdir -p /run/uwsgi; chown root:nginx /run/uwsgi‘
ExecStart=/usr/local/python3.6/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target

下面兩步不做會啟動服務不成功:
#mkdir /root/uwsgi
#ln -s /usr/local/python3.6/bin/uwsgi /root/uwsgi/uwsgi

安裝nginx:
#yum install nginx

配置nginx:
# vim /etc/nginx/nginx.conf 添加如下虛擬主機配置
server {
    listen 80;
    server_name firstsite.com www.youngfit.cn;

    location = favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /root/firstsite;
    }

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/run/uwsgi/firstsite.sock;
    }
}

測試Nginx配置文件語法正確性:
    # nginx -t

設置權限:
# usermod -a -G root nginx
# chmod 710 /root

啟動nginx和uwsgi服務:
    #systemctl start nginx
    #systemctl start uwsgi

測試:
瀏覽器地址欄:http://xiangmu.buy360.xyz

ngnix+uwsgi+django+python+mysql環境部署