注:
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
我的python装在/usr/local/python3下(不在重复说明)
pip install uwsgi (方式不重要,安装上就行,安装在python3目录下)
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi(做一个软链接)
测试uwsgi能不能用
vim test.py
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 install django(方式不重要,安装上就行)
在/usr/local/python3/bin/下执行创建命令
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目录 [root@CentOS-mode 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的样式丢失
解决办法:
修改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
八、多站点配置(没有试过)
我采用运行多个uwsgi服务的方法来实现多个站点。
重复第五步,创建uwsgi9091.ini,并相应修改文件中的
socket = 127.0.0.1:9091
然后修改nginx的配置文件为:
server { listen 80; server_name localhost; location / { include uwsgi_params; uwsgi_pass 192.168.1.90:8888; #这个地址要和uwsgi.ini中的一致 uwsgi_read_timeout 2; index index.html index.htm; } } server { listen 1300; server_name localhost; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:9091; #这个地址要和uwsgi.ini中的一致 uwsgi_read_timeout 2; index index.html index.htm; } }
然后我们就可以通过http://127.0.0.1:1300来访问新的网站了。