要注意alias和root的區別
root /path至關於訪問static的時候是訪問/path/static
alias /path至關於訪問static時是訪問/path/html
1.確認環境 python2.7 django1.8 centos6.8python
A.安裝django
pip install django==1.8
B.安裝uwsgi
pip install uwsgi
報錯(-bash: pip: command not found)解決方案
1、pip下載安裝
1 安裝依賴軟件
yum install zlib zlib-devel openssl openssl-devel libcurl-devel gcc gcc-c++ -y
2 pip下載
wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb" --no-check-certificatelinux
3 pip安裝
tar -xzvf pip-1.5.4.tar.gz
cd pip-1.5.4
python setup.py installnginx
python錯誤:ImportError: No module named setuptools
這句錯誤提示的表面意思是:沒有setuptools的模塊,說明python缺乏這個模塊,那咱們只要安裝這個模塊便可解決此問題,下面咱們來安裝一下:
在命令行下:
下載setuptools包
wget http://pypi.python.org/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
開始執行setuptools安裝
python setup.py installc++
C.測試uwsgi是否安裝成功
建立測試文件 vim check.py
#!/usr/bin/python
# coding:utf8
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return "Hello World" git
啓動uwsgi
uwsgi --http :8001 --wsgi-file check.pygithub
用url訪問測試,看到hello world 說明uwsgi能夠使用
http://ip:8001
D.安裝nginx
切換安裝源
rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
安裝nginx
yum install nginx -yweb
E.部署項目代碼
安裝git命令
yum install git -y
建立代碼根目錄
mkdir /myweb
獲取代碼
cd /myweb
git clone https://github.com/xianxiaoyin/Monitoringsql
修改setting.py
添加 STATIC_ROOT = os.path.join(BASE_DIR, 'static') #指定靜態資源存放位置
收集靜態資源
cd /myweb/Monitoring
python manage.py collectstatic
報錯:
django.core.exceptions.ImproperlyConfigured: Error loading either pysqlite2 or sqlite3 modules (tried in that order): No module named _sqlite3django
解決方法:
須要安裝sqlite-devel以後,從新編譯安裝Python便可。
yum install sqlite-devel
從新編譯安裝Python:
./configure
make
make install
報錯:
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目錄下建立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.分別啓動nginx,uwsgi
service nginx restart #重啓nginx
uwsgi wsgi_conf.ini #啓動uwsgi
訪問
http://ip
訪問報502錯誤
日誌顯示(權限不夠)
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/"
解決方案:
關閉seliunx
1.臨時關閉
setenforce 0
2.永久關閉
vim /etc/sysconfig/selinux
把SELINUX=enforcing改成
SELINUX=disabled
自動搭建腳本
#!/usr/bin/env python# coding:utf-8'''install django in the productiondjango==1.8python==2.7uwsgi==2.0.14'''import osimport sysdef 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.python.org/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.python.org/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://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm') os.system('yum install nginx -y && yum install git -y')