注:
python3.5
uwsgi 2.0.15
nginx 1.11.12
django 1.10
一、安装 python3.5
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel 安装一大堆的插件,我怕记不住,在这写一下
剩下的就是./configure --prefix=/usr/local/python3 && make && make install
在此就不过多说明了
二、安装 uwsgi
- def application(env, start_response):
- start_response('200 OK', [('Content-Type','text/html')])
- return [b"Hello World"] #注意以字节形式,否则前端看不到Helloween World
运行 uwsgi --http :8001 --wsgi-file test.py
在浏览器访问 127.0.0.1:8001 如果看到 Hello World 就成功了
三、安装 django
- python3 django-admin.py startproject dida7 #创建一个项目
- cd dida7 #cd到项目里
- python3 manage.py runserver 0.0.0.0:8000 #启动项目
在浏览器内输入:http://127.0.0.1:8002,检查 django 是否运行正常。
四、安装 nginx
我的 nginx 直接从官网上下载的 http://nginx.org/en/download.html
nginx 1.11.12
- mkdir /usr/usr/nginx
- tar xf nginx-1.11.12.tar.gz -C /usr/src/nginx
- #解压文件放到/usr/src/nginx目录下
- ./configure --prefix=/usr/local/nginx && make && make install
- #安装目录/usr/local/nginx
五、创建 uwsgi.ini 文件(可以用 xml)
- 我的uwsgi.ini文件放在/root/dida7目录下,和它平级的有manage.py文件
- # myweb_uwsgi.ini file
- [uwsgi]
- # Django-related settings
- socket = :8888 #指定项目执行的端口号
- # the base directory (full path)
- chdir = /root/dida7 #指定项目的目录
- # Django s wsgi file
- wsgi-file = /root/dida7/dida7/wsgi.py
- #这个指定django项目的wsgi.py文件,wsgi.py文件创建项目就有,我项目放在/root/dida7
- # process-related settings
- # master
- master = true #主进程
- # maximum number of worker processes
- processes = 4
- # ... with appropriate permissions - may be needed
- # chmod-socket = 664
- # clear environment on exit
- vacuum = true
运行 uwsgi.ini 文件
- 先cd到/root/dida7目录
- [ dida7]# uwsgi --ini uwsgi.ini
- [uWSGI] getting INI configuration from uwsgi.ini
- *** Starting uWSGI 2.0.15 (64bit) on [Sat Apr 1 13:50:08 2017] ***
- compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-17) on 31 March 20
- 17 11:45:24os: Linux-2.6.32-642.13.1.el6.x86_64 #1 SMP Wed Jan 11 20:56:24 UTC 201
- 7nodename: CentOS-mode
- machine: x86_64
- clock source: unix
- pcre jit disabled
- detected number of CPU cores: 2
- current working directory: /root/dida7
- detected binary path: /usr/local/python3/bin/uwsgi
- uWSGI running as root, you can use --uid/--gid/--chroot options
- *** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
- chdir() to /root/dida7
- your processes number limit is 7347
- your memory page size is 4096 bytes
- detected max file descriptor number: 1024
- lock engine: pthread robust mutexes
- thunder lock: disabled (you can enable it with --thunder-lock)
- uwsgi socket 0 bound to TCP address :8888 fd 3
- Python version: 3.5.1 (default, Mar 31 2017, 10:53:29) [GCC 4.4.7 2012
- 0313 (Red Hat 4.4.7-17)]*** Python threads support is disabled. You can enable it with --enable
- -threads ***Python main interpreter initialized at 0x206b9a0
- your server socket listen backlog is limited to 100 connections
- your mercy for graceful operations on workers is 60 seconds
- mapped 363840 bytes (355 KB) for 4 cores
- *** Operational MODE: preforking ***
- WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x206b9a0
- pid: 60031 (default app)*** uWSGI is running in multiple interpreter mode ***
- spawned uWSGI master process (pid: 60031)
- spawned uWSGI worker 1 (pid: 60033, cores: 1)
- spawned uWSGI worker 2 (pid: 60034, cores: 1)
- spawned uWSGI worker 3 (pid: 60035, cores: 1)
- spawned uWSGI worker 4 (pid: 60036, cores: 1)
- 运行起来,看看有没有报错
六、修改 nginx 配置
vim /usr/local/nginx/conf/nginx.conf
- location / {
- include uwsgi_params;
- uwsgi_pass 192.168.1.90:8888; #这个地址要和uwsgi.ini中的一致
- uwsgi_read_timeout 2;
- index index.html index.htm;
- }
接下来运行 uwsgi,运行 nginx
访问 192.168.1.90 成功
七、结合 nginx,django-admin 的样式丢失
访问 django-admin 没有样式
解决办法:
修改 settings.py 文件
- STATIC_URL = '/static/'
- STATIC_ROOT = os.path.join(BASE_DIR,"static")
- #这个static是手工建立的文件夹,STAIC_ROOT的路径是dida7/static
在项目目录下执行 python3 manage.py collectstatic
会 django 安装目录下的 static 文件拷贝到我们创建的 static 目录下
现在已经在 / root/dida7/static / 下生成 admin 文件夹,里边有 CSS、img、js
修改 nginx.conf 文件
- location /static/ {
- alias /root/dida7/static/;
- } #又写了一个location定义静态文件路径
重启 uwsgi 和 nginx
成功!!! 我也是坑了两天希望能帮到各位
来源: http://www.bubuko.com/infodetail-2005291.html