要注意alias和root的區(qū)別 root /path相當(dāng)于訪問(wèn)static的時(shí)候是訪問(wèn)/path/static alias /path相當(dāng)于訪問(wèn)static時(shí)是訪問(wèn)/path/
1.確認(rèn)環(huán)境 python2.7 django1.8 centos6.8
A.安裝django pip install django==1.8 B.安裝uwsgi pip install uwsgi 報(bào)錯(cuò)(-bash: pip: command not found)解決方案 一,、pip下載安裝 1 安裝依賴軟件 yum install zlib zlib-devel openssl openssl-devel libcurl-devel gcc gcc-c++ -y 2 pip下載 wget "https://pypi./packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb" --no-check-certificate
3 pip安裝 tar -xzvf pip-1.5.4.tar.gz cd pip-1.5.4 python setup.py install
python錯(cuò)誤:ImportError: No module named setuptools 這句錯(cuò)誤提示的表面意思是:沒(méi)有setuptools的模塊,,說(shuō)明python缺少這個(gè)模塊,,那我們只要安裝這個(gè)模塊即可解決此問(wèn)題,,下面我們來(lái)安裝一下: 在命令行下: 下載setuptools包 wget http://pypi./packages/source/s/setuptools/setuptools-0.6c11.tar.gz 解壓setuptools包 tar zxvf setuptools-0.6c11.tar.gz cd setuptools-0.6c11 編譯setuptools python setup.py build 開(kāi)始執(zhí)行setuptools安裝 python setup.py install
C.測(cè)試uwsgi是否安裝成功 創(chuàng)建測(cè)試文件 vim check.py #!/usr/bin/python # coding:utf8 def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return "Hello World"
啟動(dòng)uwsgi uwsgi --http :8001 --wsgi-file check.py
用url訪問(wèn)測(cè)試,,看到hello world 說(shuō)明uwsgi可以使用 http://ip:8001 D.安裝nginx 切換安裝源 rpm -ivh http:///packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm 安裝nginx yum install nginx -y
E.部署項(xiàng)目代碼 安裝git命令 yum install git -y 創(chuàng)建代碼根目錄 mkdir /myweb 獲取代碼 cd /myweb git clone https://github.com/xianxiaoyin/Monitoring
修改setting.py 添加 STATIC_ROOT = os.path.join(BASE_DIR, 'static') #指定靜態(tài)資源存放位置 收集靜態(tài)資源 cd /myweb/Monitoring python manage.py collectstatic 報(bào)錯(cuò): django.core.exceptions.ImproperlyConfigured: Error loading either pysqlite2 or sqlite3 modules (tried in that order): No module named _sqlite3
解決方法: 需要安裝sqlite-devel之后,,重新編譯安裝Python即可,。 yum install sqlite-devel 重新編譯安裝Python: ./configure make make install
報(bào)錯(cuò): django.core.exceptions.ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting 解決方法: 把STATIC_ROOT = os.path.join(BASE_DIR, 'static')改為 STATIC_ROOT = os.path.join(BASE_DIR, 'static1')
F.配置uwsgi 在/myweb目錄下創(chuàng)建wsgi_conf.ini文件 [uwsgi] socket = 0.0.0.0:3031 chdir = /myweb/Monitoring/ wsgi-file = Monitoring/wsgi.py processes = 4 threads = 2
G.配置nginx 添加配置文件 vim test.conf server { listen 80; server_name localhost; charset utf-8; access_log /var/log/nginx/test.access.log main; index index.html; location / { root /myweb/Monitoring; uwsgi_pass 127.0.0.1:3031; include uwsgi_params; } location /static { alias /myweb/Monitoring/static; }
}
H.分別啟動(dòng)nginx,,uwsgi service nginx restart #重啟nginx uwsgi wsgi_conf.ini #啟動(dòng)uwsgi
訪問(wèn) http://ip
訪問(wèn)報(bào)502錯(cuò)誤 日志顯示(權(quán)限不夠) 2017/02/16 15:56:25 [crit] 2652#2652: *4 connect() to 127.0.0.1:3031 failed (13: Permission denied) while connecting to upstream, client: 192.168.136.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://127.0.0.1:3031", host: "192.168.136.131", referrer: "http://192.168.136.131/"
解決方案: 關(guān)閉seliunx 1.臨時(shí)關(guān)閉 setenforce 0 2.永久關(guān)閉 vim /etc/sysconfig/selinux 把SELINUX=enforcing改為 SELINUX=disabled
自動(dòng)搭建腳本
#!/usr/bin/env python # coding:utf-8 ''' install django in the production django==1.8 python==2.7 uwsgi==2.0.14 ''' import os import sys
def getVersion(command=''):# get python version version = os.popen(command) return version.read() if __name__ == '__main__': print(getVersion('python --version')) print('{} {}'.format('uwsgi', getVersion('uwsgi --version'))) #更新 os.system('yum update -y') #安裝依賴包 a = os.system('yum install zlib zlib-devel openssl openssl-devel libcurl-devel gcc gcc-c++ -y') if a != 0: sys.exit() os.system('wget http://pypi./packages/source/s/setuptools/setuptools-0.6c11.tar.gz') if os.path.exists('setuptools-0.6c11.tar.gz'): os.system('tar zxvf setuptools-0.6c11.tar.gz && cd setuptools-0.6c11 && python setup.py build && python setup.py install') os.system('rm -rf ./setuptools*') else: sys.exit('setuptools install error') os.system('wget "https://pypi./packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb" --no-check-certificate') if os.path.exists('pip-1.5.4.tar.gz'): os.system('tar -xzvf pip-1.5.4.tar.gz && cd pip-1.5.4 && python setup.py install') os.system('rm -rf ./pip*') else: sys.exit('pip install error') #安裝uwsgi os.system('pip install uwsgi ') #安裝django os.system('pip install django==1.8') #安裝nginx os.system('rpm -ivh http:///packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm') os.system('yum install nginx -y && yum install git -y')
|