要注意alias和root的区别
root /path相当于访问static的时候是访问/path/staticalias /path相当于访问static时是访问/path/1.确认环境 python2.7 django1.8 centos6.8
A.安装django
pip install django==1.8B.安装uwsgi pip install uwsgi 报错(-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.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb" --no-check-certificate3 pip安装
tar -xzvf pip-1.5.4.tar.gz cd pip-1.5.4 python setup.py installpython错误: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.测试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"启动uwsgi
uwsgi --http :8001 --wsgi-file check.py用url访问测试,看到hello world 说明uwsgi可以使用
http://ip:8001D.安装nginx 切换安装源 rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm 安装nginx yum install nginx -yE.部署项目代码
安装git命令 yum install git -y 创建代码根目录 mkdir /myweb 获取代码 cd /myweb git clone https://github.com/xianxiaoyin/Monitoring修改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 _sqlite3解决方法:
需要安装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 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.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')