3tere 发表于 2017-10-27 09:13:28

nginx+uwsgi+django环境部署部署

Django框架部署文档:

硬件环境:阿里云ecs服务器8核心、16GB、10Mbps
环境结构:nginx+uwsgi+django
所需软件:python-3.6.1、pip3.6、mysql-5.1.73 nginx-1.6.3、







1
2
# yum install zlib-develbzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-develtk-devel
# yum -y install opensslopenssl-devel






1
2
3
4
5
编译安装python3.6.1
# tar zxvf Python-3.6.1.tgz -C /usr/src/
# cd /usr/src/Python-3.6.1/
# ./configure--prefix=/usr/local/python --enable-shared CFLAGS=-fPIC
#make&&make install






##安装完python3.6过程中自动安装了pip工具
'''
#Collecting setuptools
#Collecting pip
#Installing collected packages: setuptools,pip
#Successfully installed pip-9.0.1setuptools-28.8.0
'''


现在输入python还是python2.6

1
2
3
4
5
# python
Python 2.6.6 (r266:84292, Jul 23 2015,15:22:56)
onlinux2
Type "help","copyright", "credits" or "license" for moreinformation.
>>>




将2.6python的程序备份

1
2
3
4
# which python
/usr/bin/python
# cd /usr/bin/
# mv python python.bak





将3.6的python软链接到/usr/bin/python

1
2
3
# ln -s/usr/local/python/bin/python3 /usr/bin/python
# python
python: error while loading sharedlibraries: libpython3.6m.so.1.0: cannot open shared object file: No such fileor directory





缺少libpython3.6m.so.1.0 将python安装目录下的libpython3.6m.so.1.0链接到/usr/lib64库下
[

1
root@shouke bin]# ln -s/usr/local/python/lib/libpython3.6m.so.1.0 /usr/lib64/





可以使用了

1
2
3
4
5
6
7
# python
Python 3.6.1 (default, Oct 26 2017,11:49:35)
onlinux
Type "help","copyright", "credits" or "license" for moreinformation.
>>> print("hello world")
hello world
>>>





现在yum无法使用了,因为yum是使用python解释器运行的,现在找不到原来版本的python了,所以无法运行了,

1
2
3
4
5
6
7
8
9
# yum
File "/usr/bin/yum", line 30
   except KeyboardInterrupt, e:
                            ^
SyntaxError: invalid syntax


# which yum
/usr/bin/yum





# cat /usr/bin/yum

1
2
3
4
5
6
7
8
#!/usr/bin/python2.6   ##修改第一行为这样
import sys
try:
   import yum
except ImportError:
   print >> sys.stderr, """\
There was a problem importing one of thePython mod
。。。






yum可以使用了:

1
2
3
4
5
6
# yum makecache
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
*base: mirrors.aliyuncs.com
*epel: mirrors.aliyuncs.com
*extras: mirrors.aliyuncs.com







安装 uwsgi


1
2
3
4
5
6
7
#/usr/local/python/bin/pip3 install uwsgi
Collecting uwsgi
Downloadinghttp://mirrors.aliyun.com/pypi/packages/bb/0a/45e5aa80dc135889594bb371c082d20fb7ee7303b174874c996888cc8511/uwsgi-2.0.15.tar.gz(795kB)
   100% |████████████████████████████████| 798kB 58.5MB/s
Installing collected packages: uwsgi
Running setup.py install for uwsgi ... done
Successfully installed uwsgi-2.0.15






验证安装:

1
2
#/usr/local/python/bin/uwsgi --version
2.0.15





安装django

1
2
3
4
5
6
7
8
9
# /usr/local/python/bin/pip3install django
Collecting django
Downloadinghttp://mirrors.aliyun.com/pypi/packages/82/33/f9d2871f3aed5062661711bf91b3ebb03daa52cc0e1c37925f3e0c4508c5/Django-1.11.6-py2.py3-none-any.whl(6.9MB)
   100% |████████████████████████████████| 7.0MB 68.8MB/s
Collecting pytz (from django)
Downloadinghttp://mirrors.aliyun.com/pypi/packages/55/62/e7cd0e15b76062d298413f14bb4ec3cd8568a22d274427f9c3c7286969f4/pytz-2017.2-py2.py3-none-any.whl(484kB)
   100% |████████████████████████████████| 491kB 67.8MB/s
Installing collected packages: pytz, django
Successfully installed django-1.11.6pytz-2017.2






验证安装:
[

1
2
3
4
5
root@shouke ~]# python
Python 3.6.1 (default, Oct 26 2017,11:49:35)
on linux
Type "help","copyright", "credits" or "license" for moreinformation.
>>> import django








1
2
>>> django.VERSION
(1, 11, 6, 'final', 0)





输入url:8002验证。(提示服务器ip地址加入到允许访问的列表内)
http://lesliecheung.blog.51cto.com/e/u261/themes/default/images/spacer.gif



1
2

# vim demosite/settings.py   #修改此文件





http://lesliecheung.blog.51cto.com/e/u261/themes/default/images/spacer.gif

1
# python manage.pyrunserver 0.0.0.0:8002





再次运行,访问成功:
http://lesliecheung.blog.51cto.com/e/u261/themes/default/images/spacer.gif


部署项目:

将项目上传到服务器:
http://lesliecheung.blog.51cto.com/e/u261/themes/default/images/spacer.gif
移动到项目/opt下面,并在/opt下面新建文件夹script

1
2
# ls
gitlab online_edit_scriptscript





在script目录里新建uwsgi.ini配置文件


1
2
3
4
# cd script/
# ls
uwsgi.ini
# vim uwsgi.ini





#uwsig使用配置文件启动

# 项目目录
chdir=/opt/online_edit_script/
# 指定项目的application
module=online_edit_script.wsgi:application
# 指定sock的文件路径      
socket=/opt/script/uwsgi.sock
# 进程个数      
workers=5
pidfile=/opt//script/uwsgi.pid
# 指定IP端口      
http=127.0.0.1:9001
# 指定静态文件
static-map=/static=/opt/online_edit_script/static
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 启用主进程
master=true
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=/opt/script/uwsgi.log




配置nginx:
服务器上nginx已经部署了,直接修改配置文件


1
2
3
4
5
6
7
8
9
10
11
12
13
server {
       server_name www.leslie.com;
      location /{
         include uwsgi_params;      ##加载uwsgi支持模块
         uwsgi_connect_timeout 30;    ##超时时间
            uwsgi_pass unix:/opt /script/uwsgi.sock;    #转到uwsgi这个进程

      }
      location/static/ {         ##静态文件nginx自己处理
         alias/opt/online_edit_script/static/;
      indexindex.html index.htm;
         }
}






检测语法:

1
2
3
# /application/nginx/sbin/nginx -t
nginx: the configuration file/application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file/application/nginx-1.6.3//conf/nginx.conf test is successful







安装mysql:

1
yum -y install mysql mysql-devel mysql-server mysql-client







1
pip3 install mysqlclient






修改配置文件将域名加入到允许访问的列表里面。

1
# vim /opt/online_edit_script/online_edit_script/settings.py





http://lesliecheung.blog.51cto.com/e/u261/themes/default/images/spacer.gif


初始化uwsgi


1
Uwsgi –ini /opt/script/uwsgi.ini






会生成一个进程监听本地的9001端口

1
tcp      0      0 127.0.0.1:9001            0.0.0.0:*                   LISTEN      19214/uwsgi






重启nginx:

1
2
kill all nginx
/usr/local/nginx/sbin/nginx






访问测试:

页: [1]
查看完整版本: nginx+uwsgi+django环境部署部署